Skip to content

Commit dcd7e16

Browse files
author
Jamie Klassen
authored
Merge pull request backstage#18594 from jamieklassen/serviceaccount-token-docs
improve serviceAccountToken docs
2 parents 9050b5c + 26fde68 commit dcd7e16

1 file changed

Lines changed: 49 additions & 9 deletions

File tree

docs/features/kubernetes/configuration.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ cluster. Valid values are:
110110
| `google` | This will use a user's Google access token from the [Google auth provider](https://backstage.io/docs/auth/google/provider) to access the Kubernetes API on GKE clusters. |
111111
| `googleServiceAccount` | This will use the Google Cloud service account credentials to access resources in clusters |
112112
| `oidc` | This will use [Oidc Tokens](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens) to authenticate to the Kubernetes API. When this is used the `oidcTokenProvider` field should also be set. Please note the cluster must support OIDC, at the time of writing AKS clusters do not support OIDC. |
113-
| `serviceAccount` | This will use a Kubernetes [service account](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/) to access the Kubernetes API. When this is used the `serviceAccountToken` field should also be set. |
113+
| `serviceAccount` | This will use a Kubernetes [service account](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/) to access the Kubernetes API. When this is used the `serviceAccountToken` field should also be set, or else Backstage should be running in-cluster. |
114114

115115
Check the [Kubernetes Authentication][4] section for additional explanation.
116116

@@ -127,14 +127,54 @@ CPU/Memory for pods returned by the API server. Defaults to `false`.
127127
##### `clusters.\*.serviceAccountToken` (optional)
128128

129129
The service account token to be used when using the `serviceAccount` auth
130-
provider. You could get the service account token with:
131-
132-
```sh
133-
kubectl -n <NAMESPACE> get secret $(kubectl -n <NAMESPACE> get sa <SERVICE_ACCOUNT_NAME> -o=json \
134-
| jq -r '.secrets[0].name') -o=json \
135-
| jq -r '.data["token"]' \
136-
| base64 --decode
137-
```
130+
provider. Note that, unless you have an effective credential rotation procedure
131+
in place or have a single Kubernetes cluster running both Backstage and all your
132+
services, this auth provider is probably not ideal for production.
133+
134+
Assuming you have already created a service account named `SERVICE_ACCOUNT_NAME`
135+
in namespace `NAMESPACE` and it has adequate
136+
[permissions](#role-based-access-control), here are some sample procedures to
137+
procure a long-lived service account token for use with this provider:
138+
139+
- On versions of Kubernetes [prior to
140+
1.24](https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.24.md#no-really-you-must-read-this-before-you-upgrade-1),
141+
you could get an (automatically-generated) token for a service account with:
142+
143+
```sh
144+
kubectl -n <NAMESPACE> get secret $(kubectl -n <NAMESPACE> get sa <SERVICE_ACCOUNT_NAME> -o=json \
145+
| jq -r '.secrets[0].name') -o=json \
146+
| jq -r '.data["token"]' \
147+
| base64 --decode
148+
```
149+
150+
- For Kubernetes 1.24+, as described in [this
151+
guide](https://kubernetes.io/docs/concepts/configuration/secret/#service-account-token-secrets),
152+
you can obtain a long-lived token by creating a secret:
153+
154+
```sh
155+
kubectl apply -f - <<EOF
156+
apiVersion: v1
157+
kind: Secret
158+
metadata:
159+
name: <SECRET_NAME>
160+
namespace: <NAMESPACE>
161+
annotations:
162+
kubernetes.io/service-account.name: <SERVICE_ACCOUNT_NAME>
163+
type: kubernetes.io/service-account-token
164+
EOF
165+
```
166+
167+
waiting for the token controller to populate a token, and retrieving it with:
168+
169+
```sh
170+
kubectl -n <NAMESPACE> get secret <SECRET_NAME> -o go-template='{{.data.token | base64decode}}'
171+
```
172+
173+
If a cluster has `authProvider: serviceAccount` and the `serviceAccountToken`
174+
field is omitted, Backstage will ignore the configured URL and certificate data,
175+
instead attempting to access the Kubernetes API via an in-cluster client as in
176+
[this
177+
example](https://github.com/kubernetes-client/javascript/blob/master/examples/in-cluster.js).
138178

139179
##### `clusters.\*.oidcTokenProvider` (optional)
140180

0 commit comments

Comments
 (0)