diff --git a/backend/src/db/migrations/20250321100157_k8s-self-reviewer-jwt.ts b/backend/src/db/migrations/20250321100157_k8s-self-reviewer-jwt.ts new file mode 100644 index 000000000..6cc3a2696 --- /dev/null +++ b/backend/src/db/migrations/20250321100157_k8s-self-reviewer-jwt.ts @@ -0,0 +1,19 @@ +import { Knex } from "knex"; + +import { TableName } from "../schemas"; + +export async function up(knex: Knex): Promise { + const hasReviewerJwtCol = await knex.schema.hasColumn( + TableName.IdentityKubernetesAuth, + "encryptedKubernetesTokenReviewerJwt" + ); + if (hasReviewerJwtCol) { + await knex.schema.alterTable(TableName.IdentityKubernetesAuth, (t) => { + t.binary("encryptedKubernetesTokenReviewerJwt").nullable().alter(); + }); + } +} + +export async function down(): Promise { + // we can't make it back to non nullable, it will fail +} diff --git a/backend/src/db/schemas/identity-kubernetes-auths.ts b/backend/src/db/schemas/identity-kubernetes-auths.ts index 85f210ff1..448cec386 100644 --- a/backend/src/db/schemas/identity-kubernetes-auths.ts +++ b/backend/src/db/schemas/identity-kubernetes-auths.ts @@ -28,7 +28,7 @@ export const IdentityKubernetesAuthsSchema = z.object({ allowedNamespaces: z.string(), allowedNames: z.string(), allowedAudience: z.string(), - encryptedKubernetesTokenReviewerJwt: zodBuffer, + encryptedKubernetesTokenReviewerJwt: zodBuffer.nullable().optional(), encryptedKubernetesCaCertificate: zodBuffer.nullable().optional() }); diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts index d5a0857f2..996ef670a 100644 --- a/backend/src/lib/api-docs/constants.ts +++ b/backend/src/lib/api-docs/constants.ts @@ -244,7 +244,7 @@ export const KUBERNETES_AUTH = { kubernetesHost: "The host string, host:port pair, or URL to the base of the Kubernetes API server.", caCert: "The PEM-encoded CA cert for the Kubernetes API server.", tokenReviewerJwt: - "The long-lived service account JWT token for Infisical to access the TokenReview API to validate other service account JWT tokens submitted by applications/pods.", + "Optional JWT token for accessing Kubernetes TokenReview API. If provided, this long-lived token will be used to validate service account tokens during authentication. If omitted, the client's own JWT will be used instead, which requires the client to have the system:auth-delegator ClusterRole binding.", allowedNamespaces: "The comma-separated list of trusted namespaces that service accounts must belong to authenticate with Infisical.", allowedNames: "The comma-separated list of trusted service account names that can authenticate with Infisical.", @@ -260,7 +260,7 @@ export const KUBERNETES_AUTH = { kubernetesHost: "The new host string, host:port pair, or URL to the base of the Kubernetes API server.", caCert: "The new PEM-encoded CA cert for the Kubernetes API server.", tokenReviewerJwt: - "The new long-lived service account JWT token for Infisical to access the TokenReview API to validate other service account JWT tokens submitted by applications/pods.", + "Optional JWT token for accessing Kubernetes TokenReview API. If provided, this long-lived token will be used to validate service account tokens during authentication. If omitted, the client's own JWT will be used instead, which requires the client to have the system:auth-delegator ClusterRole binding.", allowedNamespaces: "The new comma-separated list of trusted namespaces that service accounts must belong to authenticate with Infisical.", allowedNames: "The new comma-separated list of trusted service account names that can authenticate with Infisical.", diff --git a/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts b/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts index 952cfcdaf..0105afd76 100644 --- a/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts +++ b/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts @@ -24,7 +24,7 @@ const IdentityKubernetesAuthResponseSchema = IdentityKubernetesAuthsSchema.pick( allowedAudience: true }).extend({ caCert: z.string(), - tokenReviewerJwt: z.string() + tokenReviewerJwt: z.string().optional().nullable() }); export const registerIdentityKubernetesRouter = async (server: FastifyZodProvider) => { @@ -98,7 +98,7 @@ export const registerIdentityKubernetesRouter = async (server: FastifyZodProvide .object({ kubernetesHost: z.string().trim().min(1).describe(KUBERNETES_AUTH.ATTACH.kubernetesHost), caCert: z.string().trim().default("").describe(KUBERNETES_AUTH.ATTACH.caCert), - tokenReviewerJwt: z.string().trim().min(1).describe(KUBERNETES_AUTH.ATTACH.tokenReviewerJwt), + tokenReviewerJwt: z.string().trim().optional().describe(KUBERNETES_AUTH.ATTACH.tokenReviewerJwt), allowedNamespaces: z.string().describe(KUBERNETES_AUTH.ATTACH.allowedNamespaces), // TODO: validation allowedNames: z.string().describe(KUBERNETES_AUTH.ATTACH.allowedNames), allowedAudience: z.string().describe(KUBERNETES_AUTH.ATTACH.allowedAudience), @@ -195,7 +195,7 @@ export const registerIdentityKubernetesRouter = async (server: FastifyZodProvide .object({ kubernetesHost: z.string().trim().min(1).optional().describe(KUBERNETES_AUTH.UPDATE.kubernetesHost), caCert: z.string().trim().optional().describe(KUBERNETES_AUTH.UPDATE.caCert), - tokenReviewerJwt: z.string().trim().min(1).optional().describe(KUBERNETES_AUTH.UPDATE.tokenReviewerJwt), + tokenReviewerJwt: z.string().trim().nullable().optional().describe(KUBERNETES_AUTH.UPDATE.tokenReviewerJwt), allowedNamespaces: z.string().optional().describe(KUBERNETES_AUTH.UPDATE.allowedNamespaces), // TODO: validation allowedNames: z.string().optional().describe(KUBERNETES_AUTH.UPDATE.allowedNames), allowedAudience: z.string().optional().describe(KUBERNETES_AUTH.UPDATE.allowedAudience), diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts index 8e4e46bc7..e68e4e309 100644 --- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts +++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts @@ -84,6 +84,9 @@ export const identityKubernetesAuthServiceFactory = ({ tokenReviewerJwt = decryptor({ cipherTextBlob: identityKubernetesAuth.encryptedKubernetesTokenReviewerJwt }).toString(); + } else { + // if no token reviewer is provided means the incoming token has to act as reviewer + tokenReviewerJwt = serviceAccountJwt; } const { data } = await axios @@ -291,7 +294,9 @@ export const identityKubernetesAuthServiceFactory = ({ accessTokenTTL, accessTokenNumUsesLimit, accessTokenTrustedIps: JSON.stringify(reformattedAccessTokenTrustedIps), - encryptedKubernetesTokenReviewerJwt: encryptor({ plainText: Buffer.from(tokenReviewerJwt) }).cipherTextBlob, + encryptedKubernetesTokenReviewerJwt: tokenReviewerJwt + ? encryptor({ plainText: Buffer.from(tokenReviewerJwt) }).cipherTextBlob + : null, encryptedKubernetesCaCertificate: encryptor({ plainText: Buffer.from(caCert) }).cipherTextBlob }, tx @@ -387,10 +392,12 @@ export const identityKubernetesAuthServiceFactory = ({ updateQuery.encryptedKubernetesCaCertificate = encryptor({ plainText: Buffer.from(caCert) }).cipherTextBlob; } - if (tokenReviewerJwt !== undefined) { + if (tokenReviewerJwt) { updateQuery.encryptedKubernetesTokenReviewerJwt = encryptor({ plainText: Buffer.from(tokenReviewerJwt) }).cipherTextBlob; + } else if (tokenReviewerJwt === null) { + updateQuery.encryptedKubernetesTokenReviewerJwt = null; } const updatedKubernetesAuth = await identityKubernetesAuthDAL.updateById(identityKubernetesAuth.id, updateQuery); diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-types.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-types.ts index c66ec8480..b3bbcb49e 100644 --- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-types.ts +++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-types.ts @@ -9,7 +9,7 @@ export type TAttachKubernetesAuthDTO = { identityId: string; kubernetesHost: string; caCert: string; - tokenReviewerJwt: string; + tokenReviewerJwt?: string; allowedNamespaces: string; allowedNames: string; allowedAudience: string; @@ -24,7 +24,7 @@ export type TUpdateKubernetesAuthDTO = { identityId: string; kubernetesHost?: string; caCert?: string; - tokenReviewerJwt?: string; + tokenReviewerJwt?: string | null; allowedNamespaces?: string; allowedNames?: string; allowedAudience?: string; diff --git a/docs/documentation/platform/identities/kubernetes-auth.mdx b/docs/documentation/platform/identities/kubernetes-auth.mdx index b4d7cc1ac..58069f09e 100644 --- a/docs/documentation/platform/identities/kubernetes-auth.mdx +++ b/docs/documentation/platform/identities/kubernetes-auth.mdx @@ -37,7 +37,8 @@ then Infisical returns a short-lived access token that can be used to make authe To be more specific: 1. The application deployed on Kubernetes retrieves its [service account credential](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#opt-out-of-api-credential-automounting) that is a JWT token at the `/var/run/secrets/kubernetes.io/serviceaccount/token` pod path. -2. The application sends the JWT token to Infisical at the `/api/v1/auth/kubernetes-auth/login` endpoint after which Infisical forwards the JWT token to the Kubernetes API Server at the [TokenReview API](https://kubernetes.io/docs/reference/kubernetes-api/authentication-resources/token-review-v1/) 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 a long-lived service account JWT token itself (referred to onward as the token reviewer JWT token). +2. The application sends the JWT token to Infisical at the `/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. 3. Infisical checks the service account properties against set criteria such **Allowed Service Account Names** and **Allowed Namespaces**. 4. If all is well, Infisical returns a short-lived access token that the application can use to make authenticated requests to the Infisical API. @@ -53,6 +54,12 @@ In the following steps, we explore how to create and use identities for your app + + + + + **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. ```yaml infisical-service-account.yaml @@ -61,7 +68,6 @@ In the following steps, we explore how to create and use identities for your app metadata: name: infisical-auth namespace: default - ``` ``` @@ -121,7 +127,40 @@ In the following steps, we explore how to create and use identities for your app 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. - + + + + + **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: + + ```yaml client-role-binding.yaml + apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRoleBinding + metadata: + name: infisical-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.yaml + ``` + + When configuring Kubernetes Auth in Infisical, leave the **Token Reviewer JWT** field empty. Infisical will use the client's own token for validation. + + + To create an identity, head to your Organization Settings > Access Control > Machine Identities and press **Create identity**. @@ -151,7 +190,8 @@ In the following steps, we explore how to create and use identities for your app 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 Infisical to access the [TokenReview API](https://kubernetes.io/docs/reference/kubernetes-api/authentication-resources/token-review-v1/) to validate other service account JWT tokens submitted by applications/pods. This is the JWT token obtained from step 1.5. + - Token Reviewer JWT: A long-lived service account JWT token for Infisical to access the [TokenReview API](https://kubernetes.io/docs/reference/kubernetes-api/authentication-resources/token-review-v1/) 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-delegator` ClusterRole 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 Infisical. - Allowed Namespaces: A comma-separated list of trusted namespaces that service accounts must belong to authenticate with Infisical. - Allowed Audience: An optional audience claim that the service account JWT token must have to authenticate with Infisical. @@ -176,18 +216,19 @@ In the following steps, we explore how to create and use identities for your app 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](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/). - + 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](/api-reference/overview/introduction). + + > 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. - - ```javascript + + ```javascript const fs = require("fs"); try { const tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"; @@ -237,15 +278,16 @@ In the following steps, we explore how to create and use identities for your app There are a few reasons for why this might happen: - - - The access token has expired. - - The identity is insufficently permissioned to interact with the resources you wish to access. - - The client access token is being used from an untrusted IP. + +- The access token has expired. +- The identity is insufficently 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 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. diff --git a/frontend/src/hooks/api/identities/types.ts b/frontend/src/hooks/api/identities/types.ts index 00dd13d55..35a9870bc 100644 --- a/frontend/src/hooks/api/identities/types.ts +++ b/frontend/src/hooks/api/identities/types.ts @@ -350,7 +350,7 @@ export type AddIdentityKubernetesAuthDTO = { organizationId: string; identityId: string; kubernetesHost: string; - tokenReviewerJwt: string; + tokenReviewerJwt?: string; allowedNamespaces: string; allowedNames: string; allowedAudience: string; @@ -367,7 +367,7 @@ export type UpdateIdentityKubernetesAuthDTO = { organizationId: string; identityId: string; kubernetesHost?: string; - tokenReviewerJwt?: string; + tokenReviewerJwt?: string | null; allowedNamespaces?: string; allowedNames?: string; allowedAudience?: string; diff --git a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityKubernetesAuthForm.tsx b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityKubernetesAuthForm.tsx index 4d25590a1..87dc7dbfd 100644 --- a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityKubernetesAuthForm.tsx +++ b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityKubernetesAuthForm.tsx @@ -31,7 +31,7 @@ import { IdentityFormTab } from "./types"; const schema = z .object({ kubernetesHost: z.string().min(1), - tokenReviewerJwt: z.string().min(1), + tokenReviewerJwt: z.string().optional(), allowedNames: z.string(), allowedNamespaces: z.string(), allowedAudience: z.string(), @@ -166,7 +166,7 @@ export const IdentityKubernetesAuthForm = ({ await updateMutateAsync({ organizationId: orgId, kubernetesHost, - tokenReviewerJwt, + tokenReviewerJwt: tokenReviewerJwt || null, allowedNames, allowedNamespaces, allowedAudience, @@ -182,7 +182,7 @@ export const IdentityKubernetesAuthForm = ({ organizationId: orgId, identityId, kubernetesHost: kubernetesHost || "", - tokenReviewerJwt, + tokenReviewerJwt: tokenReviewerJwt || undefined, allowedNames: allowedNames || "", allowedNamespaces: allowedNamespaces || "", allowedAudience: allowedAudience || "", @@ -255,11 +255,11 @@ export const IdentityKubernetesAuthForm = ({ name="tokenReviewerJwt" render={({ field, fieldState: { error } }) => ( diff --git a/frontend/src/pages/organization/IdentityDetailsByIDPage/components/ViewIdentityAuthModal/ViewIdentityKubernetesAuthContent.tsx b/frontend/src/pages/organization/IdentityDetailsByIDPage/components/ViewIdentityAuthModal/ViewIdentityKubernetesAuthContent.tsx index b34c058b6..da0455160 100644 --- a/frontend/src/pages/organization/IdentityDetailsByIDPage/components/ViewIdentityAuthModal/ViewIdentityKubernetesAuthContent.tsx +++ b/frontend/src/pages/organization/IdentityDetailsByIDPage/components/ViewIdentityAuthModal/ViewIdentityKubernetesAuthContent.tsx @@ -70,20 +70,26 @@ export const ViewIdentityKubernetesAuthContent = ({ {data.kubernetesHost} - {data.tokenReviewerJwt}

- } - > -
- - - Reveal - -
-
+ {data.tokenReviewerJwt ? ( + + {data.tokenReviewerJwt || "Not provided"} +

+ } + > +
+ + + Reveal + +
+
+ ) : ( +

Not set

+ )}
{data.allowedNames