Skip to content

Commit

Permalink
refactor: extract scanner into class
Browse files Browse the repository at this point in the history
  • Loading branch information
tembleking committed Jul 30, 2024
1 parent d9114f7 commit 3bbeb0a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 48 deletions.
63 changes: 38 additions & 25 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import fs from 'fs';
import { generateSARIFReport } from './src/sarif';
import { cliScannerName, cliScannerResult, cliScannerURL, executeScan, numericPriorityForSeverity, pullScanner, ScanExecutionResult, ScanMode } from './src/scanner';
import { cliScannerName, cliScannerResult, defaultScannerURL, executeScan, numericPriorityForSeverity, ScanExecutionResult, ScanMode, Scanner } from './src/scanner';
import { ActionInputs, defaultSecureEndpoint } from './src/action';
import { generateSummary } from './src/summary';
import { Report } from './src/report';
Expand All @@ -25,8 +25,12 @@ export async function run() {
let scanFlags = opts.composeFlags();

let scanResult: ScanExecutionResult;

let scanner = new Scanner(
Scanner.Options.withScannerURL(opts.cliScannerURL),
);
// Download CLI Scanner from 'cliScannerURL'
let retCode = await pullScanner(opts.cliScannerURL);
let retCode = await scanner.pullScanner();
if (retCode == 0) {
// Execute Scanner
scanResult = await executeScan(scanFlags);
Expand Down Expand Up @@ -103,9 +107,8 @@ export async function processScanResult(result: ScanExecutionResult, opts: Actio
}

export {
cliScannerURL,
defaultScannerURL as cliScannerURL,
defaultSecureEndpoint,
pullScanner,
cliScannerName,
executeScan,
cliScannerResult,
Expand Down
6 changes: 3 additions & 3 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core';
import { cliScannerResult, cliScannerURL, ComposeFlags, ScanMode, scannerURLForVersion } from './scanner';
import { cliScannerResult, defaultScannerURL, ComposeFlags, ScanMode, scannerURLForVersion } from './scanner';

export const defaultSecureEndpoint = "https://secure.sysdig.com/"

Expand Down Expand Up @@ -54,7 +54,7 @@ export class ActionInputs {
static overridingParsedActionInputs(overrides: { [key: string]: any }) {

const params: ActionInputParameters = {
cliScannerURL: core.getInput('cli-scanner-url') || cliScannerURL,
cliScannerURL: core.getInput('cli-scanner-url') || defaultScannerURL,
cliScannerVersion: core.getInput('cli-scanner-version'),
registryUser: core.getInput('registry-user'),
registryPassword: core.getInput('registry-password'),
Expand Down Expand Up @@ -147,7 +147,7 @@ export class ActionInputs {

// FIXME(fede) this also modifies the opts.cliScannerURL, which is something we don't want
public composeFlags(): ComposeFlags {
if (this.params.cliScannerVersion && this.params.cliScannerURL == cliScannerURL) {
if (this.params.cliScannerVersion && this.params.cliScannerURL == defaultScannerURL) {
this.params.cliScannerURL = scannerURLForVersion(this.params.cliScannerVersion)
}

Expand Down
49 changes: 35 additions & 14 deletions src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const cliScannerArch = getRunArch()
const cliScannerURLBase = "https://download.sysdig.com/scanning/bin/sysdig-cli-scanner";
export const cliScannerName = "sysdig-cli-scanner"
export const cliScannerResult = "scan-result.json"
export const cliScannerURL = `${cliScannerURLBase}/${cliScannerVersion}/${cliScannerOS}/${cliScannerArch}/${cliScannerName}`
export const defaultScannerURL = `${cliScannerURLBase}/${cliScannerVersion}/${cliScannerOS}/${cliScannerArch}/${cliScannerName}`

export enum ScanMode {
vm = "vm",
Expand All @@ -28,21 +28,42 @@ export namespace ScanMode {
}
}

export async function pullScanner(scannerURL: string) {
let start = performance.now();
core.info('Pulling cli-scanner from: ' + scannerURL);
let cmd = `wget ${scannerURL} -O ./${cliScannerName}`;
let retCode = await exec.exec(cmd, undefined, { silent: true });

if (retCode == 0) {
cmd = `chmod u+x ./${cliScannerName}`;
await exec.exec(cmd, undefined, { silent: true });
} else {
core.error(`Falied to pull scanner using "${scannerURL}"`)
type ScannerOption = (scanner: Scanner) => void;



export class Scanner {
protected scannerURL: string;

constructor(...options: ScannerOption[]) {
this.scannerURL = defaultScannerURL;
options.forEach(o => o(this));
}

async pullScanner() {
let start = performance.now();
core.info('Pulling cli-scanner from: ' + this.scannerURL);
let cmd = `wget ${this.scannerURL} -O ./${cliScannerName}`;
let retCode = await exec.exec(cmd, undefined, { silent: true });

if (retCode == 0) {
cmd = `chmod u+x ./${cliScannerName}`;
await exec.exec(cmd, undefined, { silent: true });
} else {
core.error(`Falied to pull scanner using "${this.scannerURL}"`)
}

core.info("Scanner pull took " + Math.round(performance.now() - start) + " milliseconds.");
return retCode;
}

core.info("Scanner pull took " + Math.round(performance.now() - start) + " milliseconds.");
return retCode;
static Options = class {
static withScannerURL(scannerURL: string): ScannerOption {
return (scanner) => {
scanner.scannerURL = scannerURL
}
}
}
}

export interface ScanExecutionResult {
Expand Down
7 changes: 6 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as report_test from "./fixtures/report-test.json";

import { exec } from "@actions/exec";
import { ActionInputs } from '../src/action';
import { Scanner } from '../src/scanner';
jest.mock("@actions/exec");
const mockExec = jest.mocked(exec);

Expand Down Expand Up @@ -207,7 +208,11 @@ describe("scanner pulling", () => {
it("pulls the configured scanner", async () => {
mockExec.mockImplementation(jest.fn());

await index.pullScanner("https://foo");
let scanner = new Scanner(
Scanner.Options.withScannerURL("https://foo"),
);

await scanner.pullScanner();
expect(mockExec).toHaveBeenCalledTimes(1);
expect(mockExec.mock.calls[0][0]).toMatch(`wget https://foo -O ./${index.cliScannerName}`);
});
Expand Down

0 comments on commit 3bbeb0a

Please sign in to comment.