Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat(dynamic-secret): make GCP IAM token OAuth scopes configurable
Tuan Dang1bf366688213
diff --git a/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts b/backend/src/ee/services/dynamic-secret/providers/gcp-iam.tsindex b1459aa4ef..9e3dd460ec 100644--- a/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts+++ b/backend/src/ee/services/dynamic-secret/providers/gcp-iam.ts@@ -14,7 +14,7 @@ export const GcpIamProvider = (): TDynamicProviderFns => {return providerInputs;};- const $getToken = async (serviceAccountEmail: string, ttl: number): Promise<string> => {+ const $getToken = async (serviceAccountEmail: string, ttl: number, tokenScopes: string[]): Promise<string> => {const appCfg = getConfig();if (!appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL) {throw new InternalServerError({@@ -38,7 +38,7 @@ export const GcpIamProvider = (): TDynamicProviderFns => {targetPrincipal: serviceAccountEmail,lifetime: ttl,delegates: [],- targetScopes: ["https://www.googleapis.com/auth/iam", "https://www.googleapis.com/auth/cloud-platform"]+ targetScopes: tokenScopes});let tokenResponse: GetAccessTokenResponse | undefined;@@ -67,7 +67,7 @@ export const GcpIamProvider = (): TDynamicProviderFns => {const validateConnection = async (inputs: unknown) => {const providerInputs = await validateProviderInputs(inputs);try {- await $getToken(providerInputs.serviceAccountEmail, 10);+ await $getToken(providerInputs.serviceAccountEmail, 10, providerInputs.tokenScopes);return true;} catch (err) {const sanitizedErrorMessage = sanitizeString({@@ -89,7 +89,7 @@ export const GcpIamProvider = (): TDynamicProviderFns => {const now = Math.floor(Date.now() / 1000);const ttl = Math.max(Math.floor(expireAt / 1000) - now, 0);- const token = await $getToken(providerInputs.serviceAccountEmail, ttl);+ const token = await $getToken(providerInputs.serviceAccountEmail, ttl, providerInputs.tokenScopes);const entityId = alphaNumericNanoId(32);return { entityId, data: { SERVICE_ACCOUNT_EMAIL: providerInputs.serviceAccountEmail, TOKEN: token } };diff --git a/backend/src/ee/services/dynamic-secret/providers/models.ts b/backend/src/ee/services/dynamic-secret/providers/models.tsindex ad8bfe750d..461a0091f7 100644--- a/backend/src/ee/services/dynamic-secret/providers/models.ts+++ b/backend/src/ee/services/dynamic-secret/providers/models.ts@@ -615,7 +615,12 @@ export const DynamicSecretTotpSchema = z.discriminatedUnion("configType", []);export const DynamicSecretGcpIamSchema = z.object({- serviceAccountEmail: z.string().email().trim().min(1, "Service account email required").max(128)+ serviceAccountEmail: z.string().email().trim().min(1, "Service account email required").max(128),+ tokenScopes: z+ .array(z.string().trim().min(1))+ .min(1, "At least one scope is required")+ .default(["https://www.googleapis.com/auth/iam", "https://www.googleapis.com/auth/cloud-platform"])+ .describe("OAuth scopes for the generated access token.")});export const DynamicSecretGithubSchema = z.object({diff --git a/docs/documentation/platform/dynamic-secrets/gcp-iam.mdx b/docs/documentation/platform/dynamic-secrets/gcp-iam.mdxindex 406021fa8a..8f72024f52 100644--- a/docs/documentation/platform/dynamic-secrets/gcp-iam.mdx+++ b/docs/documentation/platform/dynamic-secrets/gcp-iam.mdx@@ -6,11 +6,17 @@ description: "Learn how to dynamically generate GCP service account tokens."The Infisical GCP IAM dynamic secret allows you to generate GCP service account tokens on demand based on service account permissions.<Warning>- GCP service account access tokens cannot be revoked. As such, revoking or regenerating a token does not invalidate the old one; it remains active until it expires.+ GCP service account access tokens cannot be revoked. As such, revoking or+ regenerating a token does not invalidate the old one; it remains active until+ it expires.</Warning><Note>- You must enable the [IAM API](https://console.cloud.google.com/apis/library/iam.googleapis.com) and [IAM Credentials API](https://console.cloud.google.com/apis/library/iamcredentials.googleapis.com) in your GCP console as a prerequisite+ You must enable the [IAM+ API](https://console.cloud.google.com/apis/library/iam.googleapis.com) and+ [IAM Credentials+ API](https://console.cloud.google.com/apis/library/iamcredentials.googleapis.com)+ in your GCP console as a prerequisite</Note><Accordion title="Self-Hosted Instance">@@ -41,6 +47,7 @@ The Infisical GCP IAM dynamic secret allows you to generate GCP service account4. You can now use GCP integration with service account impersonation.</Step></Steps>+</Accordion>## Create GCP Service Account@@ -72,6 +79,7 @@ The Infisical GCP IAM dynamic secret allows you to generate GCP service account</Step>+</Steps>## Set up Dynamic Secrets with GCP IAM@@ -99,6 +107,9 @@ The Infisical GCP IAM dynamic secret allows you to generate GCP service account<ParamField path="Service Account Email" type="string" required>The email tied to the service account created in earlier steps.</ParamField>+ <ParamField path="Token Scopes" type="string[]" default="https://www.googleapis.com/auth/iam, https://www.googleapis.com/auth/cloud-platform">+ The OAuth scopes granted to the generated access token. Defaults to the `iam` and `cloud-platform` scopes. Add scopes such as `https://www.googleapis.com/auth/drive.readonly` to access Google APIs outside `cloud-platform`'s coverage.+ </ParamField></Step><Step title="Click `Submit`">After submitting the form, you will see a dynamic secret created in the dashboard.@@ -123,6 +134,7 @@ The Infisical GCP IAM dynamic secret allows you to generate GCP service accountOnce you click the `Submit` button, a new secret lease will be generated and the credentials from it will be shown to you.+</Step></Steps>@@ -141,5 +153,6 @@ To extend the life of the generated dynamic secret leases past its initial time<Warning>- Lease renewals cannot exceed the maximum TTL set when configuring the dynamic secret+ Lease renewals cannot exceed the maximum TTL set when configuring the dynamic+ secret</Warning>diff --git a/frontend/src/hooks/api/dynamicSecret/types.ts b/frontend/src/hooks/api/dynamicSecret/types.tsindex ca86764d14..2fe4b72813 100644--- a/frontend/src/hooks/api/dynamicSecret/types.ts+++ b/frontend/src/hooks/api/dynamicSecret/types.ts@@ -414,6 +414,7 @@ export type TDynamicSecretProvider =type: DynamicSecretProviders.GcpIam;inputs: {serviceAccountEmail: string;+ tokenScopes: string[];};}| {diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/GcpIamInputForm.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/GcpIamInputForm.tsxindex a0134bfde6..9a12e51a78 100644--- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/GcpIamInputForm.tsx+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/ActionBar/CreateDynamicSecretForm/GcpIamInputForm.tsx@@ -1,10 +1,12 @@-import { Controller, useForm } from "react-hook-form";+import { Controller, FieldValues, useFieldArray, useForm } from "react-hook-form";+import { faTrash } from "@fortawesome/free-solid-svg-icons";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";import { zodResolver } from "@hookform/resolvers/zod";import ms from "ms";import { z } from "zod";import { TtlFormLabel } from "@app/components/features";-import { Button, FilterableSelect, FormControl, Input } from "@app/components/v2";+import { Button, FilterableSelect, FormControl, IconButton, Input } from "@app/components/v2";import { useCreateDynamicSecret } from "@app/hooks/api";import { DynamicSecretProviders } from "@app/hooks/api/dynamicSecret/types";import { ProjectEnv } from "@app/hooks/api/types";@@ -25,7 +27,8 @@ const validateTTL = (val: string, ctx: z.RefinementCtx) => {const formSchema = z.object({provider: z.object({- serviceAccountEmail: z.string().email().trim().min(1, "Service account email required")+ serviceAccountEmail: z.string().email().trim().min(1, "Service account email required"),+ tokenScopes: z.array(z.string().trim().min(1)).min(1, "At least one scope is required")}),defaultTTL: z.string().superRefine(validateTTL),maxTTL: z@@ -41,7 +44,7 @@ const formSchema = zpath: ["maxTTL"],message: "Max TTL must be greater than or equal to Default TTL"});-type TForm = z.infer<typeof formSchema>;+type TForm = z.infer<typeof formSchema> & FieldValues;type Props = {onCompleted: () => void;@@ -67,10 +70,21 @@ export const GcpIamInputForm = ({} = useForm<TForm>({resolver: zodResolver(formSchema),defaultValues: {- environment: isSingleEnvironmentMode && environments.length > 0 ? environments[0] : undefined+ environment: isSingleEnvironmentMode && environments.length > 0 ? environments[0] : undefined,+ provider: {+ tokenScopes: [+ "https://www.googleapis.com/auth/iam",+ "https://www.googleapis.com/auth/cloud-platform"+ ]+ }}});+ const { fields, append, remove } = useFieldArray({+ control,+ name: "provider.tokenScopes"+ });+const createDynamicSecret = useCreateDynamicSecret();const handleCreateDynamicSecret = async ({@@ -187,6 +201,41 @@ export const GcpIamInputForm = ({</FormControl>)}/>++ <Controller+ control={control}+ name="provider.tokenScopes"+ render={({ fieldState: { error } }) => (+ <FormControl+ label="Token Scopes"+ isError={Boolean(error?.message)}+ errorText={error?.message}+ helperText="OAuth scopes granted to the generated access token. Scopes can only be narrowed after issuance, never widened."+ >+ <div className="space-y-2">+ {fields.map((field, index) => (+ <div key={field.id} className="flex items-center space-x-2">+ <Input+ {...control.register(`provider.tokenScopes.${index}`)}+ placeholder="https://www.googleapis.com/auth/cloud-platform"+ className="grow"+ />+ <IconButton+ onClick={() => remove(index)}+ variant="outline_bg"+ ariaLabel="Remove scope"+ >+ <FontAwesomeIcon icon={faTrash} />+ </IconButton>+ </div>+ ))}+ <Button variant="outline_bg" onClick={() => append("")} type="button">+ Add Scope+ </Button>+ </div>+ </FormControl>+ )}+ /></div>{!isSingleEnvironmentMode && (diff --git a/frontend/src/pages/secret-manager/SecretDashboardPage/components/DynamicSecretListView/EditDynamicSecretForm/EditDynamicSecretGcpIamForm.tsx b/frontend/src/pages/secret-manager/SecretDashboardPage/components/DynamicSecretListView/EditDynamicSecretForm/EditDynamicSecretGcpIamForm.tsxindex a2755e9c82..ce9a71e4aa 100644--- a/frontend/src/pages/secret-manager/SecretDashboardPage/components/DynamicSecretListView/EditDynamicSecretForm/EditDynamicSecretGcpIamForm.tsx+++ b/frontend/src/pages/secret-manager/SecretDashboardPage/components/DynamicSecretListView/EditDynamicSecretForm/EditDynamicSecretGcpIamForm.tsx@@ -1,11 +1,13 @@-import { Controller, useForm } from "react-hook-form";+import { Controller, FieldValues, useFieldArray, useForm } from "react-hook-form";+import { faTrash } from "@fortawesome/free-solid-svg-icons";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";import { zodResolver } from "@hookform/resolvers/zod";import ms from "ms";import { z } from "zod";import { TtlFormLabel } from "@app/components/features";import { createNotification } from "@app/components/notifications";-import { Button, FormControl, Input } from "@app/components/v2";+import { Button, FormControl, IconButton, Input } from "@app/components/v2";import { useUpdateDynamicSecret } from "@app/hooks/api";import { TDynamicSecret } from "@app/hooks/api/dynamicSecret/types";@@ -25,7 +27,8 @@ const validateTTL = (val: string, ctx: z.RefinementCtx) => {const formSchema = z.object({inputs: z.object({- serviceAccountEmail: z.string().email().trim().min(1, "Service account email required")+ serviceAccountEmail: z.string().email().trim().min(1, "Service account email required"),+ tokenScopes: z.array(z.string().trim().min(1)).min(1, "At least one scope is required")}),defaultTTL: z.string().superRefine(validateTTL),maxTTL: z@@ -40,7 +43,7 @@ const formSchema = zpath: ["maxTTL"],message: "Max TTL must be greater than or equal to Default TTL"});-type TForm = z.infer<typeof formSchema>;+type TForm = z.infer<typeof formSchema> & FieldValues;type Props = {onClose: () => void;@@ -67,11 +70,20 @@ export const EditDynamicSecretGcpIamForm = ({maxTTL: dynamicSecret.maxTTL,newName: dynamicSecret.name,inputs: {- ...(dynamicSecret.inputs as TForm["inputs"])+ ...(dynamicSecret.inputs as TForm["inputs"]),+ tokenScopes: (dynamicSecret.inputs as TForm["inputs"]).tokenScopes ?? [+ "https://www.googleapis.com/auth/iam",+ "https://www.googleapis.com/auth/cloud-platform"+ ]}}});+ const { fields, append, remove } = useFieldArray({+ control,+ name: "inputs.tokenScopes"+ });+const updateDynamicSecret = useUpdateDynamicSecret();const handleUpdateDynamicSecret = async ({ inputs, maxTTL, defaultTTL, newName }: TForm) => {@@ -166,6 +178,41 @@ export const EditDynamicSecretGcpIamForm = ({</FormControl>)}/>++ <Controller+ control={control}+ name="inputs.tokenScopes"+ render={({ fieldState: { error } }) => (+ <FormControl+ label="Token Scopes"+ isError={Boolean(error?.message)}+ errorText={error?.message}+ helperText="OAuth scopes granted to the generated access token. Scopes can only be narrowed after issuance, never widened."+ >+ <div className="space-y-2">+ {fields.map((field, index) => (+ <div key={field.id} className="flex items-center space-x-2">+ <Input+ {...control.register(`inputs.tokenScopes.${index}`)}+ placeholder="https://www.googleapis.com/auth/cloud-platform"+ className="grow"+ />+ <IconButton+ onClick={() => remove(index)}+ variant="outline_bg"+ ariaLabel="Remove scope"+ >+ <FontAwesomeIcon icon={faTrash} />+ </IconButton>+ </div>+ ))}+ <Button variant="outline_bg" onClick={() => append("")} type="button">+ Add Scope+ </Button>+ </div>+ </FormControl>+ )}+ /></div></div><div className="mt-4 flex items-center space-x-4">