Skip to content

Commit 861cbdd

Browse files
authored
fix: Ensure registry credentials are namespace local to Cluster (#332)
Using `LocalObjectReference` ensures user cannot override the secret namespace. Fixes #322.
1 parent 2f5266b commit 861cbdd

File tree

9 files changed

+15
-30
lines changed

9 files changed

+15
-30
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ func (ExtraAPIServerCertSANs) VariableSchema() clusterv1.VariableSchema {
243243
}
244244

245245
type RegistryCredentials struct {
246-
// The Secret containing the registry credentials and optional CA certificate
246+
// A reference to the Secret containing the registry credentials and optional CA certificate
247247
// using the keys `username`, `password` and `ca.crt`.
248248
// This credentials Secret is not required for some registries, e.g. ECR.
249249
// +optional
250-
SecretRef *corev1.ObjectReference `json:"secretRef,omitempty"`
250+
SecretRef *corev1.LocalObjectReference `json:"secretRef,omitempty"`
251251
}
252252

253253
func (RegistryCredentials) VariableSchema() clusterv1.VariableSchema {
@@ -256,19 +256,14 @@ func (RegistryCredentials) VariableSchema() clusterv1.VariableSchema {
256256
Type: "object",
257257
Properties: map[string]clusterv1.JSONSchemaProps{
258258
"secretRef": {
259-
Description: "A reference to the Secret containing the registry credentials. " +
259+
Description: "A reference to the Secret containing the registry credentials and optional CA certificate. " +
260260
"The Secret should have keys 'username', 'password' and optional 'ca.crt'. " +
261261
"This credentials Secret is not required for some registries, e.g. ECR.",
262262
Type: "object",
263263
Properties: map[string]clusterv1.JSONSchemaProps{
264264
"name": {
265-
Description: "The name of the Secret containing the registry credentials.",
266-
Type: "string",
267-
},
268-
"namespace": {
269-
Description: "The namespace of the Secret containing the registry credentials. " +
270-
"Defaults to the namespace of the Cluster. " +
271-
"that reference this variable.",
265+
Description: "The name of the Secret containing the registry credentials. This Secret must exist in " +
266+
"the same namespace as the Cluster.",
272267
Type: "string",
273268
},
274269
},

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/handlers/generic/mutation/imageregistries/credentials/inject.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,9 @@ func secretForImageRegistryCredentials(
309309
return nil, nil
310310
}
311311

312-
namespace := objectNamespace
313-
if registry.Credentials.SecretRef.Namespace != "" {
314-
namespace = registry.Credentials.SecretRef.Namespace
315-
}
316-
317312
key := ctrlclient.ObjectKey{
318313
Name: registry.Credentials.SecretRef.Name,
319-
Namespace: namespace,
314+
Namespace: objectNamespace,
320315
}
321316
secret := &corev1.Secret{}
322317
err := c.Get(ctx, key, secret)

pkg/handlers/generic/mutation/imageregistries/credentials/tests/generate_patches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestGeneratePatches(
142142
v1alpha1.ImageRegistry{
143143
URL: "https://registry.example.com",
144144
Credentials: &v1alpha1.RegistryCredentials{
145-
SecretRef: &corev1.ObjectReference{
145+
SecretRef: &corev1.LocalObjectReference{
146146
Name: validSecretName,
147147
},
148148
},
@@ -260,7 +260,7 @@ func TestGeneratePatches(
260260
v1alpha1.ImageRegistry{
261261
URL: "https://registry.example.com",
262262
Credentials: &v1alpha1.RegistryCredentials{
263-
SecretRef: &corev1.ObjectReference{
263+
SecretRef: &corev1.LocalObjectReference{
264264
Name: validSecretName,
265265
},
266266
},

pkg/handlers/generic/mutation/imageregistries/credentials/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestVariableValidation(t *testing.T) {
3838
{
3939
URL: "https://a.b.c.example.com/a/b/c",
4040
Credentials: &v1alpha1.RegistryCredentials{
41-
SecretRef: &corev1.ObjectReference{
41+
SecretRef: &corev1.LocalObjectReference{
4242
Name: "a.b.c.example.com-creds",
4343
},
4444
},

pkg/handlers/generic/mutation/mirrors/mirror.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,9 @@ func secretForMirrorCACert(
7474
return nil, nil
7575
}
7676

77-
namespace := objectNamespace
78-
if globalMirror.Credentials.SecretRef.Namespace != "" {
79-
namespace = globalMirror.Credentials.SecretRef.Namespace
80-
}
81-
8277
key := ctrlclient.ObjectKey{
8378
Name: globalMirror.Credentials.SecretRef.Name,
84-
Namespace: namespace,
79+
Namespace: objectNamespace,
8580
}
8681
secret := &corev1.Secret{}
8782
err := c.Get(ctx, key, secret)

pkg/handlers/generic/mutation/mirrors/mirror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func Test_generateMirrorCACertFile(t *testing.T) {
9999
URL: "https://registry.example.com",
100100

101101
Credentials: &v1alpha1.RegistryCredentials{
102-
SecretRef: &v1.ObjectReference{
102+
SecretRef: &v1.LocalObjectReference{
103103
Name: "my-registry-credentials-secret",
104104
},
105105
},

pkg/handlers/generic/mutation/mirrors/tests/generate_patches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestGeneratePatches(
8181
v1alpha1.GlobalImageRegistryMirror{
8282
URL: "https://registry.example.com",
8383
Credentials: &v1alpha1.RegistryCredentials{
84-
SecretRef: &corev1.ObjectReference{
84+
SecretRef: &corev1.LocalObjectReference{
8585
Name: validMirrorCASecretName,
8686
},
8787
},
@@ -145,7 +145,7 @@ func TestGeneratePatches(
145145
v1alpha1.GlobalImageRegistryMirror{
146146
URL: "https://registry.example.com",
147147
Credentials: &v1alpha1.RegistryCredentials{
148-
SecretRef: &corev1.ObjectReference{
148+
SecretRef: &corev1.LocalObjectReference{
149149
Name: validMirrorCASecretName,
150150
},
151151
},

pkg/handlers/generic/mutation/mirrors/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestVariableValidation(t *testing.T) {
3535
GlobalImageRegistryMirror: &v1alpha1.GlobalImageRegistryMirror{
3636
URL: "http://a.b.c.example.com",
3737
Credentials: &v1alpha1.RegistryCredentials{
38-
SecretRef: &corev1.ObjectReference{
38+
SecretRef: &corev1.LocalObjectReference{
3939
Name: "a.b.c.example.com-ca-cert-creds",
4040
},
4141
},

0 commit comments

Comments
 (0)