-
Notifications
You must be signed in to change notification settings - Fork 171
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
allow agent container RunAsUser/Group to be specified via annotations #60
allow agent container RunAsUser/Group to be specified via annotations #60
Conversation
646201d
to
198c487
Compare
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 looks great @joemiller ! I think this would also make a good candidate for environment variables so an operator could globally override uid/gid. Thoughts?
Yeah, makes sense. Will do |
198c487
to
a3db366
Compare
resolved merge conflicts. |
@jasonodonnell I'm looking at the env var code in |
@joemiller Yes! It's a duplication of functionality but flags existed before envs, but envs are a bit easier to configure in Kube vs running the binary. |
@jasonodonnell I'm following the pattern used to propagate https://github.com/hashicorp/vault-k8s/blob/master/agent-inject/agent/annotations.go#L137 Are you ok if I proceed this way? Or refactor? perhaps modifying |
@joemiller Yeah I'd love that actually, it's becoming quite big as is. |
1b6badd
to
ba3ba41
Compare
@jasonodonnell Updated to allow setting user/group at a global level via env vars: I refactored the code from the original commit quite a bit for this. I chose to represent the user/group as strings for the purpose of -flags and env vars, ultimately converting to int64's just before usage in the Agent. This simplified a lot of the testing and parsing and for allowing a zero value of "0" that would result in using the DefaultUser/Group instead of accidentally running as root. Also modified the It looks like I have quite a few files to resolve conflicts before it is mergeable but the code itself should be ready for review now. |
ba3ba41
to
c78bf9c
Compare
Ok, I think this is ready now. There were a significant amount of conflicts to resolve from the merge of the revokeOnShutdown work that touched all of the same parts of code as this PR (lesson learned: be fast ;) ) |
c78bf9c
to
f30434e
Compare
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.
@joemiller This looks great! I'm currently traveling but will return in a few days to functionally test and do a final review. Thanks for the refactor!
Hi @joemiller, just took a look at this. The code looks OK but I'm having issues with functional testing. First, when specifying a non-vault user, the following appears when trying to sink the token:
Next, although I set a custom group, only Here's a complicated example I am using to test this: spec:
template:
metadata:
annotations:
# AGENT INJECTOR SETTINGS
vault.hashicorp.com/agent-run-as-user: "2000"
vault.hashicorp.com/agent-run-as-group: "100"
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/secret-volume-path: "/etc/test"
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/agent-inject-status: "update"
# DATABASE SECRET
vault.hashicorp.com/secret-volume-path-db-creds: "/vault/secretfoo"
vault.hashicorp.com/agent-inject-secret-db-creds: "database/creds/db-app"
vault.hashicorp.com/agent-inject-template-db-creds: |
{{- with secret "database/creds/db-app" -}}
postgres://{{ .Data.username }}:{{ .Data.password }}@postgres.postgres.svc:5432/wizard?sslmode=disable
{{- end }}
# TLS SERVER CERTIFICATE
vault.hashicorp.com/agent-inject-secret-server.cert: "pki/issue/hashicorp-com"
vault.hashicorp.com/agent-inject-template-server.cert: |
{{- with secret "pki/issue/hashicorp-com" "common_name=www.hashicorp.com" -}}
{{ .Data.certificate }}
{{- end }}
# TLS SERVER KEY
vault.hashicorp.com/agent-inject-secret-server.key: "pki/issue/hashicorp-com"
vault.hashicorp.com/agent-inject-template-server.key: |
{{- with secret "pki/issue/hashicorp-com" "common_name=www.hashicorp.com" -}}
{{ .Data.private_key }}
{{- end }}
# TLS CA CERTIFICATE
vault.hashicorp.com/agent-inject-secret-ca.cert: "pki/issue/hashicorp-com"
vault.hashicorp.com/agent-inject-template-ca.cert: |
{{- with secret "pki/issue/hashicorp-com" "common_name=www.hashicorp.com" -}}
{{ .Data.issuing_ca }}
{{- end }}
# VAULT SETTINGS
vault.hashicorp.com/role: "db-app"
vault.hashicorp.com/tls-secret: "tls-test-client"
vault.hashicorp.com/client-cert: "/vault/tls/client.crt"
vault.hashicorp.com/client-key: "/vault/tls/client.key"
vault.hashicorp.com/ca-cert: "/vault/tls/ca.crt"
vault.hashicorp.com/agent-inject-secret-token: "auth/token/lookup-self"
vault.hashicorp.com/agent-inject-template-token: |
{{- with secret "auth/token/lookup-self" -}}{{.Data.id}}{{- end }} |
@jasonodonnell I'll look at this today, as soon as I can. |
@jasonodonnell Ok, I can re-create the issue of being unable to write the token when the user is neither '0' nor the default vault user in the vault docker image. I'm going to look at seeing if it is possible to set the ownership on the /home/vault mount to the same user as is specified by run-as-{user,group} I am also seeing the 'group' GID is not being set in the container:
I would expect gid=1000 in this case. Investigating. |
RE: group not being set. I am wondering if this is a behavior in k8s. I'm using the example pod from: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000 And the observed outcome is different than what is illustrated in the docs: $ ku exec -it security-context-demo sh
/ $ id
uid=1000 gid=0(root) groups=2000 |
Ah, I think I see -- |
And I think we can fix the permissions issue by setting |
Ok, another update. Perhaps due to my misunderstanding, but now I'm starting to grok things a little more. So, both the init container and the sidecar container store the token in I'm wondering if we should do the following:
I think this simplifies things and will make it work how we would expect. And since the init container is guaranteed to run before any regular containers it doesn't make sense to support EDIT If the user would like to set an EDIT 2 I realized we'll have the same problem writing to
that could be extended to do the
thoughts? |
Indeed I was, this makes sense!
We likely shouldn't go mucking with pod fsGroup as it could cause issues with other containers in the pod. Here's what I think should work: we can make |
I’ll try that |
@jasonodonnell After a quick local POC I think the emptyDir volume for In summary, it should look like:
|
@jasonodonnell Pushed a new (separate) commit implementing the emptyDir for Made it a separate commit for review purposes. It could be squashed. I chose 'token volume' as the name for variables and func names. Could change it to 'home volume' or something else if desired. |
4335a23
to
b666a9b
Compare
b666a9b
to
a40d0df
Compare
Sorry we couldn't get this into v0.3.0, but this will definitely be in the next release! |
ed36e4d
to
6358247
Compare
@jasonodonnell Awesome. I rebased to bring in the latest master (agent.go, vaultImage tag update) |
@jasonodonnell I looked around to see if there were any docs for the injector that I should try to update to include these annotations but I couldn't find anything. I'm happy to send PRs to document this and #57 if that is available. |
6358247
to
de7ccae
Compare
@joemiller Thanks Joe, I can update them once this is merged. I plan to review this tomorrow! |
What's the update on this? I came accross the need for it for secrets populated in a |
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.
LGTM! Thanks for the contribution!
…hashicorp#60) * allow agent container RunAsUser/Group to be specified via annotations * set run-as-{user,group} globally in the injector via environment variables * emptyDir volume for token sink (/home/vault) in init and sidecar containers
This PR adds two new annotations:
vault.hashicorp.com/agent-run-as-user
vault.hashicorp.com/agent-run-as-group
Which allows overriding the default userid (100) and groupid (1000) that the Vault Agent init and sidecar containers will run as.
This complements the
vault.hashicorp.com/agent-inject-command-
annotation in #57An example use case for these PRs is described in #56
TL;DR -- Given an example such as running an nginx container with dynamic TLS certs retrieved from Vault, it is necessary to send a
SIGHUP
to nginx to load the new certs. This requires specifying a command to run after new certs are rendered (#57) and that command will need to be run from the Vault Agent sidecar as the same userID that nginx is running in the main/app container (addressed in this PR).