|
| 1 | +import { ExtractedLayers, HistoryEntry } from "../extractor/types"; |
| 2 | +import { LayerAttributionEntry } from "../facts"; |
| 3 | +import { getApkDbFileContent } from "../inputs/apk/static"; |
| 4 | +import { getAptDbFileContent } from "../inputs/apt/static"; |
| 5 | +import { getChiselManifestContent } from "../inputs/chisel/static"; |
| 6 | +import { getRpmDbFileContent } from "../inputs/rpm/static"; |
| 7 | +import { analyze as apkAnalyze } from "./package-managers/apk"; |
| 8 | +import { analyze as aptAnalyze } from "./package-managers/apt"; |
| 9 | +import { analyze as chiselAnalyze } from "./package-managers/chisel"; |
| 10 | +import { analyze as rpmAnalyze } from "./package-managers/rpm"; |
| 11 | +import { AnalysisType } from "./types"; |
| 12 | + |
| 13 | +export interface LayerAttributionResult { |
| 14 | + entries: LayerAttributionEntry[]; |
| 15 | + pkgLayerMap: Map<string, { layerIndex: number; diffID: string }>; |
| 16 | +} |
| 17 | + |
| 18 | +function buildHistoryInstructions( |
| 19 | + history: HistoryEntry[] | null | undefined, |
| 20 | +): string[] { |
| 21 | + if (!history) { |
| 22 | + return []; |
| 23 | + } |
| 24 | + return history.filter((h) => !h.empty_layer).map((h) => h.created_by ?? ""); |
| 25 | +} |
| 26 | + |
| 27 | +function pkgKey(name: string, version: string): string { |
| 28 | + return `${name}@${version}`; |
| 29 | +} |
| 30 | + |
| 31 | +async function parseLayerPackages( |
| 32 | + layer: ExtractedLayers, |
| 33 | + analysisType: AnalysisType, |
| 34 | + targetImage: string, |
| 35 | +): Promise<Map<string, true>> { |
| 36 | + const result = new Map<string, true>(); |
| 37 | + |
| 38 | + if (analysisType === AnalysisType.Apk) { |
| 39 | + const content = getApkDbFileContent(layer); |
| 40 | + if (!content) { |
| 41 | + return result; |
| 42 | + } |
| 43 | + const analysis = await apkAnalyze(targetImage, content); |
| 44 | + for (const pkg of analysis.Analysis) { |
| 45 | + result.set(pkgKey(pkg.Name, pkg.Version), true); |
| 46 | + } |
| 47 | + } else if (analysisType === AnalysisType.Apt) { |
| 48 | + const aptFiles = getAptDbFileContent(layer); |
| 49 | + if (!aptFiles.dpkgFile) { |
| 50 | + return result; |
| 51 | + } |
| 52 | + const analysis = await aptAnalyze(targetImage, aptFiles); |
| 53 | + for (const pkg of analysis.Analysis) { |
| 54 | + result.set(pkgKey(pkg.Name, pkg.Version), true); |
| 55 | + } |
| 56 | + } else if (analysisType === AnalysisType.Rpm) { |
| 57 | + const pkgs = await getRpmDbFileContent(layer); |
| 58 | + if (!pkgs.length) { |
| 59 | + return result; |
| 60 | + } |
| 61 | + const analysis = await rpmAnalyze(targetImage, pkgs, []); |
| 62 | + for (const pkg of analysis.Analysis) { |
| 63 | + result.set(pkgKey(pkg.Name, pkg.Version), true); |
| 64 | + } |
| 65 | + } else if (analysisType === AnalysisType.Chisel) { |
| 66 | + const pkgs = getChiselManifestContent(layer); |
| 67 | + if (!pkgs.length) { |
| 68 | + return result; |
| 69 | + } |
| 70 | + const analysis = await chiselAnalyze(targetImage, pkgs); |
| 71 | + for (const pkg of analysis.Analysis) { |
| 72 | + result.set(pkgKey(pkg.Name, pkg.Version), true); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +export async function computeLayerAttribution( |
| 80 | + orderedLayers: ExtractedLayers[], |
| 81 | + analysisType: AnalysisType, |
| 82 | + rootFsLayers: string[], |
| 83 | + manifestLayers: string[], |
| 84 | + history: HistoryEntry[] | null | undefined, |
| 85 | + targetImage: string, |
| 86 | +): Promise<LayerAttributionResult> { |
| 87 | + const instructions = buildHistoryInstructions(history); |
| 88 | + const entries: LayerAttributionEntry[] = []; |
| 89 | + const pkgLayerMap = new Map<string, { layerIndex: number; diffID: string }>(); |
| 90 | + const limit = Math.min(orderedLayers.length, rootFsLayers.length); |
| 91 | + |
| 92 | + let previousPkgs = new Map<string, true>(); |
| 93 | + |
| 94 | + for (let i = 0; i < limit; i++) { |
| 95 | + const diffID = rootFsLayers[i]; |
| 96 | + const digest = manifestLayers[i]; |
| 97 | + const instruction = instructions[i]; |
| 98 | + |
| 99 | + const currentPkgs = await parseLayerPackages( |
| 100 | + orderedLayers[i], |
| 101 | + analysisType, |
| 102 | + targetImage, |
| 103 | + ); |
| 104 | + if (currentPkgs.size === 0) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + const newPkgs: string[] = []; |
| 109 | + for (const key of currentPkgs.keys()) { |
| 110 | + if (!previousPkgs.has(key)) { |
| 111 | + newPkgs.push(key); |
| 112 | + pkgLayerMap.set(key, { layerIndex: i, diffID }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + const removedPkgs: string[] = []; |
| 117 | + for (const key of previousPkgs.keys()) { |
| 118 | + if (!currentPkgs.has(key)) { |
| 119 | + removedPkgs.push(key); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (newPkgs.length > 0 || removedPkgs.length > 0) { |
| 124 | + const entry: LayerAttributionEntry = { |
| 125 | + layerIndex: i, |
| 126 | + diffID, |
| 127 | + packages: newPkgs, |
| 128 | + }; |
| 129 | + if (digest) { |
| 130 | + entry.digest = digest; |
| 131 | + } |
| 132 | + if (instruction) { |
| 133 | + entry.instruction = instruction; |
| 134 | + } |
| 135 | + if (removedPkgs.length > 0) { |
| 136 | + entry.removedPackages = removedPkgs; |
| 137 | + } |
| 138 | + entries.push(entry); |
| 139 | + } |
| 140 | + |
| 141 | + previousPkgs = currentPkgs; |
| 142 | + } |
| 143 | + |
| 144 | + return { entries, pkgLayerMap }; |
| 145 | +} |
0 commit comments