mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-04-10 12:48:02 +00:00
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package validation
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
|
"k8s.io/client-go/tools/events"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
|
|
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
|
|
"github.com/projectcapsule/capsule/pkg/runtime/handlers"
|
|
)
|
|
|
|
type rbRegexHandler struct{}
|
|
|
|
func RoleBindingRegexHandler() handlers.TypedHandler[*capsulev1beta2.Tenant] {
|
|
return &rbRegexHandler{}
|
|
}
|
|
|
|
func (h *rbRegexHandler) OnCreate(
|
|
_ client.Client,
|
|
tnt *capsulev1beta2.Tenant,
|
|
decoder admission.Decoder,
|
|
_ events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(_ context.Context, req admission.Request) *admission.Response {
|
|
return h.validate(tnt, decoder)
|
|
}
|
|
}
|
|
|
|
func (h *rbRegexHandler) OnDelete(
|
|
client.Client,
|
|
*capsulev1beta2.Tenant,
|
|
admission.Decoder,
|
|
events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(context.Context, admission.Request) *admission.Response {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *rbRegexHandler) OnUpdate(
|
|
_ client.Client,
|
|
tnt *capsulev1beta2.Tenant,
|
|
old *capsulev1beta2.Tenant,
|
|
decoder admission.Decoder,
|
|
_ events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(_ context.Context, req admission.Request) *admission.Response {
|
|
return h.validate(tnt, decoder)
|
|
}
|
|
}
|
|
|
|
func (h *rbRegexHandler) validate(tnt *capsulev1beta2.Tenant, decoder admission.Decoder) *admission.Response {
|
|
if len(tnt.Spec.AdditionalRoleBindings) > 0 {
|
|
for _, binding := range tnt.Spec.AdditionalRoleBindings {
|
|
for _, subject := range binding.Subjects {
|
|
if subject.Kind == rbacv1.ServiceAccountKind {
|
|
err := validation.IsDNS1123Subdomain(subject.Name)
|
|
if len(err) > 0 {
|
|
response := admission.Denied(fmt.Sprintf("Subject Name '%v' for binding '%v' is invalid. %v", subject.Name, binding.ClusterRoleName, strings.Join(err, ", ")))
|
|
|
|
return &response
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|