forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.go
194 lines (171 loc) · 5.83 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
// Copyright 2018 Istio Authors
//
// 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 (
"crypto/tls"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"time"
"github.com/hashicorp/go-multierror"
"istio.io/pkg/log"
"istio.io/pkg/probe"
mixervalidate "istio.io/istio/mixer/pkg/validate"
"istio.io/istio/pkg/config/schemas"
"istio.io/istio/pkg/kube"
)
const (
dns1123LabelMaxLength int = 63
dns1123LabelFmt string = "[a-zA-Z0-9]([-a-z-A-Z0-9]*[a-zA-Z0-9])?"
httpsHandlerReadinessFreq = time.Second
)
var dns1123LabelRegexp = regexp.MustCompile("^" + dns1123LabelFmt + "$")
// This is for lint fix
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
func webhookHTTPSHandlerReady(client httpClient, vc *WebhookParameters) error {
readinessURL := &url.URL{
Scheme: "https",
Host: fmt.Sprintf("localhost:%v", vc.Port),
Path: httpsHandlerReadyPath,
}
req := &http.Request{
Method: http.MethodGet,
URL: readinessURL,
}
response, err := client.Do(req)
if err != nil {
return fmt.Errorf("HTTP request to %v failed: %v", readinessURL, err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("GET %v returned non-200 status=%v",
readinessURL, response.StatusCode)
}
return nil
}
//RunValidation start running Galley validation mode
func RunValidation(ready chan<- struct{}, stopCh chan struct{}, vc *WebhookParameters, kubeConfig string,
livenessProbeController, readinessProbeController probe.Controller) {
log.Infof("Galley validation started with\n%s", vc)
mixerValidator := mixervalidate.NewDefaultValidator(false)
clientset, err := kube.CreateClientset(kubeConfig, "")
if err != nil {
log.Fatalf("could not create k8s clientset: %v", err)
}
vc.MixerValidator = mixerValidator
vc.PilotDescriptor = schemas.Istio
vc.Clientset = clientset
wh, err := NewWebhook(*vc)
if err != nil {
log.Fatalf("cannot create validation webhook service: %v", err)
}
if livenessProbeController != nil {
validationLivenessProbe := probe.NewProbe()
validationLivenessProbe.SetAvailable(nil)
validationLivenessProbe.RegisterProbe(livenessProbeController, "validationLiveness")
defer validationLivenessProbe.SetAvailable(errors.New("stopped"))
}
if readinessProbeController != nil {
validationReadinessProbe := probe.NewProbe()
validationReadinessProbe.SetAvailable(errors.New("init"))
validationReadinessProbe.RegisterProbe(readinessProbeController, "validationReadiness")
go func() {
ready := false
client := &http.Client{
Timeout: time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
for {
if err := webhookHTTPSHandlerReady(client, vc); err != nil {
validationReadinessProbe.SetAvailable(errors.New("not ready"))
scope.Infof("https handler for validation webhook is not ready: %v", err)
ready = false
} else {
validationReadinessProbe.SetAvailable(nil)
if !ready {
scope.Info("https handler for validation webhook is ready")
ready = true
}
}
select {
case <-stopCh:
validationReadinessProbe.SetAvailable(errors.New("stopped"))
return
case <-time.After(httpsHandlerReadinessFreq):
// check again
}
}
}()
}
go wh.Run(ready, stopCh)
defer wh.Stop()
}
// isDNS1123Label tests for a string that conforms to the definition of a label in
// DNS (RFC 1123).
func isDNS1123Label(value string) bool {
return len(value) <= dns1123LabelMaxLength && dns1123LabelRegexp.MatchString(value)
}
// validatePort checks that the network port is in range
func validatePort(port int) error {
if 1 <= port && port <= 65535 {
return nil
}
return fmt.Errorf("port number %d must be in the range 1..65535", port)
}
// Validate tests if the WebhookParameters has valid params.
func (args *WebhookParameters) Validate() error {
if args == nil {
return errors.New("nil WebhookParameters")
}
var errs *multierror.Error
if args.EnableValidation {
// Validate the options that exposed to end users
if args.WebhookName == "" || !isDNS1123Label(args.WebhookName) {
errs = multierror.Append(errs, fmt.Errorf("invalid webhook name: %q", args.WebhookName)) // nolint: lll
}
if args.DeploymentName == "" || !isDNS1123Label(args.DeploymentAndServiceNamespace) {
errs = multierror.Append(errs, fmt.Errorf("invalid deployment namespace: %q", args.DeploymentAndServiceNamespace)) // nolint: lll
}
if args.DeploymentName == "" || !isDNS1123Label(args.DeploymentName) {
errs = multierror.Append(errs, fmt.Errorf("invalid deployment name: %q", args.DeploymentName))
}
if args.ServiceName == "" || !isDNS1123Label(args.ServiceName) {
errs = multierror.Append(errs, fmt.Errorf("invalid service name: %q", args.ServiceName))
}
if len(args.WebhookConfigFile) == 0 {
errs = multierror.Append(errs, errors.New("webhookConfigFile not specified"))
}
if len(args.CertFile) == 0 {
errs = multierror.Append(errs, errors.New("cert file not specified"))
}
if len(args.KeyFile) == 0 {
errs = multierror.Append(errs, errors.New("key file not specified"))
}
if len(args.CACertFile) == 0 {
errs = multierror.Append(errs, errors.New("CA cert file not specified"))
}
if err := validatePort(int(args.Port)); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs.ErrorOrNil()
}