-
Notifications
You must be signed in to change notification settings - Fork 154
feat(oci): support insecure OCI registry #1374
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
Open
l-qing
wants to merge
4
commits into
tektoncd:main
Choose a base branch
from
l-qing:feat/support-insecure-oci-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−7
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e484b16
feat(oci): support insecure OCI registry
l-qing 590d65d
chore(oci): add security warning for insecure OCI registry connections
l-qing ec9b775
fix(oci): pass remote options to SignedUnknown calls
l-qing 806fde2
docs(oci): add security warning for insecure registry flag
l-qing 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
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 |
|---|---|---|
|
|
@@ -15,11 +15,14 @@ package oci | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/tektoncd/chains/pkg/chains/formats" | ||
| "github.com/tektoncd/chains/pkg/chains/formats/simple" | ||
|
|
@@ -41,6 +44,7 @@ import ( | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
| logtesting "knative.dev/pkg/logging/testing" | ||
| "knative.dev/pkg/webhook/certificates/resources" | ||
| ) | ||
|
|
||
| const namespace = "oci-test" | ||
|
|
@@ -239,3 +243,133 @@ func TestBackend_StorePayload(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestBackend_StorePayload_Insecure tests the StorePayload functionality with both secure and insecure configurations. | ||
| // It verifies that: | ||
| // 1. In secure mode, the backend should reject connections to untrusted registries due to TLS certificate verification failure | ||
| // 2. In insecure mode, the backend should successfully connect and upload signatures, bypassing TLS verification | ||
| func TestBackend_StorePayload_Insecure(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🌟 |
||
| // Setup test registry with self-signed certificate | ||
| s, registryURL := setupTestRegistry(t) | ||
| defer s.Close() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| insecure bool | ||
| wantErr bool | ||
| wantErrMsg string | ||
| description string | ||
| }{ | ||
| { | ||
| name: "secure mode with untrusted certificate", | ||
| insecure: false, | ||
| wantErr: true, | ||
| wantErrMsg: "tls: failed to verify certificate: x509:", | ||
| description: "Should reject connection to registry with self-signed certificate", | ||
| }, | ||
| { | ||
| name: "insecure mode bypassing TLS verification", | ||
| insecure: true, | ||
| wantErr: false, | ||
| wantErrMsg: "", | ||
| description: "Should successfully connect and upload signature despite untrusted certificate", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Initialize backend with test configuration | ||
| b := &Backend{ | ||
| cfg: config.Config{ | ||
| Storage: config.StorageConfigs{ | ||
| OCI: config.OCIStorageConfig{ | ||
| Insecure: tc.insecure, | ||
| }, | ||
| }, | ||
| }, | ||
| getAuthenticator: func(context.Context, objects.TektonObject, kubernetes.Interface) (remote.Option, error) { | ||
| return remote.WithAuthFromKeychain(authn.DefaultKeychain), nil | ||
| }, | ||
| } | ||
|
|
||
| // Create test reference and payload | ||
| ref := registryURL + "/task/test@sha256:0000000000000000000000000000000000000000000000000000000000000000" | ||
| simple := simple.SimpleContainerImage{ | ||
| Critical: payload.Critical{ | ||
| Identity: payload.Identity{ | ||
| DockerReference: registryURL + "/task/test", | ||
| }, | ||
| Image: payload.Image{ | ||
| DockerManifestDigest: strings.Split(ref, "@")[1], | ||
| }, | ||
| Type: payload.CosignSignatureType, | ||
| }, | ||
| } | ||
|
|
||
| rawPayload, err := json.Marshal(simple) | ||
| if err != nil { | ||
| t.Fatalf("failed to marshal payload: %v", err) | ||
| } | ||
|
|
||
| // Test StorePayload functionality | ||
| ctx := logtesting.TestContextWithLogger(t) | ||
| err = b.StorePayload(ctx, objects.NewTaskRunObjectV1(tr), rawPayload, "test", config.StorageOpts{ | ||
| PayloadFormat: formats.PayloadTypeSimpleSigning, | ||
| }) | ||
|
|
||
| // Validate test results based on expected outcome | ||
| if tc.wantErr { | ||
| if err == nil { | ||
| t.Errorf("%s: expected error but got nil", tc.description) | ||
| return | ||
| } | ||
| if tc.wantErrMsg != "" && !strings.Contains(err.Error(), tc.wantErrMsg) { | ||
| t.Errorf("%s: error message mismatch\ngot: %v\nwant: %v", tc.description, err, tc.wantErrMsg) | ||
| } | ||
| } else if err != nil { | ||
| t.Errorf("%s: expected success but got error: %v", tc.description, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // setupTestRegistry sets up a test registry with TLS configuration | ||
| func setupTestRegistry(t *testing.T) (*httptest.Server, string) { | ||
| t.Helper() | ||
|
|
||
| cert, err := generateSelfSignedCert() | ||
| if err != nil { | ||
| t.Fatalf("failed to generate self-signed cert: %v", err) | ||
| } | ||
|
|
||
| reg := registry.New() | ||
| s := httptest.NewUnstartedServer(reg) | ||
| s.TLS = &tls.Config{ | ||
| Certificates: []tls.Certificate{cert}, | ||
| } | ||
| s.StartTLS() | ||
|
|
||
| u, _ := url.Parse(s.URL) | ||
| return s, u.Host | ||
| } | ||
|
|
||
| // generateSelfSignedCert generates a self-signed certificate for testing purposes | ||
| // It uses knative's certificate generation utilities to create a proper certificate chain | ||
| func generateSelfSignedCert() (tls.Certificate, error) { | ||
| // Generate certificates with 24 hour validity | ||
| notAfter := time.Now().Add(24 * time.Hour) | ||
|
|
||
| // Use test service name and namespace | ||
| serverKey, serverCert, _, err := resources.CreateCerts(context.Background(), "test-registry", "test-namespace", notAfter) | ||
| if err != nil { | ||
| return tls.Certificate{}, fmt.Errorf("failed to generate certificates: %w", err) | ||
| } | ||
|
|
||
| // Parse the generated certificates | ||
| cert, err := tls.X509KeyPair(serverCert, serverKey) | ||
| if err != nil { | ||
| return tls.Certificate{}, fmt.Errorf("failed to parse certificate: %w", err) | ||
| } | ||
|
|
||
| return cert, nil | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that the method added in #1395 requires these opts to be passed.