Skip to content

Commit b1d2d17

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 b1d2d17

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

internal/controller/ocirepository_controller.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,12 @@ 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+
serverName, err := extractServerNameFromURL(obj.Spec.URL)
976+
if err != nil {
977+
return nil, fmt.Errorf("failed to extract server name for TLS: %w", err)
978+
}
979+
tlsConfig.ServerName = serverName
974980
transport.TLSClientConfig = tlsConfig
975981
}
976982

@@ -981,6 +987,29 @@ func (r *OCIRepositoryReconciler) transport(ctx context.Context, obj *sourcev1.O
981987
return transport, nil
982988
}
983989

990+
// extractServerNameFromURL extracts the server name from an OCI repository URL
991+
// for use in TLS configuration. It returns the hostname without port
992+
// that should be used as the ServerName in TLS handshakes.
993+
func extractServerNameFromURL(ociURL string) (string, error) {
994+
if !strings.HasPrefix(ociURL, sourcev1.OCIRepositoryPrefix) {
995+
return "", fmt.Errorf("URL must be in format 'oci://<domain>/<org>/<repo>'")
996+
}
997+
998+
// Convert OCI URL to a parseable format by replacing oci:// with https://
999+
// This allows us to use the standard url package
1000+
u, err := url.Parse(strings.Replace(ociURL, sourcev1.OCIRepositoryPrefix, "https://", 1))
1001+
if err != nil {
1002+
return "", fmt.Errorf("failed to parse OCI URL: %w", err)
1003+
}
1004+
1005+
hostname := u.Hostname()
1006+
if hostname == "" {
1007+
return "", fmt.Errorf("failed to extract hostname from OCI URL")
1008+
}
1009+
1010+
return hostname, nil
1011+
}
1012+
9841013
// getTLSConfig gets the TLS configuration for the transport based on the
9851014
// specified secret reference in the OCIRepository object, or the insecure flag.
9861015
func (r *OCIRepositoryReconciler) getTLSConfig(ctx context.Context, obj *sourcev1.OCIRepository) (*cryptotls.Config, error) {

internal/controller/ocirepository_controller_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,3 +3705,58 @@ 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",
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: "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+
3748+
for _, tt := range tests {
3749+
t.Run(tt.name, func(t *testing.T) {
3750+
g := NewWithT(t)
3751+
3752+
got, err := extractServerNameFromURL(tt.url)
3753+
if tt.wantErr {
3754+
g.Expect(err).To(HaveOccurred())
3755+
return
3756+
}
3757+
3758+
g.Expect(err).NotTo(HaveOccurred())
3759+
g.Expect(got).To(Equal(tt.want))
3760+
})
3761+
}
3762+
}

0 commit comments

Comments
 (0)