mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
PodSecurity: hostNamespaces: cleanup
Use slice instead of set to accumulate errors Add unit test to exercise message Update docs to clarify undefined values are permitted
This commit is contained in:
parent
62b71175e7
commit
826c57701c
@ -21,7 +21,6 @@ import (
|
|||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
|
||||||
"k8s.io/pod-security-admission/api"
|
"k8s.io/pod-security-admission/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ spec.hostNetwork
|
|||||||
spec.hostPID
|
spec.hostPID
|
||||||
spec.hostIPC
|
spec.hostIPC
|
||||||
|
|
||||||
**Allowed Values:** false
|
**Allowed Values:** undefined, false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -57,25 +56,25 @@ func CheckHostNamespaces() Check {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func hostNamespaces_1_0(podMetadata *metav1.ObjectMeta, podSpec *corev1.PodSpec) CheckResult {
|
func hostNamespaces_1_0(podMetadata *metav1.ObjectMeta, podSpec *corev1.PodSpec) CheckResult {
|
||||||
hostNamespaces := sets.NewString()
|
var hostNamespaces []string
|
||||||
|
|
||||||
if podSpec.HostNetwork {
|
if podSpec.HostNetwork {
|
||||||
hostNamespaces.Insert("hostNetwork")
|
hostNamespaces = append(hostNamespaces, "hostNetwork=true")
|
||||||
}
|
}
|
||||||
|
|
||||||
if podSpec.HostPID {
|
if podSpec.HostPID {
|
||||||
hostNamespaces.Insert("hostPID")
|
hostNamespaces = append(hostNamespaces, "hostPID=true")
|
||||||
}
|
}
|
||||||
|
|
||||||
if podSpec.HostIPC {
|
if podSpec.HostIPC {
|
||||||
hostNamespaces.Insert("hostIPC")
|
hostNamespaces = append(hostNamespaces, "hostIPC=true")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(hostNamespaces) > 0 {
|
if len(hostNamespaces) > 0 {
|
||||||
return CheckResult{
|
return CheckResult{
|
||||||
Allowed: false,
|
Allowed: false,
|
||||||
ForbiddenReason: "host namespaces",
|
ForbiddenReason: "host namespaces",
|
||||||
ForbiddenDetail: strings.Join(hostNamespaces.List(), ", "),
|
ForbiddenDetail: strings.Join(hostNamespaces, ", "),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 The Kubernetes Authors.
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package policy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHostNamespaces(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
pod *corev1.Pod
|
||||||
|
expectReason string
|
||||||
|
expectDetail string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "multiple host namespaces",
|
||||||
|
pod: &corev1.Pod{Spec: corev1.PodSpec{
|
||||||
|
HostNetwork: true,
|
||||||
|
HostIPC: true,
|
||||||
|
HostPID: true,
|
||||||
|
}},
|
||||||
|
expectReason: `host namespaces`,
|
||||||
|
expectDetail: `hostNetwork=true, hostPID=true, hostIPC=true`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
result := hostNamespaces_1_0(&tc.pod.ObjectMeta, &tc.pod.Spec)
|
||||||
|
if result.Allowed {
|
||||||
|
t.Fatal("expected disallowed")
|
||||||
|
}
|
||||||
|
if e, a := tc.expectReason, result.ForbiddenReason; e != a {
|
||||||
|
t.Errorf("expected\n%s\ngot\n%s", e, a)
|
||||||
|
}
|
||||||
|
if e, a := tc.expectDetail, result.ForbiddenDetail; e != a {
|
||||||
|
t.Errorf("expected\n%s\ngot\n%s", e, a)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user