Configuring Service Accounts

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

Overview

When a person uses the OKD CLI or web console, their API token authenticates them to the OKD API. However, when a regular user’s credentials are not available, it is common for components to make API calls independently. For example:

  • Replication controllers make API calls to create or delete pods.

  • Applications inside containers can make API calls for discovery purposes.

  • External applications can make API calls for monitoring or integration purposes.

Service accounts provide a flexible way to control API access without sharing a regular user’s credentials.

User Names and Groups

Every service account has an associated user name that can be granted roles, just like a regular user. The user name is derived from its project and name:

  1. system:serviceaccount:<project>:<name>

For example, to add the view role to the robot service account in the top-secret project:

  1. $ oc policy add-role-to-user view system:serviceaccount:top-secret:robot

If you want to grant access to a specific service account in a project, you can use the -z flag. From the project to which the service account belongs, use the -z flag and specify the <serviceaccount_name>. This is highly recommended, as it helps prevent typos and ensures that access is granted only to the specified service account. For example:

  1. $ oc policy add-role-to-user <role_name> -z <serviceaccount_name>

If not in the project, use the -n option to indicate the project namespace it applies to, as shown in the examples below.

Every service account is also a member of two groups:

system:serviceaccount

Includes all service accounts in the system.

system:serviceaccount:<project>

Includes all service accounts in the specified project.

For example, to allow all service accounts in all projects to view resources in the top-secret project:

  1. $ oc policy add-role-to-group view system:serviceaccount -n top-secret

To allow all service accounts in the managers project to edit resources in the top-secret project:

  1. $ oc policy add-role-to-group edit system:serviceaccount:managers -n top-secret

Managing Service Accounts

Service accounts are API objects that exist within each project. To manage service accounts, you can use the oc command with the sa or serviceaccount object type or use the web console.

To get a list of existing service accounts in the current project:

  1. $ oc get sa
  2. NAME SECRETS AGE
  3. builder 2 2d
  4. default 2 2d
  5. deployer 2 2d

To create a new service account:

  1. $ oc create sa robot
  2. serviceaccount "robot" created

As soon as a service account is created, two secrets are automatically added to it:

  • an API token

  • credentials for the OpenShift Container Registry

These can be seen by describing the service account:

  1. $ oc describe sa robot
  2. Name: robot
  3. Namespace: project1
  4. Labels: <none>
  5. Annotations: <none>
  6. Image pull secrets: robot-dockercfg-qzbhb
  7. Mountable secrets: robot-token-f4khf
  8. robot-dockercfg-qzbhb
  9. Tokens: robot-token-f4khf
  10. robot-token-z8h44

The system ensures that service accounts always have an API token and registry credentials.

The generated API token and registry credentials do not expire, but they can be revoked by deleting the secret. When the secret is deleted, a new one is automatically generated to take its place.

Enabling Service Account Authentication

Service accounts authenticate to the API using tokens signed by a private RSA key. The authentication layer verifies the signature using a matching public RSA key.

To enable service account token generation, update the **serviceAccountConfig** stanza in the /etc/origin/master/master-config.yml file on the master to specify a **privateKeyFile** (for signing), and a matching public key file in the **publicKeyFiles** list:

  1. serviceAccountConfig:
  2. ...
  3. masterCA: ca.crt (1)
  4. privateKeyFile: serviceaccount.private.key (2)
  5. publicKeyFiles:
  6. - serviceaccount.public.key (3)
  7. - ...
1CA file used to validate the API server’s serving certificate.
2Private RSA key file (for token signing).
3Public RSA key files (for token verification). If private key files are provided, then the public key component is used. Multiple public key files can be specified, and a token will be accepted if it can be validated by one of the public keys. This allows rotation of the signing key, while still accepting tokens generated by the previous signer.

Managed Service Accounts

Service accounts are required in each project to run builds, deployments, and other pods. The **managedNames** setting in the /etc/origin/master/master-config.yml file on the master controls which service accounts are automatically created in every project:

  1. serviceAccountConfig:
  2. ...
  3. managedNames: (1)
  4. - builder (2)
  5. - deployer (3)
  6. - default (4)
  7. - ...
1List of service accounts to automatically create in every project.
2A builder service account in each project is required by build pods, and is given the system:image-builder role, which allows pushing images to any image stream in the project using the internal container registry.
3A deployer service account in each project is required by deployment pods, and is given the system:deployer role, which allows viewing and modifying replication controllers and pods in the project.
4A default service account is used by all other pods unless they specify a different service account.

All service accounts in a project are given the system:image-puller role, which allows pulling images from any image stream in the project using the internal container registry.

Infrastructure Service Accounts

Several infrastructure controllers run using service account credentials. The following service accounts are created in the OKD infrastructure project (openshift-infra) at server start, and given the following roles cluster-wide:

Service AccountDescription

replication-controller

Assigned the system:replication-controller role

deployment-controller

Assigned the system:deployment-controller role

build-controller

Assigned the system:build-controller role. Additionally, the build-controller service account is included in the privileged security context constraint in order to create privileged build pods.

To configure the project where those service accounts are created, set the **openshiftInfrastructureNamespace** field in the /etc/origin/master/master-config.yml file on the master:

  1. policyConfig:
  2. ...
  3. openshiftInfrastructureNamespace: openshift-infra

Service Accounts and Secrets

Set the **limitSecretReferences** field in the /etc/origin/master/master-config.yml file on the master to true to require pod secret references to be whitelisted by their service accounts. Set its value to false to allow pods to reference any secret in the project.

  1. serviceAccountConfig:
  2. ...
  3. limitSecretReferences: false