2017-06-09 09:30:34 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2017 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 dockershim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-22 02:49:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-09 09:30:34 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-11-22 02:49:52 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-19 17:39:53 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-09 09:30:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetSeccompSecurityOpts(t *testing.T) {
|
|
|
|
tests := []struct {
|
2017-07-19 08:30:27 +00:00
|
|
|
msg string
|
|
|
|
seccompProfile string
|
|
|
|
expectedOpts []string
|
2017-06-09 09:30:34 +00:00
|
|
|
}{{
|
2017-07-19 08:30:27 +00:00
|
|
|
msg: "No security annotations",
|
|
|
|
seccompProfile: "",
|
|
|
|
expectedOpts: []string{"seccomp=unconfined"},
|
2017-06-09 09:30:34 +00:00
|
|
|
}, {
|
2017-07-19 08:30:27 +00:00
|
|
|
msg: "Seccomp unconfined",
|
|
|
|
seccompProfile: "unconfined",
|
|
|
|
expectedOpts: []string{"seccomp=unconfined"},
|
2017-06-09 09:30:34 +00:00
|
|
|
}, {
|
2017-07-19 08:30:27 +00:00
|
|
|
msg: "Seccomp default",
|
2018-04-19 17:39:53 +00:00
|
|
|
seccompProfile: v1.SeccompProfileRuntimeDefault,
|
|
|
|
expectedOpts: nil,
|
|
|
|
}, {
|
|
|
|
msg: "Seccomp deprecated default",
|
|
|
|
seccompProfile: v1.DeprecatedSeccompProfileDockerDefault,
|
2017-07-19 08:30:27 +00:00
|
|
|
expectedOpts: nil,
|
2017-06-09 09:30:34 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2017-07-19 08:30:27 +00:00
|
|
|
opts, err := getSeccompSecurityOpts(test.seccompProfile, '=')
|
2017-06-09 09:30:34 +00:00
|
|
|
assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
|
|
|
|
assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
|
|
|
|
for _, opt := range test.expectedOpts {
|
|
|
|
assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadSeccompLocalhostProfiles(t *testing.T) {
|
2017-11-22 02:49:52 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "seccomp-local-profile-test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
testProfile := `{"foo": "bar"}`
|
|
|
|
err = ioutil.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-06-09 09:30:34 +00:00
|
|
|
tests := []struct {
|
2017-07-19 08:30:27 +00:00
|
|
|
msg string
|
|
|
|
seccompProfile string
|
|
|
|
expectedOpts []string
|
|
|
|
expectErr bool
|
2017-06-09 09:30:34 +00:00
|
|
|
}{{
|
2017-11-22 02:49:52 +00:00
|
|
|
msg: "Seccomp localhost/test profile should return correct seccomp profiles",
|
|
|
|
seccompProfile: "localhost/" + filepath.Join(tmpdir, "test"),
|
2017-07-19 08:30:27 +00:00
|
|
|
expectedOpts: []string{`seccomp={"foo":"bar"}`},
|
|
|
|
expectErr: false,
|
2017-06-09 09:30:34 +00:00
|
|
|
}, {
|
2017-11-22 02:49:52 +00:00
|
|
|
msg: "Non-existent profile should return error",
|
|
|
|
seccompProfile: "localhost/" + filepath.Join(tmpdir, "fixtures/non-existent"),
|
|
|
|
expectedOpts: nil,
|
|
|
|
expectErr: true,
|
2017-06-09 09:30:34 +00:00
|
|
|
}, {
|
2017-11-22 02:49:52 +00:00
|
|
|
msg: "Relative profile path should return error",
|
|
|
|
seccompProfile: "localhost/fixtures/test",
|
2017-07-19 08:30:27 +00:00
|
|
|
expectedOpts: nil,
|
|
|
|
expectErr: true,
|
2017-06-09 09:30:34 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2017-07-19 08:30:27 +00:00
|
|
|
opts, err := getSeccompSecurityOpts(test.seccompProfile, '=')
|
2017-06-09 09:30:34 +00:00
|
|
|
if test.expectErr {
|
|
|
|
assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
|
|
|
|
assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
|
|
|
|
for _, opt := range test.expectedOpts {
|
|
|
|
assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|