123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- /*
- 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 dockershim
- import (
- "fmt"
- "strconv"
- "testing"
- dockercontainer "github.com/docker/docker/api/types/container"
- "github.com/stretchr/testify/assert"
- runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
- )
- func TestModifyContainerConfig(t *testing.T) {
- var uid int64 = 123
- var username = "testuser"
- var gid int64 = 423
- cases := []struct {
- name string
- sc *runtimeapi.LinuxContainerSecurityContext
- expected *dockercontainer.Config
- isErr bool
- }{
- {
- name: "container.SecurityContext.RunAsUser set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUser: &runtimeapi.Int64Value{Value: uid},
- },
- expected: &dockercontainer.Config{
- User: strconv.FormatInt(uid, 10),
- },
- isErr: false,
- },
- {
- name: "container.SecurityContext.RunAsUsername set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUsername: username,
- },
- expected: &dockercontainer.Config{
- User: username,
- },
- isErr: false,
- },
- {
- name: "container.SecurityContext.RunAsUsername and container.SecurityContext.RunAsUser set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUsername: username,
- RunAsUser: &runtimeapi.Int64Value{Value: uid},
- },
- expected: &dockercontainer.Config{
- User: username,
- },
- isErr: false,
- },
- {
- name: "no RunAsUser value set",
- sc: &runtimeapi.LinuxContainerSecurityContext{},
- expected: &dockercontainer.Config{},
- isErr: false,
- },
- {
- name: "RunAsUser value set, RunAsGroup set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUser: &runtimeapi.Int64Value{Value: uid},
- RunAsGroup: &runtimeapi.Int64Value{Value: gid},
- },
- expected: &dockercontainer.Config{
- User: "123:423",
- },
- isErr: false,
- },
- {
- name: "RunAsUsername value set, RunAsGroup set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUsername: username,
- RunAsGroup: &runtimeapi.Int64Value{Value: gid},
- },
- expected: &dockercontainer.Config{
- User: "testuser:423",
- },
- isErr: false,
- },
- {
- name: "RunAsUser/RunAsUsername not set, RunAsGroup set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsGroup: &runtimeapi.Int64Value{Value: gid},
- },
- isErr: true,
- },
- {
- name: "RunAsUser/RunAsUsername both set, RunAsGroup set",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- RunAsUser: &runtimeapi.Int64Value{Value: uid},
- RunAsUsername: username,
- RunAsGroup: &runtimeapi.Int64Value{Value: gid},
- },
- expected: &dockercontainer.Config{
- User: "testuser:423",
- },
- isErr: false,
- },
- }
- for _, tc := range cases {
- dockerCfg := &dockercontainer.Config{}
- err := modifyContainerConfig(tc.sc, dockerCfg)
- if tc.isErr {
- assert.NotNil(t, err)
- } else {
- assert.Nil(t, err)
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- }
- func TestModifyHostConfig(t *testing.T) {
- setNetworkHC := &dockercontainer.HostConfig{}
- // When we have Privileged pods, we do not need to use the
- // Masked / Readonly paths.
- setPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
- setPrivSC.Privileged = true
- setPrivSC.MaskedPaths = []string{"/hello/world/masked"}
- setPrivSC.ReadonlyPaths = []string{"/hello/world/readonly"}
- setPrivHC := &dockercontainer.HostConfig{
- Privileged: true,
- }
- unsetPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
- unsetPrivSC.Privileged = false
- unsetPrivSC.MaskedPaths = []string{"/hello/world/masked"}
- unsetPrivSC.ReadonlyPaths = []string{"/hello/world/readonly"}
- unsetPrivHC := &dockercontainer.HostConfig{
- Privileged: false,
- MaskedPaths: []string{"/hello/world/masked"},
- ReadonlyPaths: []string{"/hello/world/readonly"},
- }
- setCapsHC := &dockercontainer.HostConfig{
- CapAdd: []string{"addCapA", "addCapB"},
- CapDrop: []string{"dropCapA", "dropCapB"},
- }
- setSELinuxHC := &dockercontainer.HostConfig{
- SecurityOpt: []string{
- fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
- fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
- fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
- fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
- },
- }
- cases := []struct {
- name string
- sc *runtimeapi.LinuxContainerSecurityContext
- expected *dockercontainer.HostConfig
- }{
- {
- name: "fully set container.SecurityContext",
- sc: fullValidSecurityContext(),
- expected: fullValidHostConfig(),
- },
- {
- name: "empty container.SecurityContext",
- sc: &runtimeapi.LinuxContainerSecurityContext{},
- expected: setNetworkHC,
- },
- {
- name: "container.SecurityContext.Privileged",
- sc: setPrivSC,
- expected: setPrivHC,
- },
- {
- name: "container.SecurityContext.NoPrivileges",
- sc: unsetPrivSC,
- expected: unsetPrivHC,
- },
- {
- name: "container.SecurityContext.Capabilities",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- Capabilities: inputCapabilities(),
- },
- expected: setCapsHC,
- },
- {
- name: "container.SecurityContext.SELinuxOptions",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- SelinuxOptions: inputSELinuxOptions(),
- },
- expected: setSELinuxHC,
- },
- }
- for _, tc := range cases {
- dockerCfg := &dockercontainer.HostConfig{}
- modifyHostConfig(tc.sc, dockerCfg, '=')
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- func TestModifyHostConfigWithGroups(t *testing.T) {
- supplementalGroupsSC := &runtimeapi.LinuxContainerSecurityContext{}
- supplementalGroupsSC.SupplementalGroups = []int64{2222}
- supplementalGroupHC := &dockercontainer.HostConfig{}
- supplementalGroupHC.GroupAdd = []string{"2222"}
- testCases := []struct {
- name string
- securityContext *runtimeapi.LinuxContainerSecurityContext
- expected *dockercontainer.HostConfig
- }{
- {
- name: "nil",
- securityContext: nil,
- expected: &dockercontainer.HostConfig{},
- },
- {
- name: "SupplementalGroup",
- securityContext: supplementalGroupsSC,
- expected: supplementalGroupHC,
- },
- }
- for _, tc := range testCases {
- dockerCfg := &dockercontainer.HostConfig{}
- modifyHostConfig(tc.securityContext, dockerCfg, '=')
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- func TestModifyHostConfigAndNamespaceOptionsForContainer(t *testing.T) {
- priv := true
- sandboxID := "sandbox"
- sandboxNSMode := fmt.Sprintf("container:%v", sandboxID)
- setPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
- setPrivSC.Privileged = priv
- setPrivHC := &dockercontainer.HostConfig{
- Privileged: true,
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- }
- setCapsHC := &dockercontainer.HostConfig{
- CapAdd: []string{"addCapA", "addCapB"},
- CapDrop: []string{"dropCapA", "dropCapB"},
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- }
- setSELinuxHC := &dockercontainer.HostConfig{
- SecurityOpt: []string{
- fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
- fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
- fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
- fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
- },
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- }
- cases := []struct {
- name string
- sc *runtimeapi.LinuxContainerSecurityContext
- expected *dockercontainer.HostConfig
- }{
- {
- name: "container.SecurityContext.Privileged",
- sc: setPrivSC,
- expected: setPrivHC,
- },
- {
- name: "container.SecurityContext.Capabilities",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- Capabilities: inputCapabilities(),
- },
- expected: setCapsHC,
- },
- {
- name: "container.SecurityContext.SELinuxOptions",
- sc: &runtimeapi.LinuxContainerSecurityContext{
- SelinuxOptions: inputSELinuxOptions(),
- },
- expected: setSELinuxHC,
- },
- }
- for _, tc := range cases {
- dockerCfg := &dockercontainer.HostConfig{}
- modifyHostConfig(tc.sc, dockerCfg, '=')
- modifyContainerNamespaceOptions(tc.sc.GetNamespaceOptions(), sandboxID, dockerCfg)
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- func TestModifySandboxNamespaceOptions(t *testing.T) {
- cases := []struct {
- name string
- nsOpt *runtimeapi.NamespaceOption
- expected *dockercontainer.HostConfig
- }{
- {
- name: "Host Network NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Network: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: namespaceModeHost,
- },
- },
- {
- name: "Host IPC NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Ipc: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- IpcMode: namespaceModeHost,
- NetworkMode: "default",
- },
- },
- {
- name: "Host PID NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- PidMode: namespaceModeHost,
- NetworkMode: "default",
- },
- },
- {
- name: "Pod PID NamespaceOption (for sandbox is same as container ns option)",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_POD,
- },
- expected: &dockercontainer.HostConfig{
- PidMode: "",
- NetworkMode: "default",
- },
- },
- {
- name: "Target PID NamespaceOption (invalid for sandbox)",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_TARGET,
- TargetId: "same-container",
- },
- expected: &dockercontainer.HostConfig{
- PidMode: "",
- NetworkMode: "default",
- },
- },
- }
- for _, tc := range cases {
- dockerCfg := &dockercontainer.HostConfig{}
- modifySandboxNamespaceOptions(tc.nsOpt, dockerCfg, nil)
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- func TestModifyContainerNamespaceOptions(t *testing.T) {
- sandboxID := "sandbox"
- sandboxNSMode := fmt.Sprintf("container:%v", sandboxID)
- cases := []struct {
- name string
- nsOpt *runtimeapi.NamespaceOption
- expected *dockercontainer.HostConfig
- }{
- {
- name: "Host Network NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Network: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- UTSMode: namespaceModeHost,
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- },
- },
- {
- name: "Host IPC NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Ipc: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- },
- },
- {
- name: "Host PID NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_NODE,
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- PidMode: namespaceModeHost,
- },
- },
- {
- name: "Pod PID NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_POD,
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode(sandboxNSMode),
- },
- },
- {
- name: "Target PID NamespaceOption",
- nsOpt: &runtimeapi.NamespaceOption{
- Pid: runtimeapi.NamespaceMode_TARGET,
- TargetId: "some-container",
- },
- expected: &dockercontainer.HostConfig{
- NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
- IpcMode: dockercontainer.IpcMode(sandboxNSMode),
- PidMode: dockercontainer.PidMode("container:some-container"),
- },
- },
- }
- for _, tc := range cases {
- dockerCfg := &dockercontainer.HostConfig{}
- modifyContainerNamespaceOptions(tc.nsOpt, sandboxID, dockerCfg)
- assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
- }
- }
- func fullValidSecurityContext() *runtimeapi.LinuxContainerSecurityContext {
- return &runtimeapi.LinuxContainerSecurityContext{
- Privileged: true,
- Capabilities: inputCapabilities(),
- SelinuxOptions: inputSELinuxOptions(),
- }
- }
- func inputCapabilities() *runtimeapi.Capability {
- return &runtimeapi.Capability{
- AddCapabilities: []string{"addCapA", "addCapB"},
- DropCapabilities: []string{"dropCapA", "dropCapB"},
- }
- }
- func inputSELinuxOptions() *runtimeapi.SELinuxOption {
- user := "user"
- role := "role"
- stype := "type"
- level := "level"
- return &runtimeapi.SELinuxOption{
- User: user,
- Role: role,
- Type: stype,
- Level: level,
- }
- }
- func fullValidHostConfig() *dockercontainer.HostConfig {
- return &dockercontainer.HostConfig{
- Privileged: true,
- CapAdd: []string{"addCapA", "addCapB"},
- CapDrop: []string{"dropCapA", "dropCapB"},
- SecurityOpt: []string{
- fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
- fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
- fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
- fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
- },
- }
- }
|