Skip to content

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

feat(kubernetes-auth): enhance TLS verification logic and validation

Victor Hugo dos Santos59101600ebb6
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 83a825385e..0b0b997261 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -573,9 +573,9 @@ export const KUBERNETES_AUTH = {
identityId: "The ID of the machine identity to attach the configuration onto.",
kubernetesHost: "The host string, host:port pair, or URL to the base of the Kubernetes API server.",
caCert:
- "The PEM-encoded CA certificate used to validate the Kubernetes API server's TLS certificate. Required when verifyTlsCertificate is true.",
+ "The PEM-encoded CA certificate used to validate the Kubernetes API server's TLS certificate. Required when verifyTlsCertificate is true. Supplying a non-empty caCert always implies verifyTlsCertificate=true; explicitly setting the toggle to false in the same request is rejected.",
verifyTlsCertificate:
- "Whether to verify the Kubernetes API server's TLS certificate against the configured CA certificate. When true, caCert is required. When false, the connection is still over HTTPS but the API server's certificate is not verified. If omitted, defaults to true when caCert is provided and false otherwise.",
+ "Whether to verify the Kubernetes API server's TLS certificate against the configured CA certificate. When true, caCert is required. When false, the connection is still over HTTPS but the API server's certificate is not verified, and caCert must be empty. If omitted, defaults to true when caCert is provided and false otherwise.",
tokenReviewerJwt:
"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.",
tokenReviewMode:
@@ -595,9 +595,9 @@ export const KUBERNETES_AUTH = {
identityId: "The ID of the machine identity to update the auth method for.",
kubernetesHost: "The new host string, host:port pair, or URL to the base of the Kubernetes API server.",
caCert:
- "The new PEM-encoded CA certificate used to validate the Kubernetes API server's TLS certificate. Required when verifyTlsCertificate is true.",
+ "The new PEM-encoded CA certificate used to validate the Kubernetes API server's TLS certificate. Required when verifyTlsCertificate is true. Supplying a non-empty caCert always implies verifyTlsCertificate=true; the update is rejected if the resulting effective state would store a CA together with verifyTlsCertificate=false.",
verifyTlsCertificate:
- "Whether to verify the Kubernetes API server's TLS certificate against the configured CA certificate. When true, caCert is required. When false, the connection is still over HTTPS but the API server's certificate is not verified. If omitted while supplying a non-empty caCert in the same update, the toggle is auto-promoted to true; otherwise the stored value is preserved.",
+ "Whether to verify the Kubernetes API server's TLS certificate against the configured CA certificate. When true, caCert is required. When false, the connection is still over HTTPS but the API server's certificate is not verified, and the resulting effective CA must be empty. If omitted while supplying a non-empty caCert in the same update, the toggle is auto-promoted to true; otherwise the stored value is preserved.",
tokenReviewerJwt:
"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.",
tokenReviewMode:
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 1afeaae910..74df7a1ed6 100644
--- a/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts
+++ b/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts
@@ -275,6 +275,18 @@ export const registerIdentityKubernetesRouter = async (server: FastifyZodProvide
"A CA certificate is required when TLS certificate verification is enabled. Either paste the Kubernetes API server's CA certificate or disable verification."
});
}
+ if (
+ data.verifyTlsCertificate === false &&
+ data.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
+ data.caCert?.length
+ ) {
+ ctx.addIssue({
+ path: ["verifyTlsCertificate"],
+ code: z.ZodIssueCode.custom,
+ message:
+ "TLS certificate verification cannot be disabled when a CA certificate is provided. Either remove the CA certificate or enable verification."
+ });
+ }
}),
response: {
200: z.object({
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 97c3151a90..266046838c 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
@@ -837,6 +837,20 @@ export const identityKubernetesAuthServiceFactory = ({
return extractIPDetails(accessTokenTrustedIp.ipAddress);
});
+ if (
+ caCert &&
+ caCert.length > 0 &&
+ verifyTlsCertificate === false &&
+ tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api
+ ) {
+ throw new BadRequestError({
+ message:
+ "TLS certificate verification cannot be disabled when a CA certificate is provided. Either remove the CA certificate or enable verification."
+ });
+ }
+
+ const resolvedVerifyTlsCertificate = verifyTlsCertificate ?? Boolean(caCert);
+
let isGatewayV1 = true;
if (gatewayId) {
if (!plan.gateway) {
@@ -881,7 +895,7 @@ export const identityKubernetesAuthServiceFactory = ({
const gatewayExecutor = $createGatewayValidationRequest(gatewayId, {
kubernetesHost,
caCert: caCert || undefined,
- verifyTlsCertificate
+ verifyTlsCertificate: resolvedVerifyTlsCertificate
});
logger.info({ gatewayId, kubernetesHost }, "Validating Kubernetes connectivity through gateway");
await validateKubernetesHostConnectivity({ gatewayExecutor });
@@ -924,7 +938,7 @@ export const identityKubernetesAuthServiceFactory = ({
const gatewayExecutor = $createGatewayValidationRequest(validationGateway.id, {
kubernetesHost,
caCert: caCert || undefined,
- verifyTlsCertificate
+ verifyTlsCertificate: resolvedVerifyTlsCertificate
});
await validateKubernetesHostConnectivity({ gatewayExecutor });
if (tokenReviewerJwt) {
@@ -936,7 +950,7 @@ export const identityKubernetesAuthServiceFactory = ({
await validateKubernetesHostConnectivity({
kubernetesHost,
caCert: caCert || undefined,
- verifyTlsCertificate
+ verifyTlsCertificate: resolvedVerifyTlsCertificate
});
if (tokenReviewerJwt) {
@@ -945,7 +959,7 @@ export const identityKubernetesAuthServiceFactory = ({
kubernetesHost,
tokenReviewerJwt,
caCert: caCert || undefined,
- verifyTlsCertificate
+ verifyTlsCertificate: resolvedVerifyTlsCertificate
});
}
}
@@ -980,7 +994,7 @@ export const identityKubernetesAuthServiceFactory = ({
gatewayId: resolvedGatewayId,
gatewayV2Id: resolvedGatewayV2Id,
gatewayPoolId: gatewayPoolId ?? null,
- verifyTlsCertificate: verifyTlsCertificate ?? Boolean(caCert),
+ verifyTlsCertificate: resolvedVerifyTlsCertificate,
accessTokenTrustedIps: JSON.stringify(reformattedAccessTokenTrustedIps),
encryptedKubernetesTokenReviewerJwt: tokenReviewerJwt
? encryptor({ plainText: Buffer.from(tokenReviewerJwt) }).cipherTextBlob
@@ -1198,8 +1212,9 @@ export const identityKubernetesAuthServiceFactory = ({
effectiveCaCert = undefined;
}
- // Auto-promote enableSsl when the caller is supplying a non-empty CA in this
- // update without explicitly setting the toggle. Required for backwards compatibility
+ // Auto-promote verifyTlsCertificate when the caller is supplying a non-empty
+ // CA in this update without explicitly setting the toggle. Required for
+ // backwards compatibility
let resolvedVerifyTlsCertificate: boolean | undefined;
if (verifyTlsCertificate !== undefined) {
resolvedVerifyTlsCertificate = verifyTlsCertificate;
@@ -1219,6 +1234,18 @@ export const identityKubernetesAuthServiceFactory = ({
});
}
+ if (
+ effectiveVerifyTlsCertificate === false &&
+ effectiveTokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
+ effectiveCaCert &&
+ effectiveCaCert.length > 0
+ ) {
+ throw new BadRequestError({
+ message:
+ "TLS certificate verification cannot be disabled when a CA certificate is provided. Either remove the CA certificate or enable verification."
+ });
+ }
+
let validationGatewayId: string | null = effectiveGatewayId ?? null;
if (!validationGatewayId && effectiveGatewayPoolId) {
const picked = await gatewayPoolService.pickRandomHealthyGateway(effectiveGatewayPoolId);
diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-validators.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-validators.ts
index e1ca117bb1..fd00c77fc7 100644
--- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-validators.ts
+++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-validators.ts
@@ -55,7 +55,7 @@ export const validateKubernetesHostConnectivity = async ({
const httpsAgent = new https.Agent({
ca: caCert || undefined,
- rejectUnauthorized: verifyTlsCertificate ?? true
+ rejectUnauthorized: verifyTlsCertificate ?? false
});
await blockLocalAndPrivateIpAddresses(kubernetesHost);
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 25d8f2a34d..8f130a11e5 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
@@ -114,6 +114,19 @@ const schema = z
message: "A CA certificate is required when TLS certificate verification is enabled."
});
}
+
+ if (
+ data.verifyTlsCertificate === false &&
+ data.tokenReviewMode === IdentityKubernetesAuthTokenReviewMode.Api &&
+ data.caCert?.length
+ ) {
+ ctx.addIssue({
+ path: ["verifyTlsCertificate"],
+ code: z.ZodIssueCode.custom,
+ message:
+ "TLS certificate verification cannot be disabled while a CA certificate is provided. Either remove the CA certificate or enable verification."
+ });
+ }
});
export type FormData = z.infer<typeof schema>;
@@ -404,13 +417,6 @@ export const IdentityKubernetesAuthForm = ({
};
const tokenReviewMode = watch("tokenReviewMode");
- const watchedCaCert = watch("caCert");
-
- useEffect(() => {
- if (watchedCaCert && watchedCaCert.length > 0) {
- setValue("verifyTlsCertificate", true, { shouldDirty: true });
- }
- }, [watchedCaCert, setValue]);
return (
<form
@@ -723,45 +729,59 @@ export const IdentityKubernetesAuthForm = ({
<Controller
control={control}
name="verifyTlsCertificate"
- render={({ field: { value, onChange }, fieldState: { error } }) => (
- <FormControl isError={Boolean(error?.message)} errorText={error?.message}>
- <Switch
- className="bg-mineshaft-400/50 shadow-inner data-[state=checked]:bg-green/80"
- id="k8s-verify-tls-certificate"
- thumbClassName="bg-mineshaft-800"
- isChecked={value}
- onCheckedChange={onChange}
- >
- <p className="w-44 text-sm font-normal text-mineshaft-400">
- Verify TLS Certificate
- <Tooltip
- className="max-w-md"
- content={
- <div className="flex flex-col gap-2">
- <p>
- When enabled, Infisical validates the Kubernetes API server&apos;s
- TLS certificate against the CA certificate provided below.
- </p>
- <p>
- Leaving this disabled means any host that responds at the configured
- Kubernetes URL will be trusted, regardless of its certificate. The
- connection is still over HTTPS, but the API server&apos;s identity
- is not verified. Only do this for testing or if you cannot supply a
- CA certificate.
- </p>
- </div>
- }
- >
- <FontAwesomeIcon
- icon={faQuestionCircle}
- size="sm"
- className="ml-1 text-mineshaft-400"
- />
- </Tooltip>
- </p>
- </Switch>
- </FormControl>
- )}
+ render={({ field: { value, onChange }, fieldState: { error } }) => {
+ const hasCaCert = Boolean(watch("caCert")?.length);
+ return (
+ <FormControl isError={Boolean(error?.message)} errorText={error?.message}>
+ <Switch
+ className="bg-mineshaft-400/50 shadow-inner data-[state=checked]:bg-green/80"
+ id="k8s-verify-tls-certificate"
+ thumbClassName="bg-mineshaft-800"
+ isChecked={hasCaCert ? true : value}
+ onCheckedChange={onChange}
+ isDisabled={hasCaCert}
+ >
+ <p className="w-44 text-sm font-normal text-mineshaft-400">
+ Verify TLS Certificate
+ <Tooltip
+ className="max-w-md"
+ content={
+ <div className="flex flex-col gap-2">
+ {hasCaCert ? (
+ <p>
+ Verification is always on while a CA certificate is provided. To
+ disable verification, clear the CA certificate field below.
+ </p>
+ ) : (
+ <>
+ <p>
+ When enabled, Infisical validates the Kubernetes API
+ server&apos;s TLS certificate against the CA certificate
+ provided below.
+ </p>
+ <p>
+ Leaving this disabled means any host that responds at the
+ configured Kubernetes URL will be trusted, regardless of its
+ certificate. The connection is still over HTTPS, but the API
+ server&apos;s identity is not verified. Only do this for
+ testing or if you cannot supply a CA certificate.
+ </p>
+ </>
+ )}
+ </div>
+ }
+ >
+ <FontAwesomeIcon
+ icon={faQuestionCircle}
+ size="sm"
+ className="ml-1 text-mineshaft-400"
+ />
+ </Tooltip>
+ </p>
+ </Switch>
+ </FormControl>
+ );
+ }}
/>
<Controller
control={control}
@@ -780,12 +800,25 @@ export const IdentityKubernetesAuthForm = ({
<p>
The PEM-encoded CA certificate that issued the Kubernetes API
server&apos;s TLS certificate. Required when TLS certificate
- verification is enabled.
+ verification is enabled. Providing a CA certificate forces TLS
+ verification on.
</p>
</div>
}
>
- <TextArea {...field} placeholder="-----BEGIN CERTIFICATE----- ..." />
+ <TextArea
+ {...field}
+ placeholder="-----BEGIN CERTIFICATE----- ..."
+ onChange={(e) => {
+ field.onChange(e);
+ if (e.target.value.length > 0 && !verifyTlsCertificate) {
+ setValue("verifyTlsCertificate", true, {
+ shouldDirty: true,
+ shouldValidate: true
+ });
+ }
+ }}
+ />
</FormControl>
);
}}