-
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: preventing serviceaccount privilege escalation
- Loading branch information
1 parent
132ffd5
commit 75525ac
Showing
11 changed files
with
154 additions
and
14 deletions.
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
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,35 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" | ||
) | ||
|
||
const ( | ||
NodeSelectorAnnotation = "scheduler.alpha.kubernetes.io/node-selector" | ||
) | ||
|
||
func BuildNodeSelector(tnt *capsulev1beta1.Tenant, nsAnnotations map[string]string) map[string]string { | ||
if nsAnnotations == nil { | ||
nsAnnotations = make(map[string]string) | ||
} | ||
|
||
selector := make([]string, 0, len(tnt.Spec.NodeSelector)) | ||
|
||
for k, v := range tnt.Spec.NodeSelector { | ||
selector = append(selector, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
// Sorting the resulting slice: iterating over maps is randomized, and we could end-up | ||
// in multiple reconciliations upon multiple node selectors. | ||
sort.Strings(selector) | ||
|
||
nsAnnotations[NodeSelectorAnnotation] = strings.Join(selector, ",") | ||
|
||
return nsAnnotations | ||
} |
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,64 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package namespace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
capsulewebhook "github.com/clastix/capsule/pkg/webhook" | ||
"github.com/clastix/capsule/pkg/webhook/utils" | ||
) | ||
|
||
type ownerReferenceHandler struct{} | ||
|
||
func OwnerReferenceHandler() capsulewebhook.Handler { | ||
return &ownerReferenceHandler{} | ||
} | ||
|
||
func (r *ownerReferenceHandler) OnCreate(client.Client, *admission.Decoder, record.EventRecorder) capsulewebhook.Func { | ||
return func(ctx context.Context, req admission.Request) *admission.Response { | ||
return nil | ||
} | ||
} | ||
|
||
func (r *ownerReferenceHandler) OnDelete(client.Client, *admission.Decoder, record.EventRecorder) capsulewebhook.Func { | ||
return func(ctx context.Context, req admission.Request) *admission.Response { | ||
return nil | ||
} | ||
} | ||
|
||
func (r *ownerReferenceHandler) OnUpdate(_ client.Client, decoder *admission.Decoder, _ record.EventRecorder) capsulewebhook.Func { | ||
return func(ctx context.Context, req admission.Request) *admission.Response { | ||
oldNs := &corev1.Namespace{} | ||
if err := decoder.DecodeRaw(req.OldObject, oldNs); err != nil { | ||
return utils.ErroredResponse(err) | ||
} | ||
|
||
newNs := &corev1.Namespace{} | ||
if err := decoder.Decode(req, newNs); err != nil { | ||
return utils.ErroredResponse(err) | ||
} | ||
|
||
if len(newNs.OwnerReferences) == 0 { | ||
response := admission.Errored(http.StatusBadRequest, fmt.Errorf("the OwnerReference cannot be removed")) | ||
|
||
return &response | ||
} | ||
|
||
if oldNs.GetOwnerReferences()[0].UID != newNs.GetOwnerReferences()[0].UID { | ||
response := admission.Errored(http.StatusBadRequest, fmt.Errorf("the OwnerReference cannot be changed")) | ||
|
||
return &response | ||
} | ||
|
||
return nil | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package namespace | ||
|
||
import ( | ||
|
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
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
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