snyk-docker-plugin
Version:
Snyk CLI docker plugin
58 lines • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDistributionDigestsFromRegistry = fetchDistributionDigestsFromRegistry;
const docker_registry_v2_client_1 = require("@snyk/docker-registry-v2-client");
const Debug = require("debug");
const error_utils_1 = require("../error-utils");
const fetch_registry_provenance_attestations_1 = require("./fetch-registry-provenance-attestations");
const debug = Debug("snyk");
// Same Accept header the Docker Registry Agent uses, so OCI indexes are returned as-is.
const ACCEPT_MANIFEST = [
docker_registry_v2_client_1.contentTypes.OCI_INDEX_V1,
docker_registry_v2_client_1.contentTypes.OCI_MANIFEST_V1,
docker_registry_v2_client_1.contentTypes.MANIFEST_LIST_V2,
docker_registry_v2_client_1.contentTypes.MANIFEST_V2,
].join(",");
// The variant an architecture implies when none is given, as Docker normalizes it.
// Registries may list it explicitly (arm64/v8, arm/v7, amd64/v1) while local images
// omit it. Kept in sync with the registry client's platform matching.
const DEFAULT_VARIANTS = {
amd64: "v1",
arm64: "v8",
arm: "v7",
};
/**
* Looks up the manifest and index digests for the scanned image in its registry.
* Digests are returned only when the registry's manifest for the platform points
* at the same config digest that was scanned; otherwise (local-only or stale
* image, other platform, auth/network failure) nothing is returned.
*/
async function fetchDistributionDigestsFromRegistry(ref) {
var _a;
const { registryBase, repo, imageReference, username, password } = ref;
for (const platform of platformCandidates(ref.platform)) {
try {
const manifest = await (0, docker_registry_v2_client_1.getManifest)(registryBase, repo, imageReference, username, password, { acceptManifest: ACCEPT_MANIFEST, ...fetch_registry_provenance_attestations_1.REGISTRY_REQUEST_TIMEOUTS }, platform);
if (((_a = manifest === null || manifest === void 0 ? void 0 : manifest.config) === null || _a === void 0 ? void 0 : _a.digest) !== ref.expectedConfigDigest) {
debug(`[distribution-digests] registry manifest for ${repo}@${imageReference} does not match the scanned image; skipping`);
return undefined;
}
if (!manifest.manifestDigest) {
return undefined;
}
return { manifest: manifest.manifestDigest, index: manifest.indexDigest };
}
catch (error) {
debug(`[distribution-digests] failed to fetch manifest for ${repo}@${imageReference}: ${(0, error_utils_1.getErrorMessage)(error)}`);
}
}
return undefined;
}
function platformCandidates(platform) {
const defaultVariant = platform && DEFAULT_VARIANTS[platform.architecture];
if (platform && !platform.variant && defaultVariant) {
return [platform, { ...platform, variant: defaultVariant }];
}
return [platform];
}
//# sourceMappingURL=fetch-registry-distribution-digests.js.map