Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: address k8s auth template review findings

Scott Wilson63548706b915
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 7a0655b6ba..d687bae8be 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -1612,6 +1612,7 @@ interface UpdateIdentityKubernetesAuthEvent {
type: EventType.UPDATE_IDENTITY_KUBENETES_AUTH;
metadata: {
identityId: string;
+ identityName?: string;
kubernetesHost?: string;
allowedNamespaces?: string;
allowedNames?: string;
@@ -1620,6 +1621,7 @@ interface UpdateIdentityKubernetesAuthEvent {
accessTokenNumUsesLimit?: number;
accessTokenTrustedIps?: Array<TIdentityTrustedIp>;
templateId?: string | null;
+ templateName?: string;
};
}
@@ -2062,6 +2064,7 @@ interface UpdateIdentityLdapAuthEvent {
type: EventType.UPDATE_IDENTITY_LDAP_AUTH;
metadata: {
identityId: string;
+ identityName?: string;
accessTokenTTL?: number;
accessTokenMaxTTL?: number;
accessTokenNumUsesLimit?: number;
@@ -2069,6 +2072,7 @@ interface UpdateIdentityLdapAuthEvent {
allowedFields?: TAllowedFields[];
url?: string;
templateId?: string | null;
+ templateName?: string;
lockoutEnabled?: boolean;
lockoutThreshold?: number;
lockoutDurationSeconds?: number;
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
index e44e80dc00..eb16df2ef7 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts
@@ -16,8 +16,10 @@ import { TPermissionServiceFactory } from "@app/ee/services/permission/permissio
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { chunkArray } from "@app/lib/fn";
import { TOrgPermission } from "@app/lib/types";
+import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { ActorType } from "@app/services/auth/auth-type";
import { TIdentityKubernetesAuthDALFactory } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-dal";
+import { IdentityKubernetesAuthTokenReviewMode } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-types";
import { validateKubernetesConnectionFields } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-validators";
import { TIdentityLdapAuthDALFactory } from "@app/services/identity-ldap-auth/identity-ldap-auth-dal";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
@@ -78,12 +80,16 @@ export const identityAuthTemplateServiceFactory = ({
};
// template credentials are write-only: every read path strips them so they never
- // reach the client (the edit UI keeps a stored secret by omitting the field)
+ // reach the client (the edit UI keeps a stored secret by omitting the field). A
+ // has<Field> presence flag replaces each secret so the edit UI can offer an explicit
+ // "clear stored value" action without ever reading the credential
const $redactTemplateSecrets = (authMethod: string, fields: Record<string, unknown>) => {
const secretFields = TEMPLATE_SECRET_FIELDS_BY_METHOD[authMethod as IdentityAuthTemplateMethod] ?? [];
const redacted = { ...fields };
secretFields.forEach((key) => {
+ const value = redacted[key];
delete redacted[key];
+ redacted[`has${key.charAt(0).toUpperCase()}${key.slice(1)}`] = typeof value === "string" && value.length > 0;
});
return redacted;
};
@@ -91,20 +97,22 @@ export const identityAuthTemplateServiceFactory = ({
// audit the platform-driven rewrite of linked identities; runs after the propagation
// transaction commits so the tx never waits on Redis, chunked to bound concurrency
const $auditTemplatePropagation = async ({
- identityIds,
+ identities,
authMethod,
templateId,
+ templateName,
orgId
}: {
- identityIds: string[];
+ identities: { identityId: string; identityName?: string }[];
authMethod: string;
templateId: string;
+ templateName: string;
orgId: string;
}) => {
- for (const batch of chunkArray(identityIds, AUDIT_FANOUT_CHUNK_SIZE)) {
+ for (const batch of chunkArray(identities, AUDIT_FANOUT_CHUNK_SIZE)) {
// eslint-disable-next-line no-await-in-loop
await Promise.all(
- batch.map((identityId) =>
+ batch.map(({ identityId, identityName }) =>
auditLogService.createAuditLog({
actor: {
type: ActorType.PLATFORM,
@@ -113,8 +121,14 @@ export const identityAuthTemplateServiceFactory = ({
orgId,
event:
authMethod === IdentityAuthTemplateMethod.LDAP
- ? { type: EventType.UPDATE_IDENTITY_LDAP_AUTH, metadata: { identityId, templateId } }
- : { type: EventType.UPDATE_IDENTITY_KUBENETES_AUTH, metadata: { identityId, templateId } }
+ ? {
+ type: EventType.UPDATE_IDENTITY_LDAP_AUTH,
+ metadata: { identityId, identityName, templateId, templateName }
+ }
+ : {
+ type: EventType.UPDATE_IDENTITY_KUBENETES_AUTH,
+ metadata: { identityId, identityName, templateId, templateName }
+ }
})
)
);
@@ -241,6 +255,17 @@ export const identityAuthTemplateServiceFactory = ({
verifyTlsCertificate: kubernetesFields.verifyTlsCertificate ?? Boolean(kubernetesFields.caCert?.length)
};
$validateKubernetesTemplateFields(normalizedFields);
+ // parity with the identity attach flow: a template-authored host must not let the
+ // backend dial local or private addresses (a gateway-dialed host is exempt; private
+ // hosts are the normal case behind a gateway)
+ if (
+ normalizedFields.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
+ normalizedFields.kubernetesHost &&
+ !normalizedFields.gatewayId &&
+ !normalizedFields.gatewayPoolId
+ ) {
+ await blockLocalAndPrivateIpAddresses(normalizedFields.kubernetesHost);
+ }
if (normalizedFields.gatewayId || normalizedFields.gatewayPoolId) {
$authorizeKubernetesTemplateGateway({
gatewayId: normalizedFields.gatewayId,
@@ -321,6 +346,15 @@ export const identityAuthTemplateServiceFactory = ({
}
}
+ // captured before the propagation writes so the audit entries can carry readable
+ // identity names alongside the ids
+ const linkedIdentities = fieldPatch
+ ? ((await identityAuthTemplateDAL.findTemplateUsages(templateId, template.authMethod)) as {
+ identityId: string;
+ identityName: string;
+ }[])
+ : [];
+
const currentTemplateFields = JSON.parse(
decryptor({ cipherTextBlob: template.templateFields }).toString()
) as Record<string, unknown>;
@@ -338,6 +372,17 @@ export const identityAuthTemplateServiceFactory = ({
merged.verifyTlsCertificate = Boolean(patch.caCert?.length);
}
$validateKubernetesTemplateFields(merged);
+ // the merged host propagates to every linked identity and is dialed by the backend
+ // at login, so it gets the same local/private-address block as the identity attach
+ // flow (gateway-dialed hosts are exempt)
+ if (
+ merged.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
+ merged.kubernetesHost &&
+ !merged.gatewayId &&
+ !merged.gatewayPoolId
+ ) {
+ await blockLocalAndPrivateIpAddresses(merged.kubernetesHost);
+ }
kubernetesPropagationData = {
kubernetesHost: merged.kubernetesHost ?? null,
tokenReviewMode: merged.tokenReviewMode,
@@ -438,10 +483,15 @@ export const identityAuthTemplateServiceFactory = ({
return { updatedTemplate: authTemplate, propagatedIdentityIds: identityIds };
});
+ const identityNameById = new Map(linkedIdentities.map((usage) => [usage.identityId, usage.identityName]));
await $auditTemplatePropagation({
- identityIds: propagatedIdentityIds,
+ identities: propagatedIdentityIds.map((identityId) => ({
+ identityId,
+ identityName: identityNameById.get(identityId)
+ })),
authMethod: template.authMethod,
templateId: template.id,
+ templateName: template.name,
orgId: template.orgId
});
@@ -474,6 +524,11 @@ export const identityAuthTemplateServiceFactory = ({
OrgPermissionSubjects.MachineIdentityAuthTemplate
);
+ const linkedIdentities = (await identityAuthTemplateDAL.findTemplateUsages(templateId, template.authMethod)) as {
+ identityId: string;
+ identityName: string;
+ }[];
+
const { deletedTemplate, unlinkedIdentityIds } = await identityAuthTemplateDAL.transaction(async (tx) => {
// Unlink identity auth records; the copied config values are kept so linked
// identities keep authenticating
@@ -499,10 +554,15 @@ export const identityAuthTemplateServiceFactory = ({
return { deletedTemplate: deletedTpl, unlinkedIdentityIds: identityIds };
});
+ const identityNameById = new Map(linkedIdentities.map((usage) => [usage.identityId, usage.identityName]));
await $auditTemplatePropagation({
- identityIds: unlinkedIdentityIds,
+ identities: unlinkedIdentityIds.map((identityId) => ({
+ identityId,
+ identityName: identityNameById.get(identityId)
+ })),
authMethod: template.authMethod,
templateId: template.id,
+ templateName: template.name,
orgId: template.orgId
});
@@ -688,6 +748,11 @@ export const identityAuthTemplateServiceFactory = ({
throw new NotFoundError({ message: "Template not found" });
}
+ const linkedIdentities = (await identityAuthTemplateDAL.findTemplateUsages(templateId, template.authMethod)) as {
+ identityId: string;
+ identityName: string;
+ }[];
+
let unlinkedIdentityIds: string[] = [];
if (template.authMethod === IdentityAuthTemplateMethod.LDAP) {
const updatedRows = await identityLdapAuthDAL.updateByTemplateId(
@@ -707,10 +772,15 @@ export const identityAuthTemplateServiceFactory = ({
});
}
+ const identityNameById = new Map(linkedIdentities.map((usage) => [usage.identityId, usage.identityName]));
await $auditTemplatePropagation({
- identityIds: unlinkedIdentityIds,
+ identities: unlinkedIdentityIds.map((identityId) => ({
+ identityId,
+ identityName: identityNameById.get(identityId)
+ })),
authMethod: template.authMethod,
templateId: template.id,
+ templateName: template.name,
orgId: template.orgId
});
};
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 6217a1ece1..9196461280 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
@@ -1090,7 +1090,9 @@ export const identityKubernetesAuthServiceFactory = ({
return {
...identityKubernetesAuth,
caCert: caCert ?? "",
- tokenReviewerJwt,
+ // a template's reviewer JWT is an org-scoped write-only credential; identity-level
+ // readers must not be able to recover it through the identity endpoints
+ tokenReviewerJwt: template ? "" : tokenReviewerJwt,
orgId: identityMembershipOrg.scopeOrgId
};
};
@@ -1498,11 +1500,12 @@ export const identityKubernetesAuthServiceFactory = ({
}).toString()
: "";
- const updatedTokenReviewerJwt = updatedKubernetesAuth.encryptedKubernetesTokenReviewerJwt
- ? decryptor({
- cipherTextBlob: updatedKubernetesAuth.encryptedKubernetesTokenReviewerJwt
- }).toString()
- : "";
+ const updatedTokenReviewerJwt =
+ !updatedKubernetesAuth.templateId && updatedKubernetesAuth.encryptedKubernetesTokenReviewerJwt
+ ? decryptor({
+ cipherTextBlob: updatedKubernetesAuth.encryptedKubernetesTokenReviewerJwt
+ }).toString()
+ : "";
await identityAccessTokenService.invalidateTrustedIpsCache(identityId, IdentityAuthMethod.KUBERNETES_AUTH);
return {
@@ -1578,8 +1581,11 @@ export const identityKubernetesAuthServiceFactory = ({
caCert = decryptor({ cipherTextBlob: identityKubernetesAuth.encryptedKubernetesCaCertificate }).toString();
}
+ // a template's reviewer JWT is an org-scoped write-only credential copied onto the
+ // identity row; identity-level readers must not be able to recover it. Manually
+ // configured identities keep read-back (the caller supplied that value themselves).
let tokenReviewerJwt = "";
- if (identityKubernetesAuth.encryptedKubernetesTokenReviewerJwt) {
+ if (!identityKubernetesAuth.templateId && identityKubernetesAuth.encryptedKubernetesTokenReviewerJwt) {
tokenReviewerJwt = decryptor({
cipherTextBlob: identityKubernetesAuth.encryptedKubernetesTokenReviewerJwt
}).toString();
diff --git a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
index 736c9f1f03..8593a44661 100644
--- a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
+++ b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
@@ -4,7 +4,7 @@ import { requestContext } from "@fastify/request-context";
import slugify from "@sindresorhus/slugify";
import { AccessScope, ActionProjectType, IdentityAuthMethod, OrganizationActionScope } from "@app/db/schemas";
-import { TIdentityAuthTemplateDALFactory } from "@app/ee/services/identity-auth-template";
+import { IdentityAuthTemplateMethod, TIdentityAuthTemplateDALFactory } from "@app/ee/services/identity-auth-template";
import { testLDAPConfig } from "@app/ee/services/ldap-config/ldap-fns";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import {
@@ -437,6 +437,9 @@ export const identityLdapAuthServiceFactory = ({
const template = templateId
? await identityAuthTemplateDAL.findByIdAndOrgId(templateId, identityMembershipOrg.scopeOrgId)
: undefined;
+ if (templateId && (!template || template.authMethod !== IdentityAuthTemplateMethod.LDAP)) {
+ throw new NotFoundError({ message: `LDAP auth template with ID '${templateId}' not found` });
+ }
let ldapConfig: { bindDN: string; bindPass: string; searchBase: string; url: string; ldapCaCertificate?: string };
if (template) {
@@ -644,6 +647,9 @@ export const identityLdapAuthServiceFactory = ({
const template = templateId
? await identityAuthTemplateDAL.findByIdAndOrgId(templateId, identityMembershipOrg.scopeOrgId)
: undefined;
+ if (templateId && (!template || template.authMethod !== IdentityAuthTemplateMethod.LDAP)) {
+ throw new NotFoundError({ message: `LDAP auth template with ID '${templateId}' not found` });
+ }
let config: {
bindDN?: string;
bindPass?: string;
diff --git a/docs/documentation/platform/identities/auth-templates.mdx b/docs/documentation/platform/identities/auth-templates.mdx
index 206d7dfbfa..6b96c14c75 100644
--- a/docs/documentation/platform/identities/auth-templates.mdx
+++ b/docs/documentation/platform/identities/auth-templates.mdx
@@ -22,6 +22,10 @@ Auth templates are managed in **Organization Settings > Access Control > Identit
![Identity Auth Templates Section](/images/platform/identities/auth-templates/templates-section.png)
+<Warning>
+ Editing a template propagates its authentication settings to every identity linked to it. Treat permission to edit templates as equivalent to permission to manage authentication for all identities that use them, and grant it accordingly.
+</Warning>
+
### Creating a Template
<Steps>
diff --git a/frontend/src/hooks/api/identityAuthTemplates/types.ts b/frontend/src/hooks/api/identityAuthTemplates/types.ts
index 9b830c6ca8..9488130452 100644
--- a/frontend/src/hooks/api/identityAuthTemplates/types.ts
+++ b/frontend/src/hooks/api/identityAuthTemplates/types.ts
@@ -11,6 +11,8 @@ export interface LdapTemplateFields {
bindPass: string;
searchBase: string;
ldapCaCertificate?: string;
+ // presence flag returned in place of the write-only secret
+ hasBindPass?: boolean;
}
export interface KubernetesTemplateFields {
@@ -22,6 +24,8 @@ export interface KubernetesTemplateFields {
gatewayId?: string | null;
gatewayPoolId?: string | null;
allowedAudience?: string;
+ // presence flag returned in place of the write-only secret
+ hasTokenReviewerJwt?: boolean;
}
export type TemplateFieldsByMethod = {
diff --git a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityAuthTemplateModal.tsx b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityAuthTemplateModal.tsx
index 04fd57c118..08aa1e063d 100644
--- a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityAuthTemplateModal.tsx
+++ b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityAuthTemplateModal.tsx
@@ -78,6 +78,7 @@ const schema = z
.default(IdentityKubernetesAuthTokenReviewMode.Api),
kubernetesHost: z.string().optional(),
tokenReviewerJwt: z.string().optional(),
+ clearTokenReviewerJwt: z.boolean().default(false),
gatewayId: z.string().optional().nullable(),
gatewayPoolId: z.string().optional().nullable(),
caCert: z.string().optional(),
@@ -134,6 +135,7 @@ const emptyFormValues: FormData = {
tokenReviewMode: IdentityKubernetesAuthTokenReviewMode.Api,
kubernetesHost: "",
tokenReviewerJwt: "",
+ clearTokenReviewerJwt: false,
gatewayId: null,
gatewayPoolId: null,
caCert: "",
@@ -211,6 +213,10 @@ export const IdentityAuthTemplateModal = ({ popUp, handlePopUpToggle }: Props) =
const selectedMethod = watch("method");
const tokenReviewMode = watch("tokenReviewMode");
+ const hasStoredTokenReviewerJwt =
+ isEdit &&
+ template?.authMethod === MachineIdentityAuthMethod.KUBERNETES &&
+ Boolean(template.templateFields?.hasTokenReviewerJwt);
const onFormSubmit = async (data: FormData) => {
const isApiMode = data.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api;
@@ -251,7 +257,10 @@ export const IdentityAuthTemplateModal = ({ popUp, handlePopUpToggle }: Props) =
if (
data.method === MachineIdentityAuthMethod.KUBERNETES &&
isApiMode &&
- !data.tokenReviewerJwt
+ !data.tokenReviewerJwt &&
+ // an explicit clear keeps the "" in the patch, which clears the stored credential
+ // and propagates client-token review to every linked identity
+ !data.clearTokenReviewerJwt
) {
delete (patchFields as Partial<KubernetesTemplateFields>).tokenReviewerJwt;
}
@@ -582,6 +591,34 @@ export const IdentityAuthTemplateModal = ({ popUp, handlePopUpToggle }: Props) =
)
}
/>
+
+ {hasStoredTokenReviewerJwt && !watch("tokenReviewerJwt") && (
+ <Controller
+ control={control}
+ name="clearTokenReviewerJwt"
+ render={({ field: { value, onChange } }) => (
+ <Field>
+ <div className="flex items-center gap-2">
+ <Switch
+ id="identity-auth-template-clear-token-reviewer-jwt"
+ checked={value}
+ onCheckedChange={onChange}
+ />
+ <FieldLabel
+ htmlFor="identity-auth-template-clear-token-reviewer-jwt"
+ className="mb-0"
+ >
+ Remove stored token on save
+ </FieldLabel>
+ </div>
+ <p className="text-xs text-muted">
+ A token reviewer JWT is currently stored. Removing it switches linked
+ identities to reviewing tokens with the client&apos;s own JWT.
+ </p>
+ </Field>
+ )}
+ />
+ )}
</>
)}