Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract scanner into class #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
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
47 changes: 33 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,40 @@ 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
9 changes: 7 additions & 2 deletions 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 @@ -57,7 +58,7 @@ describe("input parsing", () => {
let opts = ActionInputs.parseActionInputs();

expect(opts.params).toEqual({
cliScannerURL: index.cliScannerURL,
cliScannerURL: index.defaultScannerURL,
cliScannerVersion: "",
registryUser: "",
registryPassword: "",
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
Loading