Files
Oliver Bähler 0b11582e4a feat: add scheduler enforcement rule (#1971)
* fix(controller): decode old object for delete requests

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* chore: modernize golang

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* chore: modernize golang

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* chore: modernize golang

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* fix: preserve ca-bundles injected from external providers

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>

* feat: abstract ruling

Signed-off-by: Oliver Baehler <oliver@sudo-i.net>

* feat: migrate events api

* feat: migrate events api

* feat: migrate events api

* feat: migrate events api

* feat: migrate events  api

* feat: migrate events api

* feat: migrate events api

* feat: migrate events api

* feat: migrate events api

* feat: migrate events api

---------

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>
Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
2026-06-19 14:34:16 +02:00

112 lines
2.8 KiB
Go

// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package serviceaccounts
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"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/api/meta"
ad "github.com/projectcapsule/capsule/pkg/runtime/admission"
"github.com/projectcapsule/capsule/pkg/runtime/configuration"
"github.com/projectcapsule/capsule/pkg/runtime/events"
"github.com/projectcapsule/capsule/pkg/runtime/handlers"
"github.com/projectcapsule/capsule/pkg/users"
)
type promotion struct {
cfg configuration.Configuration
}
func Promotion(cfg configuration.Configuration) handlers.TypedHandlerWithTenantUser[*corev1.ServiceAccount] {
return &promotion{cfg: cfg}
}
func (h *promotion) OnCreate(
_ client.Client,
_ client.Reader,
user users.AdmissionUser,
sa *corev1.ServiceAccount,
decoder admission.Decoder,
recorder events.EventRecorder,
tnt *capsulev1beta2.Tenant,
) handlers.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
return h.handle(ctx, req, user, recorder, sa, tnt)
}
}
func (h *promotion) OnUpdate(
_ client.Client,
_ client.Reader,
user users.AdmissionUser,
old *corev1.ServiceAccount,
sa *corev1.ServiceAccount,
decoder admission.Decoder,
recorder events.EventRecorder,
tnt *capsulev1beta2.Tenant,
) handlers.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
return h.handle(ctx, req, user, recorder, sa, tnt)
}
}
func (h *promotion) OnDelete(
client.Client,
client.Reader,
users.AdmissionUser,
*corev1.ServiceAccount,
admission.Decoder,
events.EventRecorder,
*capsulev1beta2.Tenant,
) handlers.Func {
return func(context.Context, admission.Request) *admission.Response {
return nil
}
}
func (h *promotion) handle(
ctx context.Context,
req admission.Request,
user users.AdmissionUser,
recorder events.EventRecorder,
sa *corev1.ServiceAccount,
tnt *capsulev1beta2.Tenant,
) *admission.Response {
_, hasPromotion := sa.Labels[meta.ServiceAccountPromotionLabel]
if !hasPromotion {
return nil
}
if !h.cfg.AllowServiceAccountPromotion() {
return ad.Deny("service account promotion is disabled. Contact cluster administrators")
}
// We don't want to allow promoted serviceaccounts to promote other serviceaccounts
if ok := users.IsTenantOwnerByStatus(tnt, user); ok {
return nil
}
msg := fmt.Sprintf("%s not allowed to promote serviceaccount to tenant owner", user.Username)
recorder.LabeledEvent(
sa,
corev1.EventTypeWarning,
events.ReasonPromotionDenied,
events.ActionValidationDenied,
msg,
).
WithRelated(tnt).
WithTenantLabel(tnt).
WithRequestAnnotations(req).
Emit(ctx)
return ad.Deny(msg)
}