diff --git a/backend/src/services/integration-auth/integration-sync-secret.ts b/backend/src/services/integration-auth/integration-sync-secret.ts index f18ced7e7..b571aa9fd 100644 --- a/backend/src/services/integration-auth/integration-sync-secret.ts +++ b/backend/src/services/integration-auth/integration-sync-secret.ts @@ -2772,13 +2772,23 @@ const syncSecretsAzureDevops = async ({ * Sync/push [secrets] to GitLab repo with name [integration.app] */ const syncSecretsGitLab = async ({ + createManySecretsRawFn, integrationAuth, integration, secrets, accessToken }: { + createManySecretsRawFn: (params: TCreateManySecretsRawFn) => Promise>; integrationAuth: TIntegrationAuths; - integration: TIntegrations; + integration: TIntegrations & { + projectId: string; + environment: { + id: string; + name: string; + slug: string; + }; + secretPath: string; + }; secrets: Record; accessToken: string; }) => { @@ -2835,6 +2845,82 @@ const syncSecretsGitLab = async ({ return isValid; }); + if (!integration.lastUsed) { + const secretsToAddToInfisical: { [key: string]: GitLabSecret } = {}; + const secretsToRemoveInGitlab: GitLabSecret[] = []; + + if (!metadata.initialSyncBehavior) { + metadata.initialSyncBehavior = IntegrationInitialSyncBehavior.OVERWRITE_TARGET; + } + + getSecretsRes.forEach((gitlabSecret) => { + // first time using integration + // -> apply initial sync behavior + switch (metadata.initialSyncBehavior) { + // Override all the secrets in GitLab + case IntegrationInitialSyncBehavior.OVERWRITE_TARGET: { + if (!(gitlabSecret.key in secrets)) { + secretsToRemoveInGitlab.push(gitlabSecret); + } + break; + } + case IntegrationInitialSyncBehavior.PREFER_SOURCE: { + // if the secret is not in infisical, we need to add it to infisical + if (!(gitlabSecret.key in secrets)) { + secrets[gitlabSecret.key] = { + value: gitlabSecret.value + }; + // need to remove prefix and suffix from what we're saving to Infisical + const prefix = metadata?.secretPrefix || ""; + const suffix = metadata?.secretSuffix || ""; + let processedKey = gitlabSecret.key; + + // Remove prefix if it exists at the start + if (prefix && processedKey.startsWith(prefix)) { + processedKey = processedKey.slice(prefix.length); + } + + // Remove suffix if it exists at the end + if (suffix && processedKey.endsWith(suffix)) { + processedKey = processedKey.slice(0, -suffix.length); + } + + secretsToAddToInfisical[processedKey] = gitlabSecret; + } + break; + } + default: { + throw new Error(`Invalid initial sync behavior: ${metadata.initialSyncBehavior}`); + } + } + }); + + if (Object.keys(secretsToAddToInfisical).length) { + await createManySecretsRawFn({ + projectId: integration.projectId, + environment: integration.environment.slug, + path: integration.secretPath, + secrets: Object.keys(secretsToAddToInfisical).map((key) => ({ + secretName: key, + secretValue: secretsToAddToInfisical[key].value, + type: SecretType.Shared, + secretComment: "" + })) + }); + } + + for await (const gitlabSecret of secretsToRemoveInGitlab) { + await request.delete( + `${gitLabApiUrl}/v4/projects/${integration?.appId}/variables/${gitlabSecret.key}?filter[environment_scope]=${integration.targetEnvironment}`, + { + headers: { + Authorization: `Bearer ${accessToken}` + } + } + ); + } + } + for await (const key of Object.keys(secrets)) { const existingSecret = getSecretsRes.find((s) => s.key === key); if (!existingSecret) { @@ -4554,7 +4640,8 @@ export const syncIntegrationSecrets = async ({ integrationAuth, integration, secrets, - accessToken + accessToken, + createManySecretsRawFn }); break; case Integrations.RENDER: diff --git a/frontend/src/pages/secret-manager/integrations/GitlabConfigurePage/GitlabConfigurePage.tsx b/frontend/src/pages/secret-manager/integrations/GitlabConfigurePage/GitlabConfigurePage.tsx index 632f8f25a..f09e13024 100644 --- a/frontend/src/pages/secret-manager/integrations/GitlabConfigurePage/GitlabConfigurePage.tsx +++ b/frontend/src/pages/secret-manager/integrations/GitlabConfigurePage/GitlabConfigurePage.tsx @@ -34,6 +34,7 @@ import { useGetIntegrationAuthById, useGetIntegrationAuthTeams } from "@app/hooks/api/integrationAuth"; +import { IntegrationSyncBehavior } from "@app/hooks/api/integrations/types"; const gitLabEntities = [ { name: "Individual", value: "individual" }, @@ -45,6 +46,14 @@ enum TabSections { Options = "options" } +const initialSyncBehaviors = [ + { + label: "No Import - Overwrite all values in GitLab", + value: IntegrationSyncBehavior.OVERWRITE_TARGET + }, + { label: "Import - Prefer values from Infisical", value: IntegrationSyncBehavior.PREFER_SOURCE } +]; + const schema = z.object({ targetEntity: z.enum([gitLabEntities[0].value, gitLabEntities[1].value]), targetTeamId: z.string().optional(), @@ -55,7 +64,8 @@ const schema = z.object({ secretPrefix: z.string().optional(), secretSuffix: z.string().optional(), shouldMaskSecrets: z.boolean().optional(), - shouldProtectSecrets: z.boolean() + shouldProtectSecrets: z.boolean().default(false), + initialSyncBehavior: z.nativeEnum(IntegrationSyncBehavior) }); type FormData = z.infer; @@ -74,7 +84,8 @@ export const GitlabConfigurePage = () => { secretPath: "/", secretPrefix: "", secretSuffix: "", - selectedSourceEnvironment: currentWorkspace.environments[0].slug + selectedSourceEnvironment: currentWorkspace.environments[0].slug, + initialSyncBehavior: IntegrationSyncBehavior.PREFER_SOURCE } }); const selectedSourceEnvironment = watch("selectedSourceEnvironment"); @@ -135,7 +146,8 @@ export const GitlabConfigurePage = () => { secretPrefix, secretSuffix, shouldMaskSecrets, - shouldProtectSecrets + shouldProtectSecrets, + initialSyncBehavior }: FormData) => { try { setIsLoading(true); @@ -155,7 +167,8 @@ export const GitlabConfigurePage = () => { secretPrefix, secretSuffix, shouldMaskSecrets, - shouldProtectSecrets + shouldProtectSecrets, + initialSyncBehavior } }); @@ -178,7 +191,11 @@ export const GitlabConfigurePage = () => { integrationAuthTeams ? (
{ - if (!data.secretPrefix && !data.secretSuffix) { + if ( + !data.secretPrefix && + !data.secretSuffix && + data.initialSyncBehavior === IntegrationSyncBehavior.OVERWRITE_TARGET + ) { handlePopUpOpen("confirmIntegration", data); return; } @@ -378,6 +395,36 @@ export const GitlabConfigurePage = () => { )} /> + ( + + + + )} + /> @@ -462,11 +509,6 @@ export const GitlabConfigurePage = () => { Create Integration - {/*
-
-
Pro Tip
- After creating an integration, your secrets will start syncing immediately. This might cause an unexpected override of current secrets in GitLab with secrets from Infisical. -
*/} handlePopUpToggle("confirmIntegration", isOpen)}