spring-cloud / spring-cloud-deployer-kubernetes

The Spring Cloud Deployer implementation for Kubernetes
Apache License 2.0
157 stars 97 forks source link

Add support to configure SecurityContext/allowPrivilegeEscalation for the deployed containers #512

Closed gowrishdc closed 1 year ago

gowrishdc commented 1 year ago

Description Related Issue in spring cloud data flow project: issue created in dataflow project for the same

Note: I believe the code fix is probably in this project so adding /linking it here to get attention.

Require the Stream and Task PODS that are created by the deploy process have the capability to control controller level securityContext/allowPrivilegeEscalation attribute.

The resulting deployment creates the securityContext/allowPrivilegeEscalation for the container sections. However, it does not create the securityContext for the initContainers that are created for the "log" sink application which has a deployment count of 3. As part of that the App is "scaled" deployed with initContainer that does not have the securityContext.

The security policy stops all deployment that does not have securityContext/allowPrivilegeEscalation: false and that is causing this stream deployment to fail with this error message:

Error Message: [psp-allow-privilege-escalation-container] OPA-GATEKEEPER CONSTRAINT: Container index-provider is attempting to run without a required securityContext/allowPrivilegeEscalation, Allowed = false.]

Steps to reproduce:

> stream create --name "words" --definition "http --server.port=9001 | splitter --expression=payload.split(' ') | log"
> stream deploy --name "words" --propertiesFile words-stream.properties
> cat words-stream.properties
app.http.server.port=9001
app.splitter.expression=payload.split(' ')
app.splitter.producer.partitionKeyExpression=payload
deployer.log.count=3

deployer.http.kubernetes.deployment-labels=applicationid:123456
deployer.log.kubernetes.deployment-labels=applicationid:123456
deployer.splitter.kubernetes.deployment-labels=applicationid:123456

deployer.http.kubernetes.containerSecurityContext={allowPrivilegeEscalation: false}
deployer.log.kubernetes.containerSecurityContext={allowPrivilegeEscalation: false}
deployer.splitter.kubernetes.containerSecurityContext={allowPrivilegeEscalation: false}

Please let me know if you need more information.

Additional information:

https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/blob/e6f4b23705722e48f789a0ae8adb7bd790f860db/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppDeployer.java#L314

https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/blob/e6f4b23705722e48f789a0ae8adb7bd790f860db/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppDeployer.java#L491-L506

Should the initContainer need something like this that is done for the container section: https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/blob/e6f4b23705722e48f789a0ae8adb7bd790f860db/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/AbstractKubernetesDeployer.java#L256-L259

corneil commented 1 year ago

We will need to add ContainerSecurityContext to InitContainer and ensure that the creation of the init container uses the properties. We will prioritize this for SCDF 2.10.1 which is planned for early in the new year.

onobc commented 1 year ago

Closed via https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/commit/de4ed599d948850f6c964971ce92fe03bcc66ffb