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:
committed by
Facebook Github Bot
parent
f99ef6996e
commit
db1b175d44
32
flow-typed/npm/tmp_vx.x.x.js
vendored
Normal file
32
flow-typed/npm/tmp_vx.x.x.js
vendored
Normal 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'>;
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
import LogManager from '../fb-stubs/Logger';
|
import LogManager from '../fb-stubs/Logger';
|
||||||
import {RecurringError} from './errors';
|
import {RecurringError} from './errors';
|
||||||
|
import {promisify} from 'util';
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const adb = require('adbkit-fb');
|
const adb = require('adbkit-fb');
|
||||||
import {
|
import {
|
||||||
@@ -14,6 +15,7 @@ import {
|
|||||||
isInstalled as opensslInstalled,
|
isInstalled as opensslInstalled,
|
||||||
} from './openssl-wrapper-with-promises';
|
} from './openssl-wrapper-with-promises';
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const tmpFile = promisify(require('tmp').file);
|
||||||
|
|
||||||
// Desktop file paths
|
// Desktop file paths
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
@@ -126,23 +128,14 @@ export default class CertificateProvider {
|
|||||||
|
|
||||||
generateClientCertificate(csr: string): Promise<string> {
|
generateClientCertificate(csr: string): Promise<string> {
|
||||||
console.debug('Creating new client cert', logTag);
|
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', {
|
return openssl('x509', {
|
||||||
req: true,
|
req: true,
|
||||||
in: csrFile,
|
in: path,
|
||||||
CA: caCert,
|
CA: caCert,
|
||||||
CAkey: caKey,
|
CAkey: caKey,
|
||||||
CAcreateserial: true,
|
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> {
|
extractAppNameFromCSR(csr: string): Promise<string> {
|
||||||
const csrFile = this.writeToTempFile(csr);
|
return this.writeToTempFile(csr)
|
||||||
return openssl('req', {
|
.then(path =>
|
||||||
in: csrFile,
|
openssl('req', {
|
||||||
noout: true,
|
in: path,
|
||||||
subject: true,
|
noout: true,
|
||||||
nameopt: true,
|
subject: true,
|
||||||
RFC2253: false,
|
nameopt: true,
|
||||||
})
|
RFC2253: false,
|
||||||
.then(subject => {
|
}).then(subject => {
|
||||||
|
return [path, subject];
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.then(([path, subject]) => {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
fs.unlink(csrFile, err => {
|
fs.unlink(path, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
@@ -481,10 +478,10 @@ export default class CertificateProvider {
|
|||||||
.then(_ => undefined);
|
.then(_ => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeToTempFile(content: string): string {
|
writeToTempFile(content: string): Promise<string> {
|
||||||
const fileName = getFilePath(`deviceCSR-${Math.random() * 1000000}`);
|
return tmpFile().then((path, fd, cleanupCallback) =>
|
||||||
fs.writeFileSync(fileName, content);
|
promisify(fs.writeFile)(path, content).then(_ => path),
|
||||||
return fileName;
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user