kubernetes/pkg/kubelet/sysctl/allowlist_test.go
Wesley Williams ff165c8823
Replace usage of Whitelist with Allowlist within Kubelet's sysctl package (#102298)
* Change uses of whitelist to allowlist in kubelet sysctl

* Rename whitelist files to allowlist in Kubelet sysctl

* Further renames of whitelist to allowlist in Kubelet

* Rename podsecuritypolicy uses of whitelist to allowlist

* Update pkg/kubelet/kubelet.go

Co-authored-by: Danielle <dani@builds.terrible.systems>

Co-authored-by: Danielle <dani@builds.terrible.systems>
2021-08-04 18:59:35 -07:00

85 lines
2.5 KiB
Go

/*
Copyright 2016 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 sysctl
import (
"testing"
"k8s.io/kubernetes/pkg/security/podsecuritypolicy/sysctl"
)
func TestNewAllowlist(t *testing.T) {
type Test struct {
sysctls []string
err bool
}
for _, test := range []Test{
{sysctls: []string{"kernel.msg*", "kernel.sem"}},
{sysctls: []string{" kernel.msg*"}, err: true},
{sysctls: []string{"kernel.msg* "}, err: true},
{sysctls: []string{"net.-"}, err: true},
{sysctls: []string{"net.*.foo"}, err: true},
{sysctls: []string{"foo"}, err: true},
} {
_, err := NewAllowlist(append(sysctl.SafeSysctlAllowlist(), test.sysctls...))
if test.err && err == nil {
t.Errorf("expected an error creating a allowlist for %v", test.sysctls)
} else if !test.err && err != nil {
t.Errorf("got unexpected error creating a allowlist for %v: %v", test.sysctls, err)
}
}
}
func TestAllowlist(t *testing.T) {
type Test struct {
sysctl string
hostNet, hostIPC bool
}
valid := []Test{
{sysctl: "kernel.shm_rmid_forced"},
{sysctl: "net.ipv4.ip_local_port_range"},
{sysctl: "kernel.msgmax"},
{sysctl: "kernel.sem"},
}
invalid := []Test{
{sysctl: "kernel.shm_rmid_forced", hostIPC: true},
{sysctl: "net.ipv4.ip_local_port_range", hostNet: true},
{sysctl: "foo"},
{sysctl: "net.a.b.c", hostNet: false},
{sysctl: "net.ipv4.ip_local_port_range.a.b.c", hostNet: false},
{sysctl: "kernel.msgmax", hostIPC: true},
{sysctl: "kernel.sem", hostIPC: true},
}
w, err := NewAllowlist(append(sysctl.SafeSysctlAllowlist(), "kernel.msg*", "kernel.sem"))
if err != nil {
t.Fatalf("failed to create allowlist: %v", err)
}
for _, test := range valid {
if err := w.validateSysctl(test.sysctl, test.hostNet, test.hostIPC); err != nil {
t.Errorf("expected to be allowlisted: %+v, got: %v", test, err)
}
}
for _, test := range invalid {
if err := w.validateSysctl(test.sysctl, test.hostNet, test.hostIPC); err == nil {
t.Errorf("expected to be rejected: %+v", test)
}
}
}