Skip to content

Commit

Permalink
Merge pull request #4 from snyk/fix/send-correct-pkg-manager-type
Browse files Browse the repository at this point in the history
Fix: send correct pkg manager type
  • Loading branch information
michael-go authored Apr 1, 2018
2 parents 8dab28e + ad0ab83 commit 5ee4950
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 72 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist: trusty
sudo: false
sudo: true
notifications:
email: false
language: node_js
Expand All @@ -10,6 +10,10 @@ node_js:
cache:
directories:
- node_modules
script:
- npm install
- npm run lint
- npm test
jobs:
include:
- stage: npm release
Expand Down
25 changes: 14 additions & 11 deletions lib/fetch-snyk-docker-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = fetch
const version = pkgInfo['snyk-docker-analyzer']['version'];

function getBinaryName() {
var arch = os.arch();
var arch = os.arch();
if (arch !== 'x64') {
throw new Error(`Unsupported arch ${arch} - only amd64 is supported`);
}
Expand Down Expand Up @@ -42,27 +42,29 @@ function fetch(binaryPath) {
return resolve(binaryPath);
}
createBinaryPath(binaryPath);
const snyk_docker_analyzer_url = `https://s3.amazonaws.com/snyk-docker-analyzer-releases/${version}/${getBinaryName()}`;

const downloadUrl =
`https://s3.amazonaws.com/snyk-docker-analyzer-releases/${version}/${getBinaryName()}`; // jscs:ignore maximumLineLength

var bar;
const LOCAL_SNYK_DOCKER_ANALYZER_EXCEUTION_PERMISSION = 0755
const req = request(snyk_docker_analyzer_url);
const req = request(downloadUrl);
req
.on('response', function (res) {
if (res.statusCode >= 400) {
var err = new Error('Bad HTTP response for snyk-docker-analyzer download');
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`, {
bar = new ProgressBar(` downloading ${getBinaryName()} [:bar] :rate/Kbps :percent :etas remaining`, { // jscs:ignore maximumLineLength

complete: '=',
incomplete: '.',
width: 20,
total: total / 1000
total: total / 1000,
});
})
.on('data', function (chunk) {
Expand All @@ -71,13 +73,14 @@ function fetch(binaryPath) {
}
})
.on('error', function (err) {
console.log(err)
reject(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);
const CHMOD_WITH_EXEC = 0755;
fs.chmodSync(binaryPath, CHMOD_WITH_EXEC);
resolve(binaryPath);
})
.pipe(fs.createWriteStream(binaryPath + '.part'))
Expand Down
103 changes: 55 additions & 48 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {

function inspect(root, targetFile) {
return fetchSnykDockerAnalyzer()
.then(function (analyzerBinaryPath) {
.then(function (analyzerBinaryPath) {
return Promise.all([
getMetaData(root, targetFile),
getDependencies(analyzerBinaryPath, root, targetFile),
Expand All @@ -21,13 +21,9 @@ function inspect(root, targetFile) {
};
});
})
.catch(function (err) {
console.log(err);
})
}

function getMetaData(root, targetFile) {
console.log(targetFile)
function getMetaData(root, targetImage) {
return subProcess.execute('docker', ['version'], {cwd: root})
.then(function (output) {
var runtime;
Expand All @@ -39,21 +35,19 @@ function getMetaData(root, targetFile) {
return {
name: 'snyk-docker-plugin',
runtime: runtime,
targetFile: pathToPosix(targetFile),
targetFile: pathToPosix(targetImage),
};
});
}



function getDependencies(analyzerBinaryPath, command, targetFile) {
function getDependencies(analyzerBinaryPath, command, targetImage) {
return subProcess.execute(
analyzerBinaryPath,
buildArgs(targetFile)
buildArgs(targetImage)
)
.then(function (output) {
scanResults = JSON.parse(output);
return convertDependecies(targetFile, scanResults);
return Promise.resolve(convertDependecies(targetImage, scanResults));
})
.catch(function (error) {
if (typeof error === 'string') {
Expand All @@ -67,48 +61,61 @@ function getDependencies(analyzerBinaryPath, command, targetFile) {
}
throw new Error(errorMsg);
}

throw error;
})
}

function convertDependecies(targetFile, scanResults) {
packageSplit = targetFile.split(':')
packageName = packageSplit[0]
if (packageSplit.length == 1) {
packageVersion = 'latest'
} else {
packageVersion = packageSplit[1]
function convertDependecies(targetImage, scanResults) {
targetSplit = targetImage.split(':');
imageName = targetSplit[0];
imageVersion = targetSplit[1];
imageVersion = (imageVersion ? imageVersion : 'lateset');

root = {};
root.name = imageName;
root.version = imageVersion;
root.from = [imageName + '@' + imageVersion];

var scanResult = scanResults.filter(function (res) {
return res.Analysis && res.Analysis.length > 0;
})[0];

var pkgType;
switch (scanResult.AnalyzeType) {
case 'Apt': {
pkgType = 'deb';
break;
}
default: {
pkgType = scanResult.AnalyzeType.toLowerCase()
}
}
packageData = {}
packageData['name'] = packageName
packageData['version'] = packageVersion
packageFormatVersion = packageName + ':' + packageVersion
packageData['packageFormatVersion'] = packageFormatVersion
packageData['from'] = [packageFormatVersion]
packageData['dependencies'] = {}
scanResults.map(function (scanResult) {
analyzeType = scanResult['AnalyzeType'].toLowerCase()
dependencies = scanResult['Analysis']
dependencies.map(function (dependency) {
dependencyName = dependency['Name'].split('/').pop(0)
dependencyVersion = dependency['Version'] || dependency['Sha1']
fullDependecnyName = dependencyName + '@' + dependencyVersion
packageData['dependencies'][fullDependecnyName] = {
name: dependencyName,
version: dependencyVersion,
dependencies: [],
from: [
packageFormatVersion,
fullDependecnyName,
],
}
})
});
return packageData;
}
root.packageFormatVersion = pkgType + ':0.0.1';

pkgs = scanResult['Analysis']

root.dependencies = pkgs.reduce(function (acc, pkg) {
name = pkg['Name'].split('/').pop(0);
version = pkg['Version'];

acc[name] = {
name: name,
version: version,
dependencies: {},
from: [
root['from'][0],
name + '@' + version,
],
}
return acc;
}, {});

return root;
}

function buildArgs(targetFile) {
var args = ['analyze',targetFile];
function buildArgs(targetImage) {
var args = ['analyze', targetImage];
return args;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"snyk-docker-analyzer": {
"version": "1.0.2"
"version": "1.0.4"
},
"author": "snyk.io",
"license": "Apache-2.0",
"devDependencies": {
"jscs": "^3.0.7",
"semantic-release": "^6.3.6",
"tap": "^10.7.0",
"tap": "^11.1.3",
"tap-only": "0.0.5"
},
"dependencies": {
Expand Down
10 changes: 0 additions & 10 deletions test/inspect.test.js

This file was deleted.

Loading

0 comments on commit 5ee4950

Please sign in to comment.