2016-07-30 00:13:32 +00:00
|
|
|
/*
|
|
|
|
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 apparmor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetAppArmorFS(t *testing.T) {
|
|
|
|
// This test only passes on systems running AppArmor with the default configuration.
|
|
|
|
// The test should be manually run if modifying the getAppArmorFS function.
|
|
|
|
t.Skip()
|
|
|
|
|
|
|
|
const expectedPath = "/sys/kernel/security/apparmor"
|
|
|
|
actualPath, err := getAppArmorFS()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedPath, actualPath)
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:44:50 +00:00
|
|
|
func TestValidateHost(t *testing.T) {
|
2016-07-30 00:13:32 +00:00
|
|
|
// This test only passes on systems running AppArmor with the default configuration.
|
|
|
|
// The test should be manually run if modifying the getAppArmorFS function.
|
|
|
|
t.Skip()
|
|
|
|
|
|
|
|
assert.NoError(t, validateHost("docker"))
|
|
|
|
assert.Error(t, validateHost("rkt"))
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:44:50 +00:00
|
|
|
func TestValidateProfile(t *testing.T) {
|
2016-07-30 00:13:32 +00:00
|
|
|
loadedProfiles := map[string]bool{
|
|
|
|
"docker-default": true,
|
|
|
|
"foo-bar": true,
|
|
|
|
"baz": true,
|
|
|
|
"/usr/sbin/ntpd": true,
|
|
|
|
"/usr/lib/connman/scripts/dhclient-script": true,
|
|
|
|
"/usr/lib/NetworkManager/nm-dhcp-client.action": true,
|
|
|
|
"/usr/bin/evince-previewer//sanitized_helper": true,
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
profile string
|
|
|
|
expectValid bool
|
|
|
|
}{
|
|
|
|
{"", true},
|
2016-08-25 22:40:32 +00:00
|
|
|
{ProfileRuntimeDefault, true},
|
2016-07-30 00:13:32 +00:00
|
|
|
{"baz", false}, // Missing local prefix.
|
2016-08-25 22:40:32 +00:00
|
|
|
{ProfileNamePrefix + "/usr/sbin/ntpd", true},
|
|
|
|
{ProfileNamePrefix + "foo-bar", true},
|
|
|
|
{ProfileNamePrefix + "unloaded", false}, // Not loaded.
|
|
|
|
{ProfileNamePrefix + "", false},
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
err := validateProfile(test.profile, loadedProfiles)
|
|
|
|
if test.expectValid {
|
|
|
|
assert.NoError(t, err, "Profile %s should be valid", test.profile)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err, "Profile %s should not be valid", test.profile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:44:50 +00:00
|
|
|
func TestValidateBadHost(t *testing.T) {
|
2016-07-30 00:13:32 +00:00
|
|
|
hostErr := errors.New("expected host error")
|
|
|
|
v := &validator{
|
|
|
|
validateHostErr: hostErr,
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
profile string
|
|
|
|
expectValid bool
|
|
|
|
}{
|
|
|
|
{"", true},
|
2016-08-25 22:40:32 +00:00
|
|
|
{ProfileRuntimeDefault, false},
|
|
|
|
{ProfileNamePrefix + "docker-default", false},
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-08-16 22:44:50 +00:00
|
|
|
err := v.Validate(getPodWithProfile(test.profile))
|
2016-07-30 00:13:32 +00:00
|
|
|
if test.expectValid {
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.NoError(t, err, "Pod with profile %q should be valid", test.profile)
|
2016-07-30 00:13:32 +00:00
|
|
|
} else {
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.Equal(t, hostErr, err, "Pod with profile %q should trigger a host validation error", test.profile)
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:44:50 +00:00
|
|
|
func TestValidateValidHost(t *testing.T) {
|
2016-07-30 00:13:32 +00:00
|
|
|
v := &validator{
|
|
|
|
appArmorFS: "./testdata/",
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
profile string
|
|
|
|
expectValid bool
|
|
|
|
}{
|
|
|
|
{"", true},
|
2016-08-25 22:40:32 +00:00
|
|
|
{ProfileRuntimeDefault, true},
|
|
|
|
{ProfileNamePrefix + "docker-default", true},
|
|
|
|
{ProfileNamePrefix + "foo-container", true},
|
|
|
|
{ProfileNamePrefix + "/usr/sbin/ntpd", true},
|
2016-07-30 00:13:32 +00:00
|
|
|
{"docker-default", false},
|
2016-08-25 22:40:32 +00:00
|
|
|
{ProfileNamePrefix + "foo", false},
|
|
|
|
{ProfileNamePrefix + "", false},
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-08-16 22:44:50 +00:00
|
|
|
err := v.Validate(getPodWithProfile(test.profile))
|
2016-07-30 00:13:32 +00:00
|
|
|
if test.expectValid {
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.NoError(t, err, "Pod with profile %q should be valid", test.profile)
|
2016-07-30 00:13:32 +00:00
|
|
|
} else {
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.Error(t, err, "Pod with profile %q should trigger a validation error", test.profile)
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test multi-container pod.
|
|
|
|
pod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Annotations: map[string]string{
|
2016-08-25 22:40:32 +00:00
|
|
|
ContainerAnnotationKeyPrefix + "init": ProfileNamePrefix + "foo-container",
|
|
|
|
ContainerAnnotationKeyPrefix + "test1": ProfileRuntimeDefault,
|
|
|
|
ContainerAnnotationKeyPrefix + "test2": ProfileNamePrefix + "docker-default",
|
2016-07-30 00:13:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
InitContainers: []api.Container{
|
|
|
|
{Name: "init"},
|
|
|
|
},
|
|
|
|
Containers: []api.Container{
|
|
|
|
{Name: "test1"},
|
|
|
|
{Name: "test2"},
|
|
|
|
{Name: "no-profile"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.NoError(t, v.Validate(pod), "Multi-container pod should validate")
|
2016-07-30 00:13:32 +00:00
|
|
|
for k, val := range pod.Annotations {
|
|
|
|
pod.Annotations[k] = val + "-bad"
|
2016-08-16 22:44:50 +00:00
|
|
|
assert.Error(t, v.Validate(pod), "Multi-container pod with invalid profile %s:%s", k, pod.Annotations[k])
|
2016-07-30 00:13:32 +00:00
|
|
|
pod.Annotations[k] = val // Restore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseProfileName(t *testing.T) {
|
|
|
|
tests := []struct{ line, expected string }{
|
|
|
|
{"foo://bar/baz (kill)", "foo://bar/baz"},
|
|
|
|
{"foo-bar (enforce)", "foo-bar"},
|
|
|
|
{"/usr/foo/bar/baz (complain)", "/usr/foo/bar/baz"},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
name := parseProfileName(test.line)
|
|
|
|
assert.Equal(t, test.expected, name, "Parsing %s", test.line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPodWithProfile(profile string) *api.Pod {
|
|
|
|
annotations := map[string]string{
|
2016-08-25 22:40:32 +00:00
|
|
|
ContainerAnnotationKeyPrefix + "test": profile,
|
2016-07-30 00:13:32 +00:00
|
|
|
}
|
|
|
|
if profile == "" {
|
|
|
|
annotations = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Annotations: annotations,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|