Skip to content

Commit ddba5ff

Browse files
authored
fix: allow http registries by disabling TLS check (#1203)
**What problem does this PR solve?**: Disables TLS if the registry is HTTP **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent f26818b commit ddba5ff

File tree

2 files changed

+102
-11
lines changed

2 files changed

+102
-11
lines changed

pkg/webhook/preflight/generic/registry.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,35 @@ func (r *registryCheck) checkRegistry(
8686
)
8787
return result
8888
}
89-
mirrorHost := config.Host{
89+
registryHost := config.Host{
9090
Name: registryURLParsed.Host,
9191
}
92+
if registryURLParsed.Scheme != "http" && registryURLParsed.Scheme != "https" {
93+
result.Allowed = false
94+
result.Causes = append(result.Causes,
95+
preflight.Cause{
96+
Message: fmt.Sprintf(
97+
"Registry URL scheme %q is not supported. Use http or https.",
98+
registryURLParsed.Scheme,
99+
),
100+
Field: r.field + ".url",
101+
},
102+
)
103+
return result
104+
}
105+
if registryURLParsed.Scheme == "http" {
106+
registryHost.TLS = config.TLSDisabled
107+
}
108+
92109
if credentials != nil && credentials.SecretRef != nil {
93-
mirrorCredentialsSecret := &corev1.Secret{}
110+
credentialsSecret := &corev1.Secret{}
94111
err := r.kclient.Get(
95112
ctx,
96113
types.NamespacedName{
97114
Namespace: r.cluster.Namespace,
98115
Name: credentials.SecretRef.Name,
99116
},
100-
mirrorCredentialsSecret,
117+
credentialsSecret,
101118
)
102119
if apierrors.IsNotFound(err) {
103120
result.Allowed = false
@@ -121,21 +138,21 @@ func (r *registryCheck) checkRegistry(
121138
)
122139
return result
123140
}
124-
username, ok := mirrorCredentialsSecret.Data["username"]
141+
username, ok := credentialsSecret.Data["username"]
125142
if ok {
126-
mirrorHost.User = string(username)
143+
registryHost.User = string(username)
127144
}
128-
password, ok := mirrorCredentialsSecret.Data["password"]
145+
password, ok := credentialsSecret.Data["password"]
129146
if ok {
130-
mirrorHost.Pass = string(password)
147+
registryHost.Pass = string(password)
131148
}
132-
if caCert, ok := mirrorCredentialsSecret.Data["ca.crt"]; ok {
133-
mirrorHost.RegCert = string(caCert)
149+
if caCert, ok := credentialsSecret.Data["ca.crt"]; ok {
150+
registryHost.RegCert = string(caCert)
134151
}
135152
}
136153
rc := regClientGetter(
137-
regclient.WithConfigHost(mirrorHost),
138-
regclient.WithUserAgent("regclient/example"),
154+
regclient.WithConfigHost(registryHost),
155+
regclient.WithUserAgent("regclient/caren"),
139156
)
140157
_, err = rc.Ping(ctx,
141158
ref.Ref{

pkg/webhook/preflight/generic/registry_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,38 @@ func TestRegistryCheck(t *testing.T) {
232232
Allowed: true,
233233
},
234234
},
235+
{
236+
name: "image registry with valid configuration using http",
237+
imageRegistry: &carenv1.ImageRegistry{
238+
URL: "http://registry.example.com",
239+
Credentials: &carenv1.RegistryCredentials{
240+
SecretRef: &carenv1.LocalObjectReference{
241+
Name: "test-secret",
242+
},
243+
},
244+
},
245+
kclient: &mockK8sClient{
246+
getSecretFunc: func(ctx context.Context,
247+
key types.NamespacedName,
248+
obj ctrlclient.Object,
249+
opts ...ctrlclient.GetOption,
250+
) error {
251+
secret := obj.(*corev1.Secret)
252+
secret.Data = map[string][]byte{
253+
"username": []byte("testuser"),
254+
}
255+
return nil
256+
},
257+
},
258+
mockRegClientPingerFactory: func(...regclient.Opt) regClientPinger {
259+
return &mockRegClient{
260+
pingFunc: func(ref.Ref) error { return nil },
261+
}
262+
},
263+
want: preflight.CheckResult{
264+
Allowed: true,
265+
},
266+
},
235267
{
236268
name: "image registry with invalid URL",
237269
field: "cluster.spec.topology.variables[.name=clusterConfig].value.imageRegistries[0]",
@@ -275,6 +307,48 @@ func TestRegistryCheck(t *testing.T) {
275307
},
276308
},
277309
},
310+
{
311+
name: "image registry with invalid URL scheme",
312+
field: "cluster.spec.topology.variables[.name=clusterConfig].value.imageRegistries[0]",
313+
imageRegistry: &carenv1.ImageRegistry{
314+
URL: "tcp://some-registry.lol",
315+
Credentials: &carenv1.RegistryCredentials{
316+
SecretRef: &carenv1.LocalObjectReference{
317+
Name: "test-secret",
318+
},
319+
},
320+
},
321+
kclient: &mockK8sClient{
322+
getSecretFunc: func(ctx context.Context,
323+
key types.NamespacedName,
324+
obj ctrlclient.Object,
325+
opts ...ctrlclient.GetOption,
326+
) error {
327+
secret := obj.(*corev1.Secret)
328+
secret.Data = map[string][]byte{
329+
"username": []byte("testuser"),
330+
"password": []byte("testpass"),
331+
"ca.crt": []byte("test-ca-cert"),
332+
}
333+
return nil
334+
},
335+
},
336+
mockRegClientPingerFactory: func(...regclient.Opt) regClientPinger {
337+
return &mockRegClient{
338+
pingFunc: func(ref.Ref) error { return nil },
339+
}
340+
},
341+
want: preflight.CheckResult{
342+
Allowed: false,
343+
InternalError: false,
344+
Causes: []preflight.Cause{
345+
{
346+
Message: "Registry URL scheme \"tcp\" is not supported. Use http or https.",
347+
Field: "cluster.spec.topology.variables[.name=clusterConfig].value.imageRegistries[0].url",
348+
},
349+
},
350+
},
351+
},
278352
}
279353

280354
for _, tc := range testCases {

0 commit comments

Comments
 (0)