Skip to content

Commit

Permalink
feat: Run the IaC queries in validate & test in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilianna Papastefanou committed Jan 12, 2021
1 parent cdff3af commit 70937e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
37 changes: 21 additions & 16 deletions src/lib/iac/detect-iac.ts
Expand Up @@ -90,14 +90,14 @@ async function getDirectoryFiles(
root: string,
options: { maxDepth?: number } = {},
) {
const iacFiles: IacFileInDirectory[] = [];
const dirPath = pathLib.resolve(root, '.');
const supportedExtensions = new Set(Object.keys(projectTypeByFileType));

const directoryPaths = makeDirectoryIterator(dirPath, {
maxDepth: options.maxDepth,
});

const promises: Promise<IacFileInDirectory>[] = [];
for (const filePath of directoryPaths) {
const fileType = pathLib
.extname(filePath)
Expand All @@ -107,23 +107,28 @@ async function getDirectoryFiles(
continue;
}

try {
const projectType = await getProjectTypeForIacFile(filePath);
iacFiles.push({
filePath,
projectType,
fileType,
});
} catch (err) {
iacFiles.push({
filePath,
fileType,
failureReason:
err instanceof CustomError ? err.userMessage : 'Unhandled Error',
});
}
promises.push(
(async () => {
try {
const projectType = await getProjectTypeForIacFile(filePath);
return {
filePath,
projectType,
fileType,
};
} catch (err) {
return {
filePath,
fileType,
failureReason:
err instanceof CustomError ? err.userMessage : 'Unhandled Error',
};
}
})(),
);
}

const iacFiles = await Promise.all(promises);
if (iacFiles.length === 0) {
throw IacDirectoryWithoutAnyIacFileError();
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/request/request.ts
Expand Up @@ -8,6 +8,7 @@ import { getProxyForUrl } from 'proxy-from-env';
import * as ProxyAgent from 'proxy-agent';
import * as analytics from '../analytics';
import { Agent } from 'http';
import * as https from 'https';
import { Global } from '../../cli/args';
import { Payload } from './types';
import { getVersion } from '../version';
Expand Down Expand Up @@ -93,14 +94,15 @@ export = function makeRequest(
url = url + '?' + querystring.stringify(payload.qs);
delete payload.qs;
}

const agent = new https.Agent({ keepAlive: true, maxSockets: 10 });
const options: needle.NeedleOptions = {
json: payload.json,
headers: payload.headers,
timeout: payload.timeout,
// eslint-disable-next-line @typescript-eslint/camelcase
follow_max: 5,
family: payload.family,
agent,
};

const proxyUri = getProxyForUrl(url);
Expand Down
33 changes: 21 additions & 12 deletions src/lib/snyk-test/run-test.ts
Expand Up @@ -226,23 +226,28 @@ async function sendAndParseResults(
options: Options & TestOptions,
): Promise<TestResult[]> {
const results: TestResult[] = [];
const promises: Promise<TestResult>[] = [];
for (const payload of payloads) {
await spinner.clear<void>(spinnerLbl)();
await spinner(spinnerLbl);
if (options.iac) {
const iacScan: IacScan = payload.body as IacScan;
analytics.add('iac type', !!iacScan.type);
const res = (await sendTestPayload(payload)) as IacTestResponse;

const projectName =
iacScan.projectNameOverride || iacScan.originalProjectName;
const result = await parseIacTestResult(
res,
iacScan.targetFile,
projectName,
options.severityThreshold,
promises.push(
(async () => {
const iacScan: IacScan = payload.body as IacScan;
analytics.add('iac type', !!iacScan.type);
const res = (await sendTestPayload(payload)) as IacTestResponse;

const projectName =
iacScan.projectNameOverride || iacScan.originalProjectName;
const result = await parseIacTestResult(
res,
iacScan.targetFile,
projectName,
options.severityThreshold,
);
return result;
})(),
);
results.push(result);
} else {
/** sendTestPayload() deletes the request.body from the payload once completed. */
const payloadCopy = Object.assign({}, payload);
Expand Down Expand Up @@ -292,6 +297,10 @@ async function sendAndParseResults(
});
}
}

if (promises.length) {
return await Promise.all(promises);
}
return results;
}

Expand Down

0 comments on commit 70937e9

Please sign in to comment.