-
Notifications
You must be signed in to change notification settings - Fork 140
/
server.go
292 lines (253 loc) · 8.9 KB
/
server.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright Aeraki 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 server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/hashicorp/go-multierror"
"istio.io/istio/pilot/pkg/config/kube/crd"
"istio.io/istio/pkg/config/schema/collection"
"istio.io/istio/pkg/config/schema/resource"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/kube"
"istio.io/pkg/log"
kubeApiAdmissionv1 "k8s.io/api/admission/v1"
kubeApiAdmissionv1beta1 "k8s.io/api/admission/v1beta1"
kubeApiApps "k8s.io/api/apps/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
var scope = log.RegisterScope("aerakiValidationServer", "aeraki validation webhook server", 0)
var (
runtimeScheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
// Expect AdmissionRequest to only include these top-level field names
validFields = map[string]bool{
"apiVersion": true,
"kind": true,
"metadata": true,
"spec": true,
"status": true,
}
)
// nolint: gochecknoinits
func init() {
_ = kubeApiApps.AddToScheme(runtimeScheme)
_ = kubeApiAdmissionv1.AddToScheme(runtimeScheme)
_ = kubeApiAdmissionv1beta1.AddToScheme(runtimeScheme)
}
// Options contains the configuration for the Istio Pilot validation
// admission controller.
type Options struct {
// Schemas provides a description of all configuration resources.
Schemas collection.Schemas
// DomainSuffix is the DNS domain suffix for Aeraki CRD resources,
// e.g. cluster.local.
// DefaultKubernetesDomain = "cluster.local"
DomainSuffix string
// Port where the webhook is served. the number should be greater than 1024 for non-root
// user, because non-root user cannot bind port number less than 1024
// Mainly used for testing. Webhook server is started by Istiod.
Port uint
// Use an existing mux instead of creating our own.
Mux *http.ServeMux
}
// String produces a stringified version of the arguments for debugging.
func (o Options) String() string {
buf := &bytes.Buffer{}
_, _ = fmt.Fprintf(buf, "DomainSuffix: %s\n", o.DomainSuffix)
_, _ = fmt.Fprintf(buf, "Port: %d\n", o.Port)
return buf.String()
}
// Webhook implements the validating admission webhook for validating Istio configuration.
type Webhook struct {
schemas collection.Schemas
domainSuffix string
}
// New creates a new instance of the admission webhook server.
func New(o Options) (*Webhook, error) {
if o.Mux == nil {
scope.Error("mux not set correctly")
return nil, errors.New("expected mux to be passed, but was not passed")
}
wh := &Webhook{
schemas: o.Schemas,
domainSuffix: o.DomainSuffix,
}
o.Mux.HandleFunc("/validate", wh.serveValidate)
o.Mux.HandleFunc("/validate/", wh.serveValidate)
return wh, nil
}
func toAdmissionResponse(err error) *kube.AdmissionResponse {
return &kube.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
type admitFunc func(*kube.AdmissionRequest) *kube.AdmissionResponse
func serve(w http.ResponseWriter, r *http.Request, admit admitFunc) {
var body []byte
if r.Body != nil {
if data, err := kube.HTTPConfigReader(r); err == nil {
body = data
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
if len(body) == 0 {
reportValidationHTTPError(http.StatusBadRequest)
http.Error(w, "no body found", http.StatusBadRequest)
return
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
reportValidationHTTPError(http.StatusUnsupportedMediaType)
http.Error(w, "invalid Content-Type, want `application/json`", http.StatusUnsupportedMediaType)
return
}
var reviewResponse *kube.AdmissionResponse
var obj runtime.Object
var ar *kube.AdmissionReview
if out, _, err := deserializer.Decode(body, nil, obj); err != nil {
reviewResponse = toAdmissionResponse(fmt.Errorf("could not decode body: %v", err))
} else {
ar, err = kube.AdmissionReviewKubeToAdapter(out)
if err != nil {
reviewResponse = toAdmissionResponse(fmt.Errorf("could not decode object: %v", err))
} else {
reviewResponse = admit(ar.Request)
}
}
response := kube.AdmissionReview{}
response.Response = reviewResponse
var responseKube runtime.Object
var apiVersion string
if ar != nil {
apiVersion = ar.APIVersion
response.TypeMeta = ar.TypeMeta
if response.Response != nil {
if ar.Request != nil {
response.Response.UID = ar.Request.UID
}
}
}
responseKube = kube.AdmissionReviewAdapterToKube(&response, apiVersion)
resp, err := json.Marshal(responseKube)
if err != nil {
reportValidationHTTPError(http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("could encode response: %v", err), http.StatusInternalServerError)
return
}
if _, err := w.Write(resp); err != nil {
reportValidationHTTPError(http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("could write response: %v", err), http.StatusInternalServerError)
}
}
func (wh *Webhook) serveValidate(w http.ResponseWriter, r *http.Request) {
serve(w, r, wh.validate)
}
func (wh *Webhook) validate(request *kube.AdmissionRequest) *kube.AdmissionResponse {
switch request.Operation {
case kube.Create, kube.Update:
default:
scope.Warnf("Unsupported webhook operation %v", request.Operation)
reportValidationFailed(request, reasonUnsupportedOperation)
return &kube.AdmissionResponse{Allowed: true}
}
var obj crd.IstioKind
if err := json.Unmarshal(request.Object.Raw, &obj); err != nil {
scope.Infof("cannot decode configuration: %v", err)
reportValidationFailed(request, reasonYamlDecodeError)
return toAdmissionResponse(fmt.Errorf("cannot decode configuration: %v", err))
}
gvk := obj.GroupVersionKind()
// TODO(jasonwzm) remove this when multi-version is supported. v1beta1 shares the same
// schema as v1lalpha3. Fake conversion and validate against v1alpha3.
// if gvk.Group == "networking.istio.io" && gvk.Version == "v1beta1" {
// gvk.Version = "v1alpha3"
// }
s, exists := wh.schemas.FindByGroupVersionKind(resource.FromKubernetesGVK(&gvk))
if !exists {
scope.Infof("unrecognized type %v", obj.Kind)
reportValidationFailed(request, reasonUnknownType)
return toAdmissionResponse(fmt.Errorf("unrecognized type %v", obj.Kind))
}
out, err := crd.ConvertObject(s, &obj, wh.domainSuffix)
if err != nil {
scope.Infof("error decoding configuration: %v", err)
reportValidationFailed(request, reasonCRDConversionError)
return toAdmissionResponse(fmt.Errorf("error decoding configuration: %v", err))
}
warnings, err := s.ValidateConfig(*out)
if err != nil {
scope.Infof("configuration is invalid: %v", err)
reportValidationFailed(request, reasonInvalidConfig)
return toAdmissionResponse(fmt.Errorf("configuration is invalid: %v", err))
}
if reason, err := checkFields(request.Object.Raw, request.Kind.Kind, request.Namespace, obj.Name); err != nil {
reportValidationFailed(request, reason)
return toAdmissionResponse(err)
}
reportValidationPass(request)
return &kube.AdmissionResponse{Allowed: true, Warnings: toKubeWarnings(warnings)}
}
func toKubeWarnings(warn validation.Warning) []string {
if warn == nil {
return nil
}
me, ok := warn.(*multierror.Error)
if ok {
res := []string{}
for _, e := range me.Errors {
res = append(res, e.Error())
}
return res
}
return []string{warn.Error()}
}
func checkFields(raw []byte, kind, namespace, name string) (string, error) {
trial := make(map[string]json.RawMessage)
if err := json.Unmarshal(raw, &trial); err != nil {
scope.Infof("cannot decode configuration fields: %v", err)
return reasonYamlDecodeError, fmt.Errorf("cannot decode configuration fields: %v", err)
}
for key := range trial {
if _, ok := validFields[key]; !ok {
scope.Infof("unknown field %q on %s resource %s/%s",
key, kind, namespace, name)
return reasonInvalidConfig, fmt.Errorf("unknown field %q on %s resource %s/%s",
key, kind, namespace, name)
}
}
return "", nil
}
// validatePort checks that the network port is in range
func validatePort(port int) error {
if port >= 1 && port <= 65535 {
return nil
}
return fmt.Errorf("port number %d must be in the range 1..65535", port)
}
// Validate tests if the Options has valid params.
func (o Options) Validate() error {
var errs *multierror.Error
if err := validatePort(int(o.Port)); err != nil {
errs = multierror.Append(errs, err)
}
return errs.ErrorOrNil()
}