-
-
Notifications
You must be signed in to change notification settings - Fork 18
feat!: Add (Pod)SecurityContextBuilder::with_stackable_defaults
#1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbernauer
wants to merge
10
commits into
main
Choose a base branch
from
chore/security-context-stackable-defaults
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−17
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8e27a99
feat!: Add `PodSecurityContextBuilder::with_stackable_defaults`
sbernauer 6a8b770
Merge remote-tracking branch 'origin/main' into chore/security-contex…
sbernauer a193ef1
We decided on only runAsNonRoot for now
sbernauer abbc85c
Remove stackable_default_pod_security_context fn
sbernauer c6e34bc
Use builder functions to ensure we can override
sbernauer 77291e7
Do the same thing for SecurityContextBuilder
sbernauer f3ce81f
typo
sbernauer 8f8f449
changelog
sbernauer 3bd1b69
Remove SecurityContextBuilder::run_as_root
sbernauer 19b939e
fix: Remove SecurityContextBuilder::default
sbernauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,16 +10,29 @@ pub struct SecurityContextBuilder { | |
| } | ||
|
|
||
| impl SecurityContextBuilder { | ||
| /// Convenience function for a wide use-case. | ||
| pub fn run_as_root() -> SecurityContext { | ||
| SecurityContext { | ||
| run_as_user: Some(0), | ||
| ..SecurityContext::default() | ||
| } | ||
| /// Construct a new [`SecurityContextBuilder`] that is pre-filled with Stackable's defaults. | ||
| /// | ||
| /// Currently the defaults are: | ||
| /// | ||
| /// * `runAsNonRoot: true` | ||
| pub fn with_stackable_defaults() -> Self { | ||
|
siegfriedweber marked this conversation as resolved.
|
||
| // We are using the builder functions to ensure that builder functions exist to override these settings. | ||
| let mut builder = Self { | ||
| security_context: SecurityContext::default(), | ||
| }; | ||
|
|
||
| // Reason: Running as root is bad | ||
| builder.run_as_non_root(true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should default to |
||
|
|
||
| builder | ||
| } | ||
|
|
||
| pub fn new() -> Self { | ||
| Self::default() | ||
| /// Convenience function for a wide use-case. | ||
| /// | ||
| /// Please only use this is really needed. | ||
| pub fn run_as_root(&mut self) { | ||
|
siegfriedweber marked this conversation as resolved.
Outdated
|
||
| self.run_as_user(0); | ||
| self.run_as_non_root(false); | ||
| } | ||
|
|
||
| pub fn allow_privilege_escalation(&mut self, value: bool) -> &mut Self { | ||
|
|
@@ -144,14 +157,39 @@ impl SecurityContextBuilder { | |
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Default)] | ||
| /// A builder to construct a [`PodSecurityContext`]. | ||
| /// | ||
| /// # Basic usage | ||
| /// | ||
| /// ``` | ||
| /// use stackable_operator::builder::pod::security::PodSecurityContextBuilder; | ||
| /// | ||
| /// let _ = PodSecurityContextBuilder::with_stackable_defaults() | ||
| /// // Configure any arbitrary fields | ||
| /// .run_as_user(1234) | ||
| /// .build(); | ||
| /// ``` | ||
| #[derive(Clone, Debug)] | ||
| pub struct PodSecurityContextBuilder { | ||
| pod_security_context: PodSecurityContext, | ||
| } | ||
|
|
||
| impl PodSecurityContextBuilder { | ||
|
siegfriedweber marked this conversation as resolved.
|
||
| pub fn new() -> Self { | ||
| Self::default() | ||
| /// Construct a new [`PodSecurityContextBuilder`] that is pre-filled with Stackable's defaults. | ||
| /// | ||
| /// Currently the defaults are: | ||
| /// | ||
| /// * `runAsNonRoot: true` | ||
| pub fn with_stackable_defaults() -> Self { | ||
|
siegfriedweber marked this conversation as resolved.
|
||
| // We are using the builder functions to ensure that builder functions exist to override these settings. | ||
| let mut builder = Self { | ||
| pod_security_context: PodSecurityContext::default(), | ||
| }; | ||
|
|
||
| // Reason: Running as root is bad | ||
| builder.run_as_non_root(true); | ||
|
|
||
| builder | ||
| } | ||
|
|
||
| pub fn build(&self) -> PodSecurityContext { | ||
|
|
@@ -173,8 +211,8 @@ impl PodSecurityContextBuilder { | |
| self | ||
| } | ||
|
|
||
| pub fn run_as_non_root(&mut self) -> &mut Self { | ||
| self.pod_security_context.run_as_non_root = Some(true); | ||
| pub fn run_as_non_root(&mut self, non_root: bool) -> &mut Self { | ||
| self.pod_security_context.run_as_non_root = Some(non_root); | ||
| self | ||
| } | ||
|
|
||
|
|
@@ -381,13 +419,13 @@ mod tests { | |
|
|
||
| #[test] | ||
| fn security_context_builder() { | ||
| let mut builder = PodSecurityContextBuilder::new(); | ||
| let mut builder = PodSecurityContextBuilder::with_stackable_defaults(); | ||
| let context = builder | ||
| .fs_group(1000) | ||
| .fs_group_change_policy("policy") | ||
| .run_as_user(1001) | ||
| .run_as_group(1001) | ||
| .run_as_non_root() | ||
| .run_as_non_root(true) | ||
| .supplemental_groups(&[1002, 1003]) | ||
| .se_linux_level("level") | ||
| .se_linux_role("role") | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.