Kubernetes: precreate workingDir as nonroot when required#6322
Conversation
|
Hi @6543, what do you think about this alternative approach to #6312 for allowing the clone step to run unprivileged? This approach is more self contained to kubernetes only, so other backends remain unaffected. This PR doesn't cover the As an example, with this PR changes (in addition to #6322 and #6307) it's possible to run the clone step completely unprivileged by setting the following agent env vars: WOODPECKER_BACKEND_K8S_DEFAULT_SECCTX='{"runAsUser":1000,"runAsGroup":1000,"fsGroup":1000,"fsGroupChangePolicy": "OnRootMismatch"}'
WOODPECKER_BACKEND_K8S_ENFORCED_SECCTX='{"privileged":false,"runAsNonRoot":true,"allowPrivilegeEscalation":false,"seccompProfile": {"type": "RuntimeDefault"}, "capabilities": {"drop": ["ALL"]}}'And using this workflow: clone:
git:
image: docker.io/woodpeckerci/plugin-git
settings:
depth: 0
home: "/tmp"
steps:
- name: check-clone-succeeded
image: alpine
commands:
- pwd
- ls -lahNot as convenient as if |
b82ea45 to
b730cca
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6322 +/- ##
==========================================
- Coverage 41.29% 41.12% -0.18%
==========================================
Files 431 431
Lines 28792 28821 +29
==========================================
- Hits 11890 11852 -38
- Misses 15836 15899 +63
- Partials 1066 1070 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
6994e5b to
8a70a27
Compare
|
I took the opportunity to rebase from main to make merging easier |
|
@Aex12 thanks for the work ... took us some time to get reviewed :) |
Problem
When the working directory is set to a directory that doesn't exists (for example, as
plugin-gitdoes), kubelet will pre-create it with ownership set toroot:rootand permissions0755. This makes pods running as non-root unable to write to it, causing permission errors.Solution
Added a
podInitContainerfunction that conditionally creates an init container to pre-create the working directory with the correct permissions before the main step container starts.Behavior
RunAsUser == 0or unset), no init container is created. Kubelet handles directory creation automaticallyFSGroupChangePolicyhandles permissionsbusybox:stable-muslwith minimal resource limits (5m CPU, 5Mi memory) and drops all capabilities.Related issues and PRs