-
Notifications
You must be signed in to change notification settings - Fork 53
/
validation.go
232 lines (212 loc) · 5.79 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package v1
import (
"fmt"
"github.com/rancher/opni/pkg/validation"
"github.com/samber/lo"
)
func (c *Cluster) Validate() error {
if c.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(c.Id); err != nil {
return err
}
if err := validation.ValidateLabels(c.GetMetadata().GetLabels()); err != nil {
return err
}
return nil
}
func (ls *LabelSelector) Validate() error {
if ls.MatchLabels != nil {
if err := validation.ValidateLabels(ls.MatchLabels); err != nil {
return err
}
}
for _, l := range ls.MatchExpressions {
if err := l.Validate(); err != nil {
return err
}
}
return nil
}
func (ls *LabelSelector) WithRestrictInternalLabels() validation.Validator {
return (*restrictedLabelSelector)(ls)
}
// Same as regular label selector, but restricts use of internal labels
type restrictedLabelSelector LabelSelector
func (ls *restrictedLabelSelector) Validate() error {
if ls.MatchLabels != nil {
if err := validation.ValidateLabels(ls.MatchLabels); err != nil {
return err
}
for k := range ls.MatchLabels {
if IsLabelInternal(k) {
return fmt.Errorf("%w: %q", ErrInternalLabelInSelector, k)
}
}
}
for _, l := range ls.MatchExpressions {
if err := l.Validate(); err != nil {
return err
}
if IsLabelInternal(l.Key) {
return fmt.Errorf("%w: %q", ErrInternalLabelInSelector, l.Key)
}
}
return nil
}
func (r *LabelSelectorRequirement) Validate() error {
if r.Key == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "key")
}
if r.Operator == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "operator")
}
if err := validation.ValidateLabelName(r.Key); err != nil {
return err
}
switch LabelSelectorOperator(r.Operator) {
case LabelSelectorOpIn, LabelSelectorOpNotIn, LabelSelectorOpExists, LabelSelectorOpDoesNotExist:
default:
return fmt.Errorf("%w: unknown operator %q (values are case-sensitive)", validation.ErrInvalidValue, r.Operator)
}
for _, value := range r.Values {
if err := validation.ValidateLabelValue(value); err != nil {
return err
}
}
return nil
}
func (s *ClusterSelector) Validate() error {
for _, clusterID := range s.ClusterIDs {
if err := validation.ValidateID(clusterID); err != nil {
return err
}
}
if len(lo.Uniq(s.ClusterIDs)) != len(s.ClusterIDs) {
return fmt.Errorf("%w: %s", validation.ErrDuplicate, "clusterIDs")
}
if s.LabelSelector != nil {
if err := s.LabelSelector.Validate(); err != nil {
return err
}
}
return nil
}
func (r *Role) Validate() error {
if r.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(r.Id); err != nil {
return fmt.Errorf("%w: %q", err, r.Id)
}
for _, clusterID := range r.ClusterIDs {
if err := validation.ValidateID(clusterID); err != nil {
return fmt.Errorf("%w: %q", err, clusterID)
}
}
if len(lo.Uniq(r.ClusterIDs)) != len(r.ClusterIDs) {
return fmt.Errorf("%w: %s", validation.ErrDuplicate, "clusterIDs")
}
if r.MatchLabels != nil {
if err := r.MatchLabels.WithRestrictInternalLabels().Validate(); err != nil {
return err
}
}
if len(r.ClusterIDs) == 0 && len(r.GetMatchLabels().GetMatchLabels()) == 0 && len(r.GetMatchLabels().GetMatchExpressions()) == 0 {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "role must have at least one cluster ID or label selector")
}
return nil
}
func (rb *RoleBinding) Validate() error {
if rb.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if rb.RoleId == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "roleId")
}
if err := validation.ValidateID(rb.Id); err != nil {
return fmt.Errorf("%w: %q", err, rb.Id)
}
if err := validation.ValidateID(rb.RoleId); err != nil {
return fmt.Errorf("%w: %q", err, rb.RoleId)
}
if len(rb.Subjects) == 0 {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "subjects")
}
for _, subject := range rb.Subjects {
if err := validation.ValidateSubject(subject); err != nil {
return err
}
}
return nil
}
func (ref *Reference) Validate() error {
if ref.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(ref.Id); err != nil {
return err
}
return nil
}
func (sar *SubjectAccessRequest) Validate() error {
if sar.Subject == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "subject")
}
if err := validation.ValidateSubject(sar.Subject); err != nil {
return err
}
return nil
}
func (o MatchOptions) Validate() error {
if _, ok := MatchOptions_name[int32(o)]; !ok {
return fmt.Errorf("%w: MatchOptions(%d)", validation.ErrInvalidValue, o)
}
return nil
}
func (tc *TokenCapability) Validate() error {
if tc.Type == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "type")
}
if ref := tc.GetReference(); ref != nil {
if err := ref.Validate(); err != nil {
return err
}
}
return nil
}
func (cc *ClusterCapability) Validate() error {
if cc.Name == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "name")
}
return nil
}
func (l *ChallengeRequestList) Validate() error {
for _, cr := range l.GetItems() {
if err := cr.Validate(); err != nil {
return err
}
}
return nil
}
func (cr *ChallengeRequest) Validate() error {
if len(cr.GetChallenge()) == 0 {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "challenge")
}
return nil
}
func (l *ChallengeResponseList) Validate() error {
for _, cr := range l.GetItems() {
if err := cr.Validate(); err != nil {
return err
}
}
return nil
}
func (cr *ChallengeResponse) Validate() error {
if len(cr.GetResponse()) == 0 {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "response")
}
return nil
}