Skip to content

Commit 557ae52

Browse files
committed
fixup! fixup! Migrate OCIRepository controller to runtime/secrets
Add ServerName support for TLS configuration Signed-off-by: cappyzawa <[email protected]>
1 parent abf4939 commit 557ae52

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

internal/controller/ocirepository_controller.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,14 @@ func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *sourcev1.O
971971
return nil, err
972972
}
973973
if tlsConfig != nil {
974+
// Set ServerName for proper virtual hosting support
975+
if tlsConfig.ServerName == "" {
976+
serverName, err := extractServerNameFromURL(obj.Spec.URL)
977+
if err != nil {
978+
return nil, fmt.Errorf("failed to extract server name for TLS: %w", err)
979+
}
980+
tlsConfig.ServerName = serverName
981+
}
974982
transport.TLSClientConfig = tlsConfig
975983
}
976984

@@ -981,6 +989,25 @@ func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *sourcev1.O
981989
return transport, nil
982990
}
983991

992+
// extractServerNameFromURL extracts the server name from an OCI repository URL
993+
// for use in TLS configuration. It returns the hostname (with port if present)
994+
// that should be used as the ServerName in TLS handshakes.
995+
func extractServerNameFromURL(url string) (string, error) {
996+
if !strings.HasPrefix(url, sourcev1.OCIRepositoryPrefix) {
997+
return "", fmt.Errorf("URL must be in format 'oci://<domain>/<org>/<repo>'")
998+
}
999+
1000+
cleanURL := strings.TrimPrefix(url, sourcev1.OCIRepositoryPrefix)
1001+
1002+
// Parse as a reference to get the registry part
1003+
ref, err := name.ParseReference(cleanURL)
1004+
if err != nil {
1005+
return "", fmt.Errorf("failed to parse OCI URL: %w", err)
1006+
}
1007+
1008+
return ref.Context().RegistryStr(), nil
1009+
}
1010+
9841011
// getTLSConfig gets the TLS configuration for the transport based on the
9851012
// specified secret reference in the OCIRepository object, or the insecure flag.
9861013
func (r *OCIRepositoryReconciler) getTLSConfig(ctx context.Context, obj *sourcev1.OCIRepository) (*cryptotls.Config, error) {

internal/controller/ocirepository_controller_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,3 +3705,63 @@ func TestOCIContentConfigChanged(t *testing.T) {
37053705
})
37063706
}
37073707
}
3708+
3709+
func TestOCIRepositoryReconciler_extractServerNameFromURL(t *testing.T) {
3710+
tests := []struct {
3711+
name string
3712+
url string
3713+
want string
3714+
wantErr bool
3715+
}{
3716+
{
3717+
name: "valid OCI URL with hostname",
3718+
url: "oci://registry.example.com/myorg/myrepo",
3719+
want: "registry.example.com",
3720+
},
3721+
{
3722+
name: "valid OCI URL with hostname and port",
3723+
url: "oci://registry.example.com:8443/myorg/myrepo",
3724+
want: "registry.example.com:8443",
3725+
},
3726+
{
3727+
name: "valid OCI URL with tag",
3728+
url: "oci://ghcr.io/fluxcd/flux2/manifests:v2.0.0",
3729+
want: "ghcr.io",
3730+
},
3731+
{
3732+
name: "valid OCI URL with digest",
3733+
url: "oci://docker.io/library/nginx@sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
3734+
want: "index.docker.io",
3735+
},
3736+
{
3737+
name: "invalid URL without oci:// prefix",
3738+
url: "https://registry.example.com/myorg/myrepo",
3739+
wantErr: true,
3740+
},
3741+
{
3742+
name: "invalid URL format",
3743+
url: "oci://",
3744+
wantErr: true,
3745+
},
3746+
{
3747+
name: "invalid URL with malformed registry",
3748+
url: "oci://",
3749+
wantErr: true,
3750+
},
3751+
}
3752+
3753+
for _, tt := range tests {
3754+
t.Run(tt.name, func(t *testing.T) {
3755+
g := NewWithT(t)
3756+
3757+
got, err := extractServerNameFromURL(tt.url)
3758+
if tt.wantErr {
3759+
g.Expect(err).To(HaveOccurred())
3760+
return
3761+
}
3762+
3763+
g.Expect(err).NotTo(HaveOccurred())
3764+
g.Expect(got).To(Equal(tt.want))
3765+
})
3766+
}
3767+
}

0 commit comments

Comments
 (0)