-
Notifications
You must be signed in to change notification settings - Fork 127
Reapply "Add OCI image support to Linux scanner (#1708)" (#1716) #1717
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1fc97b6
Reapply "Add OCI image support to Linux scanner (#1708)" (#1716)
jasonpaulos 368faef
Merge branch 'main' into users/jasonpaulos/reapply-1708
jasonpaulos 8cb7652
Reapply "Add Docker archive support to Linux scanner (#1711)" (#1715)…
jasonpaulos 5088040
Handle parse failures gracefully
jasonpaulos 5242099
Merge branch 'main' into users/jasonpaulos/reapply-1708
jasonpaulos d4e5b86
Fix merge error
jasonpaulos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
src/Microsoft.ComponentDetection.Detectors/linux/Contracts/SourceClassExtensions.cs
This file contains hidden or 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,26 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Linux.Contracts; | ||
|
|
||
| using System.Text.Json; | ||
|
|
||
| /// <summary> | ||
| /// Extends the auto-generated <see cref="SourceClass"/> with a method to | ||
| /// deserialize its untyped <see cref="Metadata"/> into a | ||
| /// strongly-typed <see cref="SyftSourceMetadata"/>. | ||
| /// </summary> | ||
| public partial class SourceClass | ||
| { | ||
| /// <summary> | ||
| /// Deserializes the <see cref="Metadata"/> property into a <see cref="SyftSourceMetadata"/>. | ||
| /// Returns null if <see cref="Metadata"/> is null or not a <see cref="JsonElement"/>. | ||
| /// </summary> | ||
| /// <returns>A deserialized <see cref="SyftSourceMetadata"/> instance, or null.</returns> | ||
| internal SyftSourceMetadata? GetSyftSourceMetadata() | ||
| { | ||
| if (this.Metadata is JsonElement element) | ||
| { | ||
| return JsonSerializer.Deserialize<SyftSourceMetadata>(element.GetRawText()); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.ComponentDetection.Detectors/linux/Contracts/SyftSourceLayer.cs
This file contains hidden or 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,18 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Linux.Contracts; | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// Represents a single layer in the image source metadata from Syft output. | ||
| /// </summary> | ||
| internal class SyftSourceLayer | ||
| { | ||
| [JsonPropertyName("mediaType")] | ||
| public string? MediaType { get; set; } | ||
|
|
||
| [JsonPropertyName("digest")] | ||
| public string? Digest { get; set; } | ||
|
|
||
| [JsonPropertyName("size")] | ||
| public long? Size { get; set; } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/Microsoft.ComponentDetection.Detectors/linux/Contracts/SyftSourceMetadata.cs
This file contains hidden or 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,46 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Linux.Contracts; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// Represents the metadata from a Syft scan source of type "image". | ||
| /// Contains image details such as layers, labels, tags, and image ID. | ||
| /// Deserialized from the <c>source.metadata</c> field in Syft JSON output, | ||
| /// which is typed as <c>object</c> in the auto-generated <see cref="SourceClass"/>. | ||
| /// </summary> | ||
| internal class SyftSourceMetadata | ||
| { | ||
| [JsonPropertyName("userInput")] | ||
| public string? UserInput { get; set; } | ||
|
|
||
| [JsonPropertyName("imageID")] | ||
| public string? ImageId { get; set; } | ||
|
|
||
| [JsonPropertyName("manifestDigest")] | ||
| public string? ManifestDigest { get; set; } | ||
|
|
||
| [JsonPropertyName("mediaType")] | ||
| public string? MediaType { get; set; } | ||
|
|
||
| [JsonPropertyName("tags")] | ||
| public string[]? Tags { get; set; } | ||
|
|
||
| [JsonPropertyName("imageSize")] | ||
| public long? ImageSize { get; set; } | ||
|
|
||
| [JsonPropertyName("layers")] | ||
| public SyftSourceLayer[]? Layers { get; set; } | ||
|
|
||
| [JsonPropertyName("repoDigests")] | ||
| public string[]? RepoDigests { get; set; } | ||
|
|
||
| [JsonPropertyName("architecture")] | ||
| public string? Architecture { get; set; } | ||
|
|
||
| [JsonPropertyName("os")] | ||
| public string? Os { get; set; } | ||
|
|
||
| [JsonPropertyName("labels")] | ||
| public Dictionary<string, string>? Labels { get; set; } | ||
| } |
This file contains hidden or 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
120 changes: 120 additions & 0 deletions
120
src/Microsoft.ComponentDetection.Detectors/linux/ImageReference.cs
This file contains hidden or 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,120 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Linux; | ||
|
|
||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Specifies the type of image reference. | ||
| /// </summary> | ||
| internal enum ImageReferenceKind | ||
| { | ||
| /// <summary> | ||
| /// A Docker image reference (e.g., "node:latest", "sha256:abc123"). | ||
| /// </summary> | ||
| DockerImage, | ||
|
|
||
| /// <summary> | ||
| /// An OCI Image Layout directory on disk (e.g., "oci-dir:/path/to/image"). | ||
| /// </summary> | ||
| OciLayout, | ||
|
|
||
| /// <summary> | ||
| /// An OCI archive (tarball) file on disk (e.g., "oci-archive:/path/to/image.tar"). | ||
| /// </summary> | ||
| OciArchive, | ||
|
|
||
| /// <summary> | ||
| /// A Docker archive (tarball) file on disk created by "docker save" (e.g., "docker-archive:/path/to/image.tar"). | ||
| /// </summary> | ||
| DockerArchive, | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents a parsed image reference from the scan input, with its type and cleaned reference string. | ||
| /// </summary> | ||
| internal class ImageReference | ||
| { | ||
| private const string OciDirPrefix = "oci-dir:"; | ||
| private const string OciArchivePrefix = "oci-archive:"; | ||
| private const string DockerArchivePrefix = "docker-archive:"; | ||
|
|
||
| /// <summary> | ||
| /// Gets the original input string as provided by the user. | ||
| /// </summary> | ||
| public required string OriginalInput { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the cleaned reference string with any scheme prefix removed. | ||
| /// For Docker images, this is lowercased. For file paths, case is preserved. | ||
| /// </summary> | ||
| public required string Reference { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the kind of image reference. | ||
| /// </summary> | ||
| public required ImageReferenceKind Kind { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Parses an input image string into an <see cref="ImageReference"/>. | ||
| /// </summary> | ||
| /// <param name="input">The raw image input string.</param> | ||
| /// <returns>A parsed <see cref="ImageReference"/>.</returns> | ||
| public static ImageReference Parse(string input) | ||
| { | ||
| if (input.StartsWith(OciDirPrefix, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var path = input[OciDirPrefix.Length..]; | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| { | ||
| throw new ArgumentException($"Input with '{OciDirPrefix}' prefix must include a path.", nameof(input)); | ||
| } | ||
|
|
||
| return new ImageReference | ||
| { | ||
| OriginalInput = input, | ||
| Reference = path, | ||
| Kind = ImageReferenceKind.OciLayout, | ||
| }; | ||
| } | ||
|
|
||
| if (input.StartsWith(OciArchivePrefix, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var path = input[OciArchivePrefix.Length..]; | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| { | ||
| throw new ArgumentException($"Input with '{OciArchivePrefix}' prefix must include a path.", nameof(input)); | ||
| } | ||
|
|
||
|
jasonpaulos marked this conversation as resolved.
|
||
| return new ImageReference | ||
| { | ||
| OriginalInput = input, | ||
| Reference = path, | ||
| Kind = ImageReferenceKind.OciArchive, | ||
| }; | ||
| } | ||
|
|
||
| if (input.StartsWith(DockerArchivePrefix, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var path = input[DockerArchivePrefix.Length..]; | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| { | ||
| throw new ArgumentException($"Input with '{DockerArchivePrefix}' prefix must include a path.", nameof(input)); | ||
| } | ||
|
|
||
| return new ImageReference | ||
| { | ||
| OriginalInput = input, | ||
| Reference = path, | ||
| Kind = ImageReferenceKind.DockerArchive, | ||
| }; | ||
| } | ||
|
|
||
| #pragma warning disable CA1308 | ||
| return new ImageReference | ||
| { | ||
| OriginalInput = input, | ||
| Reference = input.ToLowerInvariant(), | ||
| Kind = ImageReferenceKind.DockerImage, | ||
| }; | ||
| #pragma warning restore CA1308 | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.