From 8b443e0957652d5ba05c4921bf2937b055d0034f Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Wed, 11 Jun 2025 02:51:22 +0800 Subject: [PATCH] misc: url and ssl config not needed when gateway auth --- .../dynamic-secret/providers/kubernetes.ts | 20 ++- .../dynamic-secret/providers/models.ts | 37 ++++- .../platform/dynamic-secrets/kubernetes.mdx | 65 ++++++-- frontend/src/hooks/api/dynamicSecret/types.ts | 4 +- .../KubernetesInputForm.tsx | 153 +++++++++-------- .../EditDynamicSecretKubernetesForm.tsx | 156 ++++++++++-------- 6 files changed, 263 insertions(+), 172 deletions(-) diff --git a/backend/src/ee/services/dynamic-secret/providers/kubernetes.ts b/backend/src/ee/services/dynamic-secret/providers/kubernetes.ts index 04234c1f8..41c297a61 100644 --- a/backend/src/ee/services/dynamic-secret/providers/kubernetes.ts +++ b/backend/src/ee/services/dynamic-secret/providers/kubernetes.ts @@ -20,6 +20,9 @@ import { const EXTERNAL_REQUEST_TIMEOUT = 10 * 1000; +// This value is just a placeholder. When using gateway auth method, the url is irrelevant. +const GATEWAY_AUTH_DEFAULT_URL = "https://kubernetes.default.svc.cluster.local"; + type TKubernetesProviderDTO = { gatewayService: Pick; }; @@ -37,7 +40,7 @@ const generateUsername = (usernameTemplate?: string | null) => { export const KubernetesProvider = ({ gatewayService }: TKubernetesProviderDTO): TDynamicProviderFns => { const validateProviderInputs = async (inputs: unknown) => { const providerInputs = await DynamicSecretKubernetesSchema.parseAsync(inputs); - if (!providerInputs.gatewayId) { + if (!providerInputs.gatewayId && providerInputs.url) { await blockLocalAndPrivateIpAddresses(providerInputs.url); } @@ -272,7 +275,9 @@ export const KubernetesProvider = ({ gatewayService }: TKubernetesProviderDTO): ); }; - const url = new URL(providerInputs.url); + const rawUrl = + providerInputs.authMethod === KubernetesAuthMethod.Gateway ? GATEWAY_AUTH_DEFAULT_URL : providerInputs.url || ""; + const url = new URL(rawUrl); const k8sGatewayHost = url.hostname; const k8sPort = url.port ? Number(url.port) : 443; const k8sHost = `${url.protocol}//${url.hostname}`; @@ -488,7 +493,9 @@ export const KubernetesProvider = ({ gatewayService }: TKubernetesProviderDTO): return { ...res.data, serviceAccountName: providerInputs.serviceAccountName }; }; - const url = new URL(providerInputs.url); + const rawUrl = + providerInputs.authMethod === KubernetesAuthMethod.Gateway ? GATEWAY_AUTH_DEFAULT_URL : providerInputs.url || ""; + const url = new URL(rawUrl); const k8sHost = `${url.protocol}//${url.hostname}`; const k8sGatewayHost = url.hostname; const k8sPort = url.port ? Number(url.port) : 443; @@ -611,7 +618,12 @@ export const KubernetesProvider = ({ gatewayService }: TKubernetesProviderDTO): }; if (providerInputs.credentialType === KubernetesCredentialType.Dynamic) { - const url = new URL(providerInputs.url); + const rawUrl = + providerInputs.authMethod === KubernetesAuthMethod.Gateway + ? GATEWAY_AUTH_DEFAULT_URL + : providerInputs.url || ""; + + const url = new URL(rawUrl); const k8sGatewayHost = url.hostname; const k8sPort = url.port ? Number(url.port) : 443; const k8sHost = `${url.protocol}//${url.hostname}`; diff --git a/backend/src/ee/services/dynamic-secret/providers/models.ts b/backend/src/ee/services/dynamic-secret/providers/models.ts index 100f13bb5..2c244575e 100644 --- a/backend/src/ee/services/dynamic-secret/providers/models.ts +++ b/backend/src/ee/services/dynamic-secret/providers/models.ts @@ -1,3 +1,4 @@ +import RE2 from "re2"; import { z } from "zod"; import { TDynamicSecretLeaseConfig } from "../../dynamic-secret-lease/dynamic-secret-lease-types"; @@ -325,7 +326,12 @@ export const LdapSchema = z.union([ export const DynamicSecretKubernetesSchema = z .discriminatedUnion("credentialType", [ z.object({ - url: z.string().url().trim().min(1), + url: z + .string() + .optional() + .refine((val: string | undefined) => !val || new RE2(/^https?:\/\/.+/).test(val), { + message: "Invalid URL. Must start with http:// or https:// (e.g. https://example.com)" + }), clusterToken: z.string().trim().optional(), ca: z.string().optional(), sslEnabled: z.boolean().default(false), @@ -341,7 +347,13 @@ export const DynamicSecretKubernetesSchema = z authMethod: z.nativeEnum(KubernetesAuthMethod).default(KubernetesAuthMethod.Api) }), z.object({ - url: z.string().url().trim().min(1), + url: z + .string() + .url() + .optional() + .refine((val: string | undefined) => !val || new RE2(/^https?:\/\/.+/).test(val), { + message: "Invalid URL. Must start with http:// or https:// (e.g. https://example.com)" + }), clusterToken: z.string().trim().optional(), ca: z.string().optional(), sslEnabled: z.boolean().default(false), @@ -369,12 +381,21 @@ export const DynamicSecretKubernetesSchema = z message: "When auth method is set to Gateway, a gateway must be selected" }); } - if ((data.authMethod === KubernetesAuthMethod.Api || !data.authMethod) && !data.clusterToken) { - ctx.addIssue({ - path: ["clusterToken"], - code: z.ZodIssueCode.custom, - message: "When auth method is set to Manual Token, a cluster token must be provided" - }); + if (data.authMethod === KubernetesAuthMethod.Api || !data.authMethod) { + if (!data.clusterToken) { + ctx.addIssue({ + path: ["clusterToken"], + code: z.ZodIssueCode.custom, + message: "When auth method is set to Token, a cluster token must be provided" + }); + } + if (!data.url) { + ctx.addIssue({ + path: ["url"], + code: z.ZodIssueCode.custom, + message: "When auth method is set to Token, a cluster URL must be provided" + }); + } } }); diff --git a/docs/documentation/platform/dynamic-secrets/kubernetes.mdx b/docs/documentation/platform/dynamic-secrets/kubernetes.mdx index 713aefae6..87c5b3e89 100644 --- a/docs/documentation/platform/dynamic-secrets/kubernetes.mdx +++ b/docs/documentation/platform/dynamic-secrets/kubernetes.mdx @@ -162,6 +162,12 @@ This feature is ideal for scenarios where you need to: tokens for the target service account. + + When using Gateway authentication, the Gateway will access the Kubernetes API server + using its internal cluster URL (typically https://kubernetes.default.svc) and TLS configuration. + You don't need to specify these values separately in the dynamic secret configuration. + + 1. Deploy the Infisical Gateway in your cluster 2. Set up RBAC permissions for the Gateway's service account: ```yaml rbac.yaml @@ -206,6 +212,7 @@ This feature is ideal for scenarios where you need to: - Automatically clean up service accounts after token expiration - Assign different roles to different users or applications - Maintain strict control over service account permissions + - Support multiple namespaces with a single dynamic secret configuration ### Prerequisites @@ -213,6 +220,16 @@ This feature is ideal for scenarios where you need to: - Cluster access token with permissions to create service accounts and manage RBAC - (Optional) [Gateway](/documentation/platform/gateways/overview) for private cluster access + ### Namespace Support + + When configuring a dynamic secret, you can specify multiple allowed namespaces as a comma-separated list. During lease creation, you can then specify which namespace to use from this allowed list. This provides flexibility while maintaining security by: + + - Allowing a single dynamic secret configuration to support multiple namespaces + - Restricting service account creation to only the specified allowed namespaces + - Enabling fine-grained control over which namespaces can be used for each lease + + For example, if you configure a dynamic secret with allowed namespaces "default,kube-system,monitoring", you can create leases that use any of these namespaces while preventing access to other namespaces in your cluster. + ### Authentication Setup Choose your authentication method: @@ -318,6 +335,12 @@ This feature is ideal for scenarios where you need to: manage service accounts, their tokens, and RBAC resources. + + When using Gateway authentication, the Gateway will access the Kubernetes API server + using its internal cluster URL (typically https://kubernetes.default.svc) and TLS configuration. + You don't need to specify these values separately in the dynamic secret configuration. + + 1. Deploy the Infisical Gateway in your cluster 2. Set up RBAC permissions for the Gateway's service account: ```yaml rbac.yaml @@ -401,13 +424,13 @@ This feature is ideal for scenarios where you need to: Select a gateway for private cluster access. If not specified, the Internet Gateway will be used. - Kubernetes API server URL (e.g., https://kubernetes.default.svc) + Kubernetes API server URL (e.g., https://kubernetes.default.svc). Not required when using Gateway authentication as the Gateway will use its internal cluster URL. - Whether to enable SSL verification for the Kubernetes API server connection. + Whether to enable SSL verification for the Kubernetes API server connection. Not required when using Gateway authentication as the Gateway will use its internal TLS configuration. - Custom CA certificate for the Kubernetes API server. Leave blank to use the system/public CA. + Custom CA certificate for the Kubernetes API server. Leave blank to use the system/public CA. Not required when using Gateway authentication as the Gateway will use its internal TLS configuration. Choose between Token (API) or Gateway authentication. If using Gateway, the Gateway must be deployed in your Kubernetes cluster. @@ -418,18 +441,30 @@ This feature is ideal for scenarios where you need to: Choose between Static (predefined service account) or Dynamic (temporary service accounts with role assignments) - - Name of the service account to generate tokens for (required for Static credentials) - - - Kubernetes namespace where the service account exists or will be created - - - Type of role to assign (ClusterRole or Role) (required for Dynamic credentials) - - - Name of the role to assign to the temporary service account (required for Dynamic credentials) - + + + + + Name of the service account to generate tokens for + + + Kubernetes namespace where the service account exists + + + + + + Kubernetes namespace(s) where the service accounts will be created. You can specify multiple namespaces as a comma-separated list (e.g., "default,kube-system"). During lease creation, you can specify which namespace to use from this allowed list. + + + Type of role to assign (ClusterRole or Role) + + + Name of the role to assign to the temporary service account + + + + Optional list of audiences to include in the generated token diff --git a/frontend/src/hooks/api/dynamicSecret/types.ts b/frontend/src/hooks/api/dynamicSecret/types.ts index 440c534f0..2357a83c0 100644 --- a/frontend/src/hooks/api/dynamicSecret/types.ts +++ b/frontend/src/hooks/api/dynamicSecret/types.ts @@ -290,7 +290,7 @@ export type TDynamicSecretProvider = type: DynamicSecretProviders.Kubernetes; inputs: | { - url: string; + url?: string; clusterToken?: string; ca?: string; serviceAccountName: string; @@ -302,7 +302,7 @@ export type TDynamicSecretProvider = authMethod: string; } | { - url: string; + url?: string; clusterToken?: string; ca?: string; credentialType: KubernetesDynamicSecretCredentialType.Dynamic; diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/KubernetesInputForm.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/KubernetesInputForm.tsx index 5cbfbf319..00a9f4f6f 100644 --- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/KubernetesInputForm.tsx +++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/KubernetesInputForm.tsx @@ -61,7 +61,7 @@ const formSchema = z .object({ provider: z.discriminatedUnion("credentialType", [ z.object({ - url: z.string().url().trim().min(1), + url: z.string().trim().optional(), clusterToken: z.string().trim().optional(), ca: z.string().optional(), sslEnabled: z.boolean().default(false), @@ -80,7 +80,7 @@ const formSchema = z authMethod: z.nativeEnum(AuthMethod).default(AuthMethod.Api) }), z.object({ - url: z.string().url().trim().min(1), + url: z.string().trim().optional(), clusterToken: z.string().trim().optional(), ca: z.string().optional(), sslEnabled: z.boolean().default(false), @@ -130,12 +130,21 @@ const formSchema = z message: "When auth method is set to Gateway, a gateway must be selected" }); } - if (data.provider.authMethod === AuthMethod.Api && !data.provider.clusterToken) { - ctx.addIssue({ - path: ["provider.clusterToken"], - code: z.ZodIssueCode.custom, - message: "When auth method is set to Token, a cluster token must be provided" - }); + if (data.provider.authMethod === AuthMethod.Api) { + if (!data.provider.clusterToken) { + ctx.addIssue({ + path: ["provider.clusterToken"], + code: z.ZodIssueCode.custom, + message: "When auth method is set to Token, a cluster token must be provided" + }); + } + if (!data.provider.url) { + ctx.addIssue({ + path: ["provider.url"], + code: z.ZodIssueCode.custom, + message: "When auth method is set to Token, a cluster URL must be provided" + }); + } } }); @@ -347,69 +356,6 @@ export const KubernetesInputForm = ({ )} - ( - - - - )} - /> - -
- - Enable SSL - - If enabled, you can optionally provide a custom CA certificate. Leave - blank to use the system/public CA. - - } - > - - - - ( - - )} - /> -
- - ( - -