Using bound service account tokens

You can use bound service account tokens, which improves the ability to integrate with cloud provider identity access management (IAM) services, such as AWS IAM.

About bound service account tokens

You can use bound service account tokens to limit the scope of permissions for a given service account token. These tokens are audience and time-bound. This facilitates the authentication of a service account to an IAM role and the generation of temporary credentials mounted to a pod. You can request bound service account tokens by using volume projection and the TokenRequest API.

Configuring bound service account tokens using volume projection

You can configure pods to request bound service account tokens by using volume projection.

Prerequisites

  • You have access to the cluster as a user with the cluster-admin role.

  • You have created a service account. This procedure assumes that the service account is named build-robot.

Procedure

  1. Optional: Set the service account issuer.

    This step is typically not required if the bound tokens are used only within the cluster.

    If you update the serviceAccountIssuer field and there are bound tokens already in use, all bound tokens with the previous issuer value will be invalidated. Unless the holder of a bound token has explicit support for a change in issuer, the holder will not request a new bound token until pods have been restarted.

    If necessary, you can manually restart all pods in the cluster so that the holder will request a new bound token. Before doing this, wait for a new revision of the Kubernetes API server pods to roll out with your service account issuer changes.

    1. Edit the cluster Authentication object:

      1. $ oc edit authentications cluster
    2. Set the spec.serviceAccountIssuer field to the desired service account issuer value:

      1. spec:
      2. serviceAccountIssuer: https://test.default.svc (1)
      1This value should be a URL from which the recipient of a bound token can source the public keys necessary to verify the signature of the token. The default is https://kubernetes.default.svc.
    3. Save the file to apply the changes.

    4. Optional: Manually restart all pods in the cluster so that the holder will request a new bound token.

      1. Wait for a new revision of the Kubernetes API server pods to roll out. It can take several minutes for all nodes to update to the new revision. Run the following command:

        1. $ oc get kubeapiserver -o=jsonpath='{range .items[0].status.conditions[?(@.type=="NodeInstallerProgressing")]}{.reason}{"\n"}{.message}{"\n"}'

        Review the NodeInstallerProgressing status condition for the Kubernetes API server to verify that all nodes are at the latest revision. The output shows AllNodesAtLatestRevision upon successful update:

        1. AllNodesAtLatestRevision
        2. 3 nodes are at revision 12 (1)
        1In this example, the latest revision number is 12.

        If the output shows a message similar to one of the following messages, the update is still in progress. Wait a few minutes and try again.

        • 3 nodes are at revision 11; 0 nodes have achieved new revision 12

        • 2 nodes are at revision 11; 1 nodes are at revision 12

      2. Manually restart all pods in the cluster:

        Be aware that running this command causes a service interruption, because it deletes every running pod in every namespace. These pods will automatically restart after they are deleted.

        1. $ for I in $(oc get ns -o jsonpath='{range .items[*]} {.metadata.name}{"\n"} {end}'); \
        2. do oc delete pods --all -n $I; \
        3. sleep 1; \
        4. done
  1. Configure a pod to use a bound service account token by using volume projection.

    1. Create a file called pod-projected-svc-token.yaml with the following contents:

      1. apiVersion: v1
      2. kind: Pod
      3. metadata:
      4. name: nginx
      5. spec:
      6. containers:
      7. - image: nginx
      8. name: nginx
      9. volumeMounts:
      10. - mountPath: /var/run/secrets/tokens
      11. name: vault-token
      12. serviceAccountName: build-robot (1)
      13. volumes:
      14. - name: vault-token
      15. projected:
      16. sources:
      17. - serviceAccountToken:
      18. path: vault-token (2)
      19. expirationSeconds: 7200 (3)
      20. audience: vault (4)
      1A reference to an existing service account.
      2The path relative to the mount point of the file to project the token into.
      3Optionally set the expiration of the service account token, in seconds. The default is 3600 seconds (1 hour) and must be at least 600 seconds (10 minutes). The kubelet will start trying to rotate the token if the token is older than 80 percent of its time to live or if the token is older than 24 hours.
      4Optionally set the intended audience of the token. The recipient of a token should verify that the recipient identity matches the audience claim of the token, and should otherwise reject the token. The audience defaults to the identifier of the API server.
    2. Create the pod:

      1. $ oc create -f pod-projected-svc-token.yaml

      The kubelet requests and stores the token on behalf of the pod, makes the token available to the pod at a configurable file path, and refreshes the token as it approaches expiration.

  2. The application that uses the bound token must handle reloading the token when it rotates.

    The kubelet rotates the token if it is older than 80 percent of its time to live, or if the token is older than 24 hours.