@@ -4,24 +4,25 @@ use k8s_openapi::api::core::v1::{
44} ;
55
66/// A builder for [`SecurityContext`] objects (not to be confused with `PodSecurityContext`).
7- #[ derive( Clone , Default ) ]
7+ #[ derive( Clone ) ]
88pub struct SecurityContextBuilder {
99 security_context : SecurityContext ,
1010}
1111
1212impl SecurityContextBuilder {
13- /// Convenience function for a wide use-case.
14- pub fn run_as_root ( ) -> SecurityContext {
15- SecurityContext {
16- run_as_user : Some ( 0 ) ,
17- ..SecurityContext :: default ( )
13+ /// Construct a new [`SecurityContextBuilder`] that is pre-filled with Stackable's defaults.
14+ ///
15+ /// We currently don't have any defaults we set.
16+ ///
17+ /// We intentionally don't set `runAsNonRoot`, as we set that in
18+ /// [`PodSecurityContextBuilder::with_stackable_defaults`] already and don't want to confuse
19+ /// users by setting it on the Pod and container.
20+ pub fn with_stackable_defaults ( ) -> Self {
21+ Self {
22+ security_context : SecurityContext :: default ( ) ,
1823 }
1924 }
2025
21- pub fn new ( ) -> Self {
22- Self :: default ( )
23- }
24-
2526 pub fn allow_privilege_escalation ( & mut self , value : bool ) -> & mut Self {
2627 self . security_context . allow_privilege_escalation = Some ( value) ;
2728 self
@@ -144,14 +145,39 @@ impl SecurityContextBuilder {
144145 }
145146}
146147
147- #[ derive( Clone , Default ) ]
148+ /// A builder to construct a [`PodSecurityContext`].
149+ ///
150+ /// # Basic usage
151+ ///
152+ /// ```
153+ /// use stackable_operator::builder::pod::security::PodSecurityContextBuilder;
154+ ///
155+ /// let _ = PodSecurityContextBuilder::with_stackable_defaults()
156+ /// // Configure any arbitrary fields
157+ /// .run_as_user(1234)
158+ /// .build();
159+ /// ```
160+ #[ derive( Clone , Debug ) ]
148161pub struct PodSecurityContextBuilder {
149162 pod_security_context : PodSecurityContext ,
150163}
151164
152165impl PodSecurityContextBuilder {
153- pub fn new ( ) -> Self {
154- Self :: default ( )
166+ /// Construct a new [`PodSecurityContextBuilder`] that is pre-filled with Stackable's defaults.
167+ ///
168+ /// Currently the defaults are:
169+ ///
170+ /// * `runAsNonRoot: true`
171+ pub fn with_stackable_defaults ( ) -> Self {
172+ // We are using the builder functions to ensure that builder functions exist to override these settings.
173+ let mut builder = Self {
174+ pod_security_context : PodSecurityContext :: default ( ) ,
175+ } ;
176+
177+ // Reason: Running as root is bad
178+ builder. run_as_non_root ( true ) ;
179+
180+ builder
155181 }
156182
157183 pub fn build ( & self ) -> PodSecurityContext {
@@ -173,8 +199,8 @@ impl PodSecurityContextBuilder {
173199 self
174200 }
175201
176- pub fn run_as_non_root ( & mut self ) -> & mut Self {
177- self . pod_security_context . run_as_non_root = Some ( true ) ;
202+ pub fn run_as_non_root ( & mut self , non_root : bool ) -> & mut Self {
203+ self . pod_security_context . run_as_non_root = Some ( non_root ) ;
178204 self
179205 }
180206
@@ -381,13 +407,13 @@ mod tests {
381407
382408 #[ test]
383409 fn security_context_builder ( ) {
384- let mut builder = PodSecurityContextBuilder :: new ( ) ;
410+ let mut builder = PodSecurityContextBuilder :: with_stackable_defaults ( ) ;
385411 let context = builder
386412 . fs_group ( 1000 )
387413 . fs_group_change_policy ( "policy" )
388414 . run_as_user ( 1001 )
389415 . run_as_group ( 1001 )
390- . run_as_non_root ( )
416+ . run_as_non_root ( true )
391417 . supplemental_groups ( & [ 1002 , 1003 ] )
392418 . se_linux_level ( "level" )
393419 . se_linux_role ( "role" )
0 commit comments