forked from open-policy-agent/gatekeeper
/
policy.go
278 lines (252 loc) · 9.04 KB
/
policy.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
package webhook
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"strings"
templv1alpha1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1alpha1"
opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
rtypes "github.com/open-policy-agent/frameworks/constraint/pkg/types"
"github.com/open-policy-agent/gatekeeper/pkg/apis/config/v1alpha1"
"github.com/open-policy-agent/gatekeeper/pkg/controller/config"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)
func init() {
AddToManagerFuncs = append(AddToManagerFuncs, AddPolicyWebhook)
}
const (
namespace = "gatekeeper-system"
)
var log = logf.Log.WithName("webhook")
var (
runtimeScheme = k8sruntime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
enableManualDeploy = flag.Bool("enable-manual-deploy", false, "allow users to manually create webhook related objects")
port = flag.Int("port", 443, "port for the server. defaulted to 443 if unspecified ")
webhookName = flag.String("webhook-name", "validation.gatekeeper.sh", "domain name of the webhook, with at least three segments separated by dots. defaulted to validation.gatekeeper.sh if unspecified ")
)
// AddPolicyWebhook registers the policy webhook server with the manager
// below: notations add permissions kube-mgmt needs. Access cannot yet be restricted on a namespace-level granularity
// +kubebuilder:rbac:groups=*,resources=*,verbs=get;list;watch
// +kubebuilder:rbac:groups=,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
func AddPolicyWebhook(mgr manager.Manager, opa opa.Client) error {
validatingWh, err := builder.NewWebhookBuilder().
Validating().
Name(*webhookName).
Path("/v1/admit").
Rules(admissionregistrationv1beta1.RuleWithOperations{
Operations: []admissionregistrationv1beta1.OperationType{admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update},
Rule: admissionregistrationv1beta1.Rule{
APIGroups: []string{"*"},
APIVersions: []string{"*"},
Resources: []string{"*"},
},
}).
Handlers(&validationHandler{opa: opa, client: mgr.GetClient()}).
WithManager(mgr).
Build()
if err != nil {
return err
}
serverOptions := webhook.ServerOptions{
CertDir: "/certs",
Port: int32(*port),
}
if *enableManualDeploy == false {
serverOptions.BootstrapOptions = &webhook.BootstrapOptions{
ValidatingWebhookConfigName: *webhookName,
Secret: &types.NamespacedName{
Namespace: namespace,
Name: "gatekeeper-webhook-server-secret",
},
Service: &webhook.Service{
Namespace: namespace,
Name: "gatekeeper-controller-manager-service",
Selectors: map[string]string{
"control-plane": "controller-manager",
"controller-tools.k8s.io": "1.0",
},
},
}
} else {
disableWebhookConfigInstaller := true
serverOptions.DisableWebhookConfigInstaller = &disableWebhookConfigInstaller
}
s, err := webhook.NewServer("policy-admission-server", mgr, serverOptions)
if err != nil {
return err
}
if err := s.Register(validatingWh); err != nil {
return err
}
return nil
}
var _ admission.Handler = &validationHandler{}
type validationHandler struct {
opa opa.Client
client client.Client
// for testing
injectedConfig *v1alpha1.Config
}
// Handle the validation request
func (h *validationHandler) Handle(ctx context.Context, req atypes.Request) atypes.Response {
log := log.WithValues("hookType", "validation")
if isGkServiceAccount(req.AdmissionRequest.UserInfo) {
return admission.ValidationResponse(true, "Gatekeeper does not self-manage")
}
if req.AdmissionRequest.Operation == admissionv1beta1.Delete {
// oldObject is the existing object.
// It is null for DELETE operations in API servers prior to v1.15.0.
// https://github.com/kubernetes/website/pull/14671
if req.AdmissionRequest.OldObject.Raw == nil {
vResp := admission.ValidationResponse(false, "For admission webhooks registered for DELETE operations, please use Kubernetes v1.15.0+.")
vResp.Response.Result.Code = http.StatusInternalServerError
return vResp
} else {
// For admission webhooks registered for DELETE operations on k8s built APIs or CRDs,
// the apiserver now sends the existing object as admissionRequest.Request.OldObject to the webhook
// object is the new object being admitted.
// It is null for DELETE operations.
// https://github.com/kubernetes/kubernetes/pull/76346
req.AdmissionRequest.Object = req.AdmissionRequest.OldObject
}
}
if userErr, err := h.validateGatekeeperResources(ctx, req); err != nil {
vResp := admission.ValidationResponse(false, err.Error())
if vResp.Response.Result == nil {
vResp.Response.Result = &metav1.Status{}
}
if userErr {
vResp.Response.Result.Code = http.StatusUnprocessableEntity
} else {
vResp.Response.Result.Code = http.StatusInternalServerError
}
return vResp
}
resp, err := h.reviewRequest(ctx, req)
if err != nil {
log.Error(err, "error executing query")
vResp := admission.ValidationResponse(false, err.Error())
if vResp.Response.Result == nil {
vResp.Response.Result = &metav1.Status{}
}
vResp.Response.Result.Code = http.StatusInternalServerError
return vResp
}
res := resp.Results()
if len(res) != 0 {
var msgs []string
for _, r := range res {
msgs = append(msgs, fmt.Sprintf("[denied by %s] %s", r.Constraint.GetName(), r.Msg))
}
vResp := admission.ValidationResponse(false, strings.Join(msgs, "\n"))
if vResp.Response.Result == nil {
vResp.Response.Result = &metav1.Status{}
}
vResp.Response.Result.Code = http.StatusForbidden
return vResp
}
return admission.ValidationResponse(true, "")
}
func (h *validationHandler) getConfig(ctx context.Context) (*v1alpha1.Config, error) {
if h.injectedConfig != nil {
return h.injectedConfig, nil
}
if h.client == nil {
return nil, errors.New("no client available to retrieve validation config")
}
cfg := &v1alpha1.Config{}
return cfg, h.client.Get(ctx, config.CfgKey, cfg)
}
func isGkServiceAccount(user authenticationv1.UserInfo) bool {
saGroup := fmt.Sprintf("system:serviceaccounts:%s", namespace)
for _, g := range user.Groups {
if g == saGroup {
return true
}
}
return false
}
// validateGatekeeperResources returns whether an issue is user error (vs internal) and any errors
// validating internal resources
func (h *validationHandler) validateGatekeeperResources(ctx context.Context, req atypes.Request) (bool, error) {
if req.AdmissionRequest.Kind.Group == "templates.gatekeeper.sh" && req.AdmissionRequest.Kind.Kind == "ConstraintTemplate" {
return h.validateTemplate(ctx, req)
}
if req.AdmissionRequest.Kind.Group == "constraints.gatekeeper.sh" {
return h.validateConstraint(ctx, req)
}
return false, nil
}
func (h *validationHandler) validateTemplate(ctx context.Context, req atypes.Request) (bool, error) {
templ := &templv1alpha1.ConstraintTemplate{}
if _, _, err := deserializer.Decode(req.AdmissionRequest.Object.Raw, nil, templ); err != nil {
return false, err
}
if _, err := h.opa.CreateCRD(ctx, templ); err != nil {
return true, err
}
return false, nil
}
func (h *validationHandler) validateConstraint(ctx context.Context, req atypes.Request) (bool, error) {
obj := &unstructured.Unstructured{}
if _, _, err := deserializer.Decode(req.AdmissionRequest.Object.Raw, nil, obj); err != nil {
return false, err
}
if err := h.opa.ValidateConstraint(ctx, obj); err != nil {
return true, err
}
return false, nil
}
// traceSwitch returns true if a request should be traced
func (h *validationHandler) reviewRequest(ctx context.Context, req atypes.Request) (*rtypes.Responses, error) {
cfg, _ := h.getConfig(ctx)
traceEnabled := false
dump := false
for _, trace := range cfg.Spec.Validation.Traces {
if trace.User != req.AdmissionRequest.UserInfo.Username {
continue
}
gvk := v1alpha1.GVK{
Group: req.AdmissionRequest.Kind.Group,
Version: req.AdmissionRequest.Kind.Version,
Kind: req.AdmissionRequest.Kind.Kind,
}
if gvk == trace.Kind {
traceEnabled = true
if trace.Dump == "All" {
dump = true
}
}
}
resp, err := h.opa.Review(ctx, req.AdmissionRequest, opa.Tracing(traceEnabled))
if traceEnabled {
log.Info(resp.TraceDump())
}
if dump {
dump, err := h.opa.Dump(ctx)
if err != nil {
log.Error(err, "dump error")
} else {
log.Info(dump)
}
}
return resp, err
}