Reuse adb client in certificate provider

Summary: Avoid making duplicate clients, and this one uses a more reliable creation method.

Reviewed By: passy

Differential Revision: D14241448

fbshipit-source-id: 95846a373335818758c8c4a282ed4db26b62d986
This commit is contained in:
John Knox
2019-02-27 05:39:05 -08:00
committed by Facebook Github Bot
parent 99bb43fe32
commit 6dc2215753

View File

@@ -9,7 +9,6 @@ import type {Logger} from '../fb-interfaces/Logger';
import {RecurringError} from './errors'; import {RecurringError} from './errors';
import {promisify} from 'util'; import {promisify} from 'util';
const fs = require('fs'); const fs = require('fs');
const adb = require('adbkit-fb');
import { import {
openssl, openssl,
isInstalled as opensslInstalled, isInstalled as opensslInstalled,
@@ -20,7 +19,8 @@ const tmpFile = promisify(tmp.file);
const tmpDir = promisify(tmp.dir); const tmpDir = promisify(tmp.dir);
import iosUtil from '../fb-stubs/iOSContainerUtility'; import iosUtil from '../fb-stubs/iOSContainerUtility';
import {reportPlatformFailures} from './metrics'; import {reportPlatformFailures} from './metrics';
import adbConfig from './adbConfig'; import {getAdbClient} from './adbClient';
const adb = require('adbkit-fb');
// Desktop file paths // Desktop file paths
const os = require('os'); const os = require('os');
@@ -71,13 +71,13 @@ export type SecureServerConfig = {|
*/ */
export default class CertificateProvider { export default class CertificateProvider {
logger: Logger; logger: Logger;
adb: any; adb: Promise<any>;
certificateSetup: Promise<void>; certificateSetup: Promise<void>;
server: Server; server: Server;
constructor(server: Server, logger: Logger) { constructor(server: Server, logger: Logger) {
this.logger = logger; this.logger = logger;
this.adb = adb.createClient(adbConfig()); this.adb = getAdbClient();
this.certificateSetup = reportPlatformFailures( this.certificateSetup = reportPlatformFailures(
this.ensureServerCertExists(), this.ensureServerCertExists(),
'ensureServerCertExists', 'ensureServerCertExists',
@@ -260,41 +260,43 @@ export default class CertificateProvider {
deviceCsrFilePath: string, deviceCsrFilePath: string,
csr: string, csr: string,
): Promise<string> { ): Promise<string> {
return this.adb.listDevices().then((devices: Array<{id: string}>) => { return this.adb
const deviceMatchList = devices.map(device => .then(client => client.listDevices())
this.androidDeviceHasMatchingCSR( .then((devices: Array<{id: string}>) => {
deviceCsrFilePath, const deviceMatchList = devices.map(device =>
device.id, this.androidDeviceHasMatchingCSR(
appName, deviceCsrFilePath,
csr, device.id,
) appName,
.then(isMatch => {
return {id: device.id, isMatch};
})
.catch(e => {
console.error(
`Unable to check for matching CSR in ${device.id}:${appName}`,
logTag,
);
return {id: device.id, isMatch: false};
}),
);
return Promise.all(deviceMatchList).then(devices => {
const matchingIds = devices.filter(m => m.isMatch).map(m => m.id);
if (matchingIds.length == 0) {
throw new RecurringError(
`No matching device found for app: ${appName}`,
);
}
if (matchingIds.length > 1) {
console.error(
new RecurringError('More than one matching device found for CSR'),
csr, csr,
); )
} .then(isMatch => {
return matchingIds[0]; return {id: device.id, isMatch};
})
.catch(e => {
console.error(
`Unable to check for matching CSR in ${device.id}:${appName}`,
logTag,
);
return {id: device.id, isMatch: false};
}),
);
return Promise.all(deviceMatchList).then(devices => {
const matchingIds = devices.filter(m => m.isMatch).map(m => m.id);
if (matchingIds.length == 0) {
throw new RecurringError(
`No matching device found for app: ${appName}`,
);
}
if (matchingIds.length > 1) {
console.error(
new RecurringError('More than one matching device found for CSR'),
csr,
);
}
return matchingIds[0];
});
}); });
});
} }
getTargetiOSDeviceId( getTargetiOSDeviceId(
@@ -417,7 +419,9 @@ export default class CertificateProvider {
); );
} }
return this.adb return this.adb
.shell(deviceId, `echo '${command}' | run-as '${user}'`) .then(client =>
client.shell(deviceId, `echo '${command}' | run-as '${user}'`),
)
.then(adb.util.readAll) .then(adb.util.readAll)
.then(buffer => buffer.toString()) .then(buffer => buffer.toString())
.then(output => { .then(output => {