-
Notifications
You must be signed in to change notification settings - Fork 327
/
validation.go
114 lines (94 loc) · 3.8 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
/*
* 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 validation
import (
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
gatewayconfig "tkestack.io/tke/pkg/gateway/apis/config"
)
// ValidateGatewayConfiguration validates `gc` and returns an error if it is invalid
func ValidateGatewayConfiguration(gc *gatewayconfig.GatewayConfiguration) error {
var allErrors []error
fld := field.NewPath("components")
if gc.Components.Platform != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Platform, fld.Child("platform"))...)
}
if gc.Components.Business != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Business, fld.Child("business"))...)
}
if gc.Components.Notify != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Notify, fld.Child("notify"))...)
}
if gc.Components.Monitor != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Monitor, fld.Child("monitor"))...)
}
if gc.Components.Auth != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Auth, fld.Child("auth"))...)
}
if gc.Components.Registry != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Registry, fld.Child("registry"))...)
}
if gc.Components.LogAgent != nil {
allErrors = append(allErrors, validateComponent(gc.Components.LogAgent, fld.Child("logagent"))...)
}
if gc.Components.Audit != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Audit, fld.Child("audit"))...)
}
if gc.Components.Application != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Application, fld.Child("application"))...)
}
if gc.Components.Mesh != nil {
allErrors = append(allErrors, validateComponent(gc.Components.Mesh, fld.Child("mesh"))...)
}
return utilerrors.NewAggregate(allErrors)
}
func validateComponent(c *gatewayconfig.Component, fld *field.Path) []error {
var allErrors []error
if c.Address == "" {
allErrors = append(allErrors, field.Required(fld.Child("address"), "must be specify"))
}
authMode := 0
if c.FrontProxy != nil {
authMode++
subFld := fld.Child("frontProxy")
if c.FrontProxy.ClientKeyFile == "" {
allErrors = append(allErrors, field.Required(subFld.Child("clientKeyFile"), "must be specify"))
}
if c.FrontProxy.ClientCertFile == "" {
allErrors = append(allErrors, field.Required(subFld.Child("clientCertFile"), "must be specify"))
}
if c.FrontProxy.UsernameHeader == "" {
allErrors = append(allErrors, field.Required(subFld.Child("usernameHeader"), "must be specify"))
}
if c.FrontProxy.GroupsHeader == "" {
allErrors = append(allErrors, field.Required(subFld.Child("groupsHeader"), "must be specify"))
}
if c.FrontProxy.ExtraPrefixHeader == "" {
allErrors = append(allErrors, field.Required(subFld.Child("extraPrefixHeader"), "must be specify"))
}
}
if c.Passthrough != nil {
authMode++
}
if authMode == 0 {
allErrors = append(allErrors, field.Required(fld, "at least 1 authentication mode is required"))
} else if authMode > 1 {
allErrors = append(allErrors, field.Required(fld, "authentication mode can only specify at most one"))
}
return allErrors
}