Skip to content

Commit c26f858

Browse files
committed
fix: use central parsing to create oci metadata
1 parent 9abf986 commit c26f858

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

lib/analyzer/image-inspector.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,11 @@ async function getImageArchive(
268268

269269
function extractImageDetails(targetImage: string): ImageDetails {
270270
const parsed = parseImageReference(targetImage);
271-
const hostname = parsed.registryForPull;
272-
const isDockerHub = hostname === "registry-1.docker.io";
273-
const imageName =
274-
isDockerHub && !parsed.repository.includes("/")
275-
? "library/" + parsed.repository
276-
: parsed.repository;
277-
const tag = parsed.tailReferenceForPull;
278-
return { hostname, imageName, tag };
271+
return {
272+
hostname: parsed.registryForPull,
273+
imageName: parsed.normalizedRepository,
274+
tag: parsed.tailReferenceForPull,
275+
};
279276
}
280277

281278
function isLocalImageSameArchitecture(

lib/extractor/oci-distribution-metadata.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseAll } from "@swimlane/docker-reference";
1+
import { parseImageReference } from "../image-reference";
22

33
export interface OCIDistributionMetadata {
44
// Must be a valid host, including port if one was used to pull the image.
@@ -32,17 +32,15 @@ export function constructOCIDisributionMetadata({
3232
| OCIDistributionMetadata
3333
| undefined {
3434
try {
35-
const ref = parseAll(imageName);
36-
if (!ref.domain || !ref.repository) {
37-
return;
38-
}
39-
35+
const parsed = parseImageReference(imageName);
36+
// Normalize the hostname. Note this function uses slightly different logic from the one in the image-reference.ts file.
37+
const hostname = parsed.registry ? parsed.registry : "docker.io";
4038
const metadata: OCIDistributionMetadata = {
41-
registryHost: ref.domain,
42-
repository: ref.repository,
39+
registryHost: hostname,
40+
repository: parsed.normalizedRepository,
4341
manifestDigest,
4442
indexDigest,
45-
imageTag: ref.tag,
43+
imageTag: parsed.tag,
4644
};
4745

4846
if (!ociDistributionMetadataIsValid(metadata)) {

lib/image-reference.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,20 @@ export class ParsedImageReference {
5454
return ref;
5555
}
5656

57+
/**
58+
* Whether the image is from Docker Hub.
59+
* This is true if the registry is "registry-1.docker.io" or "docker.io" or undefined.
60+
*/
61+
get isDockerHub(): boolean {
62+
return this.registry === "registry-1.docker.io" || this.registry === "docker.io" || this.registry === undefined;
63+
}
64+
5765
/**
5866
* The registry to use for pulling the image.
5967
* If the registry is not set, use Docker Hub.
6068
*/
6169
get registryForPull(): string {
62-
if (this.registry === "docker.io" || this.registry === undefined) {
70+
if (this.isDockerHub || this.registry === undefined) {
6371
return "registry-1.docker.io";
6472
}
6573
return this.registry;
@@ -74,6 +82,17 @@ export class ParsedImageReference {
7482
get tailReferenceForPull(): string {
7583
return this.digest ?? this.tag ?? "latest";
7684
}
85+
86+
/**
87+
* The normalized repository name.
88+
* If the image is from Docker Hub and the repository does not have a namespace, add the default namespace "library".
89+
*/
90+
get normalizedRepository(): string {
91+
if (this.isDockerHub && !this.repository.includes("/")) {
92+
return "library/" + this.repository;
93+
}
94+
return this.repository;
95+
}
7796
}
7897

7998
/**

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"@snyk/docker-registry-v2-client": "^2.24.0",
3434
"@snyk/rpm-parser": "^3.4.1",
3535
"@snyk/snyk-docker-pull": "^3.15.0",
36-
"@swimlane/docker-reference": "^2.0.1",
3736
"adm-zip": "^0.5.16",
3837
"chalk": "^2.4.2",
3938
"debug": "^4.4.3",
@@ -62,7 +61,6 @@
6261
"@commitlint/config-conventional": "^17.0.2",
6362
"@mongodb-js/zstd": "^2.0.1",
6463
"@semantic-release/exec": "^6.0.3",
65-
"semantic-release": "^19.0.5",
6664
"@types/adm-zip": "^0.4.34",
6765
"@types/debug": "^4.1.5",
6866
"@types/jest": "^29.5.5",
@@ -73,6 +71,7 @@
7371
"jest": "^29.7.0",
7472
"npm-run-all": "^4.1.5",
7573
"prettier": "^2.7.1",
74+
"semantic-release": "^19.0.5",
7675
"ts-jest": "^29.1.1",
7776
"ts-node": "^10.2.1",
7877
"tsc-watch": "^4.2.8",

0 commit comments

Comments
 (0)