From db1b175d44d089862eb978b45dde0655a295eb1d Mon Sep 17 00:00:00 2001 From: John Knox Date: Tue, 28 Aug 2018 04:15:16 -0700 Subject: [PATCH] Use library to create temporary files Summary: Makes the code a bit cleaner and maintainable. Reviewed By: passy Differential Revision: D9496316 fbshipit-source-id: a59a41b12a57bb2eedc25b154c6f9b0cdc77dd8a --- flow-typed/npm/tmp_vx.x.x.js | 32 +++++++++++++++++ src/utils/CertificateProvider.js | 59 +++++++++++++++----------------- 2 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 flow-typed/npm/tmp_vx.x.x.js diff --git a/flow-typed/npm/tmp_vx.x.x.js b/flow-typed/npm/tmp_vx.x.x.js new file mode 100644 index 000000000..60fc7aa57 --- /dev/null +++ b/flow-typed/npm/tmp_vx.x.x.js @@ -0,0 +1,32 @@ +// flow-typed signature: 5764ce2922249ed6bf930c177a5582d4 +// flow-typed version: <>/tmp_v0.0.33/flow_v0.76.0 + +/** + * This is an autogenerated libdef stub for: + * + * 'tmp' + * + * Fill this stub out by replacing all the `any` types. + * + * Once filled out, we encourage you to share your work with the + * community by sending a pull request to: + * https://github.com/flowtype/flow-typed + */ + +declare module 'tmp' { + declare module.exports: any; +} + +/** + * We include stubs for each file inside this npm package in case you need to + * require those files directly. Feel free to delete any files that aren't + * needed. + */ +declare module 'tmp/lib/tmp' { + declare module.exports: any; +} + +// Filename aliases +declare module 'tmp/lib/tmp.js' { + declare module.exports: $Exports<'tmp/lib/tmp'>; +} diff --git a/src/utils/CertificateProvider.js b/src/utils/CertificateProvider.js index 83a7d4a8b..a973ddec4 100644 --- a/src/utils/CertificateProvider.js +++ b/src/utils/CertificateProvider.js @@ -7,6 +7,7 @@ import LogManager from '../fb-stubs/Logger'; import {RecurringError} from './errors'; +import {promisify} from 'util'; const fs = require('fs'); const adb = require('adbkit-fb'); import { @@ -14,6 +15,7 @@ import { isInstalled as opensslInstalled, } from './openssl-wrapper-with-promises'; const path = require('path'); +const tmpFile = promisify(require('tmp').file); // Desktop file paths const os = require('os'); @@ -126,23 +128,14 @@ export default class CertificateProvider { generateClientCertificate(csr: string): Promise { console.debug('Creating new client cert', logTag); - const csrFile = this.writeToTempFile(csr); - // Create a certificate for the client, using the details in the CSR. - return openssl('x509', { - req: true, - in: csrFile, - CA: caCert, - CAkey: caKey, - CAcreateserial: true, - }).then(cert => { - return new Promise(function(resolve, reject) { - fs.unlink(csrFile, err => { - if (err) { - reject(err); - } else { - resolve(cert); - } - }); + + return this.writeToTempFile(csr).then(path => { + return openssl('x509', { + req: true, + in: path, + CA: caCert, + CAkey: caKey, + CAcreateserial: true, }); }); } @@ -305,17 +298,21 @@ export default class CertificateProvider { } extractAppNameFromCSR(csr: string): Promise { - const csrFile = this.writeToTempFile(csr); - return openssl('req', { - in: csrFile, - noout: true, - subject: true, - nameopt: true, - RFC2253: false, - }) - .then(subject => { + return this.writeToTempFile(csr) + .then(path => + openssl('req', { + in: path, + noout: true, + subject: true, + nameopt: true, + RFC2253: false, + }).then(subject => { + return [path, subject]; + }), + ) + .then(([path, subject]) => { return new Promise(function(resolve, reject) { - fs.unlink(csrFile, err => { + fs.unlink(path, err => { if (err) { reject(err); } else { @@ -481,10 +478,10 @@ export default class CertificateProvider { .then(_ => undefined); } - writeToTempFile(content: string): string { - const fileName = getFilePath(`deviceCSR-${Math.random() * 1000000}`); - fs.writeFileSync(fileName, content); - return fileName; + writeToTempFile(content: string): Promise { + return tmpFile().then((path, fd, cleanupCallback) => + promisify(fs.writeFile)(path, content).then(_ => path), + ); } }