|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * nodes/nodes-pods-secrets.adoc |
| 4 | + |
| 5 | +:_content-type: PROCEDURE |
| 6 | +[id="nodes-application-secrets-creating-using-sa_{context}"] |
| 7 | += Creating and using secrets |
| 8 | + |
| 9 | +As an administrator, you can create a service account token secret. This allows you to distribute a service account token to applications that must authenticate to the API. |
| 10 | + |
| 11 | +.Procedure |
| 12 | + |
| 13 | +. Create a service account in your namespace by running the following command: |
| 14 | ++ |
| 15 | +[source,terminal] |
| 16 | +---- |
| 17 | +$ oc create sa <service_account_name> -n <your_namespace> |
| 18 | +---- |
| 19 | + |
| 20 | +. Save the following YAML example to a file named `service-account-token-secret.yaml`. The example includes a `Secret` object configuration that you can use to generate a service account token: |
| 21 | ++ |
| 22 | +[source,yaml] |
| 23 | +---- |
| 24 | +apiVersion: v1 |
| 25 | +kind: Secret |
| 26 | +metadata: |
| 27 | + name: <secret_name> <1> |
| 28 | + annotations: |
| 29 | + kubernetes.io/service-account.name: "sa-name" <2> |
| 30 | +type: kubernetes.io/service-account-token <3> |
| 31 | +---- |
| 32 | +<1> Replace `<secret_name>` with the name of your service token secret. |
| 33 | +<2> Specifies an existing service account name. If you are creating both the `ServiceAccount` and the `Secret` objects, create the `ServiceAccount` object first. |
| 34 | +<3> Specifies a service account token secret type. |
| 35 | + |
| 36 | +. Generate the service account token by applying the file: |
| 37 | ++ |
| 38 | +[source,terminal] |
| 39 | +---- |
| 40 | +$ oc apply -f service-account-token-secret.yaml |
| 41 | +---- |
| 42 | + |
| 43 | +. Get the service account token from the secret by running the following command: |
| 44 | ++ |
| 45 | +[source,terminal] |
| 46 | +----- |
| 47 | +$ oc get secret <sa_token_secret> -o jsonpath='{.data.token}' | base64 --decode) <1> |
| 48 | +----- |
| 49 | ++ |
| 50 | +.Example output |
| 51 | +[source,terminal] |
| 52 | +---- |
| 53 | +ayJhbGciOiJSUzI1NiIsImtpZCI6IklOb2dtck1qZ3hCSWpoNnh5YnZhSE9QMkk3YnRZMVZoclFfQTZfRFp1YlUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImJ1aWxkZXItdG9rZW4tdHZrbnIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiYnVpbGRlciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjNmZGU2MGZmLTA1NGYtNDkyZi04YzhjLTNlZjE0NDk3MmFmNyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0OmJ1aWxkZXIifQ.OmqFTDuMHC_lYvvEUrjr1x453hlEEHYcxS9VKSzmRkP1SiVZWPNPkTWlfNRp6bIUZD3U6aN3N7dMSN0eI5hu36xPgpKTdvuckKLTCnelMx6cxOdAbrcw1mCmOClNscwjS1KO1kzMtYnnq8rXHiMJELsNlhnRyyIXRTtNBsy4t64T3283s3SLsancyx0gy0ujx-Ch3uKAKdZi5iT-I8jnnQ-ds5THDs2h65RJhgglQEmSxpHrLGZFmyHAQI-_SjvmHZPXEc482x3SkaQHNLqpmrpJorNqh1M8ZHKzlujhZgVooMvJmWPXTb2vnvi3DGn2XI-hZxl1yD2yGH1RBpYUHA |
| 54 | +---- |
| 55 | +<1> Replace <sa_token_secret> with the name of your service token secret. |
| 56 | + |
| 57 | +. Use your service account token to authenticate with the API of your cluster: |
| 58 | ++ |
| 59 | +[source,terminal] |
| 60 | +---- |
| 61 | +$ curl -X GET <openshift_cluster_api> --header "Authorization: Bearer <token>" <1> <2> |
| 62 | +---- |
| 63 | +<1> Replace `<openshift_cluster_api>` with the OpenShift cluster API. |
| 64 | +<2> Replace `<token>` with the service account token that is output in the preceding command. |
0 commit comments