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

Allow gcr authentication with workload identity #383

Merged
merged 8 commits into from
Aug 31, 2023
Merged

Allow gcr authentication with workload identity #383

merged 8 commits into from
Aug 31, 2023

Conversation

dhpollack
Copy link
Contributor

This allows the GCR plugin to use a workload identity federation key. This uses a special type of config file to generate a temporary oauth token for drone. This is recommended by google over using service account keys.

@rohit-gohri
Copy link

Hey @dhpollack Could you explain how you are generating the token for workload identity? Are you storing it in secrets

@rohit-gohri
Copy link

I'm trying to recreate a setup similar to https://github.com/google-github-actions/auth in drone

@dhpollack
Copy link
Contributor Author

dhpollack commented Feb 21, 2023

Hey @dhpollack Could you explain how you are generating the token for workload identity? Are you storing it in secrets

To generate a token, you don't necessarily need workload identity. The easiest way to generate a token is to have service account credentials in CI as a secret then run the following gcloud command:

export TOKEN=$(gcloud auth application-default print-access-token --quiet)
echo $TOKEN

to generate an actual workload identity config you can run the command:

export GCP_PROJECT_NUMBER=[your project number (not name)]
export POOL_ID=[name of workload identity pool]
export PROVIDER_ID=[name of your provider. related to where the request is coming from]
export SERVICE_ACCOUNT_EMAIL=[service account email]
gcloud iam workload-identity-pools create-cred-config \
  "projects/${GCP_PROJECT_NUMBER}/locations/global/workloadIdentityPools/${POOL_ID}/providers/${PROVIDER_ID}" \
  --service-account=${SERVICE_ACCOUNT} \
  --aws \
  --output-file="$HOME/.config/application_default_credentials.json"

@dhpollack
Copy link
Contributor Author

Setting up the pool itself is not entirely trivial. We used terraform, but there was a lot of playing around with the settings:

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/iam_workload_identity_pool
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/iam_workload_identity_pool_provider

The official google instructions are also helpful to know what all the options are:

https://cloud.google.com/iam/docs/workload-identity-federation

@dhpollack
Copy link
Contributor Author

Looking at the docs that you pointed to, you need to create an access_token. The service account json method is much easier but as stated in the documentation, it's not as secure.

@alzabo
Copy link

alzabo commented Apr 5, 2023

Not sure if this will help get time from maintainers for this PR, but the patch works fine for me.

As @dhpollack writes in his initial comment, use of service account key export is not recommended by Google

@dhpollack
Copy link
Contributor Author

@rutvijmehta-harness @tphoney How does one get a PR merge into master here?

@dhpollack
Copy link
Contributor Author

I found a bug with base64 encoded workload identity files, which I fixed. I also found a bug in the drone-ecr plugin where the default value for the env vars was always set to false for booleans.

Copy link

@tphoney tphoney left a comment

Choose a reason for hiding this comment

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

Great fix for the bugged method in ecr 🥂
there is a couple of nit picks around variable naming, nothing big.
Also could you squash your commits into either one commit with everything (we can do that at merge time), or 2 commits with the ecr fix in one, and the gcr work in the other. your choice.
thanks for the effort and expertise !!!

@tphoney tphoney added the enhancement New feature or request label Aug 31, 2023
@dhpollack
Copy link
Contributor Author

@tphoney I do have one more question. Does the documentation for this automatically get generated or do I need to make the changes somewhere? I couldn't find where that was done.

@tphoney tphoney merged commit 1ec7bb3 into drone-plugins:master Aug 31, 2023
@tphoney
Copy link

tphoney commented Aug 31, 2023

thanks @dhpollack your go code was fine, it was just to keep it in line with the other code.

https://github.com/drone/drone-plugin-index/blob/main/plugins/gcr/content.yaml is the raw documentation for the the plugins website here https://plugins.drone.io/plugins/gcr

once again thanks for the change !

@dhpollack
Copy link
Contributor Author

@tphoney I created a PR for the docs.

drone/drone-plugin-index#414

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

Successfully merging this pull request may close these issues.

4 participants