-
Notifications
You must be signed in to change notification settings - Fork 6
fix: add better transport control for OCI registries and properly clone them internally #202
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
5 commits
Select commit
Hold shift + click to select a range
e499059
fix: add better transport control for OCI registries and properly clo…
Racer159 5cb56b7
Merge branch 'main' into fix/add-better-transport-control
Racer159 ce27750
ordering doesn't affect outcome
Racer159 967a461
more tests with insecure skip verify is unset
Racer159 3adef5e
add logger nil check
Racer159 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,203 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2024-Present Defense Unicorns | ||
|
|
||
| package oci | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "oras.land/oras-go/v2/registry/remote/auth" | ||
| "oras.land/oras-go/v2/registry/remote/retry" | ||
| ) | ||
|
|
||
| func TestNewOrasRemote_TransportIsolation(t *testing.T) { | ||
| retryTransport := retry.DefaultClient.Transport | ||
| platform := PlatformForArch(testArch) | ||
|
|
||
| first, err := NewOrasRemote("example.com/first:latest", platform) | ||
| require.NoError(t, err) | ||
| second, err := NewOrasRemote("example.com/second:latest", platform) | ||
| require.NoError(t, err) | ||
|
|
||
| firstClient, ok := first.repo.Client.(*auth.Client) | ||
| require.True(t, ok) | ||
| secondClient, ok := second.repo.Client.(*auth.Client) | ||
| require.True(t, ok) | ||
| require.NotSame(t, firstClient.Client, secondClient.Client) | ||
| require.NotSame(t, firstClient.Cache, secondClient.Cache) | ||
| require.NotSame(t, first.progTransport, second.progTransport) | ||
| require.NotSame(t, first.progTransport.Base, second.progTransport.Base) | ||
| require.Same(t, retryTransport, retry.DefaultClient.Transport) | ||
| } | ||
|
|
||
| func TestWithTransport_SurvivesProgressChanges(t *testing.T) { | ||
| transport := &http.Transport{} | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithTransport(transport), | ||
| ) | ||
| require.NoError(t, err) | ||
| require.NotSame(t, transport, remote.progTransport.Base) | ||
| configuredTransport, ok := remote.progTransport.Base.(*http.Transport) | ||
| require.True(t, ok) | ||
|
|
||
| client, ok := remote.repo.Client.(*auth.Client) | ||
| require.True(t, ok) | ||
| require.Same(t, remote.progTransport, client.Client.Transport) | ||
|
|
||
| remote.SetProgressWriter(&TestProgressWriter{}) | ||
| require.Same(t, configuredTransport, remote.progTransport.Base) | ||
| require.Same(t, remote.progTransport, client.Client.Transport) | ||
|
|
||
| remote.ClearProgressWriter() | ||
| require.Same(t, configuredTransport, remote.progTransport.Base) | ||
| require.Same(t, remote.progTransport, client.Client.Transport) | ||
| } | ||
|
|
||
| func TestWithTransport_UsedForRequests(t *testing.T) { | ||
| server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| transport, ok := server.Client().Transport.(*http.Transport) | ||
| require.True(t, ok) | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithTransport(transport), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| client, ok := remote.repo.Client.(*auth.Client) | ||
| require.True(t, ok) | ||
| response, err := client.Client.Get(server.URL) | ||
| require.NoError(t, err) | ||
| require.NoError(t, response.Body.Close()) | ||
| require.Equal(t, http.StatusNoContent, response.StatusCode) | ||
| } | ||
|
|
||
| func TestWithUserAgent_SurvivesRepositorySetup(t *testing.T) { | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithUserAgent("zarf/test"), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| client, ok := remote.repo.Client.(*auth.Client) | ||
| require.True(t, ok) | ||
| require.Equal(t, "zarf/test", client.Header.Get("User-Agent")) | ||
| require.NotNil(t, client.Credential) | ||
| } | ||
|
|
||
| func TestWithLogger_Nil(t *testing.T) { | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithLogger(nil), | ||
| ) | ||
| require.NoError(t, err) | ||
| require.Nil(t, remote.log) | ||
| } | ||
|
|
||
| func TestWithInsecureSkipVerify_ClonesTransport(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| tlsConfig *tls.Config | ||
| }{ | ||
| {name: "nil TLS config"}, | ||
| {name: "existing TLS config", tlsConfig: &tls.Config{ServerName: "registry.example.com"}}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| transport := &http.Transport{TLSClientConfig: tt.tlsConfig} | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithTransport(transport), | ||
| WithInsecureSkipVerify(true), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| configured, ok := remote.progTransport.Base.(*http.Transport) | ||
| require.True(t, ok) | ||
| require.NotSame(t, transport, configured) | ||
| require.NotNil(t, configured.TLSClientConfig) | ||
| require.True(t, configured.TLSClientConfig.InsecureSkipVerify) | ||
| require.False(t, transport.TLSClientConfig.InsecureSkipVerify) | ||
| if tt.tlsConfig != nil { | ||
| require.NotSame(t, tt.tlsConfig, configured.TLSClientConfig) | ||
| require.Equal(t, tt.tlsConfig.ServerName, configured.TLSClientConfig.ServerName) | ||
| require.False(t, tt.tlsConfig.InsecureSkipVerify) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWithInsecureSkipVerify_OrderIndependent(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| insecure bool | ||
| insecureFirst bool | ||
| }{ | ||
| {name: "enable after transport", insecure: true, insecureFirst: false}, | ||
| {name: "enable before transport", insecure: true, insecureFirst: true}, | ||
| {name: "disable after transport", insecure: false, insecureFirst: false}, | ||
| {name: "disable before transport", insecure: false, insecureFirst: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !tt.insecure}} | ||
| mods := []Modifier{WithTransport(transport), WithInsecureSkipVerify(tt.insecure)} | ||
| if tt.insecureFirst { | ||
| mods[0], mods[1] = mods[1], mods[0] | ||
| } | ||
|
|
||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| mods..., | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| configured, ok := remote.progTransport.Base.(*http.Transport) | ||
| require.True(t, ok) | ||
| require.Equal(t, tt.insecure, configured.TLSClientConfig.InsecureSkipVerify) | ||
| require.Equal(t, !tt.insecure, transport.TLSClientConfig.InsecureSkipVerify) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWithTransport_PreservesInsecureSkipVerifyWhenUnset(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| insecure bool | ||
| }{ | ||
| {name: "secure", insecure: false}, | ||
| {name: "insecure", insecure: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: tt.insecure}} | ||
| remote, err := NewOrasRemote( | ||
| "example.com/repository:latest", | ||
| PlatformForArch(testArch), | ||
| WithTransport(transport), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| configured, ok := remote.progTransport.Base.(*http.Transport) | ||
| require.True(t, ok) | ||
| require.Equal(t, tt.insecure, configured.TLSClientConfig.InsecureSkipVerify) | ||
| }) | ||
| } | ||
| } |
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.