Skip to content

Commit

Permalink
Merge pull request #29 from bryanlatten/eslint-airbnb-base
Browse files Browse the repository at this point in the history
Minor refactor: including airbnb-base eslint
  • Loading branch information
bryanlatten authored Jun 1, 2020
2 parents fa6e44d + a9a19d6 commit a3899e6
Show file tree
Hide file tree
Showing 9 changed files with 1,688 additions and 1,319 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": "eslint:recommended",
"extends": "airbnb-base",
"env": {
"browser": false,
"node": true
},
"parser": "babel-eslint",
"globals": {
"require": true,
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mhart/alpine-node:10 as base
FROM mhart/alpine-node:12 as base

# Ensure application code makes it into the /app directory
COPY ./ /app/
Expand Down
89 changes: 39 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

const clc = require('cli-color');
const fs = require('fs');
const getStdin = require('get-stdin');
const path = require('path');
const program = require('commander');
const yaml = require('js-yaml');

const fs = require('fs');
const path = require('path');
const policyEngine = require('./lib/policyengine.js');

const DEFAULT_POLICY = 'default_policy.yaml';

var clc = require('cli-color');

program
.description('Checks a Docker image\'s properties against a policy')
.option('-p, --policy <file>', 'image policy, defaults to ./default_policy.yaml')
Expand All @@ -26,11 +22,10 @@ program
.option('-r, --range <ports>', 'low-high ports that are allowed')
.option('--layers_max <count>', 'maximum number of filesystem layers')
.option('--layers_warning <count>', 'warning number of filesystem layers')

.parse(process.argv);

var policyFile = program.policy || `./${DEFAULT_POLICY}`;
var policyPath = path.resolve(policyFile)
const policyFile = program.policy || `./${DEFAULT_POLICY}`;
const policyPath = path.resolve(policyFile);

if (!fs.existsSync(policyPath)) {
console.log('[%s] policy does not exist: %s', clc.redBright('Error'), clc.whiteBright(policyFile));
Expand All @@ -43,83 +38,77 @@ if (fs.statSync(policyPath).isDirectory()) {
process.exit(1);
}

getStdin().then(str => {

if(!str.trim()) {
console.log('\n--- Docker inspect output required ---');
program.help()
getStdin().then((str) => {
if (!str.trim()) {
console.log('\n--- Docker inspect output required ---\n');
program.outputHelp();
process.exit(1);
}

var input = JSON.parse(str);
const input = JSON.parse(str);

if (!Array.isArray(input) || input[0] === undefined) {
console.log('[%s] malformed input detected', clc.redBright('Error'));
process.exit(1);
}

var container = input[0];
const container = input[0];

console.log("\nScanning <%s>", container.Id);
console.log("Docker Build: " + container.DockerVersion);
console.log("Parent: " + container.Parent);
console.log("\nUsing policy <%s>\n", policyFile);
console.log('\nScanning <%s>', container.Id);
console.log(`Docker Build: ${container.DockerVersion}`);
console.log(`Parent: ${container.Parent}`);
console.log('\nUsing policy <%s>\n', policyFile);

var policy = require("./lib/policy.js")();
var loadedPolicy;
let loadedPolicy;

try {
loadedPolicy = yaml.safeLoad(fs.readFileSync(policyPath, 'utf8'))
}
catch (err) {
loadedPolicy = yaml.safeLoad(fs.readFileSync(policyPath, 'utf8'));
} catch (err) {
console.log('[%s] unable to parse policy YAML:', clc.redBright('Error'), err.reason);
process.exit(1);
}

var overrideMsgs = [];
loadedPolicy = policy.applyOverrides(loadedPolicy, program, overrideMsgs);
const overrideMsgs = [];
loadedPolicy = policyEngine.applyOverrides(loadedPolicy, program, overrideMsgs);

overrideMsgs.forEach(function(text) {
overrideMsgs.forEach((text) => {
console.log('<%s> %s', clc.cyanBright('Policy Override'), text);
});

if (overrideMsgs.length > 0) {
console.log(''); // Inserts a new line
}

var testStatus = policy.execute(loadedPolicy, container);
const testStatus = policyEngine.execute(loadedPolicy, container);

testStatus.getMessages().forEach(function(msg) {
testStatus.getMessages().forEach((msg) => {
const severity = msg[0];
const text = msg[1];

var severity = msg[0];
var text = msg[1];

switch(severity) {
switch (severity) {
case 'exception':
console.log("[%s] %s", clc.magenta('EXCEPTION'), text);
console.log('[%s] %s', clc.magenta('EXCEPTION'), text);
break;
case 'success':
console.log("[%s] %s", clc.whiteBright('PASS'), text);
console.log('[%s] %s', clc.whiteBright('PASS'), text);
break;
case 'failure':
console.log("[%s] %s", clc.red('FAIL'), text);
console.log('[%s] %s', clc.red('FAIL'), text);
break;
case 'warning':
console.log("[%s] %s", clc.yellowBright('WARN'), text);
console.log('[%s] %s', clc.yellowBright('WARN'), text);
break;
default:
console.log("[%s %s", clc.blue('UNKOWN'), text);
console.log('[%s %s', clc.blue('UNKOWN'), text);
break;
} // switch severity

}
});

var testIsPassing = testStatus.isPassing();
var testMessage = (testIsPassing)
? clc.green('PASS')
: clc.redBright('FAIL');
const testIsPassing = testStatus.isPassing();
const testMessage = (testIsPassing)
? clc.green('PASS')
: clc.redBright('FAIL');

console.log("\nStatus [%s]\n", testMessage);
console.log('\nStatus [%s]\n', testMessage);
process.exit(testIsPassing ? 0 : 1);

}); // getStdin
});
Loading

0 comments on commit a3899e6

Please sign in to comment.