Skip to content

Commit

Permalink
Support SBOM input for scanning (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
harmw authored Apr 6, 2022
1 parent 8e8448f commit d39ae4a
Show file tree
Hide file tree
Showing 7 changed files with 2,066 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ jobs:
debug: true
severity-cutoff: "negligible"
fail-build: false

sbom:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
sbom: tests/fixtures/test_sbom.spdx.json
debug: true
fail-build: false
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ To scan a directory, add the following step:
The `path` key allows any valid path for the current project. The root of the path (`"."` in this example) is the repository root.

## Scanning an SBOM file

Use the `sbom` key to scan an SBOM file:

```yaml
- name: Create SBOM
uses: anchore/sbom-action@v0
with:
format: spdx-json
output-file: "${{ github.event.repository.name }}-sbom.spdx.json"
- name: Scan SBOM
uses: anchore/scan-action@v3
with:
sbom: "${{ github.event.repository.name }}-sbom.spdx.json"
```

## Failing a build on vulnerability severity

By default, if any vulnerability at `medium` or higher is seen, the build fails. To have the build step fail in cases where there are vulnerabilities with a severity level different than the default, set the `severity-cutoff` field to one of `low`, `high`, or `critical`:
Expand Down Expand Up @@ -99,12 +116,13 @@ Optionally, change the `fail-build` field to `false` to avoid failing the build

### Action Inputs

The only required key is `image` or `path`; all the other keys are optional. These are all the available keys to configure this action, along with its defaults:
The inputs `image`, `path`, and `sbom` are mutually exclusive to specify the source to scan; all the other keys are optional. These are all the available keys to configure this action, along with the defaults:

| Input Name | Description | Default Value |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `image` | The image to scan, this is mutually exclusive to `path` | N/A |
| `path` | The file path to scan, this is mutually exclusive to `image` | N/A |
| `image` | The image to scan | N/A |
| `path` | The file path to scan | N/A |
| `sbom` | The SBOM to scan | N/A |
| `debug` | Verbose logging output | `false` |
| `fail-build` | Fail the build if a vulnerability is found with a higher severity. That severity defaults to `"medium"` and can be set with `severity-cutoff`. | `true` |
| `acs-report-enable` | Generate a SARIF report and set the `sarif` output parameter after successful action execution. This report is compatible with GitHub Automated Code Scanning (ACS), as the artifact to upload for display as a Code Scanning Alert report. | `true` |
Expand Down
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ branding:
icon: check-circle
inputs:
image:
description: 'The image to scan. This option is mutually exclusive with "path". '
description: 'The image to scan. This option is mutually exclusive with "path" and "sbom". '
required: false
path:
description: 'The path to scan. This option is mutually exclusive with "image".'
description: 'The path to scan. This option is mutually exclusive with "image" and "sbom".'
required: false
sbom:
description: 'The SBOM file to scan. This option is mutually exclusive with "path" and "image".'
required: false
debug:
description: "Set this to any value to enable verbose debug output"
Expand Down
31 changes: 26 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,45 @@ async function installGrype(version) {
return `${grypePath}/${grypeBinary}`;
}

// Determines if multiple arguments are defined
function multipleDefined(...args) {
let defined = false;
for (const a of args) {
if (defined && a) {
return true;
}
if (a) {
defined = true;
}
}
return false;
}

function sourceInput() {
var image = core.getInput("image");
var path = core.getInput("path");
var sbom = core.getInput("sbom");

if (image && path) {
throw new Error("Cannot use both 'image' and 'path' as sources");
if (multipleDefined(image, path, sbom)) {
throw new Error(
"The following options are mutually exclusive: image, path, sbom"
);
}

if (!(image || path)) {
if (!(image || path || sbom)) {
throw new Error(
"At least one source for scanning needs to be provided. Available options are: image, and path"
"At least one source for scanning needs to be provided. Available options are: image, path and sbom"
);
}

if (image !== "") {
return image;
}

if (sbom !== "") {
return "sbom:" + sbom;
}

return "dir:" + path;
}

Expand Down Expand Up @@ -144,7 +165,7 @@ async function runScan({
core.debug(`Installing grype version ${grypeVersion}`);
await installGrype(grypeVersion);

core.debug("Image: " + source);
core.debug("Source: " + source);
core.debug("Debug Output: " + debug);
core.debug("Fail Build: " + failBuild);
core.debug("Severity Cutoff: " + severityCutoff);
Expand Down
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,45 @@ async function installGrype(version) {
return `${grypePath}/${grypeBinary}`;
}

// Determines if multiple arguments are defined
function multipleDefined(...args) {
let defined = false;
for (const a of args) {
if (defined && a) {
return true;
}
if (a) {
defined = true;
}
}
return false;
}

function sourceInput() {
var image = core.getInput("image");
var path = core.getInput("path");
var sbom = core.getInput("sbom");

if (image && path) {
throw new Error("Cannot use both 'image' and 'path' as sources");
if (multipleDefined(image, path, sbom)) {
throw new Error(
"The following options are mutually exclusive: image, path, sbom"
);
}

if (!(image || path)) {
if (!(image || path || sbom)) {
throw new Error(
"At least one source for scanning needs to be provided. Available options are: image, and path"
"At least one source for scanning needs to be provided. Available options are: image, path and sbom"
);
}

if (image !== "") {
return image;
}

if (sbom !== "") {
return "sbom:" + sbom;
}

return "dir:" + path;
}

Expand Down Expand Up @@ -129,7 +150,7 @@ async function runScan({
core.debug(`Installing grype version ${grypeVersion}`);
await installGrype(grypeVersion);

core.debug("Image: " + source);
core.debug("Source: " + source);
core.debug("Debug Output: " + debug);
core.debug("Fail Build: " + failBuild);
core.debug("Severity Cutoff: " + severityCutoff);
Expand Down
31 changes: 29 additions & 2 deletions tests/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,33 @@ describe("scan-action", () => {
});

it("errors with invalid input", () => {
const outputs = runAction({
let outputs = runAction({
image: "some-image",
path: "some-path",
});
expect(outputs.exitCode).toBe(1);
expect(outputs.stdout).toContain(
"The following options are mutually exclusive: image, path, sbom"
);
expect(outputs.stdout).not.toContain("grype");

outputs = runAction({
image: "some-image",
sbom: "some-path",
});
expect(outputs.exitCode).toBe(1);
expect(outputs.stdout).toContain(
"The following options are mutually exclusive: image, path, sbom"
);
expect(outputs.stdout).not.toContain("grype");

outputs = runAction({
path: "some-path",
sbom: "some-image",
});
expect(outputs.exitCode).toBe(1);
expect(outputs.stdout).toContain(
"Cannot use both 'image' and 'path' as sources"
"The following options are mutually exclusive: image, path, sbom"
);
expect(outputs.stdout).not.toContain("grype");
});
Expand All @@ -77,4 +97,11 @@ describe("scan-action", () => {
});
expect(outputs.stdout).toContain("Failed minimum severity level.");
});

it("runs with sbom", () => {
const outputs = runAction({
sbom: "fixtures/test_sbom.spdx.json",
});
expect(outputs.stdout).toContain("Failed minimum severity level.");
});
});
Loading

0 comments on commit d39ae4a

Please sign in to comment.