forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VMs are unfortunatly still privileged workload(in Kubevirt). We have to integrate with new Pod Security Standards in order to allow seamless integration, upgrades. This means we now make sure that target namespace allows privileged workloads if PSA feature gate is enabled. This unfortunatly means users escalate their privileges, in terms of Pod security, by having ability to create VMs. Signed-off-by: L. Pivarc <[email protected]>
- Loading branch information
Showing
13 changed files
with
244 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
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
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,52 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package watch | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
k8sv1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/cache" | ||
"kubevirt.io/client-go/kubecli" | ||
) | ||
|
||
const PSALabel = "pod-security.kubernetes.io/enforce" | ||
|
||
func escalateNamespace(namespaceStore cache.Store, client kubecli.KubevirtClient, namespace string) error { | ||
obj, exists, err := namespaceStore.GetByKey(namespace) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get namespace, %w", err) | ||
} | ||
if !exists { | ||
return fmt.Errorf("Namespace %s not observed, %w", namespace, err) | ||
} | ||
namespaceObj := obj.(*k8sv1.Namespace) | ||
enforceLevel, labelExist := namespaceObj.Labels[PSALabel] | ||
if !labelExist || enforceLevel != "privileged" { | ||
data := []byte(fmt.Sprintf(`{"metadata": { "labels": {"%s": "privileged"}}}`, PSALabel)) | ||
_, err := client.CoreV1().Namespaces().Patch(context.TODO(), namespace, types.StrategicMergePatchType, data, v1.PatchOptions{}) | ||
if err != nil { | ||
return &syncErrorImpl{err, fmt.Sprintf("Failed to apply enforce label on namespace %s", namespace)} | ||
} | ||
} | ||
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package watch | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
k8sv1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sruntime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/testing" | ||
"k8s.io/client-go/tools/cache" | ||
"kubevirt.io/client-go/kubecli" | ||
) | ||
|
||
var _ = Describe("PSA", func() { | ||
var ( | ||
namespaceStore cache.Store | ||
client *kubecli.MockKubevirtClient | ||
kubeClient *fake.Clientset | ||
ctrl *gomock.Controller | ||
) | ||
|
||
BeforeEach(func() { | ||
namespaceStore = cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc) | ||
ctrl = gomock.NewController(GinkgoT()) | ||
client = kubecli.NewMockKubevirtClient(ctrl) | ||
kubeClient = fake.NewSimpleClientset() | ||
client.EXPECT().CoreV1().Return(kubeClient.CoreV1()).AnyTimes() | ||
}) | ||
|
||
Context("should patch namespace with enforce level", func() { | ||
BeforeEach(func() { | ||
kubeClient.Fake.PrependReactor("patch", "namespaces", | ||
func(action testing.Action) (handled bool, obj k8sruntime.Object, err error) { | ||
patchAction, ok := action.(testing.PatchAction) | ||
Expect(ok).To(BeTrue()) | ||
patchBytes := patchAction.GetPatch() | ||
namespace := &k8sv1.Namespace{} | ||
Expect(json.Unmarshal(patchBytes, namespace)).To(Succeed()) | ||
|
||
Expect(namespace.Labels).To(HaveKeyWithValue(PSALabel, "privileged")) | ||
return true, nil, nil | ||
}) | ||
}) | ||
|
||
It("when label is missing", func() { | ||
namespace := &k8sv1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
} | ||
Expect(namespaceStore.Add(namespace)).NotTo(HaveOccurred()) | ||
|
||
Expect(escalateNamespace(namespaceStore, client, "test")).To(Succeed()) | ||
}) | ||
|
||
It("when enforce label is not privileged", func() { | ||
namespace := &k8sv1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Namespace", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Labels: map[string]string{ | ||
PSALabel: "restricted", | ||
}, | ||
}, | ||
} | ||
Expect(namespaceStore.Add(namespace)).NotTo(HaveOccurred()) | ||
|
||
Expect(escalateNamespace(namespaceStore, client, "test")).To(Succeed()) | ||
}) | ||
}) | ||
It("should not patch namespace when enforce label is set to privileged", func() { | ||
namespace := &k8sv1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Labels: map[string]string{ | ||
PSALabel: "privileged", | ||
}, | ||
}, | ||
} | ||
Expect(namespaceStore.Add(namespace)).NotTo(HaveOccurred()) | ||
kubeClient.Fake.PrependReactor("patch", "namespaces", | ||
func(action testing.Action) (handled bool, obj k8sruntime.Object, err error) { | ||
Expect("Patch namespaces is not expected").To(BeEmpty()) | ||
return true, nil, nil | ||
}) | ||
Expect(escalateNamespace(namespaceStore, client, "test")).To(Succeed()) | ||
}) | ||
|
||
}) |
Oops, something went wrong.