Kubernetes Auth
Learn how to authenticate with Hanzo KMS in Kubernetes
Kubernetes Auth is a Kubernetes-native authentication method for applications (e.g. pods) to access Hanzo KMS.
Diagram
The following sequence diagram illustrates the Kubernetes Auth workflow for authenticating applications running in pods with Hanzo KMS.
sequenceDiagram
participant Pod as Pod
participant Infis as Hanzo KMS
participant KubernetesServer as K8s API Server
Note over Pod: Step 1: Service Account JWT Token Retrieval
Note over Pod,Infis: Step 2: JWT Token Login Operation
Pod->>Infis: Send JWT token to /api/v1/auth/kubernetes-auth/login
Infis->>KubernetesServer: Forward JWT token for validation
KubernetesServer-->>Infis: Return identity info for JWT
Note over Infis: Step 3: Identity Property Verification
Infis->>Pod: Return short-lived access token
Note over Pod,Infis: Step 4: Access Hanzo KMS API with Token
Pod->>Infis: Make authenticated requests using the short-lived access tokenConcept
At a high-level, Hanzo KMS 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 Hanzo KMS returns a short-lived access token that can be used to make authenticated requests to the Hanzo KMS API.
To be more specific:
- The application deployed on Kubernetes retrieves its service account credential that is a JWT token at the
/var/run/secrets/kubernetes.io/serviceaccount/tokenpod path. - The application sends the JWT token to Hanzo KMS at the
/api/v1/auth/kubernetes-auth/loginendpoint after which Hanzo KMS 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. Hanzo KMS 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. - Hanzo KMS checks the service account properties against set criteria such Allowed Service Account Names and Allowed Namespaces.
- If all is well, Hanzo KMS returns a short-lived access token that the application can use to make authenticated requests to the Hanzo KMS API.
We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS using Kubernetes Auth as they handle the authentication process including service account credential retrieval for you.
Guide
In the following steps, we explore how to create and use identities for your applications in Kubernetes to access the Hanzo KMS API using the Kubernetes Auth authentication method.
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 Hanzo KMS to authenticate with the Kubernetes API Server.
apiVersion: v1
kind: ServiceAccount
metadata:
name: kms-auth
namespace: defaultkubectl apply -f kms-service-account.yaml1.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 Hanzo KMS to access the TokenReview API. You can apply the following configuration file:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: role-tokenreview-binding
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: kms-auth
namespace: defaultkubectl apply -f cluster-role-binding.yaml1.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:
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
name: kms-auth-token
annotations:
kubernetes.io/service-account.name: "kms-auth"kubectl apply -f service-account-token.yaml1.4. Link the secret in step 1.3 to the service account in step 1.1:
kubectl patch serviceaccount kms-auth -p '{"secrets": [{"name": "kms-auth-token"}]}' -n default1.5. Finally, retrieve the token reviewer JWT token from the secret.
kubectl get secret kms-auth-token -n default -o=jsonpath='{.data.token}' | base64 --decodeKeep 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.
When to use this option: Choose this approach to eliminate long-lived tokens. This option simplifies Hanzo KMS 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 Hanzo KMS, add the system:auth-delegator role:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kms-client-binding-[your-app-name]
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: [your-app-service-account]
namespace: [your-app-namespace]kubectl apply -f client-role-binding.yamlWhen configuring Kubernetes Auth in Hanzo KMS, leave the Token Reviewer JWT field empty. Hanzo KMS will use the client's own token for validation.
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 Hanzo KMS 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. - Hanzo KMS Cloud users: Gateway is contact sales@hanzo.ai to purchase an enterprise license.
To deploy a gateway in your Kubernetes cluster, follow our Gateway deployment guide using helm.
To grant the gateway the system:auth-delegator ClusterRole binding, you can use the following command:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kms-token-reviewer-role-binding
namespace: default # Replace with your namespace if not default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: kms-gateway # The name of the gateway service account
namespace: default # Replace with your namespace if not defaultkubectl apply -f gateway-role-binding.yamlThe gateway service account name is kms-gateway by default if deployed using Helm.
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.

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:
- Name (required): A friendly name for the identity.
- Role (required): A role from the Organization Roles tab for the identity to assume. The organization role assigned will determine what organization level resources this identity can have access to.
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:
- Kubernetes Host / Base Kubernetes API URL: The host string, host:port pair, or URL to the base of the Kubernetes API server. This can usually be obtained by running
kubectl cluster-info. - Token Reviewer JWT: A long-lived service account JWT token for Hanzo KMS to access the TokenReview API to validate other service account JWT tokens submitted by applications/pods. This is the JWT token obtained from step 1.5(Reviewer Tab). If omitted, the client's own JWT will be used instead, which requires the client to have the
system:auth-delegatorClusterRole binding. This is shown in step 1, option 2. - Allowed Service Account Names: A comma-separated list of trusted service account names that are allowed to authenticate with Hanzo KMS.
- Allowed Namespaces: A comma-separated list of trusted namespaces that service accounts must belong to authenticate with Hanzo KMS.
- Allowed Audience: An optional audience claim that the service account JWT token must have to authenticate with Hanzo KMS.
- CA Certificate: The PEM-encoded CA cert for the Kubernetes API server. This is used by the TLS client for secure communication with the Kubernetes API server.
- Access Token TTL (default is
2592000equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time. - Access Token Max TTL (default is
2592000equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time. - Access Token Max Number of Uses (default is
0): The maximum number of times that an access token can be used; a value of0implies infinite number of uses. - Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the
0.0.0.0/0, allowing usage from any network address.
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.


To access the Hanzo KMS 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 Hanzo KMS 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 Hanzo KMS to gain access to the Hanzo KMS API.
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 Hanzo KMS.
const fs = require("fs");
try {
const tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token";
const jwtToken = fs.readFileSync(tokenPath, "utf8");
const kmsUrl = "https://app.kms.hanzo.ai"; // or your self-hosted Hanzo KMS URL
const identityId = "<your-identity-id>";
const { data } = await axios.post(
`{kmsUrl}/api/v1/auth/kubernetes-auth/login`,
{
identityId,
jwt,
}
);
console.log("result data: ", data); // access token here
} catch(err) {
console.error(err);
}We recommend using one of Hanzo KMS's clients like SDKs or the KMS Agent to authenticate with Hanzo KMS 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 Hanzo KMS API. In this case, a new access token should be obtained by performing another login operation.
FAQ
There are a few reasons for why this might happen:
- The Kubernetes Auth authentication method configuration is invalid.
- The service account JWT token has expired is malformed or invalid.
- The service account associated with the JWT token does not meet the criteria set forth in the Kubernetes Auth authentication method configuration such as Allowed Service Account Names and Allowed Namespaces.
There are a few reasons for why this might happen:
- The access token has expired.
- The identity is insufficiently permissioned to interact with the resources you wish to access.
- The client access token is being used from an untrusted IP.
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.
How is this guide?
Last updated on