Skip to content

Commit

Permalink
Integrate iOS and Android certificate providers with recorder
Browse files Browse the repository at this point in the history
Summary: Integrate and clean existing logs.

Reviewed By: antonk52

Differential Revision: D47330592

fbshipit-source-id: 7b1c7ebbe5804ccc184edd2583f5cddd598f11e8
  • Loading branch information
lblasa authored and facebook-github-bot committed Jul 10, 2023
1 parent c15ac08 commit cc0f42b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
extractBundleIdFromCSR,
} from '../../app-connectivity/certificate-exchange/certificate-utils';
import {ClientQuery} from 'flipper-common';
import {recorder} from '../../recorder';

const logTag = 'AndroidCertificateProvider';

Expand All @@ -32,8 +33,10 @@ export default class AndroidCertificateProvider extends CertificateProvider {
appDirectory: string,
csr: string,
): Promise<string> {
recorder.log(clientQuery, 'Query available devices via adb');
const devicesInAdb = await this.adb.listDevices();
if (devicesInAdb.length === 0) {
recorder.error(clientQuery, 'No devices found via adb');
throw new Error('No Android devices found');
}
const deviceMatchList = devicesInAdb.map(async (device) => {
Expand All @@ -58,6 +61,11 @@ export default class AndroidCertificateProvider extends CertificateProvider {
const matchingIds = devices.filter((m) => m.isMatch).map((m) => m.id);

if (matchingIds.length == 0) {
recorder.error(
clientQuery,
'Unable to find a matching device for the incoming request',
);

const erroredDevice = devices.find((d) => d.error);
if (erroredDevice) {
throw erroredDevice.error;
Expand All @@ -70,6 +78,7 @@ export default class AndroidCertificateProvider extends CertificateProvider {
this.santitizeString(csr),
)} Found these:${foundCsrs.join('\n\n')}`,
);

throw new Error(`No matching device found for app: ${appName}`);
}
if (matchingIds.length > 1) {
Expand All @@ -85,6 +94,11 @@ export default class AndroidCertificateProvider extends CertificateProvider {
contents: string,
csr: string,
) {
recorder.log(
clientQuery,
`Deploying file '${filename}' to device at '${destination}'`,
);

const appName = await extractBundleIdFromCSR(csr);
const deviceId = await this.getTargetDeviceId(
clientQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../../app-connectivity/certificate-exchange/certificate-utils';
import path from 'path';
import {ClientQuery} from 'flipper-common';
import {recorder} from '../../recorder';

const tmpDir = promisify(tmp.dir) as (options?: DirOptions) => Promise<string>;

Expand All @@ -43,13 +44,14 @@ export default class iOSCertificateProvider extends CertificateProvider {
return matches[1];
}

// Get all available targets
recorder.log(clientQuery, 'Query available devices');
const targets = await iosUtil.targets(
this.idbConfig.idbPath,
this.idbConfig.enablePhysicalIOS,
clientQuery,
);
if (targets.length === 0) {
recorder.error(clientQuery, 'No devices found');
throw new Error('No iOS devices found');
}
const deviceMatchList = targets.map(async (target) => {
Expand All @@ -63,7 +65,11 @@ export default class iOSCertificateProvider extends CertificateProvider {
);
return {id: target.udid, isMatch};
} 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}`,
logTag,
e,
Expand All @@ -89,22 +95,18 @@ export default class iOSCertificateProvider extends CertificateProvider {
contents: string,
csr: string,
) {
console.debug('[conn] Deploying file to device ', {
destination,
filename,
});
recorder.log(
clientQuery,
`Deploying file '${filename}' to device at '${destination}'`,
);

const bundleId = await extractBundleIdFromCSR(csr);
try {
await fs.writeFile(destination + filename, contents);
} catch (err) {
console.debug(
'[conn] Deploying file using idb as physical device is inferred',
);
const relativePathInsideApp =
this.getRelativePathInAppContainer(destination);

console.debug(`[conn] Relative path '${relativePathInsideApp}'`);

const udid = await this.getTargetDeviceId(
clientQuery,
bundleId,
Expand All @@ -120,8 +122,6 @@ export default class iOSCertificateProvider extends CertificateProvider {
contents,
);
}

console.debug('[conn] Deploying file to device complete');
}

private getRelativePathInAppContainer(absolutePath: string) {
Expand Down Expand Up @@ -176,8 +176,9 @@ export default class iOSCertificateProvider extends CertificateProvider {
clientQuery,
);
} catch (e) {
console.warn(
`[conn] Original idb pull failed. Most likely it is a physical device
recorder.log(
clientQuery,
`Original idb pull failed. Most likely it is a physical device
that requires us to handle the dest path dirrently.
Forcing a re-try with the updated dest path. See D32106952 for details.`,
e,
Expand All @@ -190,26 +191,28 @@ export default class iOSCertificateProvider extends CertificateProvider {
this.idbConfig.idbPath,
clientQuery,
);
console.info(
'[conn] Subsequent idb pull succeeded. Nevermind previous wranings.',
recorder.log(
clientQuery,
'Subsequent idb pull succeeded. Nevermind previous warnings.',
);
}

const items = await fs.readdir(dst);
if (items.length > 1) {
throw new Error('Conflict in temporary dir');
throw new Error('Conflict in temporary directory');
}
if (items.length === 0) {
throw new Error('No CSR found on device');
}

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());

return csrFromDevice === this.santitizeString(csr);
}
}

0 comments on commit cc0f42b

Please sign in to comment.