-
Notifications
You must be signed in to change notification settings - Fork 331
/
validation.go
169 lines (149 loc) · 6.51 KB
/
validation.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package projectpolicybinding
import (
"context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apiMachineryValidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"tkestack.io/tke/api/auth"
authinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/auth/internalversion"
"tkestack.io/tke/pkg/auth/util"
"tkestack.io/tke/pkg/util/log"
)
// ValidateBindingName is a ValidateNameFunc for names that must be a DNS
// subdomain.
var ValidateBindingName = apiMachineryValidation.NameIsDNSLabel
// ValidateProjectPolicyBinding tests if required fields in the projectpolicybinding are set.
func ValidateProjectPolicyBinding(ctx context.Context, binding *auth.ProjectPolicyBinding, authClient authinternalclient.AuthInterface) field.ErrorList {
allErrs := apiMachineryValidation.ValidateObjectMeta(&binding.ObjectMeta, false, ValidateBindingName, field.NewPath("metadata"))
fldSpecPath := field.NewPath("spec")
if binding.Spec.PolicyID == "" {
allErrs = append(allErrs, field.Required(fldSpecPath.Child("policyID"), "must specify policyID"))
} else {
pol, err := authClient.Policies().Get(ctx, binding.Spec.PolicyID, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
allErrs = append(allErrs, field.NotFound(fldSpecPath.Child("policyID"), binding.Spec.PolicyID))
} else {
allErrs = append(allErrs, field.InternalError(fldSpecPath.Child("policyID"), err))
}
}
if pol.Spec.Scope != auth.PolicyProject {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("scope"), pol.Spec.Scope, "policy must be project scope"))
}
}
if binding.Spec.ProjectID == "" {
allErrs = append(allErrs, field.Required(fldSpecPath.Child("projectID"), "must specify projectID"))
}
var validUsers []auth.Subject
fldUserPath := field.NewPath("spec", "users")
for i, subj := range binding.Spec.Users {
if subj.ID == "" && subj.Name == "" {
allErrs = append(allErrs, field.Required(fldUserPath, "must specify subject id or name"))
continue
}
switch {
case subj.ID != "" && subj.Name == "":
val, err := authClient.Users().Get(ctx, util.CombineTenantAndName(binding.Spec.TenantID, subj.ID), metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
log.Warn("user is not found, will removed it", log.String("policy", binding.Name), log.String("user", subj.ID))
} else {
allErrs = append(allErrs, field.InternalError(fldUserPath, err))
}
} else {
if val.Spec.TenantID != binding.Spec.TenantID {
allErrs = append(allErrs, field.Invalid(fldUserPath, subj.ID, "must in the same tenant with the policy"))
} else {
binding.Spec.Users[i].Name = val.Spec.Name
validUsers = append(validUsers, binding.Spec.Users[i])
}
}
case subj.ID == "" && subj.Name != "":
user, err := util.GetUserByName(ctx, authClient, binding.Spec.TenantID, subj.Name)
if err != nil {
if apierrors.IsNotFound(err) {
log.Warn("user is not found in tenant, will removed it", log.String("policy", binding.Name), log.String("user", subj.Name))
} else {
allErrs = append(allErrs, field.InternalError(fldUserPath, err))
}
} else {
if user.Spec.TenantID != binding.Spec.TenantID {
allErrs = append(allErrs, field.Invalid(fldUserPath, subj.ID, "must in the same tenant with the project"))
} else {
binding.Spec.Users[i].ID = user.Spec.ID
validUsers = append(validUsers, binding.Spec.Users[i])
}
}
default:
validUsers = append(validUsers, binding.Spec.Users[i])
}
}
var validGroups []auth.Subject
fldGroupPath := field.NewPath("spec", "groups")
for i, subj := range binding.Spec.Groups {
if subj.ID == "" {
allErrs = append(allErrs, field.Required(fldGroupPath, "must specify id"))
continue
}
if subj.Name == "" {
val, err := authClient.Groups().Get(ctx, util.CombineTenantAndName(binding.Spec.TenantID, subj.ID), metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
log.Warn("group of the policy is not found, will removed it", log.String("policy", binding.Name), log.String("group", subj.Name))
} else {
allErrs = append(allErrs, field.InternalError(fldGroupPath, err))
}
} else {
if val.Spec.TenantID != binding.Spec.TenantID {
allErrs = append(allErrs, field.Invalid(fldGroupPath, subj.ID, "must in the same tenant with the policy"))
} else {
binding.Spec.Groups[i].Name = val.Spec.DisplayName
validGroups = append(validGroups, binding.Spec.Groups[i])
}
}
} else {
validGroups = append(validGroups, binding.Spec.Groups[i])
}
}
if len(allErrs) == 0 {
binding.Spec.Users = validUsers
binding.Spec.Groups = validGroups
}
log.Debug("binding spec", log.Any("spec", binding.Spec))
return allErrs
}
// ValidateProjectPolicyBindingUpdate tests if required fields in the policy are set during
// an update.
func ValidateProjectPolicyBindingUpdate(ctx context.Context, new *auth.ProjectPolicyBinding, old *auth.ProjectPolicyBinding, authClient authinternalclient.AuthInterface) field.ErrorList {
allErrs := apiMachineryValidation.ValidateObjectMetaUpdate(&new.ObjectMeta, &old.ObjectMeta, field.NewPath("metadata"))
allErrs = append(allErrs, ValidateProjectPolicyBinding(ctx, new, authClient)...)
fldSpecPath := field.NewPath("spec")
if new.Spec.TenantID != old.Spec.TenantID {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("tenantID"), new.Spec.TenantID, "disallowed change the tenant"))
}
if new.Spec.ProjectID != old.Spec.ProjectID {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("projectID"), new.Spec.ProjectID, "disallowed change the projectID"))
}
if new.Spec.PolicyID != old.Spec.PolicyID {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("policyID"), new.Spec.PolicyID, "disallowed change the policyID"))
}
return allErrs
}