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

Dynamically retrieve the cluster audiences #49796

Merged
merged 6 commits into from
Dec 5, 2024

Conversation

hugoShaka
Copy link
Contributor

@hugoShaka hugoShaka commented Dec 4, 2024

Fixes #49756

I introduced a bug in #49528 by using a standard hardcoded audience for in-cluster joining. This PR makes Teleport detect the audience by looking at its own token.

Not every cluster is using kubernetes.default.svc as the default audience, some are using kubernetes.default.svc.cluster.local or custom values.

Changelog: Fix a bug breaking in-cluster joining on some Kubernetes clusters.

@hugoShaka hugoShaka force-pushed the hugo/fix-kube-default-audience branch from 9a28938 to 6443509 Compare December 4, 2024 23:38
@hugoShaka
Copy link
Contributor Author

hugoShaka commented Dec 4, 2024

Cut a v18 test build and this fixes the issue:

helm repo add teleport-staging https://charts.releases.development.teleport.dev
helm repo update
helm upgrade --install --namespace teleport-cluster --create-namespace teleport teleport-staging/teleport-cluster --version 18.0.0-dev.hugoooops.2 --set clusterName=hugo
kubectl get pods -n teleport-cluster

NAME                              READY   STATUS    RESTARTS   AGE
teleport-auth-7c77b85b9-7gjv8     1/1     Running   0          15m
teleport-proxy-84c9b8586c-t7kr8   1/1     Running   0          15m

lib/kube/token/validator.go Outdated Show resolved Hide resolved
lib/kube/token/validator.go Outdated Show resolved Hide resolved
return nil, trace.Wrap(err, "reviewing token and retrieving audience")
}

return reviewResult.Status.Audiences, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

The API docs for status.audiences mention

a TokenReview returns an empty status.audience field where status.authenticated is "true", the token is valid against the audience of the Kubernetes API server.

Can we be reasonably sure that all supported kube api servers will actually populate the field?

Copy link
Contributor

Choose a reason for hiding this comment

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

If Kubernetes doesn't guarantee that the audiences field is always defined, we should prioritize parsing the JWT token directly, as this ensures we know the field is set. My concern is that, since the documentation doesn't mandate it, there might have been a version in the past that didn't include the audiences field, or a future version might omit it. To safeguard against this, we should handle the token by unmarshaling it ourselves.

You can use jwt/v4 package for that

import "github.com/golang-jwt/jwt/v4"
...
p := jwt.NewParser()
	claims := &jwt.RegisteredClaims{}
	_, _, err := p.ParseUnverified(token, claims)
	if err != nil {
		panic(err.Error())
	}

	fmt.Println(claims.Audiences)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

(there is a (go-jose/v3/*jwt.JSONWebToken).UnsafeClaimsWithoutVerification method fyi)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed to parsing our own token without validating. The missing audience thing is for legacy Kubernetes tokens (which are in theory still supported, but we do reject them for kube >= 1.21).

So basically:

  • if there's an audience on our token we know this is a modern kube and can set the audience
  • if there are no audience on our token, this is an old kube and we do the review without specifying audience, as we were doing before

lib/kube/token/validator.go Outdated Show resolved Hide resolved
Copy link
Contributor

@bl-nero bl-nero left a comment

Choose a reason for hiding this comment

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

After fixing issues pointed out by Edoardo, this is good to go.

@hugoShaka hugoShaka requested a review from espadolini December 5, 2024 16:05
lib/kube/token/validator.go Show resolved Hide resolved
lib/kube/token/validator.go Outdated Show resolved Hide resolved
lib/kube/token/validator.go Outdated Show resolved Hide resolved
lib/kube/token/validator.go Outdated Show resolved Hide resolved
lib/kube/token/validator.go Outdated Show resolved Hide resolved
@hugoShaka hugoShaka enabled auto-merge December 5, 2024 18:34
@hugoShaka hugoShaka added this pull request to the merge queue Dec 5, 2024
Merged via the queue into master with commit d9c4906 Dec 5, 2024
40 checks passed
@hugoShaka hugoShaka deleted the hugo/fix-kube-default-audience branch December 5, 2024 19:26
@public-teleport-github-review-bot

@hugoShaka See the table below for backport results.

Branch Result
branch/v15 Create PR
branch/v16 Create PR
branch/v17 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[helm/teleport-cluster] Add audience into serviceAccountToken for proxy service
4 participants