Fix Flipper lints #7

Summary:
Fix lints according to https://www.internalfb.com/intern/staticdocs/flipper/docs/internals/linters.

Logic should be unchanged.

Reviewed By: timur-valiev

Differential Revision: D30766176

fbshipit-source-id: a7b3f78af66188704913c87321b07aff0fed270a
This commit is contained in:
Pascal Hartig
2021-09-06 08:26:01 -07:00
committed by Facebook GitHub Bot
parent 93660d6be3
commit 4ba9a334d5

View File

@@ -355,35 +355,32 @@ export default class CertificateProvider {
); );
} }
private getTargetAndroidDeviceId( private async getTargetAndroidDeviceId(
appName: string, appName: string,
deviceCsrFilePath: string, deviceCsrFilePath: string,
csr: string, csr: string,
): Promise<string> { ): Promise<string> {
return this.adb const devices = await this.adb.then((client) => client.listDevices());
.then((client) => client.listDevices())
.then((devices) => {
if (devices.length === 0) { if (devices.length === 0) {
throw new Error('No Android devices found'); throw new Error('No Android devices found');
} }
const deviceMatchList = devices.map((device) => const deviceMatchList = devices.map(async (device) => {
this.androidDeviceHasMatchingCSR( try {
const result = await this.androidDeviceHasMatchingCSR(
deviceCsrFilePath, deviceCsrFilePath,
device.id, device.id,
appName, appName,
csr, csr,
) );
.then((result) => {
return {id: device.id, ...result, error: null}; return {id: device.id, ...result, error: null};
}) } catch (e) {
.catch((e) => {
console.warn( console.warn(
`Unable to check for matching CSR in ${device.id}:${appName}`, `Unable to check for matching CSR in ${device.id}:${appName}`,
logTag, logTag,
); );
return {id: device.id, isMatch: false, foundCsr: null, error: e}; return {id: device.id, isMatch: false, foundCsr: null, error: e};
}), }
); });
return Promise.all(deviceMatchList).then((devices) => { return Promise.all(deviceMatchList).then((devices) => {
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) {
@@ -411,10 +408,9 @@ export default class CertificateProvider {
} }
return matchingIds[0]; return matchingIds[0];
}); });
});
} }
private getTargetiOSDeviceId( private async getTargetiOSDeviceId(
appName: string, appName: string,
deviceCsrFilePath: string, deviceCsrFilePath: string,
csr: string, csr: string,
@@ -424,22 +420,22 @@ export default class CertificateProvider {
// It's a simulator, the deviceId is in the filepath. // It's a simulator, the deviceId is in the filepath.
return Promise.resolve(matches[1]); return Promise.resolve(matches[1]);
} }
return iosUtil const targets = await iosUtil.targets(
.targets(this.config.idbPath, this.config.enablePhysicalIOS) this.config.idbPath,
.then((targets) => { this.config.enablePhysicalIOS,
);
if (targets.length === 0) { if (targets.length === 0) {
throw new Error('No iOS devices found'); throw new Error('No iOS devices found');
} }
const deviceMatchList = targets.map((target) => const deviceMatchList = targets.map(async (target) => {
this.iOSDeviceHasMatchingCSR( const isMatch = await this.iOSDeviceHasMatchingCSR(
deviceCsrFilePath, deviceCsrFilePath,
target.udid, target.udid,
appName, appName,
csr, csr,
).then((isMatch) => {
return {id: target.udid, isMatch};
}),
); );
return {id: target.udid, isMatch};
});
return Promise.all(deviceMatchList).then((devices) => { return Promise.all(deviceMatchList).then((devices) => {
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) {
@@ -447,7 +443,6 @@ export default class CertificateProvider {
} }
return matchingIds[0]; return matchingIds[0];
}); });
});
} }
private androidDeviceHasMatchingCSR( private androidDeviceHasMatchingCSR(
@@ -477,7 +472,7 @@ export default class CertificateProvider {
}); });
} }
private iOSDeviceHasMatchingCSR( private async iOSDeviceHasMatchingCSR(
directory: string, directory: string,
deviceId: string, deviceId: string,
bundleId: string, bundleId: string,
@@ -486,71 +481,54 @@ export default class CertificateProvider {
const originalFile = this.getRelativePathInAppContainer( const originalFile = this.getRelativePathInAppContainer(
path.resolve(directory, csrFileName), path.resolve(directory, csrFileName),
); );
return tmpDir({unsafeCleanup: true}) const dir = await tmpDir({unsafeCleanup: true});
.then((dir) => { await iosUtil.pull(
return iosUtil deviceId,
.pull(deviceId, originalFile, bundleId, dir, this.config.idbPath) originalFile,
.then(() => dir); bundleId,
}) dir,
.then((dir) => { this.config.idbPath,
return fs );
.readdir(dir) const items = await fs.readdir(dir);
.then((items) => {
if (items.length > 1) { if (items.length > 1) {
throw new Error('Conflict in temp dir'); throw new Error('Conflict in temp dir');
} }
if (items.length === 0) { if (items.length === 0) {
throw new Error('Failed to pull CSR from device'); throw new Error('Failed to pull CSR from device');
} }
return items[0]; const fileName = items[0];
})
.then((fileName) => {
const copiedFile = path.resolve(dir, fileName); const copiedFile = path.resolve(dir, fileName);
console.debug('Trying to read CSR from', copiedFile); console.debug('Trying to read CSR from', copiedFile);
return fs const data = await fs.readFile(copiedFile);
.readFile(copiedFile) const csrFromDevice = this.santitizeString(data.toString());
.then((data) => this.santitizeString(data.toString())); return csrFromDevice === this.santitizeString(csr);
});
})
.then((csrFromDevice) => csrFromDevice === this.santitizeString(csr));
} }
private santitizeString(csrString: string): string { private santitizeString(csrString: string): string {
return csrString.replace(/\r/g, '').trim(); return csrString.replace(/\r/g, '').trim();
} }
extractAppNameFromCSR(csr: string): Promise<string> { async extractAppNameFromCSR(csr: string): Promise<string> {
return this.writeToTempFile(csr) const path = await this.writeToTempFile(csr);
.then((path) => const subject = await openssl('req', {
openssl('req', {
in: path, in: path,
noout: true, noout: true,
subject: true, subject: true,
nameopt: true, nameopt: true,
RFC2253: false, RFC2253: false,
}).then((subject) => { });
return [path, subject];
}),
)
.then(async ([path, subject]) => {
await fs.unlink(path); await fs.unlink(path);
return subject;
})
.then((subject) => {
const matches = subject.trim().match(x509SubjectCNRegex); const matches = subject.trim().match(x509SubjectCNRegex);
if (!matches || matches.length < 2) { if (!matches || matches.length < 2) {
throw new Error(`Cannot extract CN from ${subject}`); throw new Error(`Cannot extract CN from ${subject}`);
} }
return matches[1]; const appName = matches[1];
})
.then((appName) => {
if (!appName.match(allowedAppNameRegex)) { if (!appName.match(allowedAppNameRegex)) {
throw new Error( throw new Error(
`Disallowed app name in CSR: ${appName}. Only alphanumeric characters and '.' allowed.`, `Disallowed app name in CSR: ${appName}. Only alphanumeric characters and '.' allowed.`,
); );
} }
return appName; return appName;
});
} }
async loadSecureServerConfig(): Promise<SecureServerConfig> { async loadSecureServerConfig(): Promise<SecureServerConfig> {
@@ -691,10 +669,10 @@ export default class CertificateProvider {
.then((_) => undefined); .then((_) => undefined);
} }
private writeToTempFile(content: string): Promise<string> { private async writeToTempFile(content: string): Promise<string> {
return tmpFile().then((path) => const path = await tmpFile();
fs.writeFile(path, content).then((_) => path), await fs.writeFile(path, content);
); return path;
} }
} }