|
| 1 | +import * as Debug from "debug"; |
| 2 | +import { createReadStream } from "fs"; |
| 3 | +import * as gunzip from "gunzip-maybe"; |
| 4 | +import { basename, normalize as normalizePath } from "path"; |
| 5 | +import { Readable } from "stream"; |
| 6 | +import { extract, Extract } from "tar-stream"; |
| 7 | +import { streamToJson } from "../stream-utils"; |
| 8 | + |
| 9 | +export class InvalidArchiveError extends Error { |
| 10 | + constructor(message: string) { |
| 11 | + super(); |
| 12 | + this.name = "InvalidArchiveError"; |
| 13 | + this.message = message; |
| 14 | + } |
| 15 | +} |
| 16 | +import { HashAlgorithm, PluginOptions } from "../types"; |
| 17 | +import { extractImageLayer } from "./layer"; |
| 18 | +import { |
| 19 | + ExtractAction, |
| 20 | + ExtractedLayers, |
| 21 | + ExtractedLayersAndManifest, |
| 22 | + ImageConfig, |
| 23 | + TarArchiveManifest, |
| 24 | +} from "./types"; |
| 25 | + |
| 26 | +const debug = Debug("snyk"); |
| 27 | + |
| 28 | +export interface ArchiveConfig { |
| 29 | + isLayerFile: (name: string) => boolean; |
| 30 | + isImageConfigFile: (name: string) => boolean; |
| 31 | + formatLabel: string; |
| 32 | + layerErrorType: string; |
| 33 | + extractImageId: (configValue: string) => string; |
| 34 | +} |
| 35 | + |
| 36 | +export const dockerArchiveConfig: ArchiveConfig = { |
| 37 | + isLayerFile: (name) => basename(name).endsWith(".tar"), |
| 38 | + isImageConfigFile: (name) => |
| 39 | + new RegExp("[A-Fa-f0-9]{64}\\.json").test(name), |
| 40 | + formatLabel: "Docker", |
| 41 | + layerErrorType: "tar", |
| 42 | + extractImageId: (configValue) => configValue.split(".")[0], |
| 43 | +}; |
| 44 | + |
| 45 | +export const kanikoArchiveConfig: ArchiveConfig = { |
| 46 | + isLayerFile: (name) => basename(name).endsWith(".tar.gz"), |
| 47 | + isImageConfigFile: (name) => |
| 48 | + new RegExp("sha256:[A-Fa-f0-9]{64}").test(name), |
| 49 | + formatLabel: "Kaniko", |
| 50 | + layerErrorType: "tar.gz", |
| 51 | + extractImageId: (configValue) => configValue, |
| 52 | +}; |
| 53 | + |
| 54 | +export function createExtractArchive( |
| 55 | + config: ArchiveConfig, |
| 56 | +): ( |
| 57 | + archiveFilesystemPath: string, |
| 58 | + extractActions: ExtractAction[], |
| 59 | + options: Partial<PluginOptions>, |
| 60 | +) => Promise<ExtractedLayersAndManifest> { |
| 61 | + return (archiveFilesystemPath, extractActions, _options) => |
| 62 | + new Promise((resolve, reject) => { |
| 63 | + const tarExtractor: Extract = extract(); |
| 64 | + const layers: Record<string, ExtractedLayers> = {}; |
| 65 | + let manifest: TarArchiveManifest; |
| 66 | + let imageConfig: ImageConfig; |
| 67 | + |
| 68 | + tarExtractor.on("entry", async (header, stream, next) => { |
| 69 | + if (header.type === "file") { |
| 70 | + const normalizedName = normalizePath(header.name); |
| 71 | + if (config.isLayerFile(normalizedName)) { |
| 72 | + try { |
| 73 | + layers[normalizedName] = await extractImageLayer( |
| 74 | + stream, |
| 75 | + extractActions, |
| 76 | + ); |
| 77 | + } catch (error) { |
| 78 | + debug(`Error extracting layer content from: '${error.message}'`); |
| 79 | + reject( |
| 80 | + new Error( |
| 81 | + `Error reading ${config.layerErrorType} archive`, |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + } else if (isManifestFile(normalizedName)) { |
| 86 | + const manifestArray = |
| 87 | + await getManifestFile<TarArchiveManifest[]>(stream); |
| 88 | + manifest = manifestArray[0]; |
| 89 | + } else if (config.isImageConfigFile(normalizedName)) { |
| 90 | + imageConfig = await getManifestFile<ImageConfig>(stream); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + stream.resume(); |
| 95 | + next(); |
| 96 | + }); |
| 97 | + |
| 98 | + tarExtractor.on("finish", () => { |
| 99 | + try { |
| 100 | + resolve( |
| 101 | + assembleLayersAndManifest(manifest, imageConfig, layers), |
| 102 | + ); |
| 103 | + } catch (error) { |
| 104 | + debug( |
| 105 | + `Error getting layers and manifest content from ${config.formatLabel} archive: ${error.message}`, |
| 106 | + ); |
| 107 | + reject( |
| 108 | + new InvalidArchiveError(`Invalid ${config.formatLabel} archive`), |
| 109 | + ); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + tarExtractor.on("error", (error) => reject(error)); |
| 114 | + |
| 115 | + createReadStream(archiveFilesystemPath) |
| 116 | + .on("error", (error) => reject(error)) |
| 117 | + .pipe(gunzip()) |
| 118 | + .pipe(tarExtractor); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +function assembleLayersAndManifest( |
| 123 | + manifest: TarArchiveManifest, |
| 124 | + imageConfig: ImageConfig, |
| 125 | + layers: Record<string, ExtractedLayers>, |
| 126 | +): ExtractedLayersAndManifest { |
| 127 | + const layersWithNormalizedNames = manifest.Layers.map((layerName) => |
| 128 | + normalizePath(layerName), |
| 129 | + ); |
| 130 | + const filteredLayers = layersWithNormalizedNames |
| 131 | + .filter((layerName) => layers[layerName]) |
| 132 | + .map((layerName) => layers[layerName]) |
| 133 | + .reverse(); |
| 134 | + |
| 135 | + if (filteredLayers.length === 0) { |
| 136 | + throw new Error("We found no layers in the provided image"); |
| 137 | + } |
| 138 | + |
| 139 | + return { |
| 140 | + layers: filteredLayers, |
| 141 | + manifest, |
| 142 | + imageConfig, |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +async function getManifestFile<T>(stream: Readable): Promise<T> { |
| 147 | + return streamToJson<T>(stream); |
| 148 | +} |
| 149 | + |
| 150 | +function isManifestFile(name: string): boolean { |
| 151 | + return name === "manifest.json"; |
| 152 | +} |
| 153 | + |
| 154 | +export function createGetImageIdFromManifest( |
| 155 | + config: ArchiveConfig, |
| 156 | +): (manifest: TarArchiveManifest) => string { |
| 157 | + return (manifest) => { |
| 158 | + try { |
| 159 | + const imageId = config.extractImageId(manifest.Config); |
| 160 | + if (imageId.includes(":")) { |
| 161 | + return imageId; |
| 162 | + } |
| 163 | + return `${HashAlgorithm.Sha256}:${imageId}`; |
| 164 | + } catch (err) { |
| 165 | + throw new Error("Failed to extract image ID from archive manifest"); |
| 166 | + } |
| 167 | + }; |
| 168 | +} |
| 169 | + |
| 170 | +export function getManifestLayers(manifest: TarArchiveManifest): string[] { |
| 171 | + return manifest.Layers.map((layer) => normalizePath(layer)); |
| 172 | +} |
0 commit comments