k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/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.DeprecatedAppArmorBetaProfileRuntimeDefault, false},
    42  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + "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.DeprecatedAppArmorBetaProfileRuntimeDefault, true},
    64  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + "docker-default", true},
    65  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + "foo-container", true},
    66  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + "/usr/sbin/ntpd", true},
    67  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + "", false}, // Empty profile explicitly forbidden.
    68  		{v1.DeprecatedAppArmorBetaProfileNamePrefix + " ", false},
    69  	}
    70  
    71  	for _, test := range tests {
    72  		err := v.Validate(getPodWithProfile(test.profile))
    73  		if test.expectValid {
    74  			assert.NoError(t, err, "Pod with profile %q should be valid", test.profile)
    75  		} else {
    76  			assert.Error(t, err, fmt.Sprintf("Pod with profile %q should trigger a validation error", test.profile))
    77  		}
    78  	}
    79  
    80  	// Test multi-container pod.
    81  	pod := &v1.Pod{
    82  		ObjectMeta: metav1.ObjectMeta{
    83  			Annotations: map[string]string{
    84  				v1.DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "init":  v1.DeprecatedAppArmorBetaProfileNamePrefix + "foo-container",
    85  				v1.DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "test1": v1.DeprecatedAppArmorBetaProfileRuntimeDefault,
    86  				v1.DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "test2": v1.DeprecatedAppArmorBetaProfileNamePrefix + "docker-default",
    87  			},
    88  		},
    89  		Spec: v1.PodSpec{
    90  			InitContainers: []v1.Container{
    91  				{Name: "init"},
    92  			},
    93  			Containers: []v1.Container{
    94  				{Name: "test1"},
    95  				{Name: "test2"},
    96  				{Name: "no-profile"},
    97  			},
    98  		},
    99  	}
   100  	assert.NoError(t, v.Validate(pod), "Multi-container pod should validate")
   101  }
   102  
   103  func getPodWithProfile(profile string) *v1.Pod {
   104  	annotations := map[string]string{
   105  		v1.DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "test": profile,
   106  	}
   107  	if profile == "" {
   108  		annotations = map[string]string{
   109  			"foo": "bar",
   110  		}
   111  	}
   112  	return &v1.Pod{
   113  		ObjectMeta: metav1.ObjectMeta{
   114  			Annotations: annotations,
   115  		},
   116  		Spec: v1.PodSpec{
   117  			Containers: []v1.Container{
   118  				{
   119  					Name: "test",
   120  				},
   121  			},
   122  		},
   123  	}
   124  }