|
| 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 { InvalidArchiveError } from ".."; |
| 8 | +import { streamToJson } from "../../stream-utils"; |
| 9 | +import { PluginOptions } from "../../types"; |
| 10 | +import { extractImageLayer } from "../layer"; |
| 11 | +import { |
| 12 | + ExtractAction, |
| 13 | + ImageConfig, |
| 14 | + KanikoArchiveManifest, |
| 15 | + KanikoExtractedLayers, |
| 16 | + KanikoExtractedLayersAndManifest, |
| 17 | +} from "../types"; |
| 18 | + |
| 19 | +const debug = Debug("snyk"); |
| 20 | + |
| 21 | +/** |
| 22 | + * Retrieve the products of files content from the specified kaniko-archive. |
| 23 | + * @param kanikoArchiveFilesystemPath Path to image file saved in kaniko-archive format. |
| 24 | + * @param extractActions Array of pattern-callbacks pairs. |
| 25 | + * @param options PluginOptions |
| 26 | + * @returns Array of extracted files products sorted by the reverse order of the layers from last to first. |
| 27 | + */ |
| 28 | +export async function extractArchive( |
| 29 | + kanikoArchiveFilesystemPath: string, |
| 30 | + extractActions: ExtractAction[], |
| 31 | + _options: Partial<PluginOptions>, |
| 32 | +): Promise<KanikoExtractedLayersAndManifest> { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + const tarExtractor: Extract = extract(); |
| 35 | + const layers: Record<string, KanikoExtractedLayers> = {}; |
| 36 | + let manifest: KanikoArchiveManifest; |
| 37 | + let imageConfig: ImageConfig; |
| 38 | + |
| 39 | + tarExtractor.on("entry", async (header, stream, next) => { |
| 40 | + if (header.type === "file") { |
| 41 | + const normalizedName = normalizePath(header.name); |
| 42 | + if (isTarGzFile(normalizedName)) { |
| 43 | + try { |
| 44 | + layers[normalizedName] = await extractImageLayer( |
| 45 | + stream, |
| 46 | + extractActions, |
| 47 | + ); |
| 48 | + } catch (error) { |
| 49 | + debug(`Error extracting layer content from: '${error.message}'`); |
| 50 | + reject(new Error("Error reading tar.gz archive")); |
| 51 | + } |
| 52 | + } else if (isManifestFile(normalizedName)) { |
| 53 | + const manifestArray = await getManifestFile<KanikoArchiveManifest[]>( |
| 54 | + stream, |
| 55 | + ); |
| 56 | + |
| 57 | + manifest = manifestArray[0]; |
| 58 | + } else if (isImageConfigFile(normalizedName)) { |
| 59 | + imageConfig = await getManifestFile<ImageConfig>(stream); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + stream.resume(); // auto drain the stream |
| 64 | + next(); // ready for next entry |
| 65 | + }); |
| 66 | + |
| 67 | + tarExtractor.on("finish", () => { |
| 68 | + try { |
| 69 | + resolve( |
| 70 | + getLayersContentAndArchiveManifest(manifest, imageConfig, layers), |
| 71 | + ); |
| 72 | + } catch (error) { |
| 73 | + debug( |
| 74 | + `Error getting layers and manifest content from Kaniko archive: ${error.message}`, |
| 75 | + ); |
| 76 | + reject(new InvalidArchiveError("Invalid Kaniko archive")); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + tarExtractor.on("error", (error) => reject(error)); |
| 81 | + |
| 82 | + createReadStream(kanikoArchiveFilesystemPath) |
| 83 | + .pipe(gunzip()) |
| 84 | + .pipe(tarExtractor); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +function getLayersContentAndArchiveManifest( |
| 89 | + manifest: KanikoArchiveManifest, |
| 90 | + imageConfig: ImageConfig, |
| 91 | + layers: Record<string, KanikoExtractedLayers>, |
| 92 | +): KanikoExtractedLayersAndManifest { |
| 93 | + // skip (ignore) non-existent layers |
| 94 | + // get the layers content without the name |
| 95 | + // reverse layers order from last to first |
| 96 | + const layersWithNormalizedNames = manifest.Layers.map((layersName) => |
| 97 | + normalizePath(layersName), |
| 98 | + ); |
| 99 | + const filteredLayers = layersWithNormalizedNames |
| 100 | + .filter((layersName) => layers[layersName]) |
| 101 | + .map((layerName) => layers[layerName]) |
| 102 | + .reverse(); |
| 103 | + |
| 104 | + if (filteredLayers.length === 0) { |
| 105 | + throw new Error("We found no layers in the provided image"); |
| 106 | + } |
| 107 | + |
| 108 | + return { |
| 109 | + layers: filteredLayers, |
| 110 | + manifest, |
| 111 | + imageConfig, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Note: consumes the stream. |
| 117 | + */ |
| 118 | +async function getManifestFile<T>(stream: Readable): Promise<T> { |
| 119 | + return streamToJson<T>(stream); |
| 120 | +} |
| 121 | + |
| 122 | +function isManifestFile(name: string): boolean { |
| 123 | + return name === "manifest.json"; |
| 124 | +} |
| 125 | + |
| 126 | +function isImageConfigFile(name: string): boolean { |
| 127 | + const configRegex = new RegExp("sha256:[A-Fa-f0-9]{64}"); |
| 128 | + return configRegex.test(name); |
| 129 | +} |
| 130 | + |
| 131 | +function isTarGzFile(name: string): boolean { |
| 132 | + return basename(name).endsWith(".tar.gz"); |
| 133 | +} |
0 commit comments