Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Sources/ContainerClient/Core/ClientImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,35 @@ extension ClientImage {
if let locallyBuiltImage {
return locallyBuiltImage
}

let shortDigestReferencedImage: ClientImage? = try {
// Check if we are looking for an image referenced by its short digest.
// The assumption is that the short digest is passed like -> <name>:<short-digest>.
let r = try Reference.parse(reference)
r.normalize()

guard let shortDigestTag = r.tag else {
return nil
}

// Check if the tag is a valid short digest value
guard (try? Utility.isShortDigest(shortDigestTag)) == true else {
return nil
}

let imageName = String(reference.prefix { $0 != ":" })
// Fetch the ClientImage with the correct short digest and image name
// The second check for name is in case of duplicate short digests
let shortDigestImage: ClientImage? = try all.first(where: { image in
try Utility.trimDigest(digest: image.digest) == shortDigestTag + "..." &&
imageName == String(Self.denormalizeReference(image.reference).prefix { $0 != ":" })
})
return shortDigestImage
}()
if let shortDigestReferencedImage {
return shortDigestReferencedImage
}

// If we don't find a match, try matching `ImageDescription.name` against the given
// input string, while also checking against its normalized form.
// Return the first match.
Expand Down
11 changes: 11 additions & 0 deletions Sources/ContainerClient/Utility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public struct Utility {
}
return digest
}

public static func isShortDigest(_ digest: String) throws -> Bool {
// Short digest must be be 24 hexadecimal characters
let pattern = "^[0-9a-f]{24}$"
let regex = try Regex(pattern)
if try regex.firstMatch(in: digest) != nil {
return true
} else {
throw ContainerizationError(.invalidArgument, message: "invalid short sha digest \(digest)")
}
}

public static func validEntityName(_ name: String) throws {
let pattern = #"^[a-zA-Z0-9][a-zA-Z0-9_.-]+$"#
Expand Down