-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24089 from Sgitario/feat_k8s_security_context
Kubernetes support of SecurityContext configuration
- Loading branch information
Showing
12 changed files
with
392 additions
and
5 deletions.
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.