gke-gcloud-auth-plugin: invalidate cached tokens when gcloud config changes #1249
This issue is currently awaiting triage.
If the repository mantainers determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.
The triage/accepted label can be added by org members by writing /triage accepted in a comment.
<details>
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
</details>
Hi @immanuwell. Thanks for your PR.
I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.
>[!TIP]
>We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.
Once the patch is verified, the new status will be reflected by the ok-to-test label.
I understand the commands that are listed here.
<details>
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
</details>
/ok-to-test
This doesn't work on macOS because Go’s os.UserConfigDir() resolves to ~/Library/Application.
Should this be treated as a cache invalidation/miss?
Let's avoid duplicate raw file contents into the kube cache. How about hashing this?
Thanks for looking into this! Thinking about reliability, the blast radius of breaking kubectl authentication is huge. Let's take a step back and think about how to roll out this feature safely.
I'd recommend the following:
Add a feature gate e.g. an env var like GKE_AUTH_PLUGIN_STRICT_CACHE_INVALIDATION. This should be opt-in at first, and we can flip it to default-on in a future release once it has bake time.
Ensure that any errors encountered while reading or parsing the gcloud config files do not bubble up and fail the kubectl command. If the file is unreadable, log a warning and gracefully fall back to the legacy caching behavior.
The gcloud CLI also supports CLOUDSDK_ACTIVE_CONFIG_NAME. What would be the behavior when it's set?
fixed, config location now follows gcloud's documented default (~/.config/gcloud on macOS and Linux; the platform config directory on Windows)
got it, cache now stores a SHA-256 hash of the active config file instead of its raw contents
CLOUDSDK_ACTIVE_CONFIG_NAME now takes precedence over active_config
updated the implementation to make strict gcloud-config cache invalidation opt-in via GKE_AUTH_PLUGIN_STRICT_CACHE_INVALIDATION=true, with fail-open behavior
@hdp617 addressed your suggestions, PTAL
/assign LogicalShark
Could you also take a look? I remember you were looking at this issue.
/lgtm
/approve
@immanuwell Thank you for looking into this issue.
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/kubernetes/cloud-provider-gcp/pull/1249#issuecomment-5075169472" title="Approved">hdp617</a>, <a href="https://github.com/kubernetes/cloud-provider-gcp/pull/1249#" title="Author self-approved">immanuwell</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":[]} -->
hdp617
immanuwell
Fixes #1122
The plugin can reuse a cached token after
CLOUDSDK_CONFIGswitches to another active account. Same kube context, wrong creds sneak through.This stores the active gcloud config in the cache key, then refreshes the token when that config changes. Old cache gets invalidated once, then its all good.
Repro
1.
export KUBECONFIG=$HOME/.config/gk8s/t/dev2.
export CLOUDSDK_CONFIG=$HOME/.config/gcloud-one3.
gcloud auth activate-service-account --key-file=keyfile-admin4.
kubectl get nodes5.
export CLOUDSDK_CONFIG=$HOME/.config/gcloud-two6.
gcloud auth activate-service-account --key-file=keyfile-restricted-user7.
kubectl get nodesBefore this PR, step 7 can still use the admin token. Kinda sneaky.
Tests
go test ./cmd/gke-gcloud-auth-plugingo test ./cmd/...