Skip to content

Commit

Permalink
Merge pull request #4 from akto-api-security/feature/wait_for_results
Browse files Browse the repository at this point in the history
Implemented wait till test completes
  • Loading branch information
ankush-jain-akto authored Nov 19, 2023
2 parents a1341ad + 57305f1 commit 4fc7557
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 9 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ inputs:
OVERRIDDEN_TEST_APP_URL:
required: false
description: 'Run tests on a different application host'
WAIT_TIME_FOR_RESULT:
required: false
description: 'Time to wait for Akto test results to complete. If it takes more time that defined, pull requests passes. Set to 0 if you want to run tests, but not block the PR'
BLOCK_LEVEL:
required: false
description: 'If vulnerabilties equal to or higher are found, the PR is failed. Set as HIGH, MEDIUM, LOW, NONE"
runs:
using: 'node16'
Expand Down
122 changes: 113 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,115 @@
const core = require('@actions/core');
const axios = require("axios")
const fs = require('fs');

async function run() {
let AKTO_START_TEST_ENDPOINT = ""
const AKTO_DASHBOARD_URL = process.env.AKTO_DASHBOARD_URL
const AKTO_API_KEY = process.env.AKTO_API_KEY
const AKTO_TEST_ID = process.env.AKTO_TEST_ID
const START_TIME_DELAY = process.env.START_TIME_DELAY
const OVERRIDEN_TEST_APP_URL = process.env.OVERRIDEN_TEST_APP_URL
const WAIT_TIME_FOR_RESULT = process.env.WAIT_TIME_FOR_RESULT
const BLOCK_LEVEL = process.env.BLOCK_LEVEL || "HIGH"
const GITHUB_STEP_SUMMARY = process.env.GITHUB_STEP_SUMMARY

function logGithubStepSummary(message) {
fs.appendFileSync(GITHUB_STEP_SUMMARY, `${message}\n`);
}

function toInt(a) {
if (a === '') return 0;

let ret = parseInt(a);

if (isNaN(ret)) return 0;

return ret;
}

async function fetchTestingRunResultSummary(testingRunResultSummaryHexId) {
try {
console.log("testingRunResultSummaryHexId: ", testingRunResultSummaryHexId);
const result = await axios.post(`${AKTO_DASHBOARD_URL}/api/fetchTestingRunResultSummary`, {
testingRunResultSummaryHexId
}, {
headers: {
'content-type': 'application/json',
'X-API-KEY': AKTO_API_KEY
}
});

return result.data;
} catch (error) {
console.error('Error fetching testing run result summaries:', error);
return null;
}
}

function exitIfBlockLevelBreached(resultLevel, blockLevel) {
if (blockLevel <= resultLevel) core.setFailed("Found vulnerabilties");
}

function parseBlockLevel(BLOCK_LEVEL) {
if (BLOCK_LEVEL === '') return 10;

if (BLOCK_LEVEL === 'HIGH') return 3;
if (BLOCK_LEVEL === 'MEDIUM') return 2;
if (BLOCK_LEVEL === 'LOW') return 1;

return 10;

const AKTO_DASHBOARD_URL = core.getInput('AKTO_DASHBOARD_URL')
const AKTO_API_KEY = core.getInput('AKTO_API_KEY')
const AKTO_TEST_ID = core.getInput('AKTO_TEST_ID')
const START_TIME_DELAY = core.getInput('START_TIME_DELAY')
const OVERRIDDEN_TEST_APP_URL = core.getInput('OVERRIDDEN_TEST_APP_URL')
}


async function waitTillComplete(testDetails, maxWaitTime) {
let testingRunResultSummaryHexId = testDetails.testingRunResultSummaryHexId
if (!testingRunResultSummaryHexId) return;

const pollStartTime = Math.floor(Date.now() / 1000);
while (true) {
pollCurrentTime = Math.floor(Date.now() / 1000);
elapsed = pollCurrentTime - pollStartTime;

if (elapsed >= maxWaitTime) {
console.log('Max poll interval reached. Exiting.');
break;
}

response = await fetchTestingRunResultSummary(testingRunResultSummaryHexId);
if (response) {
state = response.testingRunResultSummaries[0]?.state;

if (state === 'COMPLETED') {
const { countIssues } = response.testingRunResultSummaries[0];
const { HIGH, MEDIUM, LOW } = countIssues;

logGithubStepSummary(`[Results](${AKTO_DASHBOARD_URL}/dashboard/testing/${AKTO_TEST_ID}/results)`);
logGithubStepSummary(`HIGH: ${HIGH}`);
logGithubStepSummary(`MEDIUM: ${MEDIUM}`);
logGithubStepSummary(`LOW: ${LOW}`);

if (HIGH > 0 || MEDIUM > 0 || LOW > 0) {
logGithubStepSummary(`Vulnerabilities found!!`);

let blockLevel = parseBlockLevel(BLOCK_LEVEL)
exitIfBlockLevelBreached(HIGH > 0 ? 3 : (MEDIUM > 0 ? 2 : (LOW > 0 ? 1 : -10)));
}

break;
} else if (state === 'STOPPED') {
logGithubStepSummary(`Test stopped`);
break;
} else {
console.log('Waiting for akto test to be completed...');
await new Promise(resolve => setTimeout(resolve, 5000)); // 5 seconds
}
} else {
break;
}
}
}

async function run() {
let AKTO_START_TEST_ENDPOINT = ""
let startTimestamp = 0;
if(START_TIME_DELAY!=''){
let delay = parseInt(START_TIME_DELAY);
Expand All @@ -36,8 +136,8 @@ async function run() {
}
}

if (OVERRIDDEN_TEST_APP_URL) {
data["overriddenTestAppUrl"] = OVERRIDDEN_TEST_APP_URL
if (OVERRIDEN_TEST_APP_URL) {
data["overriddenTestAppUrl"] = OVERRIDEN_TEST_APP_URL
}

const config = {
Expand All @@ -53,6 +153,10 @@ async function run() {
try {
res = await axios(config)
console.log("Akto CI/CD test started")

let waitTimeForResult = toInt(WAIT_TIME_FOR_RESULT)
waitTillComplete(res.data, waitTimeForResult);

} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit 4fc7557

Please sign in to comment.