Skip to content

Commit

Permalink
refactor: make scan mode an enum (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
tembleking authored Jul 30, 2024
1 parent b214e50 commit d9114f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
50 changes: 31 additions & 19 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.

4 changes: 2 additions & 2 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, vmMode } from './src/scanner';
import { cliScannerName, cliScannerResult, cliScannerURL, executeScan, numericPriorityForSeverity, pullScanner, ScanExecutionResult, ScanMode } from './src/scanner';
import { ActionInputs, defaultSecureEndpoint } from './src/action';
import { generateSummary } from './src/summary';
import { Report } from './src/report';
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function run() {
if (retCode == 0 || retCode == 1) {
// Transform Scan Results to other formats such as SARIF

if (opts.mode == vmMode) {
if (opts.mode == ScanMode.vm) {
await processScanResult(scanResult, opts);
}
} else {
Expand Down
33 changes: 15 additions & 18 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, iacMode, scannerURLForVersion, vmMode } from './scanner';
import { cliScannerResult, cliScannerURL, ComposeFlags, ScanMode, scannerURLForVersion } from './scanner';

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

Expand All @@ -23,7 +23,7 @@ interface ActionInputParameters {
severityAtLeast?: string;
groupByPackage: boolean;
extraParameters: string;
mode: string;
mode: ScanMode;
recursive: boolean;
minimumSeverity: string;
iacScanPath: string;
Expand Down Expand Up @@ -73,7 +73,7 @@ export class ActionInputs {
severityAtLeast: core.getInput('severity-at-least') || undefined,
groupByPackage: core.getInput('group-by-package') == 'true',
extraParameters: core.getInput('extra-parameters'),
mode: core.getInput('mode') || vmMode,
mode: ScanMode.fromString(core.getInput('mode')) || ScanMode.vm,
recursive: core.getInput('recursive') == 'true',
minimumSeverity: core.getInput('minimum-severity'),
iacScanPath: core.getInput('iac-scan-path') || './',
Expand All @@ -93,7 +93,7 @@ export class ActionInputs {
}

get mode() {
return this.params.mode || vmMode;
return this.params.mode;
}

get stopOnProcessingError() {
Expand Down Expand Up @@ -134,12 +134,12 @@ export class ActionInputs {
throw new Error("Sysdig Secure Token is required for standard execution, please set your token or remove the standalone input.");
}

if (params.mode && params.mode == vmMode && !params.imageTag) {
if (params.mode && params.mode == ScanMode.vm && !params.imageTag) {
core.setFailed("image-tag is required for VM mode.");
throw new Error("image-tag is required for VM mode.");
}

if (params.mode && params.mode == iacMode && params.iacScanPath == "") {
if (params.mode && params.mode == ScanMode.iac && params.iacScanPath == "") {
core.setFailed("iac-scan-path can't be empty, please specify the path you want to scan your manifest resources.");
throw new Error("iac-scan-path can't be empty, please specify the path you want to scan your manifest resources.");
}
Expand Down Expand Up @@ -196,27 +196,24 @@ export class ActionInputs {
flags += ` ${this.params.extraParameters}`;
}

if (this.params.mode && this.params.mode == iacMode) {
if (this.params.mode == ScanMode.iac) {
flags += ` --iac`;
}

if (this.params.recursive && this.params.mode == iacMode) {
flags += ` -r`;
}
if (this.params.recursive) {
flags += ` -r`;
}
if (this.params.minimumSeverity) {
flags += ` -f=${this.params.minimumSeverity}`;
}

if (this.params.minimumSeverity && this.params.mode == iacMode) {
flags += ` -f=${this.params.minimumSeverity}`;
flags += ` ${this.params.iacScanPath}`;
}

if (this.params.mode && this.params.mode == vmMode) {
if (this.params.mode == ScanMode.vm) {
flags += ` --json-scan-result=${cliScannerResult}`
flags += ` ${this.params.imageTag}`;
}

if (this.params.mode && this.params.mode == iacMode) {
flags += ` ${this.params.iacScanPath}`;
}

return {
envvars: envvars,
flags: flags
Expand Down
17 changes: 15 additions & 2 deletions src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ export const cliScannerName = "sysdig-cli-scanner"
export const cliScannerResult = "scan-result.json"
export const cliScannerURL = `${cliScannerURLBase}/${cliScannerVersion}/${cliScannerOS}/${cliScannerArch}/${cliScannerName}`

export const vmMode = "vm"
export const iacMode = "iac"
export enum ScanMode {
vm = "vm",
iac = "iac",
}

export namespace ScanMode {
export function fromString(str: string): ScanMode | undefined {
switch (str.toLowerCase()) {
case "vm":
return ScanMode.vm;
case "iac":
return ScanMode.iac;
}
}
}

export async function pullScanner(scannerURL: string) {
let start = performance.now();
Expand Down

0 comments on commit d9114f7

Please sign in to comment.