snyk-docker-plugin
Version:
Snyk CLI docker plugin
73 lines • 3.13 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.detect = void 0;
const Debug = require("debug");
const os_release_1 = require("../../inputs/os-release");
const types_1 = require("../../types");
const release_analyzer_1 = require("./release-analyzer");
const debug = Debug("snyk");
const releaseDetectors = {
[types_1.OsReleaseFilePath.Linux]: release_analyzer_1.tryOSRelease,
// Fallback for the case where the same file exists in different location or is a symlink to the other location
[types_1.OsReleaseFilePath.LinuxFallback]: release_analyzer_1.tryOSRelease,
// Generic fallback
[types_1.OsReleaseFilePath.Lsb]: release_analyzer_1.tryLsbRelease,
// Fallbacks for specific older distributions
[types_1.OsReleaseFilePath.Debian]: release_analyzer_1.tryDebianVersion,
[types_1.OsReleaseFilePath.Alpine]: release_analyzer_1.tryAlpineRelease,
[types_1.OsReleaseFilePath.Oracle]: release_analyzer_1.tryOracleRelease,
[types_1.OsReleaseFilePath.RedHat]: release_analyzer_1.tryRedHatRelease,
[types_1.OsReleaseFilePath.Centos]: release_analyzer_1.tryCentosRelease,
};
async function detect(extractedLayers, dockerfileAnalysis) {
/**
* We want to detect whether the OS release file existed, but it just could not be parsed successfully.
* This is so that we can distinguish between images with multiple "os-release" files - some of them
* may fail to parse while others will succeed. This will depend purely on the order of our handlers.
* We want to run all handlers and only then decide if detection succeeded or not.
*/
let hadOsReleaseFile = false;
let osRelease = null;
for (const [type, handler] of Object.entries(releaseDetectors)) {
const osReleaseFile = (0, os_release_1.getOsReleaseStatic)(extractedLayers, type);
if (!osReleaseFile) {
continue;
}
hadOsReleaseFile = true;
try {
osRelease = await handler(osReleaseFile);
}
catch (err) {
debug(`Malformed OS release file: ${err.message}`);
}
if (osRelease) {
break;
}
}
if (!osRelease && hadOsReleaseFile) {
throw new Error("Failed to parse OS release file");
}
if (!osRelease) {
if (dockerfileAnalysis && dockerfileAnalysis.baseImage === "scratch") {
// If the docker file was build from a scratch image
// then we don't have a known OS
osRelease = { name: "scratch", version: "0.0", prettyName: "" };
}
else {
osRelease = { name: "unknown", version: "0.0", prettyName: "" };
}
}
// Oracle Linux identifies itself as "ol"
if (osRelease.name.trim() === "ol") {
osRelease.name = "oracle";
}
// Support round version. ie change SLES 15 to SLES 15.0
if (osRelease.name.trim() === "sles" &&
osRelease.version &&
!osRelease.version.includes(".")) {
osRelease.version += ".0";
}
return osRelease;
}
exports.detect = detect;
//# sourceMappingURL=static.js.map
;