Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from snyk/feat/extract-red-hat-content-sets
[CAP-120] - Extracting content manifests from layers
- Loading branch information
Showing
9 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
ExtractAction, | ||
ExtractedLayers, | ||
RedHatRepo, | ||
RedHatRepos, | ||
} from "../../extractor/types"; | ||
import { streamToJson } from "../../stream-utils"; | ||
|
||
export const getRedHatReposContentAction: ExtractAction = { | ||
actionName: "redhat-content-manifests", | ||
filePathMatches: isRedHatContentManifest, | ||
callback: streamToJson, | ||
}; | ||
|
||
export function getRedHatReposFromExtractedLayers( | ||
extractedLayers: ExtractedLayers, | ||
): RedHatRepos { | ||
const reposByLayer = {}; | ||
for (const filePath in extractedLayers) { | ||
if (isRedHatContentManifest(filePath)) { | ||
const content = extractedLayers[filePath][ | ||
"redhat-content-manifests" | ||
] as any; | ||
const layerIndex = content?.metadata?.image_layer_index; | ||
const repos = content?.content_sets; | ||
if (layerIndex && repos) { | ||
reposByLayer[layerIndex] = repos as RedHatRepo; | ||
} | ||
} | ||
} | ||
return reposByLayer as RedHatRepos; | ||
} | ||
|
||
function isRedHatContentManifest(filePath: string): boolean { | ||
return filePath.startsWith("/root/buildinfo/content_manifests/"); | ||
} |
Binary file not shown.
17 changes: 17 additions & 0 deletions
17
test/fixtures/extracted-layers/with-content-manifests.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"/root/buildinfo/content_manifests/jenkins-agent-maven-35-rhel7-container-v3.11.346-2.json": { | ||
"redhat-content-manifests": { | ||
"metadata": { | ||
"icm_version": 1, | ||
"icm_spec": "https://raw.githubusercontent.com/containerbuildsystem/atomic-reactor/master/atomic_reactor/schemas/content_manifest.json", | ||
"image_layer_index": 6 | ||
}, | ||
"content_sets": [ | ||
"rhel-7-server-ose-3.11-rpms", | ||
"rhel-server-rhscl-7-rpms", | ||
"rhel-7-server-rpms" | ||
], | ||
"image_contents": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { extractImageContent } from "../../../lib/extractor"; | ||
import { getRedHatReposContentAction } from "../../../lib/inputs/rhel/static"; | ||
import { ImageType } from "../../../lib/types"; | ||
import { getFixture } from "../../util/index"; | ||
|
||
describe("extractImageContent", () => { | ||
it("extracts red hat repositories information from layers", async () => { | ||
const extractedContent = await extractImageContent( | ||
ImageType.DockerArchive, | ||
getFixture("docker-archives/docker-save/nginx-with-buildinfo.tar"), | ||
[getRedHatReposContentAction], | ||
); | ||
|
||
const numOfFoundFiles = Object.keys(extractedContent.extractedLayers) | ||
.length; | ||
expect(numOfFoundFiles).toBe(1); | ||
|
||
expect( | ||
extractedContent.extractedLayers[ | ||
"/root/buildinfo/content_manifests/jenkins-agent-maven-35-rhel7-container-v3.11.346-2.json" | ||
]["redhat-content-manifests"], | ||
).toMatchObject({ | ||
metadata: { | ||
icm_version: 1, | ||
icm_spec: | ||
"https://raw.githubusercontent.com/containerbuildsystem/atomic-reactor/master/atomic_reactor/schemas/content_manifest.json", | ||
image_layer_index: 6, | ||
}, | ||
content_sets: [ | ||
"rhel-7-server-ose-3.11-rpms", | ||
"rhel-server-rhscl-7-rpms", | ||
"rhel-7-server-rpms", | ||
], | ||
image_contents: [], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getRedHatReposFromExtractedLayers } from "../../../../lib/inputs/rhel/static"; | ||
import { getObjFromFixture } from "../../../util"; | ||
|
||
describe("getRedHatReposFromExtractedLayers()", () => { | ||
it("correctly gets list of repos and image layer index", () => { | ||
const extractedLayers = getObjFromFixture( | ||
"extracted-layers/with-content-manifests.json", | ||
); | ||
|
||
const repos = getRedHatReposFromExtractedLayers(extractedLayers); | ||
expect(repos).toMatchObject({ | ||
"6": [ | ||
"rhel-7-server-ose-3.11-rpms", | ||
"rhel-server-rhscl-7-rpms", | ||
"rhel-7-server-rpms", | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters