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.
Merge pull request kubevirt#8649 from acardace/prevent-postcopy
Run VMs as 'restricted' PSA pods
- Loading branch information
Showing
90 changed files
with
1,815 additions
and
1,303 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
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,24 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["psa.go"], | ||
importpath = "kubevirt.io/kubevirt/pkg/psa", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//vendor/k8s.io/api/core/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/tools/cache:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["psa_test.go"], | ||
embed = [":go_default_library"], | ||
deps = [ | ||
"//vendor/github.com/onsi/ginkgo/v2:go_default_library", | ||
"//vendor/github.com/onsi/gomega:go_default_library", | ||
"//vendor/k8s.io/api/core/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
], | ||
) |
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,44 @@ | ||
/* | ||
* 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 psa | ||
|
||
import ( | ||
"fmt" | ||
|
||
k8sv1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
const PSALabel = "pod-security.kubernetes.io/enforce" | ||
|
||
func IsNamespacePrivilegedWithStore(namespaceStore cache.Store, namespace string) (bool, error) { | ||
obj, exists, err := namespaceStore.GetByKey(namespace) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get namespace, %w", err) | ||
} | ||
if !exists { | ||
return false, fmt.Errorf("namespace %s not observed, %w", namespace, err) | ||
} | ||
return IsNamespacePrivileged(obj.(*k8sv1.Namespace)), nil | ||
} | ||
|
||
func IsNamespacePrivileged(namespace *k8sv1.Namespace) bool { | ||
enforceLevel, labelExist := namespace.Labels[PSALabel] | ||
return labelExist && enforceLevel == "privileged" | ||
} |
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,40 @@ | ||
package psa | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
k8sv1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("PSA", func() { | ||
var ( | ||
privilegedNamespace *k8sv1.Namespace | ||
restrictedNamespace *k8sv1.Namespace | ||
) | ||
|
||
BeforeEach(func() { | ||
privilegedNamespace = newNamespace("privileged") | ||
restrictedNamespace = newNamespace("restricted") | ||
}) | ||
|
||
Context("should report correct PSA level", func() { | ||
DescribeTable("when inspecting namespace", func(namespace *k8sv1.Namespace, privileged bool) { | ||
Expect(IsNamespacePrivileged(namespace)).To(Equal(privileged)) | ||
}, | ||
Entry("privileged", privilegedNamespace, true), | ||
Entry("restricted", restrictedNamespace, false), | ||
Entry("with no label", &k8sv1.Namespace{}, false), | ||
) | ||
}) | ||
}) | ||
|
||
func newNamespace(level string) *k8sv1.Namespace { | ||
return &k8sv1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
PSALabel: level, | ||
}, | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.