Integrate iOS and Android certificate providers with recorder

Summary: Integrate and clean existing logs.

Reviewed By: antonk52

Differential Revision: D47330592

fbshipit-source-id: 7b1c7ebbe5804ccc184edd2583f5cddd598f11e8
This commit is contained in:
Lorenzo Blasa
2023-07-10 05:52:07 -07:00
committed by Facebook GitHub Bot
parent c15ac08714
commit cc0f42be76
2 changed files with 38 additions and 21 deletions

View File

@@ -15,6 +15,7 @@ import {
extractBundleIdFromCSR, extractBundleIdFromCSR,
} from '../../app-connectivity/certificate-exchange/certificate-utils'; } from '../../app-connectivity/certificate-exchange/certificate-utils';
import {ClientQuery} from 'flipper-common'; import {ClientQuery} from 'flipper-common';
import {recorder} from '../../recorder';
const logTag = 'AndroidCertificateProvider'; const logTag = 'AndroidCertificateProvider';
@@ -32,8 +33,10 @@ export default class AndroidCertificateProvider extends CertificateProvider {
appDirectory: string, appDirectory: string,
csr: string, csr: string,
): Promise<string> { ): Promise<string> {
recorder.log(clientQuery, 'Query available devices via adb');
const devicesInAdb = await this.adb.listDevices(); const devicesInAdb = await this.adb.listDevices();
if (devicesInAdb.length === 0) { if (devicesInAdb.length === 0) {
recorder.error(clientQuery, 'No devices found via adb');
throw new Error('No Android devices found'); throw new Error('No Android devices found');
} }
const deviceMatchList = devicesInAdb.map(async (device) => { const deviceMatchList = devicesInAdb.map(async (device) => {
@@ -58,6 +61,11 @@ export default class AndroidCertificateProvider extends CertificateProvider {
const matchingIds = devices.filter((m) => m.isMatch).map((m) => m.id); const matchingIds = devices.filter((m) => m.isMatch).map((m) => m.id);
if (matchingIds.length == 0) { if (matchingIds.length == 0) {
recorder.error(
clientQuery,
'Unable to find a matching device for the incoming request',
);
const erroredDevice = devices.find((d) => d.error); const erroredDevice = devices.find((d) => d.error);
if (erroredDevice) { if (erroredDevice) {
throw erroredDevice.error; throw erroredDevice.error;
@@ -70,6 +78,7 @@ export default class AndroidCertificateProvider extends CertificateProvider {
this.santitizeString(csr), this.santitizeString(csr),
)} Found these:${foundCsrs.join('\n\n')}`, )} Found these:${foundCsrs.join('\n\n')}`,
); );
throw new Error(`No matching device found for app: ${appName}`); throw new Error(`No matching device found for app: ${appName}`);
} }
if (matchingIds.length > 1) { if (matchingIds.length > 1) {
@@ -85,6 +94,11 @@ export default class AndroidCertificateProvider extends CertificateProvider {
contents: string, contents: string,
csr: string, csr: string,
) { ) {
recorder.log(
clientQuery,
`Deploying file '${filename}' to device at '${destination}'`,
);
const appName = await extractBundleIdFromCSR(csr); const appName = await extractBundleIdFromCSR(csr);
const deviceId = await this.getTargetDeviceId( const deviceId = await this.getTargetDeviceId(
clientQuery, clientQuery,

View File

@@ -18,6 +18,7 @@ import {
} from '../../app-connectivity/certificate-exchange/certificate-utils'; } from '../../app-connectivity/certificate-exchange/certificate-utils';
import path from 'path'; import path from 'path';
import {ClientQuery} from 'flipper-common'; import {ClientQuery} from 'flipper-common';
import {recorder} from '../../recorder';
const tmpDir = promisify(tmp.dir) as (options?: DirOptions) => Promise<string>; const tmpDir = promisify(tmp.dir) as (options?: DirOptions) => Promise<string>;
@@ -43,13 +44,14 @@ export default class iOSCertificateProvider extends CertificateProvider {
return matches[1]; return matches[1];
} }
// Get all available targets recorder.log(clientQuery, 'Query available devices');
const targets = await iosUtil.targets( const targets = await iosUtil.targets(
this.idbConfig.idbPath, this.idbConfig.idbPath,
this.idbConfig.enablePhysicalIOS, this.idbConfig.enablePhysicalIOS,
clientQuery, clientQuery,
); );
if (targets.length === 0) { if (targets.length === 0) {
recorder.error(clientQuery, 'No devices found');
throw new Error('No iOS devices found'); throw new Error('No iOS devices found');
} }
const deviceMatchList = targets.map(async (target) => { const deviceMatchList = targets.map(async (target) => {
@@ -63,7 +65,11 @@ export default class iOSCertificateProvider extends CertificateProvider {
); );
return {id: target.udid, isMatch}; return {id: target.udid, isMatch};
} catch (e) { } catch (e) {
console.info( recorder.error(
clientQuery,
'Unable to find a matching device for the incoming request',
);
console.warn(
`[conn] Unable to check for matching CSR in ${target.udid}:${appName}`, `[conn] Unable to check for matching CSR in ${target.udid}:${appName}`,
logTag, logTag,
e, e,
@@ -89,22 +95,18 @@ export default class iOSCertificateProvider extends CertificateProvider {
contents: string, contents: string,
csr: string, csr: string,
) { ) {
console.debug('[conn] Deploying file to device ', { recorder.log(
destination, clientQuery,
filename, `Deploying file '${filename}' to device at '${destination}'`,
}); );
const bundleId = await extractBundleIdFromCSR(csr); const bundleId = await extractBundleIdFromCSR(csr);
try { try {
await fs.writeFile(destination + filename, contents); await fs.writeFile(destination + filename, contents);
} catch (err) { } catch (err) {
console.debug(
'[conn] Deploying file using idb as physical device is inferred',
);
const relativePathInsideApp = const relativePathInsideApp =
this.getRelativePathInAppContainer(destination); this.getRelativePathInAppContainer(destination);
console.debug(`[conn] Relative path '${relativePathInsideApp}'`);
const udid = await this.getTargetDeviceId( const udid = await this.getTargetDeviceId(
clientQuery, clientQuery,
bundleId, bundleId,
@@ -120,8 +122,6 @@ export default class iOSCertificateProvider extends CertificateProvider {
contents, contents,
); );
} }
console.debug('[conn] Deploying file to device complete');
} }
private getRelativePathInAppContainer(absolutePath: string) { private getRelativePathInAppContainer(absolutePath: string) {
@@ -176,8 +176,9 @@ export default class iOSCertificateProvider extends CertificateProvider {
clientQuery, clientQuery,
); );
} catch (e) { } catch (e) {
console.warn( recorder.log(
`[conn] Original idb pull failed. Most likely it is a physical device clientQuery,
`Original idb pull failed. Most likely it is a physical device
that requires us to handle the dest path dirrently. that requires us to handle the dest path dirrently.
Forcing a re-try with the updated dest path. See D32106952 for details.`, Forcing a re-try with the updated dest path. See D32106952 for details.`,
e, e,
@@ -190,26 +191,28 @@ export default class iOSCertificateProvider extends CertificateProvider {
this.idbConfig.idbPath, this.idbConfig.idbPath,
clientQuery, clientQuery,
); );
console.info( recorder.log(
'[conn] Subsequent idb pull succeeded. Nevermind previous wranings.', clientQuery,
'Subsequent idb pull succeeded. Nevermind previous warnings.',
); );
} }
const items = await fs.readdir(dst); const items = await fs.readdir(dst);
if (items.length > 1) { if (items.length > 1) {
throw new Error('Conflict in temporary dir'); throw new Error('Conflict in temporary directory');
} }
if (items.length === 0) { if (items.length === 0) {
throw new Error('No CSR found on device'); throw new Error('No CSR found on device');
} }
const filename = items[0]; const filename = items[0];
const pulledFile = path.resolve(dst, filename); const filepath = path.resolve(dst, filename);
console.debug(`[conn] Read CSR from '${pulledFile}'`); recorder.log(clientQuery, `Read CSR from: '${filepath}'`);
const data = await fs.readFile(pulledFile); const data = await fs.readFile(filepath);
const csrFromDevice = this.santitizeString(data.toString()); const csrFromDevice = this.santitizeString(data.toString());
return csrFromDevice === this.santitizeString(csr); return csrFromDevice === this.santitizeString(csr);
} }
} }