securityContext_test.yaml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. suite: container security context test
  2. templates:
  3. - common.yaml
  4. tests:
  5. - it: should create the securityContext correctly
  6. set:
  7. image: &image
  8. repository: nginx
  9. tag: 1.19.0
  10. pullPolicy: IfNotPresent
  11. workload:
  12. workload-name1:
  13. enabled: true
  14. primary: true
  15. type: Deployment
  16. podSpec:
  17. containers:
  18. container-name1:
  19. enabled: true
  20. primary: true
  21. imageSelector: image
  22. probes: &probes
  23. liveness:
  24. enabled: false
  25. readiness:
  26. enabled: false
  27. startup:
  28. enabled: false
  29. asserts:
  30. - documentIndex: &deploymentDoc 0
  31. isKind:
  32. of: Deployment
  33. - documentIndex: *deploymentDoc
  34. isAPIVersion:
  35. of: apps/v1
  36. - documentIndex: *deploymentDoc
  37. isSubset:
  38. path: spec.template.spec.containers[0]
  39. content:
  40. securityContext:
  41. runAsUser: 568
  42. runAsGroup: 568
  43. readOnlyRootFilesystem: true
  44. allowPrivilegeEscalation: false
  45. privileged: false
  46. runAsNonRoot: true
  47. seccompProfile:
  48. type: RuntimeDefault
  49. capabilities:
  50. add: []
  51. drop:
  52. - ALL
  53. - it: should override the securityContext runAsUser and runAsNonRoot
  54. set:
  55. image: *image
  56. workload:
  57. workload-name1:
  58. enabled: true
  59. primary: true
  60. type: Deployment
  61. podSpec:
  62. containers:
  63. container-name1:
  64. enabled: true
  65. primary: true
  66. imageSelector: image
  67. probes: *probes
  68. securityContext:
  69. runAsUser: 0
  70. runAsNonRoot: false
  71. asserts:
  72. - documentIndex: &deploymentDoc 0
  73. isKind:
  74. of: Deployment
  75. - documentIndex: *deploymentDoc
  76. isAPIVersion:
  77. of: apps/v1
  78. - documentIndex: *deploymentDoc
  79. isSubset:
  80. path: spec.template.spec.containers[0]
  81. content:
  82. securityContext:
  83. runAsUser: 0
  84. runAsGroup: 568
  85. readOnlyRootFilesystem: true
  86. allowPrivilegeEscalation: false
  87. privileged: false
  88. runAsNonRoot: false
  89. seccompProfile:
  90. type: RuntimeDefault
  91. capabilities:
  92. add: []
  93. drop:
  94. - ALL
  95. - it: should override the securityContext runAsGroup and runAsNonRoot
  96. set:
  97. image: *image
  98. workload:
  99. workload-name1:
  100. enabled: true
  101. primary: true
  102. type: Deployment
  103. podSpec:
  104. containers:
  105. container-name1:
  106. enabled: true
  107. primary: true
  108. imageSelector: image
  109. probes: *probes
  110. securityContext:
  111. runAsGroup: 0
  112. runAsNonRoot: false
  113. asserts:
  114. - documentIndex: &deploymentDoc 0
  115. isKind:
  116. of: Deployment
  117. - documentIndex: *deploymentDoc
  118. isAPIVersion:
  119. of: apps/v1
  120. - documentIndex: *deploymentDoc
  121. isSubset:
  122. path: spec.template.spec.containers[0]
  123. content:
  124. securityContext:
  125. runAsUser: 568
  126. runAsGroup: 0
  127. readOnlyRootFilesystem: true
  128. allowPrivilegeEscalation: false
  129. privileged: false
  130. runAsNonRoot: false
  131. seccompProfile:
  132. type: RuntimeDefault
  133. capabilities:
  134. add: []
  135. drop:
  136. - ALL
  137. - it: should override the securityContext readOnlyRootFilesystem
  138. set:
  139. image: *image
  140. workload:
  141. workload-name1:
  142. enabled: true
  143. primary: true
  144. type: Deployment
  145. podSpec:
  146. containers:
  147. container-name1:
  148. enabled: true
  149. primary: true
  150. imageSelector: image
  151. probes: *probes
  152. securityContext:
  153. readOnlyRootFilesystem: false
  154. asserts:
  155. - documentIndex: &deploymentDoc 0
  156. isKind:
  157. of: Deployment
  158. - documentIndex: *deploymentDoc
  159. isAPIVersion:
  160. of: apps/v1
  161. - documentIndex: *deploymentDoc
  162. isSubset:
  163. path: spec.template.spec.containers[0]
  164. content:
  165. securityContext:
  166. runAsUser: 568
  167. runAsGroup: 568
  168. readOnlyRootFilesystem: false
  169. allowPrivilegeEscalation: false
  170. privileged: false
  171. runAsNonRoot: true
  172. seccompProfile:
  173. type: RuntimeDefault
  174. capabilities:
  175. add: []
  176. drop:
  177. - ALL
  178. - it: should override the securityContext privileged
  179. set:
  180. image: *image
  181. workload:
  182. workload-name1:
  183. enabled: true
  184. primary: true
  185. type: Deployment
  186. podSpec:
  187. containers:
  188. container-name1:
  189. enabled: true
  190. primary: true
  191. imageSelector: image
  192. probes: *probes
  193. securityContext:
  194. privileged: true
  195. asserts:
  196. - documentIndex: &deploymentDoc 0
  197. isKind:
  198. of: Deployment
  199. - documentIndex: *deploymentDoc
  200. isAPIVersion:
  201. of: apps/v1
  202. - documentIndex: *deploymentDoc
  203. isSubset:
  204. path: spec.template.spec.containers[0]
  205. content:
  206. securityContext:
  207. runAsUser: 568
  208. runAsGroup: 568
  209. readOnlyRootFilesystem: true
  210. allowPrivilegeEscalation: false
  211. privileged: true
  212. runAsNonRoot: true
  213. seccompProfile:
  214. type: RuntimeDefault
  215. capabilities:
  216. add: []
  217. drop:
  218. - ALL
  219. - it: should override the securityContext allowPrivilegeEscalation
  220. set:
  221. image: *image
  222. workload:
  223. workload-name1:
  224. enabled: true
  225. primary: true
  226. type: Deployment
  227. podSpec:
  228. containers:
  229. container-name1:
  230. enabled: true
  231. primary: true
  232. imageSelector: image
  233. probes: *probes
  234. securityContext:
  235. allowPrivilegeEscalation: true
  236. asserts:
  237. - documentIndex: &deploymentDoc 0
  238. isKind:
  239. of: Deployment
  240. - documentIndex: *deploymentDoc
  241. isAPIVersion:
  242. of: apps/v1
  243. - documentIndex: *deploymentDoc
  244. isSubset:
  245. path: spec.template.spec.containers[0]
  246. content:
  247. securityContext:
  248. runAsUser: 568
  249. runAsGroup: 568
  250. readOnlyRootFilesystem: true
  251. allowPrivilegeEscalation: true
  252. privileged: false
  253. runAsNonRoot: true
  254. seccompProfile:
  255. type: RuntimeDefault
  256. capabilities:
  257. add: []
  258. drop:
  259. - ALL
  260. - it: should override the securityContext capabilities.add
  261. set:
  262. image: *image
  263. workload:
  264. workload-name1:
  265. enabled: true
  266. primary: true
  267. type: Deployment
  268. podSpec:
  269. containers:
  270. container-name1:
  271. enabled: true
  272. primary: true
  273. imageSelector: image
  274. probes: *probes
  275. securityContext:
  276. capabilities:
  277. add:
  278. - NET_ADMIN
  279. asserts:
  280. - documentIndex: &deploymentDoc 0
  281. isKind:
  282. of: Deployment
  283. - documentIndex: *deploymentDoc
  284. isAPIVersion:
  285. of: apps/v1
  286. - documentIndex: *deploymentDoc
  287. isSubset:
  288. path: spec.template.spec.containers[0]
  289. content:
  290. securityContext:
  291. runAsUser: 568
  292. runAsGroup: 568
  293. readOnlyRootFilesystem: true
  294. allowPrivilegeEscalation: false
  295. privileged: false
  296. runAsNonRoot: true
  297. seccompProfile:
  298. type: RuntimeDefault
  299. capabilities:
  300. add:
  301. - NET_ADMIN
  302. drop:
  303. - ALL
  304. - it: should override the securityContext capabilities.drop
  305. set:
  306. image: *image
  307. workload:
  308. workload-name1:
  309. enabled: true
  310. primary: true
  311. type: Deployment
  312. podSpec:
  313. containers:
  314. container-name1:
  315. enabled: true
  316. primary: true
  317. imageSelector: image
  318. probes: *probes
  319. securityContext:
  320. capabilities:
  321. drop:
  322. - NET_ADMIN
  323. asserts:
  324. - documentIndex: &deploymentDoc 0
  325. isKind:
  326. of: Deployment
  327. - documentIndex: *deploymentDoc
  328. isAPIVersion:
  329. of: apps/v1
  330. - documentIndex: *deploymentDoc
  331. isSubset:
  332. path: spec.template.spec.containers[0]
  333. content:
  334. securityContext:
  335. runAsUser: 568
  336. runAsGroup: 568
  337. readOnlyRootFilesystem: true
  338. allowPrivilegeEscalation: false
  339. privileged: false
  340. runAsNonRoot: true
  341. seccompProfile:
  342. type: RuntimeDefault
  343. capabilities:
  344. add: []
  345. drop:
  346. - NET_ADMIN
  347. - it: should override the securityContext seccompProfile.type
  348. set:
  349. image: *image
  350. workload:
  351. workload-name1:
  352. enabled: true
  353. primary: true
  354. type: Deployment
  355. podSpec:
  356. containers:
  357. container-name1:
  358. enabled: true
  359. primary: true
  360. imageSelector: image
  361. probes: *probes
  362. securityContext:
  363. seccompProfile:
  364. type: Unconfined
  365. asserts:
  366. - documentIndex: &deploymentDoc 0
  367. isKind:
  368. of: Deployment
  369. - documentIndex: *deploymentDoc
  370. isAPIVersion:
  371. of: apps/v1
  372. - documentIndex: *deploymentDoc
  373. isSubset:
  374. path: spec.template.spec.containers[0]
  375. content:
  376. securityContext:
  377. runAsUser: 568
  378. runAsGroup: 568
  379. readOnlyRootFilesystem: true
  380. allowPrivilegeEscalation: false
  381. privileged: false
  382. runAsNonRoot: true
  383. seccompProfile:
  384. type: Unconfined
  385. capabilities:
  386. add: []
  387. drop:
  388. - ALL
  389. - it: should override the securityContext all
  390. set:
  391. image: *image
  392. workload:
  393. workload-name1:
  394. enabled: true
  395. primary: true
  396. type: Deployment
  397. podSpec:
  398. containers:
  399. container-name1:
  400. enabled: true
  401. primary: true
  402. imageSelector: image
  403. probes: *probes
  404. securityContext:
  405. runAsUser: 1000
  406. runAsGroup: 1000
  407. readOnlyRootFilesystem: false
  408. allowPrivilegeEscalation: true
  409. privileged: true
  410. runAsNonRoot: false
  411. capabilities:
  412. add:
  413. - NET_ADMIN
  414. drop:
  415. - NET_BIND_SERVICE
  416. seccompProfile:
  417. type: Localhost
  418. profile: path/to/profile.json
  419. asserts:
  420. - documentIndex: &deploymentDoc 0
  421. isKind:
  422. of: Deployment
  423. - documentIndex: *deploymentDoc
  424. isAPIVersion:
  425. of: apps/v1
  426. - documentIndex: *deploymentDoc
  427. isSubset:
  428. path: spec.template.spec.containers[0]
  429. content:
  430. securityContext:
  431. runAsUser: 1000
  432. runAsGroup: 1000
  433. readOnlyRootFilesystem: false
  434. allowPrivilegeEscalation: true
  435. privileged: true
  436. runAsNonRoot: false
  437. seccompProfile:
  438. type: Localhost
  439. localhostProfile: path/to/profile.json
  440. capabilities:
  441. add:
  442. - NET_ADMIN
  443. drop:
  444. - NET_BIND_SERVICE
  445. # Failures
  446. - it: should fail with empty securityContext
  447. set:
  448. image: *image
  449. securityContext:
  450. container:
  451. workload:
  452. workload-name1:
  453. enabled: true
  454. primary: true
  455. type: Deployment
  456. podSpec:
  457. containers:
  458. container-name1:
  459. enabled: true
  460. primary: true
  461. imageSelector: image
  462. probes: *probes
  463. asserts:
  464. - failedTemplate:
  465. errorMessage: Container - Expected non-empty <.Values.securityContext.container>
  466. - it: should fail with runAsNonRoot not a bool
  467. set:
  468. image: *image
  469. workload:
  470. workload-name1:
  471. enabled: true
  472. primary: true
  473. type: Deployment
  474. podSpec:
  475. containers:
  476. container-name1:
  477. enabled: true
  478. primary: true
  479. imageSelector: image
  480. probes: *probes
  481. securityContext:
  482. runAsNonRoot: "true"
  483. asserts:
  484. - failedTemplate:
  485. errorMessage: Container - Expected <securityContext.runAsNonRoot> to be [bool], but got [true] of type [string]
  486. - it: should fail with readOnlyRootFilesystem not a bool
  487. set:
  488. image: *image
  489. workload:
  490. workload-name1:
  491. enabled: true
  492. primary: true
  493. type: Deployment
  494. podSpec:
  495. containers:
  496. container-name1:
  497. enabled: true
  498. primary: true
  499. imageSelector: image
  500. probes: *probes
  501. securityContext:
  502. readOnlyRootFilesystem: "true"
  503. asserts:
  504. - failedTemplate:
  505. errorMessage: Container - Expected <securityContext.readOnlyRootFilesystem> to be [bool], but got [true] of type [string]
  506. - it: should fail with allowPrivilegeEscalation not a bool
  507. set:
  508. image: *image
  509. workload:
  510. workload-name1:
  511. enabled: true
  512. primary: true
  513. type: Deployment
  514. podSpec:
  515. containers:
  516. container-name1:
  517. enabled: true
  518. primary: true
  519. imageSelector: image
  520. probes: *probes
  521. securityContext:
  522. allowPrivilegeEscalation: "true"
  523. asserts:
  524. - failedTemplate:
  525. errorMessage: Container - Expected <securityContext.allowPrivilegeEscalation> to be [bool], but got [true] of type [string]
  526. - it: should fail with privileged not a bool
  527. set:
  528. image: *image
  529. workload:
  530. workload-name1:
  531. enabled: true
  532. primary: true
  533. type: Deployment
  534. podSpec:
  535. containers:
  536. container-name1:
  537. enabled: true
  538. primary: true
  539. imageSelector: image
  540. probes: *probes
  541. securityContext:
  542. privileged: "true"
  543. asserts:
  544. - failedTemplate:
  545. errorMessage: Container - Expected <securityContext.privileged> to be [bool], but got [true] of type [string]
  546. - it: should fail with runAsUser not an int
  547. set:
  548. image: *image
  549. workload:
  550. workload-name1:
  551. enabled: true
  552. primary: true
  553. type: Deployment
  554. podSpec:
  555. containers:
  556. container-name1:
  557. enabled: true
  558. primary: true
  559. imageSelector: image
  560. probes: *probes
  561. securityContext:
  562. runAsUser: "568"
  563. asserts:
  564. - failedTemplate:
  565. errorMessage: Container - Expected <securityContext.runAsUser> to be [int], but got [568] of type [string]
  566. - it: should fail with runAsGroup not an int
  567. set:
  568. image: *image
  569. workload:
  570. workload-name1:
  571. enabled: true
  572. primary: true
  573. type: Deployment
  574. podSpec:
  575. containers:
  576. container-name1:
  577. enabled: true
  578. primary: true
  579. imageSelector: image
  580. probes: *probes
  581. securityContext:
  582. runAsGroup: "568"
  583. asserts:
  584. - failedTemplate:
  585. errorMessage: Container - Expected <securityContext.runAsGroup> to be [int], but got [568] of type [string]
  586. - it: should fail without seccompProfile
  587. set:
  588. image: *image
  589. securityContext:
  590. container:
  591. seccompProfile:
  592. workload:
  593. workload-name1:
  594. enabled: true
  595. primary: true
  596. type: Deployment
  597. podSpec:
  598. containers:
  599. container-name1:
  600. enabled: true
  601. primary: true
  602. imageSelector: image
  603. probes: *probes
  604. asserts:
  605. - failedTemplate:
  606. errorMessage: Container - Expected <securityContext.seccompProfile> to be defined
  607. - it: should fail with invalid seccompProfile
  608. set:
  609. image: *image
  610. workload:
  611. workload-name1:
  612. enabled: true
  613. primary: true
  614. type: Deployment
  615. podSpec:
  616. containers:
  617. container-name1:
  618. enabled: true
  619. primary: true
  620. imageSelector: image
  621. probes: *probes
  622. securityContext:
  623. seccompProfile:
  624. type: invalid
  625. asserts:
  626. - failedTemplate:
  627. errorMessage: Container - Expected <securityContext.seccompProfile> to be one of [RuntimeDefault, Localhost, Unconfined], but got [invalid]
  628. - it: should fail without profile on seccompProfile Localhost
  629. set:
  630. image: *image
  631. workload:
  632. workload-name1:
  633. enabled: true
  634. primary: true
  635. type: Deployment
  636. podSpec:
  637. containers:
  638. container-name1:
  639. enabled: true
  640. primary: true
  641. imageSelector: image
  642. probes: *probes
  643. securityContext:
  644. seccompProfile:
  645. type: Localhost
  646. profile: ""
  647. asserts:
  648. - failedTemplate:
  649. errorMessage: Container - Expected <securityContext.seccompProfile.profile> to be defined on type [Localhost]
  650. - it: should fail without capabilities
  651. set:
  652. image: *image
  653. securityContext:
  654. container:
  655. capabilities:
  656. workload:
  657. workload-name1:
  658. enabled: true
  659. primary: true
  660. type: Deployment
  661. podSpec:
  662. containers:
  663. container-name1:
  664. enabled: true
  665. primary: true
  666. imageSelector: image
  667. probes: *probes
  668. asserts:
  669. - failedTemplate:
  670. errorMessage: Container - Expected <securityContext.capabilities> to be defined
  671. - it: should fail capabilities.add not a list
  672. set:
  673. image: *image
  674. workload:
  675. workload-name1:
  676. enabled: true
  677. primary: true
  678. type: Deployment
  679. podSpec:
  680. containers:
  681. container-name1:
  682. enabled: true
  683. primary: true
  684. imageSelector: image
  685. probes: *probes
  686. securityContext:
  687. capabilities:
  688. add: invalid
  689. asserts:
  690. - failedTemplate:
  691. errorMessage: Container - Expected <securityContext.capabilities.add> to be [list], but got [string]
  692. - it: should fail capabilities.drop not a list
  693. set:
  694. image: *image
  695. workload:
  696. workload-name1:
  697. enabled: true
  698. primary: true
  699. type: Deployment
  700. podSpec:
  701. containers:
  702. container-name1:
  703. enabled: true
  704. primary: true
  705. imageSelector: image
  706. probes: *probes
  707. securityContext:
  708. capabilities:
  709. drop: invalid
  710. asserts:
  711. - failedTemplate:
  712. errorMessage: Container - Expected <securityContext.capabilities.drop> to be [list], but got [string]
  713. - it: should fail when trying to run as non root but with root user
  714. set:
  715. image: *image
  716. workload:
  717. workload-name1:
  718. enabled: true
  719. primary: true
  720. type: Deployment
  721. podSpec:
  722. containers:
  723. container-name1:
  724. enabled: true
  725. primary: true
  726. imageSelector: image
  727. probes: *probes
  728. securityContext:
  729. runAsNonRoot: true
  730. runAsUser: 0
  731. asserts:
  732. - failedTemplate:
  733. errorMessage: Container - Expected <securityContext.runAsNonRoot> to be [false] with either [runAsUser, runAsGroup] set to [0]
  734. - it: should fail when trying to run as non root but with root group
  735. set:
  736. image: *image
  737. workload:
  738. workload-name1:
  739. enabled: true
  740. primary: true
  741. type: Deployment
  742. podSpec:
  743. containers:
  744. container-name1:
  745. enabled: true
  746. primary: true
  747. imageSelector: image
  748. probes: *probes
  749. securityContext:
  750. runAsNonRoot: true
  751. runAsGroup: 0
  752. asserts:
  753. - failedTemplate:
  754. errorMessage: Container - Expected <securityContext.runAsNonRoot> to be [false] with either [runAsUser, runAsGroup] set to [0]