-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validating Tenant owner name when is a ServiceAccount
- Loading branch information
1 parent
75ebb57
commit 94c6a64
Showing
2 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenant | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" | ||
capsulewebhook "github.com/clastix/capsule/pkg/webhook" | ||
"github.com/clastix/capsule/pkg/webhook/utils" | ||
) | ||
|
||
type saNameHandler struct { | ||
} | ||
|
||
func ServiceAccountNameHandler() capsulewebhook.Handler { | ||
return &saNameHandler{} | ||
} | ||
|
||
func (h *saNameHandler) validateServiceAccountName(req admission.Request, decoder *admission.Decoder) *admission.Response { | ||
tenant := &capsulev1beta1.Tenant{} | ||
if err := decoder.Decode(req, tenant); err != nil { | ||
return utils.ErroredResponse(err) | ||
} | ||
|
||
compiler := regexp.MustCompile(`^.*:.*:.*(:.*)?$`) | ||
|
||
for _, owner := range tenant.Spec.Owners { | ||
if owner.Kind != "ServiceAccount" { | ||
continue | ||
} | ||
|
||
if !compiler.MatchString(owner.Name) { | ||
response := admission.Denied(fmt.Sprintf("owner name %s is not a valid Service Account name ", owner.Name)) | ||
|
||
return &response | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *saNameHandler) OnCreate(_ client.Client, decoder *admission.Decoder, _ record.EventRecorder) capsulewebhook.Func { | ||
return func(_ context.Context, req admission.Request) *admission.Response { | ||
return h.validateServiceAccountName(req, decoder) | ||
} | ||
} | ||
|
||
func (h *saNameHandler) OnDelete(client.Client, *admission.Decoder, record.EventRecorder) capsulewebhook.Func { | ||
return func(context.Context, admission.Request) *admission.Response { | ||
return nil | ||
} | ||
} | ||
|
||
func (h *saNameHandler) OnUpdate(_ client.Client, decoder *admission.Decoder, _ record.EventRecorder) capsulewebhook.Func { | ||
return func(_ context.Context, req admission.Request) *admission.Response { | ||
return h.validateServiceAccountName(req, decoder) | ||
} | ||
} |