Skip to content

Commit b87c0bb

Browse files
authored
feat(admission): validate labeled credentials (#4896)
1 parent f7361da commit b87c0bb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

internal/admission/validator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,8 @@ func (validator KongHTTPValidator) ValidateCredential(
236236
ctx context.Context,
237237
secret corev1.Secret,
238238
) (bool, string, error) {
239-
// If the secret doesn't contain a type key it's not a credentials secret.
240-
_, ok := secret.Data[credsvalidation.TypeKey]
241-
if !ok {
239+
// If the secret doesn't specify a credential type (either by label or the secret's key) it's not a credentials secret.
240+
if _, s := util.ExtractKongCredentialType(&secret); s == util.CredentialTypeAbsent {
242241
return true, "", nil
243242
}
244243

internal/admission/validator_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,35 @@ func TestKongHTTPValidator_ValidateCredential(t *testing.T) {
567567
wantMessage string
568568
wantErrContains string
569569
}{
570+
{
571+
name: "labeled valid key-auth credential with no consumers gets accepted",
572+
secret: corev1.Secret{
573+
ObjectMeta: metav1.ObjectMeta{
574+
Labels: map[string]string{
575+
"konghq.com/credential": "key-auth",
576+
},
577+
},
578+
Data: map[string][]byte{
579+
"key": []byte("my-key"),
580+
},
581+
},
582+
wantOK: true,
583+
},
584+
{
585+
name: "labeled invalid key-auth credential with no consumers gets rejected",
586+
secret: corev1.Secret{
587+
ObjectMeta: metav1.ObjectMeta{
588+
Labels: map[string]string{
589+
"konghq.com/credential": "key-auth",
590+
},
591+
},
592+
Data: map[string][]byte{
593+
// missing key
594+
},
595+
},
596+
wantOK: false,
597+
wantMessage: fmt.Sprintf("%s: %s", ErrTextConsumerCredentialValidationFailed, "missing required field(s): key"),
598+
},
570599
{
571600
name: "valid key-auth credential with no consumers gets accepted",
572601
secret: corev1.Secret{
@@ -586,7 +615,7 @@ func TestKongHTTPValidator_ValidateCredential(t *testing.T) {
586615
},
587616
},
588617
wantOK: false,
589-
wantMessage: fmt.Sprintf("%s: %s", ErrTextConsumerCredentialValidationFailed, "invalid credentials secret, no data present"),
618+
wantMessage: fmt.Sprintf("%s: %s", ErrTextConsumerCredentialValidationFailed, "missing required field(s): key"),
590619
},
591620
}
592621

0 commit comments

Comments
 (0)