Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Update dashboard's validateToken not to use k8s api server.#4043

Merged
absoludity merged 4 commits into
masterfrom
3896-auth-check
Jan 10, 2022
Merged

Update dashboard's validateToken not to use k8s api server.#4043
absoludity merged 4 commits into
masterfrom
3896-auth-check

Conversation

@absoludity
Copy link
Copy Markdown
Contributor

Signed-off-by: Michael Nelson minelson@vmware.com

Description of the change

Part one of removing shared/Auth's use of the k8s API server, this PR updates the validateToken check, used when Kubeapps is configured with token authentication, so that it hits the resources API (checkNamespace), rather than hitting the k8s API server directly.

To do so, I had to correct the error handling in the resources plugin to differentiate between 401s and 403s (and will later follow up as per the TODO there).

I also fixed an issue where the dashboard was checking cookie auth based only on the loginURL being set, which it is by default anyway (which resulted in an extra error being displayed when token login failed).

Benefits

One more action no longer hitting k8s API.

Possible drawbacks

Applicable issues

Additional information

Signed-off-by: Michael Nelson <minelson@vmware.com>
Signed-off-by: Michael Nelson <minelson@vmware.com>
Signed-off-by: Michael Nelson <minelson@vmware.com>
Copy link
Copy Markdown
Contributor

@antgamdia antgamdia left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, +1 anyway, but have a look at the TokenReview comment

try {
await Axios.get(url.api.k8s.base(cluster) + "/", {
headers: { Authorization: `Bearer ${token}` },
await this.resourcesClient(token).CheckNamespaceExists({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 to this approach; however, I wonder whether we should use an alternative function here. I mean, at first glance, calling CheckNamespaceExists for the validateToken function seems a bit misleading.

Perhaps we could use the can-i operation there or, even better, directly use the k8s object aimed at validating tokens, the TokenReview one (more info):

{
  "apiVersion": "authentication.k8s.io/v1",
  "kind": "TokenReview",
  "spec": {
    "token": "014fbff9a07c...", 
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'd initially gone along this direction, adding a new endpoint to the resources plugin (didn't think of using the TokenReview - that would have been a good match), but the reason I stopped was that this functionality (for validateToken) is relevant only for the "demo-only" functionality of using service account tokens to authenticate with Kubeapps.

The existing functionality just tries to get the root URL of the k8s API server and I realised that the endpoint is actually arbitrary - the check just needs to see a 200 or 403 - either is fine.

+1 to switch it over to a can-i endpoint, once we have one available in the resources plugin.

Comment thread dashboard/src/shared/Auth.test.ts Outdated
Comment on lines 69 to 70
// TODO(absoludity): tried using `expect(fn()).rejects.toThrow()` but it seems we need
// to upgrade jest for `toThrow()` to work with async.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just wondering if this ToDo still applies after having applied several upgrades to the jest deps.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nope, it works with the .toThrow now in an async function. Thanks for the nudge, updated.

Comment on lines +418 to +419
// TODO(minelson): Move to a shared helper for plugins interacting
// with the k8s cluster.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, we are using this function also in the flux and carvel plugins

Signed-off-by: Michael Nelson <minelson@vmware.com>
@absoludity absoludity merged commit e0f8d17 into master Jan 10, 2022
@absoludity absoludity deleted the 3896-auth-check branch January 10, 2022 22:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants