-
Notifications
You must be signed in to change notification settings - Fork 3
oci: extract artifact-agnostic primitives into oci/artifact (Phase 0, THV-0077) #133
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /* | ||
| Package artifact provides artifact-agnostic OCI primitives shared by the | ||
| ToolHive ecosystem: reproducible tar archive creation and extraction, | ||
| reproducible gzip compression, OCI platform helpers, and pull-hardening | ||
| (size/count/digest validation) for registry operations. | ||
|
|
||
| These primitives are independent of any particular artifact type (skills, | ||
| plugins, etc.). Artifact-specific media types, labels, and annotations live in | ||
| the packages that define those artifacts (for example oci/skills). | ||
|
|
||
| # Reproducible Archives | ||
|
|
||
| CreateTar and Compress produce byte-stable output for identical input, which is | ||
| what makes artifact digests deterministic: | ||
|
|
||
| data, err := artifact.CompressTar(files, artifact.DefaultTarOptions(), artifact.DefaultGzipOptions()) | ||
|
|
||
| # Platform Helpers | ||
|
|
||
| PlatformString and ParsePlatform convert between OCI platform values and their | ||
| "os/arch" or "os/arch/variant" string form. | ||
|
|
||
| # Pull Hardening | ||
|
|
||
| ValidatingTarget wraps an oras.Target and enforces size and structure limits on | ||
| pushed content, defending against OOM and resource exhaustion from malicious | ||
| registries during pull operations. | ||
|
|
||
| # Stability | ||
|
|
||
| This package is Alpha. Breaking changes are possible between minor versions. | ||
| */ | ||
| package artifact |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package artifact | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| // PlatformString returns the platform in "os/arch" or "os/arch/variant" format. | ||
| func PlatformString(p ocispec.Platform) string { | ||
| s := p.OS + "/" + p.Architecture | ||
| if p.Variant != "" { | ||
| s += "/" + p.Variant | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // ParsePlatform parses a platform string in "os/arch" or "os/arch/variant" format. | ||
| func ParsePlatform(s string) (ocispec.Platform, error) { | ||
| parts := strings.Split(s, "/") | ||
| if len(parts) < 2 || len(parts) > 3 { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid platform format: %q (expected os/arch or os/arch/variant)", s) | ||
| } | ||
| osName := strings.TrimSpace(parts[0]) | ||
| arch := strings.TrimSpace(parts[1]) | ||
| if osName == "" || arch == "" { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid platform format: %q (os and arch cannot be empty)", s) | ||
| } | ||
| p := ocispec.Platform{OS: osName, Architecture: arch} | ||
| if len(parts) == 3 { | ||
| variant := strings.TrimSpace(parts[2]) | ||
| if variant == "" { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid platform format: %q (variant cannot be empty)", s) | ||
| } | ||
| p.Variant = variant | ||
| } | ||
| return p, nil | ||
| } | ||
|
|
||
| // OS and architecture constants for OCI platform specifications. | ||
| const ( | ||
| // OSLinux is the Linux OS identifier used in OCI platform specs. | ||
| OSLinux = "linux" | ||
| // ArchAMD64 is the x86-64 architecture identifier used in OCI platform specs. | ||
| ArchAMD64 = "amd64" | ||
| // ArchARM64 is the 64-bit ARM architecture identifier used in OCI platform specs. | ||
| ArchARM64 = "arm64" | ||
| ) | ||
|
|
||
| // DefaultPlatforms are the default platforms for artifacts. | ||
| // These cover most Kubernetes clusters. | ||
| var DefaultPlatforms = []ocispec.Platform{ | ||
| {OS: OSLinux, Architecture: ArchAMD64}, | ||
| {OS: OSLinux, Architecture: ArchARM64}, | ||
| } |
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,128 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package artifact | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // testArchARM is the 32-bit ARM architecture identifier used in test platform specs. | ||
| const testArchARM = "arm" | ||
|
|
||
| func TestParsePlatform(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want ocispec.Platform | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "os/arch", | ||
| input: "linux/amd64", | ||
| want: ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, | ||
| }, | ||
| { | ||
| name: "os/arch/variant", | ||
| input: "linux/arm/v7", | ||
| want: ocispec.Platform{OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, | ||
| }, | ||
| { | ||
| name: "fewer than 2 parts (no slash)", | ||
| input: "linuxamd64", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "more than 3 parts", | ||
| input: "linux/amd64/v8/extra", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty os", | ||
| input: "/amd64", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty arch", | ||
| input: "linux/", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty variant", | ||
| input: "linux/arm/", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := ParsePlatform(tt.input) | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestPlatformString(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| platform ocispec.Platform | ||
| want string | ||
| }{ | ||
| { | ||
| name: "os/arch", | ||
| platform: ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, | ||
| want: "linux/amd64", | ||
| }, | ||
| { | ||
| name: "os/arch/variant", | ||
| platform: ocispec.Platform{OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, | ||
| want: "linux/arm/v7", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, tt.want, PlatformString(tt.platform)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParsePlatform_PlatformString_Roundtrip(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| platforms := []ocispec.Platform{ | ||
| {OS: OSLinux, Architecture: ArchAMD64}, | ||
| {OS: OSLinux, Architecture: ArchARM64}, | ||
| {OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, | ||
| } | ||
|
|
||
| for _, p := range platforms { | ||
| parsed, err := ParsePlatform(PlatformString(p)) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, p, parsed) | ||
| } | ||
| } | ||
|
|
||
| func TestDefaultPlatforms(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| require.Len(t, DefaultPlatforms, 2) | ||
| assert.Equal(t, ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, DefaultPlatforms[0]) | ||
| assert.Equal(t, ocispec.Platform{OS: OSLinux, Architecture: ArchARM64}, DefaultPlatforms[1]) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package artifact | ||
|
|
||
| const ( | ||
| testFileA = "a.txt" | ||
| testFileB = "b.txt" | ||
| ) |
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.