/
iam_defaulter.go
121 lines (105 loc) · 4.23 KB
/
iam_defaulter.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
// Copyright 2022 Google LLC
//
// 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 webhook
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gvks"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
var (
iamResourceRefPath = []string{"spec", "resourceRef"}
iamResourceRefField = strings.Join(iamResourceRefPath, ".")
)
type iamDefaulter struct {
smLoader *servicemappingloader.ServiceMappingLoader
serviceMetadataLoader metadata.ServiceMetadataLoader
}
func NewIAMDefaulter(smLoader *servicemappingloader.ServiceMappingLoader,
serviceMetadataLoader metadata.ServiceMetadataLoader) HandlerFunc {
return func(mgr manager.Manager) admission.Handler {
return &iamDefaulter{
smLoader: smLoader,
serviceMetadataLoader: serviceMetadataLoader,
}
}
}
func (a *iamDefaulter) Handle(_ context.Context, req admission.Request) admission.Response {
deserializer := codecs.UniversalDeserializer()
obj := &unstructured.Unstructured{}
if _, _, err := deserializer.Decode(req.AdmissionRequest.Object.Raw, nil, obj); err != nil {
klog.Error(err)
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error decoding object: %w", err))
}
if !isIAMResource(obj) {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("object of GroupVersionKind %v is not a supported IAM resource", obj.GroupVersionKind()))
}
return defaultAPIVersionForIAMResourceRef(obj, a.smLoader, a.serviceMetadataLoader)
}
func defaultAPIVersionForIAMResourceRef(obj *unstructured.Unstructured,
smLoader *servicemappingloader.ServiceMappingLoader,
serviceMetadataLoader metadata.ServiceMetadataLoader) admission.Response {
resourceRef, found, err := unstructured.NestedMap(obj.Object, iamResourceRefPath...)
if err != nil {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error getting '%v': %w", iamResourceRefField, err))
}
if !found {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("couldn't find '%v'", iamResourceRefField))
}
// No need to default spec.resourceRef.apiVersion if already set.
if _, ok := resourceRef["apiVersion"]; ok {
return constructPatchResponse(obj, obj)
}
if _, ok := resourceRef["kind"]; !ok {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("couldn't find %v.kind'", iamResourceRefField))
}
kind, ok := resourceRef["kind"].(string)
if !ok || kind == "" {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("'%v.kind' must be set to a non-empty string", iamResourceRefField))
}
apiVersion, err := apiVersionForKind(kind, smLoader, serviceMetadataLoader)
if err != nil {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("error determining apiVersion for kind '%v': %w", kind, err))
}
resourceRef["apiVersion"] = apiVersion
newObj := obj.DeepCopy()
if err := unstructured.SetNestedMap(newObj.Object, resourceRef, iamResourceRefPath...); err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("error setting '%v': %w", iamResourceRefField, err))
}
return constructPatchResponse(obj, newObj)
}
func apiVersionForKind(kind string,
smLoader *servicemappingloader.ServiceMappingLoader,
serviceMetadataLoader metadata.ServiceMetadataLoader) (string, error) {
gvk, ok := gvks.GVKForKind(kind, smLoader, serviceMetadataLoader)
if !ok {
return "", fmt.Errorf("couldn't find a GroupVersionKind for kind '%v'", kind)
}
return gvk.GroupVersion().String(), nil
}