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: automatically remove user registry secrets #435

Merged
merged 49 commits into from
Oct 26, 2020

Conversation

olevski
Copy link
Member

@olevski olevski commented Oct 18, 2020

Closes #333

A few things to note:

  • The scripts that are used by the cronjob can be moved to a fully independent docker image. I decided to not do this just because it was easier to piggy-back on the notebooks docker image. This is now an independent image.
  • Use {servername}-registry to name user registry secrets (servername is escaped to prevent underscores from the project name causing issues.) It was decided to use the following pattern {safeusername}-registry-{uuid4()}.
  • Wrote unit tests following the ones that are already present for the api and other code
  • The logic for removing secrets is as follows:
    1. In the cronjob check for any secret in the designated namespace that match the regex (.+)-registry$ .+-registry-[a-z0-9-]{36}$ and has the label component=singleuser-server, with the capture group indicating the servername for the user server.
    2. In addition to the regex test also test for secret.type == "kubernetes.io/dockerconfigjson"and for the presence of the labels ["renku.io/commit-sha", "renku.io/projectName", "renku.io/username"] which are used to then identify the pod that used the secret.
    3. If any secret passes all tests from the previous two steps, then:
      a) Check to see if the pod that used the secret is still around, if so and if the pod's status is running or completed, then delete the secret
      b) If the pod that used the secret cannot be found (based on the servername labels) then delete the secret only if the age of the secret is older than the designated threshold

This is currently in place at https://tasko.dev.renku.ch/. Tested with a public and private projects.

@olevski olevski requested review from a team as code owners October 18, 2020 21:30
@rokroskar
Copy link
Member

I believe you need to also check the username of the pod explicitly - checking the server name is not enough. See GHSA-v7m9-9497-p9gr - you can create user names that will map to the same escaped version of the name. Come to think of it, all of the secret handling probably needs to be rethought a bit - adding annotations would probably be the way to go.

Copy link
Contributor

@ableuler ableuler left a comment

Choose a reason for hiding this comment

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

Thanks @olevski for this nicely done and well documented PR and particularly for test-covering the added functionality right away!

I think the biggest change necessary is related to how we map a secret to a pod (see comment below). Particularly, there's a difference between the k8s pod name and the JH server name that I didn't really point out (sorry about that). While the k8s pod name IS unique and includes both username, repo name and commit, different users can have servers with the same servername, even after fixing the CVE that @rokroskar has pointed out. For example check out https://<renku-domain>/jupyterhub/hub/home for a list of the servernames of your running servers. My initial thought was to add the pod name directly as annotation to the secret, but the secret is created in the notebooks-service while the pod name is generated in the spawner, so we'd either have to duplicate the logic which generates the pod name or - what I have described in the comment - rely custom renku.io annotations (or labels!) for matching secrets and pods.

renku_notebooks/util/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
renku_notebooks/util/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
helm-chart/renku-notebooks/templates/cronjob.yaml Outdated Show resolved Hide resolved
Copy link
Member

@rokroskar rokroskar left a comment

Choose a reason for hiding this comment

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

Thanks @olevski this looks good! Just a few minor comments for clarification.

chartpress.yaml Outdated Show resolved Hide resolved
clean_user_registry_secrets/Pipfile Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
clean_user_registry_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
Copy link
Member

@rokroskar rokroskar left a comment

Choose a reason for hiding this comment

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

oops missed this the first time around.. copy/paste error I presume

cull_secrets/clean_user_registry_secrets.py Outdated Show resolved Hide resolved
tests/test_clean_user_registry_secrets.py Outdated Show resolved Hide resolved
@olevski
Copy link
Member Author

olevski commented Oct 23, 2020

@ableuler , @rokroskar I addressed all the comments. Also I deployed the latest from this branch to my namespace and tested the cases when:

  • the secret is deleted when the pod is around but is running
  • the secret is not deleted when the age of the secret is less than 5 mins but the pod is not around
  • the secret is deleted when the pod is not around and the secret is old enough

@rokroskar
Copy link
Member

One last thing and you might leave this to another PR, which would be fine imo, but we should have some way of controlling, through the values file, a) the cron setup and b) the max secret age

@olevski
Copy link
Member Author

olevski commented Oct 23, 2020

One last thing and you might leave this to another PR, which would be fine imo, but we should have some way of controlling, through the values file, a) the cron setup and b) the max secret age

No problem I can add this to this PR @rokroskar.

Copy link
Member

@rokroskar rokroskar left a comment

Choose a reason for hiding this comment

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

🚀

@olevski olevski merged commit 334f16b into master Oct 26, 2020
@olevski olevski deleted the automatically-remove-user-registry-secrets branch October 26, 2020 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

automatically remove user registry secrets
4 participants