Skip to content
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

feat: use security contexts and mount configs instead of chown init containers #223

Merged
merged 7 commits into from
Jul 19, 2024

Conversation

bpow
Copy link
Contributor

@bpow bpow commented Jul 3, 2024

This implements several changes to allow the pods to run as arbitrary user id
throughout all of the lifecycle stages. Primarily this is by avoiding the need to
chown for certificates, etc., instead copying them to an emptyDir which gives them
appropriate ownership for the pod's securityContext.

The postgres cert job example also needed modification because the prior version
required root to be able to install additional packages for cert creation. Instead
I used an alpine/openssl image to create the certs in an initContainer, then
alpine/curl to store the certs in relevant secrets. The middle commit (currently
6256125 if it doesn't get rebased) just prevents writing certain potentially-sensitive
information to logs in case those aren't locked down as well as you'd like. I'd be
OK rebasing without that commit if desired.

Relevant "user story" is in #177

I'm not familiar with the testing mechanisms provided by helm, but for "verification",
I have deployed to two different clusters (one kubernetes and one openshift) that do
not permit containers to run as root (optionally defining runAsUser in relevant
securityContext to provide a specific userId rather than the runtime-assigned id).

In both cases, I was able to exec a shell in the zitadel-debug replicaset
container and verify that the files created had ownership of the running userId
and mode of 400.

Definition of Ready

  • I am happy with the code
  • Short description of the feature/issue is added in the pr description
  • PR is linked to the corresponding user story
  • Acceptance criteria are met
  • All open todos and follow ups are defined in a new ticket and justified
  • Deviations from the acceptance criteria and design are agreed with the PO and documented.
  • No debug or dead code
  • My code has no repetitions
  • Documentation/examples are up-to-date
  • All non-functional requirements are met
  • If possible, the test configuration is adjusted so acceptance tests cover my changes

bpow added 3 commits July 1, 2024 12:45
The prior example used apk to add openssl and curl, which needed root.
This makes the certs in an initContainer (alpine/openssl), then sends
to k8s in a separate container (alpine/curl), sharing state through
an emptyDir
…ing user)

This relies on the fact that an emptyDir is set up with appropriate
permissions for whichever userid is running in the pod. The prior solution
required running chown/chmod by root in the init container. This obviates
the need for root, which is not permitted in many kubernetes installs
as a security precaution.

Fixes zitadel#177 and also fixes zitadel#178 (since there is no chown anymore).
@hifabienne hifabienne added the os-contribution This is a contribution from our open-source community label Jul 3, 2024
@hifabienne hifabienne requested a review from eliobischof July 3, 2024 09:29
@eliobischof
Copy link
Member

Thanks for this great PR 🥳
I actually don't see a need anymore for the chown/copy init container altogether.
The defaults fsGroup 1000 and runAsUser 1000 with readOnly volume mounts are good enough IMO.
I committed my suggestions directly to this PR.
What do you think @bpow and @stebenz?

stebenz
stebenz previously approved these changes Jul 18, 2024
This makes files user/group readable but not by others. Without
this, mode is set to 0644 (rw for user, r for group and others).

Also removes an unused emptyDir in debug replicaset
@bpow
Copy link
Contributor Author

bpow commented Jul 18, 2024

Thanks for looking over this! I agree that it's much cleaner to just use fsGroup and avoid the copying altogether. I think if going with that, though, it's probably safer to explicitly set defaultMode for the involved volumes (at least on the cluster I checked on, without doing so you end up with rw-r--r--).

I've pushed an additional commit with that change (and cleaning up an emptyDir volume in the debug replicaSet which is not needed if not copying secrets).

Copy link
Member

@eliobischof eliobischof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @bpow thanks for your help to make the chart more secure 🥳

If you'd like to have a small gift in return, please send us a mail to [email protected]. We will send you a form with questions about your address and shirt size.

@eliobischof eliobischof changed the title Allow "secure" examples/configurations to run "rootlessly" (e.g., as an arbitrary user) feat: use security contexts instead of chown init containers Jul 19, 2024
@eliobischof eliobischof changed the title feat: use security contexts instead of chown init containers feat: use security contexts and mount configs instead of chown init containers Jul 19, 2024
@eliobischof eliobischof merged commit 25d612f into zitadel:main Jul 19, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os-contribution This is a contribution from our open-source community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants