-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admission) implement validation for secret-based credentials
The validation currently consists of verifying if each and every required field is provided by the user or not. In future, the validation can be made smarter to include an Admin API call to Kong to verify if the creation of such a credential will succeed or not (see TODO in code). From #446
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package admission | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestKongHTTPValidator_ValidateCredential(t *testing.T) { | ||
type args struct { | ||
secret corev1.Secret | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantOK bool | ||
wantMessage string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid key-auth credential", | ||
args: args{ | ||
secret: corev1.Secret{ | ||
Data: map[string][]byte{ | ||
"key": []byte("foo"), | ||
"credType": []byte("key-auth"), | ||
}, | ||
}, | ||
}, | ||
wantOK: true, | ||
wantMessage: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid key-auth credential", | ||
args: args{ | ||
secret: corev1.Secret{ | ||
Data: map[string][]byte{ | ||
"key-wrong": []byte("foo"), | ||
"credType": []byte("key-auth"), | ||
}, | ||
}, | ||
}, | ||
wantOK: false, | ||
wantMessage: "missing required field(s): key", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid credential type", | ||
args: args{ | ||
secret: corev1.Secret{ | ||
Data: map[string][]byte{ | ||
"credType": []byte("foo"), | ||
}, | ||
}, | ||
}, | ||
wantOK: false, | ||
wantMessage: "invalid credential type: foo", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "non-kong secrets are passed", | ||
args: args{ | ||
secret: corev1.Secret{ | ||
Data: map[string][]byte{ | ||
"key": []byte("foo"), | ||
}, | ||
}, | ||
}, | ||
wantOK: true, | ||
wantMessage: "", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
validator := KongHTTPValidator{} | ||
got, got1, err := validator.ValidateCredential(tt.args.secret) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("KongHTTPValidator.ValidateCredential() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.wantOK { | ||
t.Errorf("KongHTTPValidator.ValidateCredential() got = %v, want %v", got, tt.wantOK) | ||
} | ||
if got1 != tt.wantMessage { | ||
t.Errorf("KongHTTPValidator.ValidateCredential() got1 = %v, want %v", got1, tt.wantMessage) | ||
} | ||
}) | ||
} | ||
} |