security_context_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dockershim
  14. import (
  15. "fmt"
  16. "strconv"
  17. "testing"
  18. dockercontainer "github.com/docker/docker/api/types/container"
  19. "github.com/stretchr/testify/assert"
  20. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  21. )
  22. func TestModifyContainerConfig(t *testing.T) {
  23. var uid int64 = 123
  24. var username = "testuser"
  25. var gid int64 = 423
  26. cases := []struct {
  27. name string
  28. sc *runtimeapi.LinuxContainerSecurityContext
  29. expected *dockercontainer.Config
  30. isErr bool
  31. }{
  32. {
  33. name: "container.SecurityContext.RunAsUser set",
  34. sc: &runtimeapi.LinuxContainerSecurityContext{
  35. RunAsUser: &runtimeapi.Int64Value{Value: uid},
  36. },
  37. expected: &dockercontainer.Config{
  38. User: strconv.FormatInt(uid, 10),
  39. },
  40. isErr: false,
  41. },
  42. {
  43. name: "container.SecurityContext.RunAsUsername set",
  44. sc: &runtimeapi.LinuxContainerSecurityContext{
  45. RunAsUsername: username,
  46. },
  47. expected: &dockercontainer.Config{
  48. User: username,
  49. },
  50. isErr: false,
  51. },
  52. {
  53. name: "container.SecurityContext.RunAsUsername and container.SecurityContext.RunAsUser set",
  54. sc: &runtimeapi.LinuxContainerSecurityContext{
  55. RunAsUsername: username,
  56. RunAsUser: &runtimeapi.Int64Value{Value: uid},
  57. },
  58. expected: &dockercontainer.Config{
  59. User: username,
  60. },
  61. isErr: false,
  62. },
  63. {
  64. name: "no RunAsUser value set",
  65. sc: &runtimeapi.LinuxContainerSecurityContext{},
  66. expected: &dockercontainer.Config{},
  67. isErr: false,
  68. },
  69. {
  70. name: "RunAsUser value set, RunAsGroup set",
  71. sc: &runtimeapi.LinuxContainerSecurityContext{
  72. RunAsUser: &runtimeapi.Int64Value{Value: uid},
  73. RunAsGroup: &runtimeapi.Int64Value{Value: gid},
  74. },
  75. expected: &dockercontainer.Config{
  76. User: "123:423",
  77. },
  78. isErr: false,
  79. },
  80. {
  81. name: "RunAsUsername value set, RunAsGroup set",
  82. sc: &runtimeapi.LinuxContainerSecurityContext{
  83. RunAsUsername: username,
  84. RunAsGroup: &runtimeapi.Int64Value{Value: gid},
  85. },
  86. expected: &dockercontainer.Config{
  87. User: "testuser:423",
  88. },
  89. isErr: false,
  90. },
  91. {
  92. name: "RunAsUser/RunAsUsername not set, RunAsGroup set",
  93. sc: &runtimeapi.LinuxContainerSecurityContext{
  94. RunAsGroup: &runtimeapi.Int64Value{Value: gid},
  95. },
  96. isErr: true,
  97. },
  98. {
  99. name: "RunAsUser/RunAsUsername both set, RunAsGroup set",
  100. sc: &runtimeapi.LinuxContainerSecurityContext{
  101. RunAsUser: &runtimeapi.Int64Value{Value: uid},
  102. RunAsUsername: username,
  103. RunAsGroup: &runtimeapi.Int64Value{Value: gid},
  104. },
  105. expected: &dockercontainer.Config{
  106. User: "testuser:423",
  107. },
  108. isErr: false,
  109. },
  110. }
  111. for _, tc := range cases {
  112. dockerCfg := &dockercontainer.Config{}
  113. err := modifyContainerConfig(tc.sc, dockerCfg)
  114. if tc.isErr {
  115. assert.NotNil(t, err)
  116. } else {
  117. assert.Nil(t, err)
  118. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  119. }
  120. }
  121. }
  122. func TestModifyHostConfig(t *testing.T) {
  123. setNetworkHC := &dockercontainer.HostConfig{}
  124. // When we have Privileged pods, we do not need to use the
  125. // Masked / Readonly paths.
  126. setPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
  127. setPrivSC.Privileged = true
  128. setPrivSC.MaskedPaths = []string{"/hello/world/masked"}
  129. setPrivSC.ReadonlyPaths = []string{"/hello/world/readonly"}
  130. setPrivHC := &dockercontainer.HostConfig{
  131. Privileged: true,
  132. }
  133. unsetPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
  134. unsetPrivSC.Privileged = false
  135. unsetPrivSC.MaskedPaths = []string{"/hello/world/masked"}
  136. unsetPrivSC.ReadonlyPaths = []string{"/hello/world/readonly"}
  137. unsetPrivHC := &dockercontainer.HostConfig{
  138. Privileged: false,
  139. MaskedPaths: []string{"/hello/world/masked"},
  140. ReadonlyPaths: []string{"/hello/world/readonly"},
  141. }
  142. setCapsHC := &dockercontainer.HostConfig{
  143. CapAdd: []string{"addCapA", "addCapB"},
  144. CapDrop: []string{"dropCapA", "dropCapB"},
  145. }
  146. setSELinuxHC := &dockercontainer.HostConfig{
  147. SecurityOpt: []string{
  148. fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
  149. fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
  150. fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
  151. fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
  152. },
  153. }
  154. cases := []struct {
  155. name string
  156. sc *runtimeapi.LinuxContainerSecurityContext
  157. expected *dockercontainer.HostConfig
  158. }{
  159. {
  160. name: "fully set container.SecurityContext",
  161. sc: fullValidSecurityContext(),
  162. expected: fullValidHostConfig(),
  163. },
  164. {
  165. name: "empty container.SecurityContext",
  166. sc: &runtimeapi.LinuxContainerSecurityContext{},
  167. expected: setNetworkHC,
  168. },
  169. {
  170. name: "container.SecurityContext.Privileged",
  171. sc: setPrivSC,
  172. expected: setPrivHC,
  173. },
  174. {
  175. name: "container.SecurityContext.NoPrivileges",
  176. sc: unsetPrivSC,
  177. expected: unsetPrivHC,
  178. },
  179. {
  180. name: "container.SecurityContext.Capabilities",
  181. sc: &runtimeapi.LinuxContainerSecurityContext{
  182. Capabilities: inputCapabilities(),
  183. },
  184. expected: setCapsHC,
  185. },
  186. {
  187. name: "container.SecurityContext.SELinuxOptions",
  188. sc: &runtimeapi.LinuxContainerSecurityContext{
  189. SelinuxOptions: inputSELinuxOptions(),
  190. },
  191. expected: setSELinuxHC,
  192. },
  193. }
  194. for _, tc := range cases {
  195. dockerCfg := &dockercontainer.HostConfig{}
  196. modifyHostConfig(tc.sc, dockerCfg, '=')
  197. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  198. }
  199. }
  200. func TestModifyHostConfigWithGroups(t *testing.T) {
  201. supplementalGroupsSC := &runtimeapi.LinuxContainerSecurityContext{}
  202. supplementalGroupsSC.SupplementalGroups = []int64{2222}
  203. supplementalGroupHC := &dockercontainer.HostConfig{}
  204. supplementalGroupHC.GroupAdd = []string{"2222"}
  205. testCases := []struct {
  206. name string
  207. securityContext *runtimeapi.LinuxContainerSecurityContext
  208. expected *dockercontainer.HostConfig
  209. }{
  210. {
  211. name: "nil",
  212. securityContext: nil,
  213. expected: &dockercontainer.HostConfig{},
  214. },
  215. {
  216. name: "SupplementalGroup",
  217. securityContext: supplementalGroupsSC,
  218. expected: supplementalGroupHC,
  219. },
  220. }
  221. for _, tc := range testCases {
  222. dockerCfg := &dockercontainer.HostConfig{}
  223. modifyHostConfig(tc.securityContext, dockerCfg, '=')
  224. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  225. }
  226. }
  227. func TestModifyHostConfigAndNamespaceOptionsForContainer(t *testing.T) {
  228. priv := true
  229. sandboxID := "sandbox"
  230. sandboxNSMode := fmt.Sprintf("container:%v", sandboxID)
  231. setPrivSC := &runtimeapi.LinuxContainerSecurityContext{}
  232. setPrivSC.Privileged = priv
  233. setPrivHC := &dockercontainer.HostConfig{
  234. Privileged: true,
  235. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  236. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  237. PidMode: dockercontainer.PidMode(sandboxNSMode),
  238. }
  239. setCapsHC := &dockercontainer.HostConfig{
  240. CapAdd: []string{"addCapA", "addCapB"},
  241. CapDrop: []string{"dropCapA", "dropCapB"},
  242. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  243. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  244. PidMode: dockercontainer.PidMode(sandboxNSMode),
  245. }
  246. setSELinuxHC := &dockercontainer.HostConfig{
  247. SecurityOpt: []string{
  248. fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
  249. fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
  250. fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
  251. fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
  252. },
  253. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  254. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  255. PidMode: dockercontainer.PidMode(sandboxNSMode),
  256. }
  257. cases := []struct {
  258. name string
  259. sc *runtimeapi.LinuxContainerSecurityContext
  260. expected *dockercontainer.HostConfig
  261. }{
  262. {
  263. name: "container.SecurityContext.Privileged",
  264. sc: setPrivSC,
  265. expected: setPrivHC,
  266. },
  267. {
  268. name: "container.SecurityContext.Capabilities",
  269. sc: &runtimeapi.LinuxContainerSecurityContext{
  270. Capabilities: inputCapabilities(),
  271. },
  272. expected: setCapsHC,
  273. },
  274. {
  275. name: "container.SecurityContext.SELinuxOptions",
  276. sc: &runtimeapi.LinuxContainerSecurityContext{
  277. SelinuxOptions: inputSELinuxOptions(),
  278. },
  279. expected: setSELinuxHC,
  280. },
  281. }
  282. for _, tc := range cases {
  283. dockerCfg := &dockercontainer.HostConfig{}
  284. modifyHostConfig(tc.sc, dockerCfg, '=')
  285. modifyContainerNamespaceOptions(tc.sc.GetNamespaceOptions(), sandboxID, dockerCfg)
  286. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  287. }
  288. }
  289. func TestModifySandboxNamespaceOptions(t *testing.T) {
  290. cases := []struct {
  291. name string
  292. nsOpt *runtimeapi.NamespaceOption
  293. expected *dockercontainer.HostConfig
  294. }{
  295. {
  296. name: "Host Network NamespaceOption",
  297. nsOpt: &runtimeapi.NamespaceOption{
  298. Network: runtimeapi.NamespaceMode_NODE,
  299. },
  300. expected: &dockercontainer.HostConfig{
  301. NetworkMode: namespaceModeHost,
  302. },
  303. },
  304. {
  305. name: "Host IPC NamespaceOption",
  306. nsOpt: &runtimeapi.NamespaceOption{
  307. Ipc: runtimeapi.NamespaceMode_NODE,
  308. },
  309. expected: &dockercontainer.HostConfig{
  310. IpcMode: namespaceModeHost,
  311. NetworkMode: "default",
  312. },
  313. },
  314. {
  315. name: "Host PID NamespaceOption",
  316. nsOpt: &runtimeapi.NamespaceOption{
  317. Pid: runtimeapi.NamespaceMode_NODE,
  318. },
  319. expected: &dockercontainer.HostConfig{
  320. PidMode: namespaceModeHost,
  321. NetworkMode: "default",
  322. },
  323. },
  324. {
  325. name: "Pod PID NamespaceOption (for sandbox is same as container ns option)",
  326. nsOpt: &runtimeapi.NamespaceOption{
  327. Pid: runtimeapi.NamespaceMode_POD,
  328. },
  329. expected: &dockercontainer.HostConfig{
  330. PidMode: "",
  331. NetworkMode: "default",
  332. },
  333. },
  334. {
  335. name: "Target PID NamespaceOption (invalid for sandbox)",
  336. nsOpt: &runtimeapi.NamespaceOption{
  337. Pid: runtimeapi.NamespaceMode_TARGET,
  338. TargetId: "same-container",
  339. },
  340. expected: &dockercontainer.HostConfig{
  341. PidMode: "",
  342. NetworkMode: "default",
  343. },
  344. },
  345. }
  346. for _, tc := range cases {
  347. dockerCfg := &dockercontainer.HostConfig{}
  348. modifySandboxNamespaceOptions(tc.nsOpt, dockerCfg, nil)
  349. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  350. }
  351. }
  352. func TestModifyContainerNamespaceOptions(t *testing.T) {
  353. sandboxID := "sandbox"
  354. sandboxNSMode := fmt.Sprintf("container:%v", sandboxID)
  355. cases := []struct {
  356. name string
  357. nsOpt *runtimeapi.NamespaceOption
  358. expected *dockercontainer.HostConfig
  359. }{
  360. {
  361. name: "Host Network NamespaceOption",
  362. nsOpt: &runtimeapi.NamespaceOption{
  363. Network: runtimeapi.NamespaceMode_NODE,
  364. },
  365. expected: &dockercontainer.HostConfig{
  366. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  367. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  368. UTSMode: namespaceModeHost,
  369. PidMode: dockercontainer.PidMode(sandboxNSMode),
  370. },
  371. },
  372. {
  373. name: "Host IPC NamespaceOption",
  374. nsOpt: &runtimeapi.NamespaceOption{
  375. Ipc: runtimeapi.NamespaceMode_NODE,
  376. },
  377. expected: &dockercontainer.HostConfig{
  378. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  379. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  380. PidMode: dockercontainer.PidMode(sandboxNSMode),
  381. },
  382. },
  383. {
  384. name: "Host PID NamespaceOption",
  385. nsOpt: &runtimeapi.NamespaceOption{
  386. Pid: runtimeapi.NamespaceMode_NODE,
  387. },
  388. expected: &dockercontainer.HostConfig{
  389. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  390. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  391. PidMode: namespaceModeHost,
  392. },
  393. },
  394. {
  395. name: "Pod PID NamespaceOption",
  396. nsOpt: &runtimeapi.NamespaceOption{
  397. Pid: runtimeapi.NamespaceMode_POD,
  398. },
  399. expected: &dockercontainer.HostConfig{
  400. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  401. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  402. PidMode: dockercontainer.PidMode(sandboxNSMode),
  403. },
  404. },
  405. {
  406. name: "Target PID NamespaceOption",
  407. nsOpt: &runtimeapi.NamespaceOption{
  408. Pid: runtimeapi.NamespaceMode_TARGET,
  409. TargetId: "some-container",
  410. },
  411. expected: &dockercontainer.HostConfig{
  412. NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),
  413. IpcMode: dockercontainer.IpcMode(sandboxNSMode),
  414. PidMode: dockercontainer.PidMode("container:some-container"),
  415. },
  416. },
  417. }
  418. for _, tc := range cases {
  419. dockerCfg := &dockercontainer.HostConfig{}
  420. modifyContainerNamespaceOptions(tc.nsOpt, sandboxID, dockerCfg)
  421. assert.Equal(t, tc.expected, dockerCfg, "[Test case %q]", tc.name)
  422. }
  423. }
  424. func fullValidSecurityContext() *runtimeapi.LinuxContainerSecurityContext {
  425. return &runtimeapi.LinuxContainerSecurityContext{
  426. Privileged: true,
  427. Capabilities: inputCapabilities(),
  428. SelinuxOptions: inputSELinuxOptions(),
  429. }
  430. }
  431. func inputCapabilities() *runtimeapi.Capability {
  432. return &runtimeapi.Capability{
  433. AddCapabilities: []string{"addCapA", "addCapB"},
  434. DropCapabilities: []string{"dropCapA", "dropCapB"},
  435. }
  436. }
  437. func inputSELinuxOptions() *runtimeapi.SELinuxOption {
  438. user := "user"
  439. role := "role"
  440. stype := "type"
  441. level := "level"
  442. return &runtimeapi.SELinuxOption{
  443. User: user,
  444. Role: role,
  445. Type: stype,
  446. Level: level,
  447. }
  448. }
  449. func fullValidHostConfig() *dockercontainer.HostConfig {
  450. return &dockercontainer.HostConfig{
  451. Privileged: true,
  452. CapAdd: []string{"addCapA", "addCapB"},
  453. CapDrop: []string{"dropCapA", "dropCapB"},
  454. SecurityOpt: []string{
  455. fmt.Sprintf("%s:%s", selinuxLabelUser('='), "user"),
  456. fmt.Sprintf("%s:%s", selinuxLabelRole('='), "role"),
  457. fmt.Sprintf("%s:%s", selinuxLabelType('='), "type"),
  458. fmt.Sprintf("%s:%s", selinuxLabelLevel('='), "level"),
  459. },
  460. }
  461. }