-
Notifications
You must be signed in to change notification settings - Fork 26
chore: log provenance attestation fields #791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,12 @@ import { | |
| ExtractedLayers, | ||
| ExtractedLayersAndManifest, | ||
| ImageConfig, | ||
| InTotoStatement, | ||
| OciArchiveManifest, | ||
| OciImageIndex, | ||
| OciManifestInfo, | ||
| OciPlatformInfo, | ||
| ProvenanceAttestation, | ||
| } from "../types"; | ||
|
|
||
| const debug = Debug("snyk"); | ||
|
|
@@ -56,7 +58,8 @@ export async function extractArchive( | |
| const metadata = await extractMetadata(ociArchiveFilesystemPath); | ||
|
|
||
| // Determine which manifest and layers we need | ||
| const { manifest, imageConfig } = resolveManifestAndConfig(metadata, options); | ||
| const { manifest, imageConfig, provenanceAttestations } = | ||
| resolveManifestAndConfig(metadata, options); | ||
|
|
||
| // Get the list of layer digests we need to extract | ||
| const requiredLayerDigests = new Set( | ||
|
|
@@ -114,6 +117,7 @@ export async function extractArchive( | |
| layers: filteredLayers, | ||
| manifest, | ||
| imageConfig, | ||
| provenanceAttestations, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -122,6 +126,7 @@ interface ArchiveMetadata { | |
| manifests: Record<string, OciArchiveManifest>; | ||
| indexFiles: Record<string, OciImageIndex>; | ||
| configs: ImageConfig[]; | ||
| rawBlobs: Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -140,6 +145,7 @@ async function extractMetadata( | |
| const configs: ImageConfig[] = []; | ||
| let mainIndexFile: OciImageIndex | undefined; | ||
| const indexFiles: Record<string, OciImageIndex> = {}; | ||
| const rawBlobs: Record<string, unknown> = {}; | ||
|
|
||
| tarExtractor.on("entry", async (header, stream, next) => { | ||
| try { | ||
|
|
@@ -148,6 +154,9 @@ async function extractMetadata( | |
|
|
||
| if (isMainIndexFile(normalizedHeaderName)) { | ||
| mainIndexFile = await streamToJson<OciImageIndex>(stream); | ||
| debug( | ||
| `[provenance-poc] Raw index.json: ${JSON.stringify(mainIndexFile, null, 2)}`, | ||
| ); | ||
| } else if ( | ||
| isBlobPath(normalizedHeaderName) && | ||
| (header.size === undefined || header.size <= MAX_JSON_SIZE_BYTES) | ||
|
|
@@ -158,6 +167,8 @@ async function extractMetadata( | |
|
|
||
| if (jsonContent !== undefined) { | ||
| const digest = getDigestFromPath(normalizedHeaderName); | ||
| rawBlobs[digest] = jsonContent; | ||
|
|
||
| if (isArchiveManifest(jsonContent)) { | ||
| manifests[digest] = jsonContent; | ||
| } else if (isImageIndexFile(jsonContent)) { | ||
|
|
@@ -180,7 +191,7 @@ async function extractMetadata( | |
| }); | ||
|
|
||
| tarExtractor.on("finish", () => { | ||
| resolve({ mainIndexFile, manifests, indexFiles, configs }); | ||
| resolve({ mainIndexFile, manifests, indexFiles, configs, rawBlobs }); | ||
| }); | ||
|
|
||
| tarExtractor.on("error", (error) => { | ||
|
|
@@ -357,6 +368,7 @@ function resolveManifestAndConfig( | |
| ): { | ||
| manifest: OciArchiveManifest; | ||
| imageConfig: ImageConfig; | ||
| provenanceAttestations: ProvenanceAttestation[]; | ||
| } { | ||
| const filteredConfigs = metadata.configs.filter((config) => { | ||
| return config?.os !== "unknown" || config?.architecture !== "unknown"; | ||
|
|
@@ -391,7 +403,15 @@ function resolveManifestAndConfig( | |
| ); | ||
| } | ||
|
|
||
| return { manifest, imageConfig }; | ||
| const provenanceAttestations = extractProvenanceAttestations(metadata); | ||
|
|
||
| if (provenanceAttestations.length === 0) { | ||
| debug( | ||
| "[provenance-poc] No provenance attestations found in this image", | ||
| ); | ||
| } | ||
|
|
||
| return { manifest, imageConfig, provenanceAttestations }; | ||
| } | ||
|
|
||
| function getManifest( | ||
|
|
@@ -517,6 +537,114 @@ function getImageConfig( | |
| ); | ||
| } | ||
|
|
||
| function extractProvenanceAttestations( | ||
| metadata: ArchiveMetadata, | ||
| ): ProvenanceAttestation[] { | ||
| const attestations: ProvenanceAttestation[] = []; | ||
|
|
||
| debug( | ||
| `[provenance-poc] Scanning ${metadata.mainIndexFile!.manifests.length} descriptors in image index`, | ||
| ); | ||
|
|
||
| for (const descriptor of metadata.mainIndexFile!.manifests) { | ||
| debug( | ||
| `[provenance-poc] Descriptor: digest=${descriptor.digest}, mediaType=${descriptor.mediaType}, ` + | ||
| `platform=${JSON.stringify(descriptor.platform)}, annotations=${JSON.stringify(descriptor.annotations)}`, | ||
| ); | ||
|
|
||
| const isAttestationManifest = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this just for provenance attestation? Are there other types that could end up with unknown architecture and "attestation-manifest" annotation? |
||
| descriptor.platform?.architecture === "unknown" && | ||
| descriptor.annotations?.["vnd.docker.reference.type"] === | ||
| "attestation-manifest"; | ||
|
|
||
| if (!isAttestationManifest) { | ||
| continue; | ||
| } | ||
|
|
||
| debug( | ||
| `[provenance-poc] Found attestation manifest descriptor: ${descriptor.digest}`, | ||
| ); | ||
|
|
||
| const nestedManifest = metadata.rawBlobs[descriptor.digest]; | ||
| if (!nestedManifest) { | ||
| debug( | ||
| `[provenance-poc] Could not find blob for attestation manifest ${descriptor.digest}`, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| debug( | ||
| `[provenance-poc] Attestation manifest content: ${JSON.stringify(nestedManifest, null, 2)}`, | ||
| ); | ||
|
|
||
| const attestationManifest = nestedManifest as OciArchiveManifest; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would malformed attestations be an issue here if they can't be casted to OciArchiveManifest? |
||
| if (!attestationManifest.layers || !Array.isArray(attestationManifest.layers)) { | ||
| debug( | ||
| `[provenance-poc] Attestation manifest ${descriptor.digest} has no layers array`, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| const attestation: ProvenanceAttestation = { | ||
| attestationManifestDigest: descriptor.digest, | ||
| mediaType: descriptor.mediaType, | ||
| annotations: descriptor.annotations || {}, | ||
| provenanceLayers: [], | ||
| }; | ||
|
|
||
| for (const layer of attestationManifest.layers) { | ||
| debug( | ||
| `[provenance-poc] Attestation layer: digest=${layer.digest}, mediaType=${layer.mediaType}, ` + | ||
| `annotations=${JSON.stringify(layer.annotations)}`, | ||
| ); | ||
|
|
||
| const isProvenanceLayer = | ||
| layer.annotations?.["in-toto.io/kind"] === "provenance" || | ||
| layer.mediaType === "application/vnd.in-toto+json"; | ||
|
|
||
| const provenanceLayer: ProvenanceAttestation["provenanceLayers"][number] = { | ||
| digest: layer.digest, | ||
| mediaType: layer.mediaType, | ||
| annotations: layer.annotations, | ||
| }; | ||
|
|
||
| if (isProvenanceLayer) { | ||
| debug( | ||
| `[provenance-poc] Found provenance layer: ${layer.digest}`, | ||
| ); | ||
|
|
||
| const inTotoBlob = metadata.rawBlobs[layer.digest]; | ||
| if (inTotoBlob) { | ||
| debug( | ||
| `[provenance-poc] In-toto statement content: ${JSON.stringify(inTotoBlob, null, 2)}`, | ||
| ); | ||
| provenanceLayer.inTotoStatement = inTotoBlob as InTotoStatement; | ||
| } else { | ||
| debug( | ||
| `[provenance-poc] Could not find blob for provenance layer ${layer.digest}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| attestation.provenanceLayers.push(provenanceLayer); | ||
| } | ||
|
|
||
| attestations.push(attestation); | ||
| } | ||
|
|
||
| debug( | ||
| `[provenance-poc] Found ${attestations.length} provenance attestation(s)`, | ||
| ); | ||
|
|
||
| if (attestations.length > 0) { | ||
| debug( | ||
| `[provenance-poc] Full attestation data: ${JSON.stringify(attestations, null, 2)}`, | ||
| ); | ||
| } | ||
|
|
||
| return attestations; | ||
| } | ||
|
|
||
| function getBestMatchForPlatform<T>( | ||
| manifests: T[], | ||
| platformInfo: OciPlatformInfo, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { ApplicationFiles } from "./analyzer/applications/types"; | |
| import { JarFingerprint } from "./analyzer/types"; | ||
| import { DockerFileAnalysis } from "./dockerfile/types"; | ||
| import { OCIDistributionMetadata } from "./extractor/oci-distribution-metadata"; | ||
| import { ProvenanceAttestation } from "./extractor/types"; | ||
| import { | ||
| AutoDetectedUserInstructions, | ||
| ImageNameInfo, | ||
|
|
@@ -104,6 +105,11 @@ export interface OCIDistributionMetadataFact { | |
| data: OCIDistributionMetadata; | ||
| } | ||
|
|
||
| export interface ProvenanceAttestationsFact { | ||
| type: "provenanceAttestations"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will also need to add this to the enum here: https://github.com/snyk/snyk-docker-plugin/blob/main/components/common.yaml#L17 |
||
| data: ProvenanceAttestation[]; | ||
| } | ||
|
|
||
| export interface PlatformFact { | ||
| type: "platform"; | ||
| data: string; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would mainIndexFile ever be undefined?