-
Notifications
You must be signed in to change notification settings - Fork 454
/
internalsecret.go
130 lines (114 loc) · 5.5 KB
/
internalsecret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validation
import (
"encoding/json"
"reflect"
corev1 "k8s.io/api/core/v1"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/gardener/gardener/pkg/apis/core"
)
// ValidateInternalSecretName can be used to check whether the given secret name is valid.
// Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed.
var ValidateInternalSecretName = apivalidation.NameIsDNSSubdomain
// ValidateInternalSecret tests if required fields in the InternalSecret are set.
func ValidateInternalSecret(secret *core.InternalSecret) field.ErrorList {
allErrs := apivalidation.ValidateObjectMeta(&secret.ObjectMeta, true, ValidateInternalSecretName, field.NewPath("metadata"))
dataPath := field.NewPath("data")
totalSize := 0
for key, value := range secret.Data {
for _, msg := range validation.IsConfigMapKey(key) {
allErrs = append(allErrs, field.Invalid(dataPath.Key(key), key, msg))
}
totalSize += len(value)
}
if totalSize > corev1.MaxSecretSize {
allErrs = append(allErrs, field.TooLong(dataPath, "", corev1.MaxSecretSize))
}
switch secret.Type {
case corev1.SecretTypeServiceAccountToken:
// Only require Annotations[kubernetes.io/service-account.name]
// Additional fields (like Annotations[kubernetes.io/service-account.uid] and Data[token]) might be contributed later by a controller loop
if value := secret.Annotations[corev1.ServiceAccountNameKey]; len(value) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("metadata", "annotations").Key(corev1.ServiceAccountNameKey), ""))
}
case corev1.SecretTypeOpaque, "":
// no-op
case corev1.SecretTypeDockercfg:
dockercfgBytes, exists := secret.Data[corev1.DockerConfigKey]
if !exists {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.DockerConfigKey), ""))
break
}
// make sure that the content is well-formed json.
if err := json.Unmarshal(dockercfgBytes, &map[string]interface{}{}); err != nil {
allErrs = append(allErrs, field.Invalid(dataPath.Key(corev1.DockerConfigKey), "<secret contents redacted>", err.Error()))
}
case corev1.SecretTypeDockerConfigJson:
dockerConfigJSONBytes, exists := secret.Data[corev1.DockerConfigJsonKey]
if !exists {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.DockerConfigJsonKey), ""))
break
}
// make sure that the content is well-formed json.
if err := json.Unmarshal(dockerConfigJSONBytes, &map[string]interface{}{}); err != nil {
allErrs = append(allErrs, field.Invalid(dataPath.Key(corev1.DockerConfigJsonKey), "<secret contents redacted>", err.Error()))
}
case corev1.SecretTypeBasicAuth:
_, usernameFieldExists := secret.Data[corev1.BasicAuthUsernameKey]
_, passwordFieldExists := secret.Data[corev1.BasicAuthPasswordKey]
// username or password might be empty, but the field must be present
if !usernameFieldExists && !passwordFieldExists {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.BasicAuthUsernameKey), ""))
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.BasicAuthPasswordKey), ""))
break
}
case corev1.SecretTypeSSHAuth:
if len(secret.Data[corev1.SSHAuthPrivateKey]) == 0 {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.SSHAuthPrivateKey), ""))
break
}
case corev1.SecretTypeTLS:
if _, exists := secret.Data[corev1.TLSCertKey]; !exists {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.TLSCertKey), ""))
}
if _, exists := secret.Data[corev1.TLSPrivateKeyKey]; !exists {
allErrs = append(allErrs, field.Required(dataPath.Key(corev1.TLSPrivateKeyKey), ""))
}
// TODO: Verify that the key matches the cert.
default:
// no-op
}
return allErrs
}
// ValidateInternalSecretUpdate tests if required fields in the InternalSecret are set.
func ValidateInternalSecretUpdate(newSecret, oldSecret *core.InternalSecret) field.ErrorList {
allErrs := apivalidation.ValidateObjectMetaUpdate(&newSecret.ObjectMeta, &oldSecret.ObjectMeta, field.NewPath("metadata"))
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSecret.Type, oldSecret.Type, field.NewPath("type"))...)
if oldSecret.Immutable != nil && *oldSecret.Immutable {
if newSecret.Immutable == nil || !*newSecret.Immutable {
allErrs = append(allErrs, field.Forbidden(field.NewPath("immutable"), "field is immutable when `immutable` is set"))
}
if !reflect.DeepEqual(newSecret.Data, oldSecret.Data) {
allErrs = append(allErrs, field.Forbidden(field.NewPath("data"), "field is immutable when `immutable` is set"))
}
// We don't validate StringData, as it was already converted back to Data
// before validation is happening.
}
allErrs = append(allErrs, ValidateInternalSecret(newSecret)...)
return allErrs
}