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

Feat/fetch docker analyzer binary #2

Merged
merged 3 commits into from
Mar 27, 2018
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/
.idea/
.idea/
dist/
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8
4
88 changes: 88 additions & 0 deletions lib/fetch-snyk-docker-analyzer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var os = require('os');
var fs = require('fs');
var fsExtra = require('fs-extra');
var path = require('path');
var request = require('request');
var ProgressBar = require('progress');
var pkgInfo = require('../package.json')

module.exports = fetch

const version = pkgInfo['snyk-docker-analyzer']['version'];

function getBinaryName() {
var arch = os.arch();
if (arch !== 'x64') {
throw new Error(`Unsupported arch ${arch} - only amd64 is supported`);
}
var suffix = '';
var platform = os.platform();
if (platform === 'win32') {
platform = 'windows';
suffix = '.exe';
}
return `snyk-docker-analyzer-${platform}-amd64${suffix}`;
}

function getBinaryLocalPath() {
var name = getBinaryName();
return path.join(__dirname, '../dist/', version, name)
}

function createBinaryPath(binaryPath) {
var binaryDirName = path.dirname(binaryPath);
fsExtra.ensureDirSync(binaryDirName);
}

function fetch(binaryPath) {
return new Promise((resolve, reject) => {
try {
var binaryPath = getBinaryLocalPath()
if (fs.existsSync(binaryPath)) {
return resolve(binaryPath);
}
createBinaryPath(binaryPath);
const snyk_docker_analyzer_url = `https://s3.amazonaws.com/snyk-docker-analyzer-releases/${version}/${getBinaryName()}`;

var bar;
const LOCAL_SNYK_DOCKER_ANALYZER_EXCEUTION_PERMISSION = 0755
const req = request(snyk_docker_analyzer_url);
req
.on('response', function (res) {
if (res.statusCode >= 400) {
var err = new Error('Bad HTTP response for snyk-docker-analyzer download');
err.statusCode = res.statusCode;
reject(err);
return;
}

var total = parseInt(res.headers['content-length'], 10);

bar = new ProgressBar(` downloading ${getBinaryName()} [:bar] :rate/Kbps :percent :etas remaining`, {
complete: '=',
incomplete: '.',
width: 20,
total: total / 1000
});
})
.on('data', function (chunk) {
if (bar) {
bar.tick(chunk.length / 1000);
}
})
.on('error', function (err) {
console.log(err)
reject(err)
})
.on('end', function () {
console.log('\n');
fs.renameSync(binaryPath + '.part', binaryPath);
fs.chmodSync(binaryPath, LOCAL_SNYK_DOCKER_ANALYZER_EXCEUTION_PERMISSION);
resolve(binaryPath);
})
.pipe(fs.createWriteStream(binaryPath + '.part'))
} catch (err) {
reject(err);
}
})
}
35 changes: 21 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
var fs = require('fs');
var path = require('path');

var subProcess = require('./sub-process');
var fetchSnykDockerAnalyzer = require('./fetch-snyk-docker-analyzer');

module.exports = {
inspect: inspect,
};

function inspect(root, targetFile) {
return Promise.all([
getMetaData(root, targetFile),
getDependencies(root, targetFile),
])
.then(function (result) {
return {
plugin: result[0],
package: result[1],
};
});
return fetchSnykDockerAnalyzer()
.then(function (analyzerBinaryPath) {
return Promise.all([
getMetaData(root, targetFile),
getDependencies(analyzerBinaryPath, root, targetFile),
])
.then(function (result) {
return {
plugin: result[0],
package: result[1],
};
});
})
.catch(function (err) {
console.log(err);
})
}

function getMetaData(root, targetFile) {
console.log(targetFile)
return subProcess.execute('docker', ['version'], {cwd: root})
.then(function (output) {
var runtime;
Expand All @@ -39,9 +46,9 @@ function getMetaData(root, targetFile) {



function getDependencies(command, targetFile) {
function getDependencies(analyzerBinaryPath, command, targetFile) {
return subProcess.execute(
'snyk-docker-cli',
analyzerBinaryPath,
buildArgs(targetFile)
)
.then(function (output) {
Expand Down Expand Up @@ -108,4 +115,4 @@ function buildArgs(targetFile) {
function pathToPosix(fpath) {
var parts = fpath.split(path.sep);
return parts.join(path.posix.sep);
}
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
"test": "tap test/*.test.js --timeout=300",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"snyk-docker-analyzer": {
"version": "1.0.2"
},
"author": "snyk.io",
"license": "Apache-2.0",
"devDependencies": {
"jscs": "^3.0.7",
"semantic-release": "^6.3.6",
"tap": "^10.7.0",
"tap-only": "0.0.5"
},
"dependencies": {
"fs-extra": "^5.0.0",
"pkginfo": "^0.4.1",
"progress": "^2.0.0",
"request": "^2.85.0"
}
}