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
This commit is contained in:
John Knox
2018-08-28 04:15:16 -07:00
committed by Facebook Github Bot
parent f99ef6996e
commit db1b175d44
2 changed files with 60 additions and 31 deletions

32
flow-typed/npm/tmp_vx.x.x.js vendored Normal file
View File

@@ -0,0 +1,32 @@
// flow-typed signature: 5764ce2922249ed6bf930c177a5582d4
// flow-typed version: <<STUB>>/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'>;
}

View File

@@ -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<string> {
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 this.writeToTempFile(csr).then(path => {
return openssl('x509', {
req: true,
in: csrFile,
in: path,
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);
}
});
});
});
}
@@ -305,17 +298,21 @@ export default class CertificateProvider {
}
extractAppNameFromCSR(csr: string): Promise<string> {
const csrFile = this.writeToTempFile(csr);
return openssl('req', {
in: csrFile,
return this.writeToTempFile(csr)
.then(path =>
openssl('req', {
in: path,
noout: true,
subject: true,
nameopt: true,
RFC2253: false,
})
.then(subject => {
}).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<string> {
return tmpFile().then((path, fd, cleanupCallback) =>
promisify(fs.writeFile)(path, content).then(_ => path),
);
}
}