Files
capsule/pkg/webhook/utils/in_capsule_groups.go
T

85 lines
2.5 KiB
Go

/*
Copyright 2020 Clastix Labs.
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 utils
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/clastix/capsule/pkg/utils"
"github.com/clastix/capsule/pkg/webhook"
)
func InCapsuleGroups(capsuleGroups []string, webhookHandler webhook.Handler) webhook.Handler {
return &handler{
handler: webhookHandler,
capsuleGroups: capsuleGroups,
}
}
type handler struct {
capsuleGroups []string
handler webhook.Handler
}
// If the user performing action is not a Capsule user, can be skipped
func (h handler) isCapsuleUser(req admission.Request) bool {
groupList := utils.NewUserGroupList(req.UserInfo.Groups)
// if the user is a ServiceAccount belonging to the kube-system namespace, definitely, it's not a Capsule user
// and we can skip the check in case of Capsule user group assigned to system:authenticated
// (ref: https://github.com/clastix/capsule/issues/234)
if groupList.Find("system:serviceaccounts:kube-system") {
return false
}
for _, group := range h.capsuleGroups {
if groupList.Find(group) {
return true
}
}
return false
}
func (h *handler) OnCreate(client client.Client, decoder *admission.Decoder) webhook.Func {
return func(ctx context.Context, req admission.Request) admission.Response {
if !h.isCapsuleUser(req) {
return admission.Allowed("")
}
return h.handler.OnCreate(client, decoder)(ctx, req)
}
}
func (h *handler) OnDelete(client client.Client, decoder *admission.Decoder) webhook.Func {
return func(ctx context.Context, req admission.Request) admission.Response {
if !h.isCapsuleUser(req) {
return admission.Allowed("")
}
return h.handler.OnDelete(client, decoder)(ctx, req)
}
}
func (h *handler) OnUpdate(client client.Client, decoder *admission.Decoder) webhook.Func {
return func(ctx context.Context, req admission.Request) admission.Response {
if !h.isCapsuleUser(req) {
return admission.Allowed("")
}
return h.handler.OnUpdate(client, decoder)(ctx, req)
}
}