Learn how to authenticate with Infisical in Kubernetes
Kubernetes Auth is a Kubernetes-native authentication method for applications (e.g. pods) to access Infisical.
The following sequence diagram illustrates the Kubernetes Auth workflow for authenticating applications running in pods with Infisical.
At a high-level, Infisical authenticates an application in Kubernetes by verifying its identity and checking that it meets specific requirements (e.g. it is bound to an allowed service account) at the /api/v1/auth/kubernetes-auth/login
endpoint. If successful,
then Infisical returns a short-lived access token that can be used to make authenticated requests to the Infisical API.
To be more specific:
/var/run/secrets/kubernetes.io/serviceaccount/token
pod path./api/v1/auth/kubernetes-auth/login
endpoint after which Infisical forwards the JWT token to the Kubernetes API Server at the TokenReview API for verification and to obtain the service account information associated with the JWT token.
Infisical is able to authenticate and interact with the TokenReview API by using either the long lived JWT token set while configuring this authentication method or by using the incoming token itself. The JWT token mentioned in this context is referred as the token reviewer JWT token.We recommend using one of Infisical’s clients like SDKs or the Infisical Agent to authenticate with Infisical using Kubernetes Auth as they handle the authentication process including service account credential retrieval for you.
In the following steps, we explore how to create and use identities for your applications in Kubernetes to access the Infisical API using the Kubernetes Auth authentication method.
Obtaining the token reviewer JWT for Infisical
Option 1: Reviewer JWT Token
When to use this option: Choose this approach when you want centralized authentication management. Only one service account needs special permissions, and your application service accounts remain unchanged.
1.1. Start by creating a service account in your Kubernetes cluster that will be used by Infisical to authenticate with the Kubernetes API Server.
1.2. Bind the service account to the system:auth-delegator
cluster role. As described here, this role allows delegated authentication and authorization checks, specifically for Infisical to access the TokenReview API. You can apply the following configuration file:
1.3. Next, create a long-lived service account JWT token (i.e. the token reviewer JWT token) for the service account using this configuration file for a new Secret
resource:
1.4. Link the secret in step 1.3 to the service account in step 1.1:
1.5. Finally, retrieve the token reviewer JWT token from the secret.
Keep this JWT token handy as you will need it for the Token Reviewer JWT field when configuring the Kubernetes Auth authentication method for the identity in step 2.
Option 2: Client JWT as Reviewer JWT Token
When to use this option: Choose this approach to eliminate long-lived tokens. This option simplifies Infisical configuration but requires each application service account to have elevated permissions.
The self-validation method eliminates the need for a separate long-lived reviewer JWT by using the same token for both authentication and validation. Instead of creating a dedicated reviewer service account, you’ll grant the necessary permissions to each application service account.
For each service account that needs to authenticate with Infisical, add the system:auth-delegator
role:
When configuring Kubernetes Auth in Infisical, leave the Token Reviewer JWT field empty. Infisical will use the client’s own token for validation.
Option 3: Use Gateway as Reviewer
When to use this option: Choose this approach when you have a gateway deployed in your Kubernetes Cluster and wish to eliminate long-lived tokens. This approach simplifies Infisical Kubernetes Auth configuration, and only one service account will need to have the elevated system:auth-delegator
ClusterRole binding.
Note: Gateway is a paid feature. - Infisical Cloud users: Gateway is available under the Enterprise Tier. - Self-Hosted Infisical: Please contact sales@infisical.com to purchase an enterprise license.
Deploying a gateway
To deploy a gateway in your Kubernetes cluster, follow our Gateway deployment guide using helm.
Grant the gateway the system:auth-delegator ClusterRole binding
To grant the gateway the system:auth-delegator
ClusterRole binding, you can use the following command:
The gateway service account name is infisical-gateway
by default if deployed using Helm.
Configure the Kubernetes Auth authentication method for the identity
To configure your Kubernetes Auth method to use the gateway as the token reviewer, set the Review Method
to “Gateway as Reviewer”, and select the gateway you want to use as the token reviewer.
Creating an identity
To create an identity, head to your Organization Settings > Access Control > Identities and press Create identity.
When creating an identity, you specify an organization level role for it to assume; you can configure roles in Organization Settings > Access Control > Organization Roles.
Now input a few details for your new identity. Here’s some guidance for each field:
Once you’ve created an identity, you’ll be redirected to a page where you can manage the identity.
Since the identity has been configured with Universal Auth by default, you should re-configure it to use Kubernetes Auth instead. To do this, press to edit the Authentication section, remove the existing Universal Auth configuration, and add a new Kubernetes Auth configuration onto the identity.
Here’s some more guidance on each field:
kubectl cluster-info
.system:auth-delegator
ClusterRole binding.
This is shown in step 1, option 2.2592000
equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time.2592000
equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time.0
): The maximum number of times that an access token can be used; a value of 0
implies infinite number of uses.0.0.0.0/0
, allowing usage from any network address.Adding an identity to a project
To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project.
To do this, head over to the project you want to add the identity to and go to Project Settings > Access Control > Machine Identities and press Add identity.
Next, select the identity you want to add to the project and the project level role you want to allow it to assume. The project role assigned will determine what project level resources this identity can have access to.
Accessing the Infisical API with the identity
To access the Infisical API as the identity, you should first make sure that the pod running your application is bound to a service account specified in the Allowed Service Account Names field of the identity’s Kubernetes Auth authentication method configuration in step 2.
Once bound, the pod will receive automatically mounted service account credentials that is a JWT token at the /var/run/secrets/kubernetes.io/serviceaccount/token
path. This token should be used to authenticate with Infisical at the /api/v1/auth/kubernetes-auth/login
endpoint.
For information on how to configure sevice accounts for pods, refer to the guide here.
We provide a code example below of how you might retrieve the JWT token and use it to authenticate with Infisical to gain access to the Infisical API.
Sample code for inside an application
The shown example uses Node.js but you can use any other language to retrieve the service account JWT token and use it to authenticate with Infisical.
We recommend using one of Infisical’s clients like SDKs or the Infisical Agent to authenticate with Infisical using Kubernetes Auth as they handle the authentication process including service account credential retrieval for you.
Each identity access token has a time-to-live (TTL) which you can infer from the response of the login operation;
the default TTL is 7200
seconds which can be adjusted.
If an identity access token exceeds its max ttl, it can no longer authenticate with the Infisical API. In this case, a new access token should be obtained by performing another login operation.
FAQ
Why is the Infisical API rejecting my service account JWT token?
There are a few reasons for why this might happen:
Why is the Infisical API rejecting my access token?
There are a few reasons for why this might happen:
What is access token renewal and TTL/Max TTL?
A identity access token can have a time-to-live (TTL) or incremental lifetime after which it expires.
In certain cases, you may want to extend the lifespan of an access token; to do so, you must set a max TTL parameter.
A token can be renewed any number of times where each call to renew it can extend the token’s lifetime by increments of the access token’s TTL. Regardless of how frequently an access token is renewed, its lifespan remains bound to the maximum TTL determined at its creation.