Skip to content

Commit 37467bd

Browse files
committed
debugg
1 parent 104322d commit 37467bd

File tree

5 files changed

+162
-101
lines changed

5 files changed

+162
-101
lines changed

.github/actions/gokakashi-scan/action.yaml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ inputs:
1010
description: 'API token for authentication with goKakashi'
1111
required: true
1212
image_name:
13-
description: 'The Docker image to scan'
13+
description: 'The image to scan'
1414
required: true
1515
severity:
1616
description: 'Comma-separated list of severity levels to report'
@@ -19,7 +19,6 @@ inputs:
1919
publish:
2020
description: 'The publish path for the scan report'
2121
required: false
22-
default: 'report_private'
2322
fail_on_severity:
2423
description: 'Comma-separated list of severity levels to fail the job on (e.g., CRITICAL,HIGH)'
2524
required: false
@@ -32,17 +31,16 @@ outputs:
3231
runs:
3332
using: 'composite'
3433
steps:
35-
# Set up Node.js
36-
- uses: actions/setup-node@v3
34+
- name: Set up Node.js
35+
uses: actions/setup-node@v3
3736
with:
3837
node-version: '20.x'
39-
cache: npm # Cache dependencies to speed up workflow runs
38+
cache: npm
4039

41-
# Install dependencies
42-
- run: pwd # Executes your script
40+
- name: Install dependencies
41+
run: npm ci
4342
shell: bash
44-
- run: npm ci # Installs dependencies from package-lock.json (clean install)
45-
shell: bash
46-
# Run the index.js script after installing dependencies
47-
- run: node index.js # Executes your script
43+
44+
- name: Run goKakashi scan
45+
run: node index.js
4846
shell: bash

.github/actions/gokakashi-scan/index.js

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,83 @@
1-
const fetch = require('node-fetch');
2-
const { setFailed, getInput, setOutput } = require('@actions/core');
1+
import fetch from 'node-fetch';
2+
import { setFailed, getInput, setOutput } from '@actions/core';
3+
4+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
5+
6+
async function makeRequest(url, options) {
7+
try {
8+
const response = await fetch(url, options);
9+
if (!response.ok) {
10+
throw new Error(`HTTP error! status: ${response.status}`);
11+
}
12+
return await response.json();
13+
} catch (error) {
14+
console.error(`Request failed: ${error.message}`);
15+
throw error;
16+
}
17+
}
18+
19+
async function pollScanStatus(apiHost, apiToken, scanId) {
20+
let status = 'queued';
21+
let retries = 0;
22+
const maxRetries = 30;
23+
const initialDelay = 10000;
24+
25+
while ((status === 'queued' || status === 'in-progress') && retries < maxRetries) {
26+
console.log(`Current scan status: ${status}. Waiting for completion...`);
27+
28+
await sleep(initialDelay * Math.pow(2, retries));
29+
30+
const statusData = await makeRequest(`${apiHost}/api/v0/scan/${scanId}/status`, {
31+
method: 'GET',
32+
headers: { 'Authorization': `Bearer ${apiToken}` }
33+
});
34+
35+
status = statusData.status;
36+
retries++;
37+
38+
if (status === 'completed') {
39+
return statusData.report_url[0];
40+
}
41+
}
42+
43+
throw new Error(`Scan did not complete within the expected time. Final status: ${status}`);
44+
}
345

446
async function run() {
547
try {
6-
// Input parameters from the action
748
const apiHost = getInput('api_host');
849
const apiToken = getInput('api_token');
950
const imageName = getInput('image_name');
1051
const severity = getInput('severity');
1152
const publish = getInput('publish');
12-
const failOnSeverity = getInput('fail_on_severity'); // Get user-defined severity level
53+
const failOnSeverity = getInput('fail_on_severity');
1354

14-
// Step 1: Trigger the scan and get the scan_id
15-
const triggerResponse = await fetch(`${apiHost}/api/v0/scan?image=${imageName}&severity=${severity}&publish=${publish}`, {
55+
// Validate inputs
56+
if (!apiHost || !apiToken || !imageName) {
57+
throw new Error('Missing required inputs: api_host, api_token, or image_name');
58+
}
59+
60+
console.log('Triggering scan...');
61+
const triggerData = await makeRequest(`${apiHost}/api/v0/scan?image=${imageName}&severity=${severity}&publish=${publish}`, {
1662
method: 'POST',
1763
headers: {
1864
'Authorization': `Bearer ${apiToken}`,
1965
'Content-Type': 'application/json'
2066
}
2167
});
2268

23-
if (!triggerResponse.ok) {
24-
throw new Error(`Failed to trigger the scan. Status: ${triggerResponse.status}`);
25-
}
26-
27-
const triggerData = await triggerResponse.json();
2869
const scanId = triggerData.scan_id;
29-
3070
console.log(`Scan triggered with scan ID: ${scanId}`);
3171

32-
// Step 2: Poll the scan status until it's completed
33-
let status = 'queued';
34-
let reportUrl = '';
72+
const reportUrl = await pollScanStatus(apiHost, apiToken, scanId);
73+
console.log(`Scan completed. Report URL: ${reportUrl}`);
74+
setOutput('report_url', reportUrl);
3575

36-
while (status === 'queued' || status === 'in-progress') {
37-
console.log(`Current scan status: ${status}. Waiting for completion...`);
38-
39-
await new Promise(r => setTimeout(r, 10000)); // Wait 10 seconds between polls
40-
41-
const statusResponse = await fetch(`${apiHost}/api/v0/scan/${scanId}/status`, {
42-
method: 'GET',
43-
headers: {
44-
'Authorization': `Bearer ${apiToken}`
45-
}
46-
});
47-
48-
if (!statusResponse.ok) {
49-
throw new Error(`Failed to get scan status. Status: ${statusResponse.status}`);
50-
}
51-
52-
const statusData = await statusResponse.json();
53-
status = statusData.status;
54-
55-
// Check if scan is completed
56-
if (status === 'completed') {
57-
reportUrl = statusData.report_url[0]; // Extract report URL
58-
console.log(`Scan completed. Report URL: ${reportUrl}`);
59-
setOutput('report_url', reportUrl); // Set the output for future steps
60-
}
61-
}
62-
63-
// If the scan did not complete successfully
64-
if (status !== 'completed') {
65-
throw new Error(`Scan failed with status: ${status}`);
66-
}
67-
68-
// Step 3: Check the scan report for vulnerabilities
69-
const reportResponse = await fetch(reportUrl);
70-
const reportData = await reportResponse.json();
76+
console.log('Fetching scan report...');
77+
const reportData = await makeRequest(reportUrl);
7178

7279
if (failOnSeverity) {
73-
// Split the severities into an array
7480
const severitiesToFailOn = failOnSeverity.split(',').map(sev => sev.trim().toUpperCase());
75-
76-
// Check if the report contains any vulnerabilities matching the specified severities
7781
const hasVulnsToFail = reportData.vulnerabilities.some(vuln =>
7882
severitiesToFailOn.includes(vuln.severity)
7983
);
@@ -86,7 +90,7 @@ async function run() {
8690
console.log('No fail_on_severity defined, proceeding without failing the job.');
8791
}
8892
} catch (error) {
89-
setFailed(error.message);
93+
setFailed(`Action failed: ${error.message}`);
9094
}
9195
}
9296

.github/actions/gokakashi-scan/package-lock.json

Lines changed: 82 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/gokakashi-scan/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
"version": "1.0.0",
44
"description": "GitHub Action for scanning images using goKakashi",
55
"main": "index.js",
6+
"private": true,
7+
"scripts": {
8+
"start": "node index.js"
9+
},
610
"dependencies": {
711
"@actions/core": "^1.10.1",
8-
"node-fetch": "^2.6.1"
12+
"node-fetch": "^3.3.2"
13+
},
14+
"engines": {
15+
"node": ">=20.0.0"
916
}
10-
}
17+
}

.github/workflows/scan.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Test GoKakashi Scan
22

33
on:
4-
workflow_dispatch: # Allows you to trigger the workflows manually
4+
workflow_dispatch:
55
push:
66
branches:
77
- build-github-actions-2
@@ -14,15 +14,16 @@ jobs:
1414
- uses: actions/checkout@v2
1515

1616
- name: Run GoKakashi Scan
17+
id: gokakashi_scan
1718
uses: ./.github/actions/gokakashi-scan
1819
with:
1920
api_host: 'http://localhost:8000'
2021
api_token: 'ashwiniag123'
2122
image_name: 'hasura/graphql-engine:v2.35.0'
2223
severity: 'CRITICAL,HIGH'
2324
publish: 'report_private'
24-
# fail_on_severity: 'HIGH,CRITICAL'
25+
# fail_on_severity: 'HIGH,CRITICAL'
2526

2627
- name: Log the Scan Report URL
2728
run: |
28-
echo "Scan report URL: ${{ steps.scan.outputs.report_url }}"
29+
echo "Scan report URL: ${{ steps.gokakashi_scan.outputs.report_url }}"

0 commit comments

Comments
 (0)