accessors.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. Copyright 2017 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 securitycontext
  14. import (
  15. "reflect"
  16. api "k8s.io/kubernetes/pkg/apis/core"
  17. )
  18. // PodSecurityContextAccessor allows reading the values of a PodSecurityContext object
  19. type PodSecurityContextAccessor interface {
  20. HostNetwork() bool
  21. HostPID() bool
  22. HostIPC() bool
  23. SELinuxOptions() *api.SELinuxOptions
  24. RunAsUser() *int64
  25. RunAsGroup() *int64
  26. RunAsNonRoot() *bool
  27. SupplementalGroups() []int64
  28. FSGroup() *int64
  29. }
  30. // PodSecurityContextMutator allows reading and writing the values of a PodSecurityContext object
  31. type PodSecurityContextMutator interface {
  32. PodSecurityContextAccessor
  33. SetHostNetwork(bool)
  34. SetHostPID(bool)
  35. SetHostIPC(bool)
  36. SetSELinuxOptions(*api.SELinuxOptions)
  37. SetRunAsUser(*int64)
  38. SetRunAsGroup(*int64)
  39. SetRunAsNonRoot(*bool)
  40. SetSupplementalGroups([]int64)
  41. SetFSGroup(*int64)
  42. // PodSecurityContext returns the current PodSecurityContext object
  43. PodSecurityContext() *api.PodSecurityContext
  44. }
  45. // NewPodSecurityContextAccessor returns an accessor for the given pod security context.
  46. // May be initialized with a nil PodSecurityContext.
  47. func NewPodSecurityContextAccessor(podSC *api.PodSecurityContext) PodSecurityContextAccessor {
  48. return &podSecurityContextWrapper{podSC: podSC}
  49. }
  50. // NewPodSecurityContextMutator returns a mutator for the given pod security context.
  51. // May be initialized with a nil PodSecurityContext.
  52. func NewPodSecurityContextMutator(podSC *api.PodSecurityContext) PodSecurityContextMutator {
  53. return &podSecurityContextWrapper{podSC: podSC}
  54. }
  55. type podSecurityContextWrapper struct {
  56. podSC *api.PodSecurityContext
  57. }
  58. func (w *podSecurityContextWrapper) PodSecurityContext() *api.PodSecurityContext {
  59. return w.podSC
  60. }
  61. func (w *podSecurityContextWrapper) ensurePodSC() {
  62. if w.podSC == nil {
  63. w.podSC = &api.PodSecurityContext{}
  64. }
  65. }
  66. func (w *podSecurityContextWrapper) HostNetwork() bool {
  67. if w.podSC == nil {
  68. return false
  69. }
  70. return w.podSC.HostNetwork
  71. }
  72. func (w *podSecurityContextWrapper) SetHostNetwork(v bool) {
  73. if w.podSC == nil && v == false {
  74. return
  75. }
  76. w.ensurePodSC()
  77. w.podSC.HostNetwork = v
  78. }
  79. func (w *podSecurityContextWrapper) HostPID() bool {
  80. if w.podSC == nil {
  81. return false
  82. }
  83. return w.podSC.HostPID
  84. }
  85. func (w *podSecurityContextWrapper) SetHostPID(v bool) {
  86. if w.podSC == nil && v == false {
  87. return
  88. }
  89. w.ensurePodSC()
  90. w.podSC.HostPID = v
  91. }
  92. func (w *podSecurityContextWrapper) HostIPC() bool {
  93. if w.podSC == nil {
  94. return false
  95. }
  96. return w.podSC.HostIPC
  97. }
  98. func (w *podSecurityContextWrapper) SetHostIPC(v bool) {
  99. if w.podSC == nil && v == false {
  100. return
  101. }
  102. w.ensurePodSC()
  103. w.podSC.HostIPC = v
  104. }
  105. func (w *podSecurityContextWrapper) SELinuxOptions() *api.SELinuxOptions {
  106. if w.podSC == nil {
  107. return nil
  108. }
  109. return w.podSC.SELinuxOptions
  110. }
  111. func (w *podSecurityContextWrapper) SetSELinuxOptions(v *api.SELinuxOptions) {
  112. if w.podSC == nil && v == nil {
  113. return
  114. }
  115. w.ensurePodSC()
  116. w.podSC.SELinuxOptions = v
  117. }
  118. func (w *podSecurityContextWrapper) RunAsUser() *int64 {
  119. if w.podSC == nil {
  120. return nil
  121. }
  122. return w.podSC.RunAsUser
  123. }
  124. func (w *podSecurityContextWrapper) SetRunAsUser(v *int64) {
  125. if w.podSC == nil && v == nil {
  126. return
  127. }
  128. w.ensurePodSC()
  129. w.podSC.RunAsUser = v
  130. }
  131. func (w *podSecurityContextWrapper) RunAsGroup() *int64 {
  132. if w.podSC == nil {
  133. return nil
  134. }
  135. return w.podSC.RunAsGroup
  136. }
  137. func (w *podSecurityContextWrapper) SetRunAsGroup(v *int64) {
  138. if w.podSC == nil && v == nil {
  139. return
  140. }
  141. w.ensurePodSC()
  142. w.podSC.RunAsGroup = v
  143. }
  144. func (w *podSecurityContextWrapper) RunAsNonRoot() *bool {
  145. if w.podSC == nil {
  146. return nil
  147. }
  148. return w.podSC.RunAsNonRoot
  149. }
  150. func (w *podSecurityContextWrapper) SetRunAsNonRoot(v *bool) {
  151. if w.podSC == nil && v == nil {
  152. return
  153. }
  154. w.ensurePodSC()
  155. w.podSC.RunAsNonRoot = v
  156. }
  157. func (w *podSecurityContextWrapper) SupplementalGroups() []int64 {
  158. if w.podSC == nil {
  159. return nil
  160. }
  161. return w.podSC.SupplementalGroups
  162. }
  163. func (w *podSecurityContextWrapper) SetSupplementalGroups(v []int64) {
  164. if w.podSC == nil && len(v) == 0 {
  165. return
  166. }
  167. w.ensurePodSC()
  168. if len(v) == 0 && len(w.podSC.SupplementalGroups) == 0 {
  169. return
  170. }
  171. w.podSC.SupplementalGroups = v
  172. }
  173. func (w *podSecurityContextWrapper) FSGroup() *int64 {
  174. if w.podSC == nil {
  175. return nil
  176. }
  177. return w.podSC.FSGroup
  178. }
  179. func (w *podSecurityContextWrapper) SetFSGroup(v *int64) {
  180. if w.podSC == nil && v == nil {
  181. return
  182. }
  183. w.ensurePodSC()
  184. w.podSC.FSGroup = v
  185. }
  186. // ContainerSecurityContextAccessor allows reading the values of a SecurityContext object
  187. type ContainerSecurityContextAccessor interface {
  188. Capabilities() *api.Capabilities
  189. Privileged() *bool
  190. ProcMount() api.ProcMountType
  191. SELinuxOptions() *api.SELinuxOptions
  192. RunAsUser() *int64
  193. RunAsGroup() *int64
  194. RunAsNonRoot() *bool
  195. ReadOnlyRootFilesystem() *bool
  196. AllowPrivilegeEscalation() *bool
  197. }
  198. // ContainerSecurityContextMutator allows reading and writing the values of a SecurityContext object
  199. type ContainerSecurityContextMutator interface {
  200. ContainerSecurityContextAccessor
  201. ContainerSecurityContext() *api.SecurityContext
  202. SetCapabilities(*api.Capabilities)
  203. SetPrivileged(*bool)
  204. SetSELinuxOptions(*api.SELinuxOptions)
  205. SetRunAsUser(*int64)
  206. SetRunAsGroup(*int64)
  207. SetRunAsNonRoot(*bool)
  208. SetReadOnlyRootFilesystem(*bool)
  209. SetAllowPrivilegeEscalation(*bool)
  210. }
  211. // NewContainerSecurityContextAccessor returns an accessor for the provided container security context
  212. // May be initialized with a nil SecurityContext
  213. func NewContainerSecurityContextAccessor(containerSC *api.SecurityContext) ContainerSecurityContextAccessor {
  214. return &containerSecurityContextWrapper{containerSC: containerSC}
  215. }
  216. // NewContainerSecurityContextMutator returns a mutator for the provided container security context
  217. // May be initialized with a nil SecurityContext
  218. func NewContainerSecurityContextMutator(containerSC *api.SecurityContext) ContainerSecurityContextMutator {
  219. return &containerSecurityContextWrapper{containerSC: containerSC}
  220. }
  221. type containerSecurityContextWrapper struct {
  222. containerSC *api.SecurityContext
  223. }
  224. func (w *containerSecurityContextWrapper) ContainerSecurityContext() *api.SecurityContext {
  225. return w.containerSC
  226. }
  227. func (w *containerSecurityContextWrapper) ensureContainerSC() {
  228. if w.containerSC == nil {
  229. w.containerSC = &api.SecurityContext{}
  230. }
  231. }
  232. func (w *containerSecurityContextWrapper) Capabilities() *api.Capabilities {
  233. if w.containerSC == nil {
  234. return nil
  235. }
  236. return w.containerSC.Capabilities
  237. }
  238. func (w *containerSecurityContextWrapper) SetCapabilities(v *api.Capabilities) {
  239. if w.containerSC == nil && v == nil {
  240. return
  241. }
  242. w.ensureContainerSC()
  243. w.containerSC.Capabilities = v
  244. }
  245. func (w *containerSecurityContextWrapper) Privileged() *bool {
  246. if w.containerSC == nil {
  247. return nil
  248. }
  249. return w.containerSC.Privileged
  250. }
  251. func (w *containerSecurityContextWrapper) SetPrivileged(v *bool) {
  252. if w.containerSC == nil && v == nil {
  253. return
  254. }
  255. w.ensureContainerSC()
  256. w.containerSC.Privileged = v
  257. }
  258. func (w *containerSecurityContextWrapper) ProcMount() api.ProcMountType {
  259. if w.containerSC == nil {
  260. return api.DefaultProcMount
  261. }
  262. if w.containerSC.ProcMount == nil {
  263. return api.DefaultProcMount
  264. }
  265. return *w.containerSC.ProcMount
  266. }
  267. func (w *containerSecurityContextWrapper) SELinuxOptions() *api.SELinuxOptions {
  268. if w.containerSC == nil {
  269. return nil
  270. }
  271. return w.containerSC.SELinuxOptions
  272. }
  273. func (w *containerSecurityContextWrapper) SetSELinuxOptions(v *api.SELinuxOptions) {
  274. if w.containerSC == nil && v == nil {
  275. return
  276. }
  277. w.ensureContainerSC()
  278. w.containerSC.SELinuxOptions = v
  279. }
  280. func (w *containerSecurityContextWrapper) RunAsUser() *int64 {
  281. if w.containerSC == nil {
  282. return nil
  283. }
  284. return w.containerSC.RunAsUser
  285. }
  286. func (w *containerSecurityContextWrapper) SetRunAsUser(v *int64) {
  287. if w.containerSC == nil && v == nil {
  288. return
  289. }
  290. w.ensureContainerSC()
  291. w.containerSC.RunAsUser = v
  292. }
  293. func (w *containerSecurityContextWrapper) RunAsGroup() *int64 {
  294. if w.containerSC == nil {
  295. return nil
  296. }
  297. return w.containerSC.RunAsGroup
  298. }
  299. func (w *containerSecurityContextWrapper) SetRunAsGroup(v *int64) {
  300. if w.containerSC == nil && v == nil {
  301. return
  302. }
  303. w.ensureContainerSC()
  304. w.containerSC.RunAsGroup = v
  305. }
  306. func (w *containerSecurityContextWrapper) RunAsNonRoot() *bool {
  307. if w.containerSC == nil {
  308. return nil
  309. }
  310. return w.containerSC.RunAsNonRoot
  311. }
  312. func (w *containerSecurityContextWrapper) SetRunAsNonRoot(v *bool) {
  313. if w.containerSC == nil && v == nil {
  314. return
  315. }
  316. w.ensureContainerSC()
  317. w.containerSC.RunAsNonRoot = v
  318. }
  319. func (w *containerSecurityContextWrapper) ReadOnlyRootFilesystem() *bool {
  320. if w.containerSC == nil {
  321. return nil
  322. }
  323. return w.containerSC.ReadOnlyRootFilesystem
  324. }
  325. func (w *containerSecurityContextWrapper) SetReadOnlyRootFilesystem(v *bool) {
  326. if w.containerSC == nil && v == nil {
  327. return
  328. }
  329. w.ensureContainerSC()
  330. w.containerSC.ReadOnlyRootFilesystem = v
  331. }
  332. func (w *containerSecurityContextWrapper) AllowPrivilegeEscalation() *bool {
  333. if w.containerSC == nil {
  334. return nil
  335. }
  336. return w.containerSC.AllowPrivilegeEscalation
  337. }
  338. func (w *containerSecurityContextWrapper) SetAllowPrivilegeEscalation(v *bool) {
  339. if w.containerSC == nil && v == nil {
  340. return
  341. }
  342. w.ensureContainerSC()
  343. w.containerSC.AllowPrivilegeEscalation = v
  344. }
  345. // NewEffectiveContainerSecurityContextAccessor returns an accessor for reading effective values
  346. // for the provided pod security context and container security context
  347. func NewEffectiveContainerSecurityContextAccessor(podSC PodSecurityContextAccessor, containerSC ContainerSecurityContextMutator) ContainerSecurityContextAccessor {
  348. return &effectiveContainerSecurityContextWrapper{podSC: podSC, containerSC: containerSC}
  349. }
  350. // NewEffectiveContainerSecurityContextMutator returns a mutator for reading and writing effective values
  351. // for the provided pod security context and container security context
  352. func NewEffectiveContainerSecurityContextMutator(podSC PodSecurityContextAccessor, containerSC ContainerSecurityContextMutator) ContainerSecurityContextMutator {
  353. return &effectiveContainerSecurityContextWrapper{podSC: podSC, containerSC: containerSC}
  354. }
  355. type effectiveContainerSecurityContextWrapper struct {
  356. podSC PodSecurityContextAccessor
  357. containerSC ContainerSecurityContextMutator
  358. }
  359. func (w *effectiveContainerSecurityContextWrapper) ContainerSecurityContext() *api.SecurityContext {
  360. return w.containerSC.ContainerSecurityContext()
  361. }
  362. func (w *effectiveContainerSecurityContextWrapper) Capabilities() *api.Capabilities {
  363. return w.containerSC.Capabilities()
  364. }
  365. func (w *effectiveContainerSecurityContextWrapper) SetCapabilities(v *api.Capabilities) {
  366. if !reflect.DeepEqual(w.Capabilities(), v) {
  367. w.containerSC.SetCapabilities(v)
  368. }
  369. }
  370. func (w *effectiveContainerSecurityContextWrapper) Privileged() *bool {
  371. return w.containerSC.Privileged()
  372. }
  373. func (w *effectiveContainerSecurityContextWrapper) SetPrivileged(v *bool) {
  374. if !reflect.DeepEqual(w.Privileged(), v) {
  375. w.containerSC.SetPrivileged(v)
  376. }
  377. }
  378. func (w *effectiveContainerSecurityContextWrapper) ProcMount() api.ProcMountType {
  379. return w.containerSC.ProcMount()
  380. }
  381. func (w *effectiveContainerSecurityContextWrapper) SELinuxOptions() *api.SELinuxOptions {
  382. if v := w.containerSC.SELinuxOptions(); v != nil {
  383. return v
  384. }
  385. return w.podSC.SELinuxOptions()
  386. }
  387. func (w *effectiveContainerSecurityContextWrapper) SetSELinuxOptions(v *api.SELinuxOptions) {
  388. if !reflect.DeepEqual(w.SELinuxOptions(), v) {
  389. w.containerSC.SetSELinuxOptions(v)
  390. }
  391. }
  392. func (w *effectiveContainerSecurityContextWrapper) RunAsUser() *int64 {
  393. if v := w.containerSC.RunAsUser(); v != nil {
  394. return v
  395. }
  396. return w.podSC.RunAsUser()
  397. }
  398. func (w *effectiveContainerSecurityContextWrapper) SetRunAsUser(v *int64) {
  399. if !reflect.DeepEqual(w.RunAsUser(), v) {
  400. w.containerSC.SetRunAsUser(v)
  401. }
  402. }
  403. func (w *effectiveContainerSecurityContextWrapper) RunAsGroup() *int64 {
  404. if v := w.containerSC.RunAsGroup(); v != nil {
  405. return v
  406. }
  407. return w.podSC.RunAsGroup()
  408. }
  409. func (w *effectiveContainerSecurityContextWrapper) SetRunAsGroup(v *int64) {
  410. if !reflect.DeepEqual(w.RunAsGroup(), v) {
  411. w.containerSC.SetRunAsGroup(v)
  412. }
  413. }
  414. func (w *effectiveContainerSecurityContextWrapper) RunAsNonRoot() *bool {
  415. if v := w.containerSC.RunAsNonRoot(); v != nil {
  416. return v
  417. }
  418. return w.podSC.RunAsNonRoot()
  419. }
  420. func (w *effectiveContainerSecurityContextWrapper) SetRunAsNonRoot(v *bool) {
  421. if !reflect.DeepEqual(w.RunAsNonRoot(), v) {
  422. w.containerSC.SetRunAsNonRoot(v)
  423. }
  424. }
  425. func (w *effectiveContainerSecurityContextWrapper) ReadOnlyRootFilesystem() *bool {
  426. return w.containerSC.ReadOnlyRootFilesystem()
  427. }
  428. func (w *effectiveContainerSecurityContextWrapper) SetReadOnlyRootFilesystem(v *bool) {
  429. if !reflect.DeepEqual(w.ReadOnlyRootFilesystem(), v) {
  430. w.containerSC.SetReadOnlyRootFilesystem(v)
  431. }
  432. }
  433. func (w *effectiveContainerSecurityContextWrapper) AllowPrivilegeEscalation() *bool {
  434. return w.containerSC.AllowPrivilegeEscalation()
  435. }
  436. func (w *effectiveContainerSecurityContextWrapper) SetAllowPrivilegeEscalation(v *bool) {
  437. if !reflect.DeepEqual(w.AllowPrivilegeEscalation(), v) {
  438. w.containerSC.SetAllowPrivilegeEscalation(v)
  439. }
  440. }