k8s.io/kubernetes@v1.29.3/pkg/security/apparmor/validate_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apparmor 18 19 import ( 20 "errors" 21 "fmt" 22 "testing" 23 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestValidateBadHost(t *testing.T) { 31 hostErr := errors.New("expected host error") 32 v := &validator{ 33 validateHostErr: hostErr, 34 } 35 36 tests := []struct { 37 profile string 38 expectValid bool 39 }{ 40 {"", true}, 41 {v1.AppArmorBetaProfileRuntimeDefault, false}, 42 {v1.AppArmorBetaProfileNamePrefix + "docker-default", false}, 43 } 44 45 for _, test := range tests { 46 err := v.Validate(getPodWithProfile(test.profile)) 47 if test.expectValid { 48 assert.NoError(t, err, "Pod with profile %q should be valid", test.profile) 49 } else { 50 assert.Equal(t, hostErr, err, "Pod with profile %q should trigger a host validation error", test.profile) 51 } 52 } 53 } 54 55 func TestValidateValidHost(t *testing.T) { 56 v := &validator{} 57 58 tests := []struct { 59 profile string 60 expectValid bool 61 }{ 62 {"", true}, 63 {v1.AppArmorBetaProfileRuntimeDefault, true}, 64 {v1.AppArmorBetaProfileNamePrefix + "docker-default", true}, 65 {v1.AppArmorBetaProfileNamePrefix + "foo-container", true}, 66 {v1.AppArmorBetaProfileNamePrefix + "/usr/sbin/ntpd", true}, 67 {"docker-default", false}, 68 {v1.AppArmorBetaProfileNamePrefix + "", false}, // Empty profile explicitly forbidden. 69 {v1.AppArmorBetaProfileNamePrefix + " ", false}, 70 } 71 72 for _, test := range tests { 73 err := v.Validate(getPodWithProfile(test.profile)) 74 if test.expectValid { 75 assert.NoError(t, err, "Pod with profile %q should be valid", test.profile) 76 } else { 77 assert.Error(t, err, fmt.Sprintf("Pod with profile %q should trigger a validation error", test.profile)) 78 } 79 } 80 81 // Test multi-container pod. 82 pod := &v1.Pod{ 83 ObjectMeta: metav1.ObjectMeta{ 84 Annotations: map[string]string{ 85 v1.AppArmorBetaContainerAnnotationKeyPrefix + "init": v1.AppArmorBetaProfileNamePrefix + "foo-container", 86 v1.AppArmorBetaContainerAnnotationKeyPrefix + "test1": v1.AppArmorBetaProfileRuntimeDefault, 87 v1.AppArmorBetaContainerAnnotationKeyPrefix + "test2": v1.AppArmorBetaProfileNamePrefix + "docker-default", 88 }, 89 }, 90 Spec: v1.PodSpec{ 91 InitContainers: []v1.Container{ 92 {Name: "init"}, 93 }, 94 Containers: []v1.Container{ 95 {Name: "test1"}, 96 {Name: "test2"}, 97 {Name: "no-profile"}, 98 }, 99 }, 100 } 101 assert.NoError(t, v.Validate(pod), "Multi-container pod should validate") 102 } 103 104 func getPodWithProfile(profile string) *v1.Pod { 105 annotations := map[string]string{ 106 v1.AppArmorBetaContainerAnnotationKeyPrefix + "test": profile, 107 } 108 if profile == "" { 109 annotations = map[string]string{ 110 "foo": "bar", 111 } 112 } 113 return &v1.Pod{ 114 ObjectMeta: metav1.ObjectMeta{ 115 Annotations: annotations, 116 }, 117 Spec: v1.PodSpec{ 118 Containers: []v1.Container{ 119 { 120 Name: "test", 121 }, 122 }, 123 }, 124 } 125 }