-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Kubernetes support of SecurityContext configuration #24089
Merged
+392
−5
Merged
Changes from all commits
Commits
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
feat: Kubernetes support of SecurityContext configuration
Allow to configure the security context section in the pods for Kubernetes, OpenShift and Knative. Fix #23866
- Loading branch information
commit d3a366b709e8f9dc53ce8b87c6642c6e0e018954
There are no files selected for viewing
This file contains 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
78 changes: 78 additions & 0 deletions
78
...oyment/src/main/java/io/quarkus/kubernetes/deployment/ApplySecuritySettingsDecorator.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import java.util.Optional; | ||
|
||
import io.dekorate.kubernetes.decorator.Decorator; | ||
import io.dekorate.kubernetes.decorator.NamedResourceDecorator; | ||
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.api.model.PodSecurityContextBuilder; | ||
import io.fabric8.kubernetes.api.model.PodSpecFluent; | ||
import io.fabric8.kubernetes.api.model.SELinuxOptions; | ||
import io.fabric8.kubernetes.api.model.SELinuxOptionsBuilder; | ||
import io.fabric8.kubernetes.api.model.SysctlBuilder; | ||
import io.fabric8.kubernetes.api.model.WindowsSecurityContextOptions; | ||
import io.fabric8.kubernetes.api.model.WindowsSecurityContextOptionsBuilder; | ||
|
||
public class ApplySecuritySettingsDecorator extends NamedResourceDecorator<PodSpecFluent> { | ||
|
||
private final SecurityContextConfig securityContext; | ||
|
||
public ApplySecuritySettingsDecorator(String resourceName, SecurityContextConfig securityContext) { | ||
super(resourceName); | ||
this.securityContext = securityContext; | ||
} | ||
|
||
@Override | ||
public void andThenVisit(PodSpecFluent podSpec, ObjectMeta resourceMeta) { | ||
PodSecurityContextBuilder securityContextBuilder = new PodSecurityContextBuilder(); | ||
|
||
securityContext.runAsUser.ifPresent(securityContextBuilder::withRunAsUser); | ||
securityContext.runAsGroup.ifPresent(securityContextBuilder::withRunAsGroup); | ||
securityContext.runAsNonRoot.ifPresent(securityContextBuilder::withRunAsNonRoot); | ||
securityContext.supplementalGroups.ifPresent(securityContextBuilder::addAllToSupplementalGroups); | ||
securityContext.fsGroup.ifPresent(securityContextBuilder::withFsGroup); | ||
securityContext.sysctls.ifPresent(map -> map.entrySet().stream() | ||
.map(entry -> new SysctlBuilder().withName(entry.getKey()).withValue(entry.getValue()).build()) | ||
.forEach(securityContextBuilder::addToSysctls)); | ||
securityContext.fsGroupChangePolicy.map(e -> e.name()).ifPresent(securityContextBuilder::withFsGroupChangePolicy); | ||
buildSeLinuxOptions().ifPresent(securityContextBuilder::withSeLinuxOptions); | ||
buildWindowsOptions().ifPresent(securityContextBuilder::withWindowsOptions); | ||
|
||
podSpec.withSecurityContext(securityContextBuilder.build()); | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return new Class[] { ResourceProvidingDecorator.class }; | ||
} | ||
|
||
private Optional<WindowsSecurityContextOptions> buildWindowsOptions() { | ||
WindowsSecurityContextOptions item = null; | ||
if (securityContext.windowsOptions.isAnyPropertySet()) { | ||
WindowsSecurityContextOptionsBuilder builder = new WindowsSecurityContextOptionsBuilder(); | ||
securityContext.windowsOptions.gmsaCredentialSpec.ifPresent(builder::withGmsaCredentialSpec); | ||
securityContext.windowsOptions.gmsaCredentialSpecName.ifPresent(builder::withGmsaCredentialSpecName); | ||
securityContext.windowsOptions.hostProcess.ifPresent(builder::withHostProcess); | ||
securityContext.windowsOptions.runAsUserName.ifPresent(builder::withRunAsUserName); | ||
item = builder.build(); | ||
} | ||
|
||
return Optional.ofNullable(item); | ||
} | ||
|
||
private Optional<SELinuxOptions> buildSeLinuxOptions() { | ||
SELinuxOptions item = null; | ||
if (securityContext.seLinuxOptions.isAnyPropertySet()) { | ||
SELinuxOptionsBuilder builder = new SELinuxOptionsBuilder(); | ||
securityContext.seLinuxOptions.user.ifPresent(builder::withUser); | ||
securityContext.seLinuxOptions.role.ifPresent(builder::withRole); | ||
securityContext.seLinuxOptions.level.ifPresent(builder::withLevel); | ||
securityContext.seLinuxOptions.type.ifPresent(builder::withType); | ||
item = builder.build(); | ||
} | ||
|
||
return Optional.ofNullable(item); | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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
151 changes: 151 additions & 0 deletions
151
...illa/deployment/src/main/java/io/quarkus/kubernetes/deployment/SecurityContextConfig.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class SecurityContextConfig { | ||
|
||
/** | ||
* SELinuxOptions to be applied to the container. | ||
*/ | ||
SeLinuxOptions seLinuxOptions; | ||
|
||
/** | ||
* The Windows specific settings applied to all containers. | ||
*/ | ||
WindowsOptions windowsOptions; | ||
|
||
/** | ||
* The UID to run the entrypoint of the container process. | ||
*/ | ||
@ConfigItem | ||
Optional<Long> runAsUser; | ||
|
||
/** | ||
* The GID to run the entrypoint of the container process. | ||
*/ | ||
@ConfigItem | ||
Optional<Long> runAsGroup; | ||
|
||
/** | ||
* Indicates that the container must run as a non-root user. | ||
*/ | ||
@ConfigItem | ||
Optional<Boolean> runAsNonRoot; | ||
|
||
/** | ||
* A list of groups applied to the first process run in each container, in addition to the container's primary GID. | ||
* If unspecified, no groups will be added to any container. | ||
*/ | ||
@ConfigItem | ||
Optional<List<Long>> supplementalGroups; | ||
|
||
/** | ||
* A special supplemental group that applies to all containers in a pod. | ||
*/ | ||
@ConfigItem | ||
Optional<Long> fsGroup; | ||
|
||
/** | ||
* Sysctls hold a list of namespaced sysctls used for the pod. | ||
*/ | ||
@ConfigItem | ||
Optional<Map<String, String>> sysctls; | ||
|
||
/** | ||
* It holds policies that will be used for applying fsGroup to a volume when volume is mounted. | ||
* Values: OnRootMismatch, Always | ||
*/ | ||
@ConfigItem | ||
Optional<PodFSGroupChangePolicy> fsGroupChangePolicy; | ||
|
||
protected boolean isAnyPropertySet() { | ||
return seLinuxOptions.isAnyPropertySet() || windowsOptions.isAnyPropertySet() || runAsUser.isPresent() | ||
|| runAsGroup.isPresent() || runAsNonRoot.isPresent() || supplementalGroups.isPresent() | ||
|| fsGroup.isPresent() || sysctls.isPresent() || fsGroupChangePolicy.isPresent(); | ||
} | ||
|
||
@ConfigGroup | ||
public static class SeLinuxOptions { | ||
|
||
/** | ||
* The SELinux level label that applies to the container. | ||
*/ | ||
@ConfigItem | ||
Optional<String> level; | ||
|
||
/** | ||
* The SELinux role label that applies to the container. | ||
*/ | ||
@ConfigItem | ||
Optional<String> role; | ||
|
||
/** | ||
* The SELinux type label that applies to the container. | ||
*/ | ||
@ConfigItem | ||
Optional<String> type; | ||
|
||
/** | ||
* The SELinux user label that applies to the container. | ||
*/ | ||
@ConfigItem | ||
Optional<String> user; | ||
|
||
protected boolean isAnyPropertySet() { | ||
return level.isPresent() || role.isPresent() || type.isPresent() || user.isPresent(); | ||
} | ||
} | ||
|
||
@ConfigGroup | ||
public static class WindowsOptions { | ||
|
||
/** | ||
* The name of the GMSA credential spec to use. | ||
*/ | ||
@ConfigItem | ||
Optional<String> gmsaCredentialSpecName; | ||
|
||
/** | ||
* GMSACredentialSpec is where the GMSA admission webhook (https://github.com/kubernetes-sigs/windows-gmsa) inlines the | ||
* contents of the GMSA credential spec named by the GMSACredentialSpecName field. | ||
*/ | ||
@ConfigItem | ||
Optional<String> gmsaCredentialSpec; | ||
|
||
/** | ||
* The UserName in Windows to run the entrypoint of the container process. | ||
*/ | ||
@ConfigItem | ||
Optional<String> runAsUserName; | ||
|
||
/** | ||
* HostProcess determines if a container should be run as a 'Host Process' container. | ||
*/ | ||
@ConfigItem | ||
Optional<Boolean> hostProcess; | ||
|
||
protected boolean isAnyPropertySet() { | ||
return gmsaCredentialSpecName.isPresent() || gmsaCredentialSpec.isPresent() || runAsUserName.isPresent() | ||
|| hostProcess.isPresent(); | ||
} | ||
} | ||
|
||
public enum PodFSGroupChangePolicy { | ||
/** | ||
* It indicates that volume's ownership and permissions will be changed only when permission and ownership of root | ||
* directory does not match with expected permissions on the volume. | ||
*/ | ||
OnRootMismatch, | ||
/** | ||
* It indicates that volume's ownership and permissions should always be changed whenever volume is mounted inside a | ||
* Pod. This the default behavior. | ||
*/ | ||
Always; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a hack ... But I tried to make the security context optional (which it is, so it makes sense), but the configuration does not deal well with having a complex and nested configuration with optional. Concretely, if I use Optional instead of the hack
isAnyPropertySet
, it fails with:This is related to #7862, which was recently closed with a workaround, but not to really give support of optional configuration groups.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view this is clear as it communicates the intention and there is no room for interpretations.
Going the Optional way it would be unclear how default values are meant to be handled.
So, +1 for this.