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

[Feature] Support switching to another service account user while testing #1256

Open
1 task done
Raazor opened this issue May 7, 2024 · 1 comment
Open
1 task done
Labels
enhancement New feature or request

Comments

@Raazor
Copy link

Raazor commented May 7, 2024

Problem Statement

I am currently testing a kyverno ClusterPolicy against a kind cluster using chainsaw. This specific policy is used to limit a service account user's permissions, e.g. creating namespaces but only when they start with chainsaw-test-* / create Roles & Rolebindings but only in namespaces with chainsaw-test-*.
To test this policy I am using chainsaw to create the policy and the service account with necessary cluster roles and bindings. Then I need to switch my user to the created service account to test that the policy would actually prevent me from, e.g. creating a namespace with arbitrary name. I understand that this is a very specific case, but maybe it can be useful for any tests related to token users / service accounts.

Currently it is working like so:

# Policy and service account users were created before
  - name: Validate Policy error cases
    try:
      - script:
          content: |
           # Paste next two lines in script -> switch-to-chainsaw-sa-user.sh
            kubectl config set-credentials user --token=$(kubectl get secret chainsaw-test-secret -o json | jq -r '.data."token"' | base64 -d 2>/dev/null)
            kubectl config set-context --current --user=user
            kubectl apply -f ./tests/ns-invalid.yaml
          check:
            ($error != null): true
      - script:
          content: |
            ./switch-to-chainsaw-sa-user.sh
            kubectl apply -f ./tests/role-invalid.yaml
          check:
            ($error != null): true
      - script:
          content: |
            ./switch-to-chainsaw-sa-user.sh
            kubectl apply -f ./tests/rolebinding-invalid.yaml
          check:
            ($error != null): true

Conveniently chainsaw somehow already handles the user switch only locally, meaning afterwards I am not locked out and have to reset to using the kind-kind user.
It is inconvenient, however, that I have to always run the script for every step, also I cannot use the chainsaw apply to multiple yaml files as I would do normaly to test error cases (on all files with "*-invalid.yaml). Mostly I also have to cleanup myself and cannot use chainsaw's automatic @cleanup step.

Solution Description

Either implement a functionality so that you can switch to another user or similar to the "cluster" keyword you could use a keyword "service-account" attached to chainsaw operations to make clear this should run using the service account / or the secret token used in the service account.

Alternatives

I have tried before to use the following, which would be overriden however by the admin user that is still configured in kubeconfig.
kubectl --token=$TOKEN some commands

Additional Context

No response

Slack discussion

No response

Research

  • I have searched other issues in this repository and mine is not recorded.
@Raazor Raazor added the enhancement New feature or request label May 7, 2024
@eddycharly
Copy link
Member

Interesting, we already create an ephemeral KUBECONFIG and there's an upcoming feature to register clusters dynamically.

I guess it shouldn't be too hard to implement.

@eddycharly eddycharly modified the milestones: v0.2.2, v0.2.3 May 13, 2024
@eddycharly eddycharly modified the milestones: v0.2.3, v0.2.4 May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants