mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #13853 from jhadvig/extend_secret
Auto commit by PR queue bot
This commit is contained in:
commit
b8413f6cdd
@ -2120,6 +2120,27 @@ const (
|
||||
|
||||
// DockerConfigJsonKey is the key of the required data for SecretTypeDockerConfigJson secrets
|
||||
DockerConfigJsonKey = ".dockerconfigjson"
|
||||
|
||||
// SecretTypeBasicAuth contains data needed for basic authentication.
|
||||
//
|
||||
// Required at least one of fields:
|
||||
// - Secret.Data["username"] - username used for authentication
|
||||
// - Secret.Data["password"] - password or token needed for authentication
|
||||
SecretTypeBasicAuth SecretType = "kubernetes.io/basic-auth"
|
||||
|
||||
// BasicAuthUsernameKey is the key of the username for SecretTypeBasicAuth secrets
|
||||
BasicAuthUsernameKey = "username"
|
||||
// BasicAuthPasswordKey is the key of the password or token for SecretTypeBasicAuth secrets
|
||||
BasicAuthPasswordKey = "password"
|
||||
|
||||
// SecretTypeSSHAuth contains data needed for SSH authetication.
|
||||
//
|
||||
// Required field:
|
||||
// - Secret.Data["ssh-privatekey"] - private SSH key needed for authentication
|
||||
SecretTypeSSHAuth SecretType = "kubernetes.io/ssh-auth"
|
||||
|
||||
// SSHAuthPrivateKey is the key of the required SSH private key for SecretTypeSSHAuth secrets
|
||||
SSHAuthPrivateKey = "ssh-privatekey"
|
||||
)
|
||||
|
||||
type SecretList struct {
|
||||
|
@ -1950,6 +1950,21 @@ func ValidateSecret(secret *api.Secret) field.ErrorList {
|
||||
if err := json.Unmarshal(dockerConfigJsonBytes, &map[string]interface{}{}); err != nil {
|
||||
allErrs = append(allErrs, field.Invalid(dataPath.Key(api.DockerConfigJsonKey), "<secret contents redacted>", err.Error()))
|
||||
}
|
||||
case api.SecretTypeBasicAuth:
|
||||
_, usernameFieldExists := secret.Data[api.BasicAuthUsernameKey]
|
||||
_, passwordFieldExists := secret.Data[api.BasicAuthPasswordKey]
|
||||
|
||||
// username or password might be empty, but the field must be present
|
||||
if !usernameFieldExists && !passwordFieldExists {
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("data[%s]").Key(api.BasicAuthUsernameKey), ""))
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("data[%s]").Key(api.BasicAuthPasswordKey), ""))
|
||||
break
|
||||
}
|
||||
case api.SecretTypeSSHAuth:
|
||||
if len(secret.Data[api.SSHAuthPrivateKey]) == 0 {
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("data[%s]").Key(api.SSHAuthPrivateKey), ""))
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
// no-op
|
||||
|
@ -4039,6 +4039,90 @@ func TestValidateDockerConfigSecret(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBasicAuthSecret(t *testing.T) {
|
||||
validBasicAuthSecret := func() api.Secret {
|
||||
return api.Secret{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "bar"},
|
||||
Type: api.SecretTypeBasicAuth,
|
||||
Data: map[string][]byte{
|
||||
api.BasicAuthUsernameKey: []byte("username"),
|
||||
api.BasicAuthPasswordKey: []byte("password"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
missingBasicAuthUsernamePasswordKeys = validBasicAuthSecret()
|
||||
// invalidBasicAuthUsernamePasswordKey = validBasicAuthSecret()
|
||||
// emptyBasicAuthUsernameKey = validBasicAuthSecret()
|
||||
// emptyBasicAuthPasswordKey = validBasicAuthSecret()
|
||||
)
|
||||
|
||||
delete(missingBasicAuthUsernamePasswordKeys.Data, api.BasicAuthUsernameKey)
|
||||
delete(missingBasicAuthUsernamePasswordKeys.Data, api.BasicAuthPasswordKey)
|
||||
|
||||
// invalidBasicAuthUsernamePasswordKey.Data[api.BasicAuthUsernameKey] = []byte("bad")
|
||||
// invalidBasicAuthUsernamePasswordKey.Data[api.BasicAuthPasswordKey] = []byte("bad")
|
||||
|
||||
// emptyBasicAuthUsernameKey.Data[api.BasicAuthUsernameKey] = []byte("")
|
||||
// emptyBasicAuthPasswordKey.Data[api.BasicAuthPasswordKey] = []byte("")
|
||||
|
||||
tests := map[string]struct {
|
||||
secret api.Secret
|
||||
valid bool
|
||||
}{
|
||||
"valid": {validBasicAuthSecret(), true},
|
||||
"missing username and password": {missingBasicAuthUsernamePasswordKeys, false},
|
||||
// "invalid username and password": {invalidBasicAuthUsernamePasswordKey, false},
|
||||
// "empty username": {emptyBasicAuthUsernameKey, false},
|
||||
// "empty password": {emptyBasicAuthPasswordKey, false},
|
||||
}
|
||||
|
||||
for name, tc := range tests {
|
||||
errs := ValidateSecret(&tc.secret)
|
||||
if tc.valid && len(errs) > 0 {
|
||||
t.Errorf("%v: Unexpected error: %v", name, errs)
|
||||
}
|
||||
if !tc.valid && len(errs) == 0 {
|
||||
t.Errorf("%v: Unexpected non-error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSSHAuthSecret(t *testing.T) {
|
||||
validSSHAuthSecret := func() api.Secret {
|
||||
return api.Secret{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "bar"},
|
||||
Type: api.SecretTypeSSHAuth,
|
||||
Data: map[string][]byte{
|
||||
api.SSHAuthPrivateKey: []byte("foo-bar-baz"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
missingSSHAuthPrivateKey := validSSHAuthSecret()
|
||||
|
||||
delete(missingSSHAuthPrivateKey.Data, api.SSHAuthPrivateKey)
|
||||
|
||||
tests := map[string]struct {
|
||||
secret api.Secret
|
||||
valid bool
|
||||
}{
|
||||
"valid": {validSSHAuthSecret(), true},
|
||||
"missing private key": {missingSSHAuthPrivateKey, false},
|
||||
}
|
||||
|
||||
for name, tc := range tests {
|
||||
errs := ValidateSecret(&tc.secret)
|
||||
if tc.valid && len(errs) > 0 {
|
||||
t.Errorf("%v: Unexpected error: %v", name, errs)
|
||||
}
|
||||
if !tc.valid && len(errs) == 0 {
|
||||
t.Errorf("%v: Unexpected non-error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEndpoints(t *testing.T) {
|
||||
successCases := map[string]api.Endpoints{
|
||||
"simple endpoint": {
|
||||
|
Loading…
Reference in New Issue
Block a user