-
Notifications
You must be signed in to change notification settings - Fork 83
Add sub-action to download Grype #152
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
Changes from all commits
edc6095
14c7231
120b574
465aba3
8e2f777
737b2cc
6da62be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| exports.GRYPE_VERSION = "v0.34.4"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: "Download Grype" | ||
| author: "Anchore" | ||
| description: "Downloads the Grype binary and provides a path to execute it" | ||
| branding: | ||
| color: blue | ||
| icon: check-circle | ||
| inputs: | ||
| grype-version: | ||
| description: "A specific version of Grype to install" | ||
| required: false | ||
| run: | ||
| description: "Flag to indicate which sub-action to run" | ||
| required: false | ||
| default: "download-grype" | ||
| outputs: | ||
| cmd: | ||
| description: "An absolute path to the Grype executable" | ||
| runs: | ||
| using: "node12" | ||
| main: "../dist/index.js" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,42 +3,10 @@ const core = require("@actions/core"); | |
| const { exec } = require("@actions/exec"); | ||
| const fs = require("fs"); | ||
| const stream = require("stream"); | ||
| const { GRYPE_VERSION } = require("./GrypeVersion"); | ||
|
|
||
| const grypeBinary = "grype"; | ||
| const grypeVersion = "0.34.4"; | ||
|
|
||
| // Find all 'content-*.json' files in the directory. dirname should include the full path | ||
| function findContent(searchDir) { | ||
| let contentFiles = []; | ||
| let match = /content-.*\.json/; | ||
| var dirItems = fs.readdirSync(searchDir); | ||
| if (dirItems) { | ||
| for (let i = 0; i < dirItems.length; i++) { | ||
| if (match.test(dirItems[i])) { | ||
| contentFiles.push(`${searchDir}/${dirItems[i]}`); | ||
| } | ||
| } | ||
| } else { | ||
| core.debug("no dir content found"); | ||
| } | ||
|
|
||
| core.debug(contentFiles.toString()); | ||
| return contentFiles; | ||
| } | ||
|
|
||
| // Load the json content of each file in a list and return them as a list | ||
| function loadContent(files) { | ||
| let contents = []; | ||
| if (files) { | ||
| files.forEach((item) => contents.push(JSON.parse(fs.readFileSync(item)))); | ||
| } | ||
| return contents; | ||
| } | ||
|
|
||
| // Merge the multiple content output types into a single array | ||
| function mergeResults(contentArray) { | ||
| return contentArray.reduce((merged, n) => merged.concat(n.content), []); | ||
| } | ||
| const grypeVersion = core.getInput("grype-version") || GRYPE_VERSION; | ||
|
|
||
| async function downloadGrype(version) { | ||
| let url = `https://raw.githubusercontent.com/anchore/grype/main/install.sh`; | ||
|
|
@@ -51,7 +19,7 @@ async function downloadGrype(version) { | |
| // Make sure the tool's executable bit is set | ||
| await exec(`chmod +x ${installPath}`); | ||
|
|
||
| let cmd = `${installPath} -b ${installPath}_grype v${version}`; | ||
| let cmd = `${installPath} -b ${installPath}_grype ${version}`; | ||
| await exec(cmd); | ||
| let grypePath = `${installPath}_grype/grype`; | ||
|
|
||
|
|
@@ -68,6 +36,7 @@ async function installGrype(version) { | |
|
|
||
| // Add tool to path for this and future actions to use | ||
| core.addPath(grypePath); | ||
| return `${grypePath}/${grypeBinary}`; | ||
| } | ||
|
|
||
| function sourceInput() { | ||
|
|
@@ -248,13 +217,20 @@ module.exports = { | |
| run, | ||
| runScan, | ||
| installGrype, | ||
| mergeResults, | ||
| findContent, | ||
| loadContent, | ||
| }; | ||
|
|
||
| if (require.main === module) { | ||
| run().catch((err) => { | ||
| throw new Error(err); | ||
| }); | ||
| const entrypoint = core.getInput("run"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to use a parameter to switch functionality here instead of compiling multiple different files; there are some limitations to the |
||
| switch (entrypoint) { | ||
| case "download-grype": { | ||
| installGrype(grypeVersion).then((path) => { | ||
| core.info(`Downloaded Grype to: ${path}`); | ||
| core.setOutput("cmd", path); | ||
| }); | ||
| break; | ||
| } | ||
| default: { | ||
| run().then(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| module.exports = { | ||
| setupFiles: ["<rootDir>/.jest/setEnvVars.js"], | ||
| verbose: true, | ||
| testPathIgnorePatterns: ["action.test.js"], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,39 @@ | ||
| const child_process = require('child_process'); | ||
| const path = require('path'); | ||
| const process = require('process'); | ||
| const child_process = require("child_process"); | ||
| const os = require("os"); | ||
| const path = require("path"); | ||
| const process = require("process"); | ||
|
|
||
| // shows how the runner will run a javascript action with env / stdout protocol | ||
| test('test runs', () => { | ||
| process.env['INPUT_DEBUG'] = 'true'; | ||
| process.env['INPUT_IMAGE-REFERENCE'] = 'docker.io/alpine:latest'; | ||
| const index_path = path.join(__dirname, '../index.js'); | ||
| console.log(child_process.execSync(`node ${index_path}`, {env: process.env}).toString()); | ||
| }); | ||
| const actionPath = path.join(__dirname, "../index.js"); | ||
|
|
||
| // Execute the action, and return any outputs | ||
| function runAction(inputs) { | ||
| // reverse core.js: const val = process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''; | ||
| for (const k in inputs) { | ||
| process.env[`INPUT_${k}`.toUpperCase()] = inputs[k]; | ||
| } | ||
| // capture stdout | ||
| const stdout = child_process | ||
| .execSync(`node ${actionPath}`, { | ||
| env: process.env, | ||
| }) | ||
| .toString("utf8"); | ||
| const outputs = {}; | ||
| // reverse setOutput command calls like: | ||
| // ::set-output name=cmd::/tmp/actions/cache/grype/0.34.4/x64/grype | ||
| for (const line of stdout.split(os.EOL)) { | ||
| const groups = line.match(/::set-output name=(\w+)::(.*)$/); | ||
| if (groups && groups.length > 2) { | ||
| outputs[groups[1]] = groups[2]; | ||
| } | ||
| } | ||
| return outputs; | ||
| } | ||
|
|
||
| describe("sbom-action", () => { | ||
| it("runs download-grype", () => { | ||
| const outputs = runAction({ | ||
| run: "download-grype", | ||
| }); | ||
| expect(outputs.cmd).toBeDefined(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comparing to blank? output === undefined? Because a successful execution doesn't output anything? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is comparing to undefined, which means this output is any sort of string. |
||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.