Skip to content

Commit f6ab727

Browse files
committed
fix: using sync file system methods, defensive checks and added logs
1 parent 8896b76 commit f6ab727

File tree

6 files changed

+95
-38
lines changed

6 files changed

+95
-38
lines changed

.github/CONTRIBUTING.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ Unit tests can be run via `npm run test:unit` command.
99

1010
To run the code, a GitHub PR against `develop` should be raised with the committed code to the branch PR. The PR runs deployment script with deploy to development environment. The script builds the code that's added as part of your change and installs it in Azure DevOps organization as an extension that can be added to run a pipeline.
1111

12+
### Local debugging
13+
14+
A number of environment variable are required for debugging, here's an example launch config for `VSCode` that sets mandatory parameters such as `AGENT_TEMPDIRECTORY`, `INPUT_failOnIssues` and `INPUT_authToken`
15+
16+
```
17+
{
18+
"version": "0.2.0",
19+
"configurations": [
20+
{
21+
"type": "node",
22+
"request": "launch",
23+
"name": "Launch Program",
24+
"program": "${workspaceFolder}/snykTask/src/index.ts",
25+
"env": {
26+
"AGENT_TEMPDIRECTORY": "some/temp/path",
27+
"INPUT_failOnIssues": "true",
28+
"INPUT_authToken": "your-auth-token-guid-from-portal",
29+
"INPUT_targetFile": "path-to-visual-studio-solution.sln",
30+
"INPUT_organization" : "your-org-guid-from-portal",
31+
"INPUT_monitorWhen" : "never",
32+
"INPUT_severityThreshold" : "low",
33+
"INPUT_failOnThreshold" : "critical"
34+
},
35+
"outFiles": [
36+
"${workspaceFolder}/**/*.js"
37+
]
38+
}
39+
]
40+
}
41+
```
42+
1243
## Release
1344
The release process is fully-automated: all you need to do is create a PR to merge `develop` into `master` and call the PR `Merge develop into master for release`.
1445

snykTask/src/__tests__/task-lib.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,29 @@ test('getOptionsToExecuteSnyk builds IExecOptions like we need it', () => {
4646
expect(options.ignoreReturnCode).toBe(true);
4747
});
4848

49-
test('finds vulnerabilities greater than medium threshold', async () => {
49+
test('finds vulnerabilities greater than medium threshold', () => {
5050
const fixturePath = 'snykTask/test/fixtures/high-vulnerabilities.json';
51-
const itemsFound = await doVulnerabilitiesExistForFailureThreshold(
51+
const itemsFound = doVulnerabilitiesExistForFailureThreshold(
5252
fixturePath,
5353
'medium',
5454
);
5555

5656
expect(itemsFound).toBe(true);
5757
});
5858

59-
test('ignores vulnerabilities lower than high threshold', async () => {
59+
test('defaults to found when file does not exist', () => {
60+
const fixturePath = 'snykTask/test/fixtures/does-not-exist.json';
61+
const itemsFound = doVulnerabilitiesExistForFailureThreshold(
62+
fixturePath,
63+
'medium',
64+
);
65+
66+
expect(itemsFound).toBe(true);
67+
});
68+
69+
test('ignores vulnerabilities lower than high threshold', () => {
6070
const fixturePath = 'snykTask/test/fixtures/low-vulnerabilities.json';
61-
const itemsFound = await doVulnerabilitiesExistForFailureThreshold(
71+
const itemsFound = doVulnerabilitiesExistForFailureThreshold(
6272
fixturePath,
6373
'high',
6474
);

snykTask/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ async function run() {
403403
) {
404404
const failureThreshold: string = taskArgs.failOnThreshold;
405405
const matchingVulnerabilitiesFound =
406-
await doVulnerabilitiesExistForFailureThreshold(
406+
doVulnerabilitiesExistForFailureThreshold(
407407
jsonReportFullPath,
408408
failureThreshold,
409409
);

snykTask/src/task-lib.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as tr from 'azure-pipelines-task-lib/toolrunner';
33
import * as tl from 'azure-pipelines-task-lib/task';
44
import stream = require('stream');
55
import * as fs from 'fs';
6-
import * as fsPromises from 'fs/promises';
76
import * as path from 'path';
87

98
export const JSON_ATTACHMENT_TYPE = 'JSON_ATTACHMENT_TYPE';
@@ -109,19 +108,32 @@ export function getSeverityOrdinal(severity: string): number {
109108
throw new Error(`Cannot get severity ordinal for ${severity} severity`);
110109
}
111110

112-
export async function doVulnerabilitiesExistForFailureThreshold(
111+
export function doVulnerabilitiesExistForFailureThreshold(
113112
filePath: string,
114113
threshold: string,
115-
): Promise<boolean> {
116-
const file = await fsPromises.readFile(filePath, 'utf8');
114+
): boolean {
115+
if (!fs.existsSync(filePath)) {
116+
console.log(
117+
`${filePath} does not exist...cannot use it to search for vulnerabilities, defaulting to detected`,
118+
);
119+
return true;
120+
}
121+
122+
const file = fs.readFileSync(filePath, 'utf8');
117123
const json = JSON.parse(file);
118124
const thresholdOrdinal = getSeverityOrdinal(threshold);
119125

120-
for (const vulnerability of json['vulnerabilities']) {
121-
if (getSeverityOrdinal(vulnerability['severity']) >= thresholdOrdinal) {
122-
return true;
126+
for (let i = 0; i < json.length; i++) {
127+
let project = json[i];
128+
for (const vulnerability of project['vulnerabilities']) {
129+
if (getSeverityOrdinal(vulnerability['severity']) >= thresholdOrdinal) {
130+
return true;
131+
}
123132
}
124133
}
125134

135+
console.log(
136+
`no vulnerabilities of at least '${threshold}' severity were detected, not failing build`,
137+
);
126138
return false;
127139
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
{
2-
"vulnerabilities": [
3-
{
4-
"severity": "critical"
5-
},
6-
{
7-
"severity": "high"
8-
}
9-
],
10-
"ok": true,
11-
"dependencyCount": 0,
12-
"org": "demo-applications"
13-
}
1+
[
2+
{
3+
"vulnerabilities": [
4+
{
5+
"severity": "critical"
6+
},
7+
{
8+
"severity": "high"
9+
}
10+
],
11+
"ok": true,
12+
"dependencyCount": 0,
13+
"org": "demo-applications"
14+
}
15+
]
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
{
2-
"vulnerabilities": [
3-
{
4-
"severity": "medium"
5-
},
6-
{
7-
"severity": "low"
8-
}
9-
],
10-
"ok": true,
11-
"dependencyCount": 0,
12-
"org": "demo-applications"
13-
}
1+
[
2+
{
3+
"vulnerabilities": [
4+
{
5+
"severity": "medium"
6+
},
7+
{
8+
"severity": "low"
9+
}
10+
],
11+
"ok": true,
12+
"dependencyCount": 0,
13+
"org": "demo-applications"
14+
}
15+
]

0 commit comments

Comments
 (0)