|
| 1 | +import * as depGraphLib from '@snyk/dep-graph'; |
| 2 | +import { ManifestFile, PluginResponse, ScanResult } from 'snyk-docker-plugin'; |
| 3 | + |
| 4 | +export interface PluginMetadata { |
| 5 | + name: 'snyk-docker-plugin'; |
| 6 | + runtime: string | undefined; |
| 7 | + packageManager: string; |
| 8 | + dockerImageId: string; |
| 9 | + imageLayers: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +/** @deprecated */ |
| 13 | +export interface LegacyPluginResponse { |
| 14 | + plugin: PluginMetadata; |
| 15 | + package: DependencyTree; |
| 16 | + manifestFiles: ManifestFile[]; |
| 17 | + hashes: string[]; |
| 18 | + |
| 19 | + /** |
| 20 | + * WARNING! This field was added by kubernetes-monitor. |
| 21 | + * It is not part of the normal plugin response. */ |
| 22 | + imageMetadata: { |
| 23 | + image: string; |
| 24 | + imageTag: string; |
| 25 | + imageDigest?: string; |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +interface ExtractedFacts { |
| 30 | + depGraph: depGraphLib.DepGraph; |
| 31 | + manifestFiles?: ManifestFile[]; |
| 32 | + hashes?: string[]; |
| 33 | + imageLayers?: string[]; |
| 34 | + rootFs?: string[]; |
| 35 | + imageId?: string; |
| 36 | + imageOsReleasePrettyName?: string; |
| 37 | + platform?: string; |
| 38 | +} |
| 39 | + |
| 40 | +/** @deprecated */ |
| 41 | +export interface DependencyTree { |
| 42 | + name: string; |
| 43 | + type: string; |
| 44 | + targetOS?: { |
| 45 | + name: string; |
| 46 | + prettyName: string; |
| 47 | + version: string; |
| 48 | + }; |
| 49 | + targetFile?: string; |
| 50 | + testedFiles?: string[]; |
| 51 | + dependencies: any; |
| 52 | + version: string | undefined; |
| 53 | + dockerImageId?: string; |
| 54 | + docker?: { |
| 55 | + dockerImageId?: string; |
| 56 | + imageLayers?: string[]; |
| 57 | + rootFs?: string[]; |
| 58 | + imageName?: string; |
| 59 | + hashes?: string[]; |
| 60 | + }; |
| 61 | + rootFs?: string[]; |
| 62 | + meta?: Meta; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Meta is an object that eventually gets passed to project.monitor.meta in Registry |
| 67 | + * and gets persisted in the database. It is currently passed unmodified straight |
| 68 | + * to the database. |
| 69 | + * |
| 70 | + * The project.monitor.meta table is an HSTORE (key-value strings) so we must ensure |
| 71 | + * that we pass exactly this type of data to avoid mistakes. |
| 72 | + */ |
| 73 | +export interface Meta extends Partial<Record<string, string>> { |
| 74 | + platform?: string; |
| 75 | +} |
| 76 | + |
| 77 | +export function extractFactsFromDockerPluginResponse( |
| 78 | + pluginResponse: PluginResponse, |
| 79 | +): ExtractedFacts { |
| 80 | + const depGraph: depGraphLib.DepGraph = pluginResponse.scanResults[0].facts.find( |
| 81 | + (fact) => fact.type === 'depGraph', |
| 82 | + )?.data; |
| 83 | + |
| 84 | + const manifestFiles: |
| 85 | + | ManifestFile[] |
| 86 | + | undefined = pluginResponse.scanResults[0].facts.find( |
| 87 | + (fact) => fact.type === 'imageManifestFiles', |
| 88 | + )?.data; |
| 89 | + |
| 90 | + const hashes: string[] | undefined = pluginResponse.scanResults[0].facts.find( |
| 91 | + (fact) => fact.type === 'keyBinariesHashes', |
| 92 | + )?.data; |
| 93 | + |
| 94 | + const imageLayers: |
| 95 | + | string[] |
| 96 | + | undefined = pluginResponse.scanResults[0].facts.find( |
| 97 | + (fact) => fact.type === 'imageLayers', |
| 98 | + )?.data; |
| 99 | + |
| 100 | + const rootFs: string[] | undefined = pluginResponse.scanResults[0].facts.find( |
| 101 | + (fact) => fact.type === 'rootFs', |
| 102 | + )?.data; |
| 103 | + |
| 104 | + const imageId: string | undefined = pluginResponse.scanResults[0].facts.find( |
| 105 | + (fact) => fact.type === 'imageId', |
| 106 | + )?.data; |
| 107 | + |
| 108 | + const imageOsReleasePrettyName: |
| 109 | + | string |
| 110 | + | undefined = pluginResponse.scanResults[0].facts.find( |
| 111 | + (fact) => fact.type === 'imageOsReleasePrettyName', |
| 112 | + )?.data; |
| 113 | + |
| 114 | + const platform = pluginResponse.scanResults[0].identity.args?.platform; |
| 115 | + |
| 116 | + return { |
| 117 | + depGraph, |
| 118 | + manifestFiles, |
| 119 | + hashes, |
| 120 | + imageLayers, |
| 121 | + rootFs, |
| 122 | + imageId, |
| 123 | + imageOsReleasePrettyName, |
| 124 | + platform, |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +export function buildDockerPropertiesOnDepTree( |
| 129 | + depTree: depGraphLib.legacy.DepTree, |
| 130 | + dockerPluginFacts: ExtractedFacts, |
| 131 | + image: string, |
| 132 | +): DependencyTree { |
| 133 | + const { |
| 134 | + hashes, |
| 135 | + imageLayers, |
| 136 | + rootFs, |
| 137 | + imageId, |
| 138 | + imageOsReleasePrettyName, |
| 139 | + platform, |
| 140 | + } = dockerPluginFacts; |
| 141 | + |
| 142 | + const mutatedDepTree = depTree as DependencyTree; |
| 143 | + mutatedDepTree.docker = { |
| 144 | + hashes, |
| 145 | + imageLayers, |
| 146 | + rootFs, |
| 147 | + dockerImageId: imageId, |
| 148 | + imageName: image, |
| 149 | + }; |
| 150 | + |
| 151 | + mutatedDepTree.dockerImageId = imageId || ''; |
| 152 | + if (mutatedDepTree.targetOS) { |
| 153 | + mutatedDepTree.targetOS.prettyName = imageOsReleasePrettyName || ''; |
| 154 | + } |
| 155 | + |
| 156 | + if (!mutatedDepTree.meta) { |
| 157 | + mutatedDepTree.meta = {}; |
| 158 | + } |
| 159 | + mutatedDepTree.meta.platform = platform; |
| 160 | + |
| 161 | + return mutatedDepTree; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Produces a DependencyTree (DepsDiscoveryResult) for every ScanResult |
| 166 | + * that contains a dependency graph. ScanResults with other data are ignored |
| 167 | + * because the data cannot be resolved to a DepTree. |
| 168 | + */ |
| 169 | +export async function getApplicationDependencyTrees( |
| 170 | + applicationScanResults: ScanResult[], |
| 171 | +): Promise<DependencyTree[]> { |
| 172 | + const dependencyTrees: DependencyTree[] = []; |
| 173 | + |
| 174 | + for (const scanResult of applicationScanResults) { |
| 175 | + const appDepGraph: depGraphLib.DepGraph | undefined = scanResult.facts.find( |
| 176 | + (fact) => fact.type === 'depGraph', |
| 177 | + )?.data; |
| 178 | + |
| 179 | + // Skip this ScanResult if we could not read a dependency graph. |
| 180 | + // Some ScanResults like Java will not contain a graph but instead a list of hashes. |
| 181 | + // These are not supported by the current API. |
| 182 | + if (appDepGraph === undefined) { |
| 183 | + continue; |
| 184 | + } |
| 185 | + |
| 186 | + const appDepTree = await depGraphLib.legacy.graphToDepTree( |
| 187 | + appDepGraph, |
| 188 | + appDepGraph.pkgManager.name, |
| 189 | + ); |
| 190 | + |
| 191 | + if (!appDepTree.name || !appDepTree.type) { |
| 192 | + continue; |
| 193 | + } |
| 194 | + |
| 195 | + const testedFiles: string[] | undefined = scanResult.facts.find( |
| 196 | + (fact) => fact.type === 'testedFiles', |
| 197 | + )?.data; |
| 198 | + |
| 199 | + dependencyTrees.push({ |
| 200 | + name: appDepTree.name, |
| 201 | + version: appDepTree.version, |
| 202 | + type: appDepTree.type, |
| 203 | + dependencies: appDepTree.dependencies, |
| 204 | + targetFile: scanResult.identity.targetFile, |
| 205 | + testedFiles, |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | + return dependencyTrees; |
| 210 | +} |
0 commit comments