Skip to content

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

update gitlab connection

Matheus Nogueira81d3d39200ee
diff --git a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
index a6356612fd..68338c1e0b 100644
--- a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
+++ b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
@@ -8,6 +8,7 @@ import {
assertProviderRepositorySizeWithinLimit,
cloneRepository,
convertPatchLineToFileLineNumber,
+ isDataSourceWebhookRequired,
replaceNonChangesWithNewlines,
toFindingDetails
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-fns";
@@ -18,6 +19,7 @@ import {
TSecretScanningFactoryInitialize,
TSecretScanningFactoryListRawResources,
TSecretScanningFactoryPostInitialization,
+ TSecretScanningFactorySetupWebhook,
TSecretScanningFactoryTeardown,
TSecretScanningFactoryValidateConfigUpdate
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-types";
@@ -49,6 +51,8 @@ export const BitbucketSecretScanningFactory = () => {
TBitbucketConnection,
TBitbucketDataSourceCredentials
> = async ({ connection, payload }, callback) => {
+ if (!isDataSourceWebhookRequired(payload)) return callback({});
+
const cfg = getConfig();
const { email, apiToken } = connection.credentials;
@@ -80,6 +84,8 @@ export const BitbucketSecretScanningFactory = () => {
TBitbucketConnection,
TBitbucketDataSourceCredentials
> = async ({ dataSourceId, credentials, connection, payload }) => {
+ if (!credentials) return;
+
const { email, apiToken } = connection.credentials;
const { webhookId, webhookSecret } = credentials;
@@ -110,6 +116,8 @@ export const BitbucketSecretScanningFactory = () => {
TBitbucketDataSourceWithConnection,
TBitbucketDataSourceCredentials
> = async ({ credentials, dataSource }) => {
+ if (!credentials) return;
+
const {
connection: {
credentials: { email, apiToken }
@@ -325,6 +333,34 @@ export const BitbucketSecretScanningFactory = () => {
}));
};
+ const setupWebhook: TSecretScanningFactorySetupWebhook<
+ TBitbucketDataSourceWithConnection,
+ TBitbucketDataSourceCredentials
+ > = async ({ dataSource: { id, connection, config } }) => {
+ const { email, apiToken } = connection.credentials;
+ const authHeader = `Basic ${Buffer.from(`${email}:${apiToken}`).toString("base64")}`;
+ const webhookSecret = alphaNumericNanoId(64);
+
+ const { data } = await request.post<{ uuid: string }>(
+ `${IntegrationUrls.BITBUCKET_API_URL}/2.0/workspaces/${encodeURIComponent(config.workspaceSlug)}/hooks`,
+ {
+ description: "Infisical webhook for push events",
+ url: `${getConfig().SITE_URL}/secret-scanning/webhooks/bitbucket?dataSourceId=${id}`,
+ active: true,
+ events: ["repo:push"],
+ secret: webhookSecret
+ },
+ {
+ headers: {
+ Authorization: authHeader,
+ Accept: "application/json"
+ }
+ }
+ );
+
+ return { webhookId: data.uuid, webhookSecret };
+ };
+
const validateConfigUpdate: TSecretScanningFactoryValidateConfigUpdate<
TBitbucketDataSourceInput["config"],
TBitbucketDataSourceWithConnection
@@ -340,6 +376,7 @@ export const BitbucketSecretScanningFactory = () => {
getDiffScanResourcePayload,
getDiffScanFindingsPayload,
teardown,
+ setupWebhook,
validateConfigUpdate
};
};
diff --git a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-service.ts b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-service.ts
index 76790dd195..58979a46bf 100644
--- a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-service.ts
+++ b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-service.ts
@@ -11,7 +11,7 @@ import { TBitbucketDataSourceCredentials, TBitbucketPushEvent } from "./bitbucke
export const bitbucketSecretScanningService = (
secretScanningV2DAL: TSecretScanningV2DALFactory,
- secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "queueResourceDiffScan">,
+ secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "handleResourcePushEvent">,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
) => {
const handlePushEvent = async (
@@ -50,8 +50,7 @@ export const bitbucketSecretScanningService = (
return;
}
- const { isScanNewCommitsEnabled, encryptedCredentials, projectId } = dataSource;
- const { includeRepos } = parsedConfig.data;
+ const { encryptedCredentials, projectId } = dataSource;
if (!encryptedCredentials) {
logger.info(
@@ -87,24 +86,11 @@ export const bitbucketSecretScanningService = (
return;
}
- if (!isScanNewCommitsEnabled) {
- logger.info(
- `secretScanningV2PushEvent: Bitbucket - ignoring due to scanning new commits being disabled [dataSourceId=${dataSource.id}] [workspaceUuid=${repository.workspace.uuid}]`
- );
- return;
- }
-
- if (includeRepos.includes("*") || includeRepos.includes(repository.full_name)) {
- await secretScanningV2Queue.queueResourceDiffScan({
- dataSourceType: SecretScanningDataSource.Bitbucket,
- payload,
- dataSourceId: dataSource.id
- });
- } else {
- logger.info(
- `secretScanningV2PushEvent: Bitbucket - ignoring due to repository not being present in config [workspaceUuid=${repository.workspace.uuid}] [dataSourceId=${dataSource.id}]`
- );
- }
+ await secretScanningV2Queue.handleResourcePushEvent({
+ dataSource,
+ dataSourceType: SecretScanningDataSource.Bitbucket,
+ payload
+ });
};
return {
diff --git a/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-factory.ts b/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-factory.ts
index d10a839fef..a1748cbbad 100644
--- a/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-factory.ts
+++ b/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-factory.ts
@@ -21,6 +21,7 @@ import {
TSecretScanningFactoryInitialize,
TSecretScanningFactoryListRawResources,
TSecretScanningFactoryPostInitialization,
+ TSecretScanningFactorySetupWebhook,
TSecretScanningFactoryTeardown,
TSecretScanningFactoryValidateConfigUpdate
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-types";
@@ -68,6 +69,9 @@ export const GitHubSecretScanningFactory = () => {
// no teardown required
};
+ // Push events arrive through the GitHub App installation, so there is no webhook to create.
+ const setupWebhook: TSecretScanningFactorySetupWebhook<TGitHubDataSourceWithConnection> = async () => undefined;
+
const validateConfigUpdate: TSecretScanningFactoryValidateConfigUpdate<
TGitHubDataSourceInput["config"],
TGitHubDataSourceWithConnection
@@ -250,6 +254,7 @@ export const GitHubSecretScanningFactory = () => {
getDiffScanResourcePayload,
getDiffScanFindingsPayload,
teardown,
+ setupWebhook,
validateConfigUpdate
};
};
diff --git a/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-service.ts b/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-service.ts
index 9cd88a3a86..b029ec621d 100644
--- a/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-service.ts
+++ b/backend/src/ee/services/secret-scanning-v2/github/github-secret-scanning-service.ts
@@ -5,11 +5,9 @@ import { SecretScanningDataSource } from "@app/ee/services/secret-scanning-v2/se
import { TSecretScanningV2QueueServiceFactory } from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-queue";
import { logger } from "@app/lib/logger";
-import { GitHubDataSourceConfigSchema } from "./github-secret-scanning-schemas";
-
export const githubSecretScanningService = (
secretScanningV2DAL: TSecretScanningV2DALFactory,
- secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "queueResourceDiffScan">
+ secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "handleResourcePushEvent">
) => {
const handleInstallationDeletedEvent = async (installationId: number) => {
const dataSource = await secretScanningV2DAL.dataSources.findOne({
@@ -55,37 +53,11 @@ export const githubSecretScanningService = (
return;
}
- const parsedConfig = GitHubDataSourceConfigSchema.safeParse(dataSource.config);
-
- if (!parsedConfig.success) {
- logger.error(
- parsedConfig.error,
- `secretScanningV2PushEvent: GitHub - Invalid data source config [dataSourceId=${dataSource.id}] [installationId=${installation.id}]`
- );
- return;
- }
-
- const { isScanNewCommitsEnabled } = dataSource;
- const { includeRepos } = parsedConfig.data;
-
- if (!isScanNewCommitsEnabled) {
- logger.info(
- `secretScanningV2PushEvent: GitHub - ignoring due to scanning new commits being disabled [dataSourceId=${dataSource.id}] [installationId=${installation.id}]`
- );
- return;
- }
-
- if (includeRepos.includes("*") || includeRepos.includes(repository.full_name)) {
- await secretScanningV2Queue.queueResourceDiffScan({
- dataSourceType: SecretScanningDataSource.GitHub,
- payload,
- dataSourceId: dataSource.id
- });
- } else {
- logger.info(
- `secretScanningV2PushEvent: GitHub - ignoring due to repository not being present in config [installationId=${installation.id}] [dataSourceId=${dataSource.id}]`
- );
- }
+ await secretScanningV2Queue.handleResourcePushEvent({
+ dataSource,
+ dataSourceType: SecretScanningDataSource.GitHub,
+ payload
+ });
};
return {
diff --git a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-enums.ts b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-enums.ts
index 89caff8026..502e67395e 100644
--- a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-enums.ts
+++ b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-enums.ts
@@ -1,4 +1,6 @@
export enum GitLabDataSourceScope {
+ // Every project the connection can read, with no group webhook to discover new ones.
+ Account = "account",
Project = "project",
Group = "group"
}
diff --git a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-factory.ts b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-factory.ts
index 887831c802..5f9d071830 100644
--- a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-factory.ts
+++ b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-factory.ts
@@ -8,6 +8,7 @@ import {
assertProviderRepositorySizeWithinLimit,
cloneRepository,
convertPatchLineToFileLineNumber,
+ isDataSourceWebhookRequired,
replaceNonChangesWithNewlines,
toFindingDetails
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-fns";
@@ -19,6 +20,7 @@ import {
TSecretScanningFactoryListRawResources,
TSecretScanningFactoryParams,
TSecretScanningFactoryPostInitialization,
+ TSecretScanningFactorySetupWebhook,
TSecretScanningFactoryTeardown,
TSecretScanningFactoryValidateConfigUpdate
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-types";
@@ -41,77 +43,56 @@ import {
} from "./gitlab-secret-scanning-types";
export const GitLabSecretScanningFactory = ({ appConnectionDAL, kmsService }: TSecretScanningFactoryParams) => {
- const initialize: TSecretScanningFactoryInitialize<
- TGitLabDataSourceInput,
- TGitLabConnection,
- TGitLabDataSourceCredentials
- > = async ({ payload: { config, name }, connection }, callback) => {
- const token = alphaNumericNanoId(64);
+ type TGitLabClient = Awaited<ReturnType<typeof getGitLabConnectionClient>>;
+ type THookableConfig = Exclude<TGitLabDataSourceInput["config"], { scope: GitLabDataSourceScope.Account }>;
- const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
- const appCfg = getConfig();
+ const getHookUrl = () => `${getConfig().SITE_URL}/secret-scanning/webhooks/gitlab`;
- if (config.scope === GitLabDataSourceScope.Project) {
- const { projectId } = config;
- const project = await client.Projects.show(projectId);
-
- if (!project) {
- throw new BadRequestError({ message: `Could not find project with ID ${projectId}.` });
+ const removeHook = async (client: TGitLabClient, config: THookableConfig, hookId: number) => {
+ try {
+ if (config.scope === GitLabDataSourceScope.Project) {
+ await client.ProjectHooks.remove(config.projectId, hookId);
+ } else {
+ await client.GroupHooks.remove(config.groupId, hookId);
}
+ } catch {
+ // do nothing, just try to clean up webhook
+ }
+ };
+
+ const createHook = async (client: TGitLabClient, config: THookableConfig, token: string, name: string) => {
+ try {
+ if (config.scope === GitLabDataSourceScope.Project) {
+ const project = await client.Projects.show(config.projectId);
+
+ if (!project) {
+ throw new BadRequestError({ message: `Could not find project with ID ${config.projectId}.` });
+ }
- let hook: Camelize<ProjectHookSchema>;
- try {
- hook = await client.ProjectHooks.add(projectId, `${appCfg.SITE_URL}/secret-scanning/webhooks/gitlab`, {
+ const hook: Camelize<ProjectHookSchema> = await client.ProjectHooks.add(config.projectId, getHookUrl(), {
token,
pushEvents: true,
enableSslVerification: true,
// @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
name: `Infisical Secret Scanning - ${name}`
});
- } catch (error) {
- if (error instanceof GitbeakerRequestError) {
- throw new BadRequestError({ message: `${error.message}: ${error.cause?.description ?? "Unknown Error"}` });
- }
-
- throw error;
+ return hook.id;
}
- try {
- return await callback({
- credentials: {
- token,
- hookId: hook.id
- }
- });
- } catch (error) {
- try {
- await client.ProjectHooks.remove(projectId, hook.id);
- } catch {
- // do nothing, just try to clean up webhook
- }
+ const group = await client.Groups.show(config.groupId);
- throw error;
+ if (!group) {
+ throw new BadRequestError({ message: `Could not find group with ID ${config.groupId}.` });
}
- }
-
- // group scope
- const { groupId } = config;
- const group = await client.Groups.show(groupId);
-
- if (!group) {
- throw new BadRequestError({ message: `Could not find group with ID ${groupId}.` });
- }
-
- let hook: Camelize<GroupHookSchema>;
- try {
- hook = await client.GroupHooks.add(groupId, `${appCfg.SITE_URL}/secret-scanning/webhooks/gitlab`, {
+ const hook: Camelize<GroupHookSchema> = await client.GroupHooks.add(config.groupId, getHookUrl(), {
token,
pushEvents: true,
enableSslVerification: true,
// @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
name: `Infisical Secret Scanning - ${name}`
});
+ return hook.id;
} catch (error) {
if (error instanceof GitbeakerRequestError) {
throw new BadRequestError({ message: `${error.message}: ${error.cause?.description ?? "Unknown Error"}` });
@@ -119,78 +100,83 @@ export const GitLabSecretScanningFactory = ({ appConnectionDAL, kmsService }: TS
throw error;
}
+ };
+ // The data source ID only exists after the row is written, so the hook is pointed at it in a second call.
+ const bindHookToDataSource = async (
+ client: TGitLabClient,
+ config: THookableConfig,
+ hookId: number,
+ dataSourceId: string
+ ) => {
try {
- return await callback({
- credentials: {
- token,
- hookId: hook.id
- }
- });
- } catch (error) {
- try {
- await client.GroupHooks.remove(groupId, hook.id);
- } catch {
- // do nothing, just try to clean up webhook
+ if (config.scope === GitLabDataSourceScope.Project) {
+ await client.ProjectHooks.edit(config.projectId, hookId, getHookUrl(), {
+ // @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
+ name: `Infisical Secret Scanning - ${dataSourceId}`,
+ custom_headers: [{ key: "x-data-source-id", value: dataSourceId }]
+ });
+ } else {
+ await client.GroupHooks.edit(config.groupId, hookId, getHookUrl(), {
+ // @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
+ name: `Infisical Secret Scanning - ${dataSourceId}`,
+ custom_headers: [{ key: "x-data-source-id", value: dataSourceId }]
+ });
}
-
+ } catch (error) {
+ await removeHook(client, config, hookId);
throw error;
}
};
- const postInitialization: TSecretScanningFactoryPostInitialization<
+ const initialize: TSecretScanningFactoryInitialize<
TGitLabDataSourceInput,
TGitLabConnection,
TGitLabDataSourceCredentials
- > = async ({ connection, dataSourceId, credentials, payload: { config } }) => {
- const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
- const appCfg = getConfig();
-
- const hookUrl = `${appCfg.SITE_URL}/secret-scanning/webhooks/gitlab`;
- const { hookId } = credentials;
+ > = async ({ payload, connection }, callback) => {
+ const { config, name } = payload;
- if (config.scope === GitLabDataSourceScope.Project) {
- const { projectId } = config;
-
- try {
- await client.ProjectHooks.edit(projectId, hookId, hookUrl, {
- // @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
- name: `Infisical Secret Scanning - ${dataSourceId}`,
- custom_headers: [{ key: "x-data-source-id", value: dataSourceId }]
- });
- } catch (error) {
- try {
- await client.ProjectHooks.remove(projectId, hookId);
- } catch {
- // do nothing, just try to clean up webhook
- }
-
- throw error;
- }
-
- return;
+ if (config.scope === GitLabDataSourceScope.Account || !isDataSourceWebhookRequired(payload)) {
+ return callback({});
}
- // group-scope
- const { groupId } = config;
+ const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
+ const token = alphaNumericNanoId(64);
+ const hookId = await createHook(client, config, token, name);
try {
- await client.GroupHooks.edit(groupId, hookId, hookUrl, {
- // @ts-expect-error gitbeaker is outdated, and the types don't support this field yet
- name: `Infisical Secret Scanning - ${dataSourceId}`,
- custom_headers: [{ key: "x-data-source-id", value: dataSourceId }]
- });
+ return await callback({ credentials: { token, hookId } });
} catch (error) {
- try {
- await client.GroupHooks.remove(groupId, hookId);
- } catch {
- // do nothing, just try to clean up webhook
- }
-
+ await removeHook(client, config, hookId);
throw error;
}
};
+ const postInitialization: TSecretScanningFactoryPostInitialization<
+ TGitLabDataSourceInput,
+ TGitLabConnection,
+ TGitLabDataSourceCredentials
+ > = async ({ connection, dataSourceId, credentials, payload: { config } }) => {
+ if (!credentials || config.scope === GitLabDataSourceScope.Account) return;
+
+ const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
+ await bindHookToDataSource(client, config, credentials.hookId, dataSourceId);
+ };
+
+ const setupWebhook: TSecretScanningFactorySetupWebhook<
+ TGitLabDataSourceWithConnection,
+ TGitLabDataSourceCredentials
+ > = async ({ dataSource: { id, name, connection, config } }) => {
+ if (config.scope === GitLabDataSourceScope.Account) return undefined;
+
+ const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
+ const token = alphaNumericNanoId(64);
+ const hookId = await createHook(client, config, token, name);
+ await bindHookToDataSource(client, config, hookId, id);
+
+ return { token, hookId };
+ };
+
const listRawResources: TSecretScanningFactoryListRawResources<TGitLabDataSourceWithConnection> = async (
dataSource
) => {
@@ -216,25 +202,21 @@ export const GitLabSecretScanningFactory = ({ appConnectionDAL, kmsService }: TS
];
}
- // group-scope
-
- const { groupId, includeProjects } = config;
-
- const projects = await client.Groups.allProjects(groupId, {
- archived: false
- });
+ const { includeProjects } = config;
- const filteredProjects: typeof projects = [];
- if (!includeProjects || includeProjects.includes("*")) {
- filteredProjects.push(...projects);
- } else {
- filteredProjects.push(...projects.filter((project) => includeProjects.includes(project.pathWithNamespace)));
- }
+ const projects =
+ config.scope === GitLabDataSourceScope.Account
+ ? await client.Projects.all({ membership: true, archived: false })
+ : await client.Groups.allProjects(config.groupId, { archived: false });
- return filteredProjects.map(({ id, pathWithNamespace }) => ({
+ const resources = projects.map(({ id, pathWithNamespace }) => ({
name: pathWithNamespace,
externalId: id.toString()
}));
+
+ if (!includeProjects || includeProjects.includes("*")) return resources;
+
+ return resources.filter((resource) => includeProjects.includes(resource.name));
};
const getFullScanPath: TSecretScanningFactoryGetFullScanPath<TGitLabDataSourceWithConnection> = async ({
@@ -277,25 +259,11 @@ export const GitLabSecretScanningFactory = ({ appConnectionDAL, kmsService }: TS
const teardown: TSecretScanningFactoryTeardown<
TGitLabDataSourceWithConnection,
TGitLabDataSourceCredentials
- > = async ({ dataSource: { connection, config }, credentials: { hookId } }) => {
- const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
-
- if (config.scope === GitLabDataSourceScope.Project) {
- const { projectId } = config;
- try {
- await client.ProjectHooks.remove(projectId, hookId);
- } catch (error) {
- // do nothing, just try to clean up webhook
- }
- return;
- }
+ > = async ({ dataSource: { connection, config }, credentials }) => {
+ if (!credentials || config.scope === GitLabDataSourceScope.Account) return;
- const { groupId } = config;
- try {
- await client.GroupHooks.remove(groupId, hookId);
- } catch (error) {
- // do nothing, just try to clean up webhook
- }
+ const client = await getGitLabConnectionClient(connection, appConnectionDAL, kmsService);
+ await removeHook(client, config, credentials.hookId);
};
const getDiffScanResourcePayload: TSecretScanningFactoryGetDiffScanResourcePayload<
@@ -391,6 +359,7 @@ export const GitLabSecretScanningFactory = ({ appConnectionDAL, kmsService }: TS
initialize,
postInitialization,
teardown,
+ setupWebhook,
getDiffScanResourcePayload,
getDiffScanFindingsPayload,
validateConfigUpdate
diff --git a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-schemas.ts b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-schemas.ts
index fde9c1917b..b5ea89ed6e 100644
--- a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-schemas.ts
+++ b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-schemas.ts
@@ -13,22 +13,28 @@ import { SecretScanningDataSources } from "@app/lib/api-docs";
import { GitLabProjectRegex } from "@app/lib/regex";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
+const GitLabIncludeProjectsSchema = z
+ .array(
+ z
+ .string()
+ .min(1)
+ .max(256)
+ .refine((value) => value === "*" || GitLabProjectRegex.test(value), "Invalid project name format")
+ )
+ .max(100, "Cannot configure more than 100 projects")
+ .default(["*"])
+ .describe(SecretScanningDataSources.CONFIG.GITLAB.includeProjects);
+
export const GitLabDataSourceConfigSchema = z.discriminatedUnion("scope", [
z.object({
scope: z.literal(GitLabDataSourceScope.Group).describe(SecretScanningDataSources.CONFIG.GITLAB.scope),
groupId: z.number().describe(SecretScanningDataSources.CONFIG.GITLAB.groupId),
groupName: z.string().trim().max(256).optional().describe(SecretScanningDataSources.CONFIG.GITLAB.groupName),
- includeProjects: z
- .array(
- z
- .string()
- .min(1)
- .max(256)
- .refine((value) => value === "*" || GitLabProjectRegex.test(value), "Invalid project name format")
- )
- .max(100, "Cannot configure more than 100 projects")
- .default(["*"])
- .describe(SecretScanningDataSources.CONFIG.GITLAB.includeProjects)
+ includeProjects: GitLabIncludeProjectsSchema
+ }),
+ z.object({
+ scope: z.literal(GitLabDataSourceScope.Account).describe(SecretScanningDataSources.CONFIG.GITLAB.scope),
+ includeProjects: GitLabIncludeProjectsSchema
}),
z.object({
scope: z.literal(GitLabDataSourceScope.Project).describe(SecretScanningDataSources.CONFIG.GITLAB.scope),
diff --git a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-service.ts b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-service.ts
index 295513ffa6..b4e2e13654 100644
--- a/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-service.ts
+++ b/backend/src/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-service.ts
@@ -1,4 +1,3 @@
-import { GitLabDataSourceScope } from "@app/ee/services/secret-scanning-v2/gitlab/gitlab-secret-scanning-enums";
import { TSecretScanningV2DALFactory } from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-dal";
import { SecretScanningDataSource } from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-enums";
import { TSecretScanningV2QueueServiceFactory } from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-queue";
@@ -12,7 +11,7 @@ import { TGitLabDataSourceCredentials, THandleGitLabPushEvent } from "./gitlab-s
export const gitlabSecretScanningService = (
secretScanningV2DAL: TSecretScanningV2DALFactory,
- secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "queueResourceDiffScan">,
+ secretScanningV2Queue: Pick<TSecretScanningV2QueueServiceFactory, "handleResourcePushEvent">,
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
) => {
const handlePushEvent = async ({ payload, token, dataSourceId }: THandleGitLabPushEvent) => {
@@ -47,8 +46,7 @@ export const gitlabSecretScanningService = (
return;
}
- const { isScanNewCommitsEnabled, encryptedCredentials, projectId } = dataSource;
- const config = parsedConfig.data;
+ const { encryptedCredentials, projectId } = dataSource;
if (!encryptedCredentials) {
logger.info(
@@ -79,28 +77,11 @@ export const gitlabSecretScanningService = (
return;
}
- if (!isScanNewCommitsEnabled) {
- logger.info(
- `secretScanningV2PushEvent: GitLab - ignoring due to scanning new commits being disabled [dataSourceId=${dataSource.id}] [projectId=${payload.project.id}]`
- );
- return;
- }
-
- if (
- config.scope === GitLabDataSourceScope.Project
- ? config.projectId.toString() === payload.project_id.toString()
- : config.includeProjects.includes("*") || config.includeProjects.includes(payload.project.path_with_namespace)
- ) {
- await secretScanningV2Queue.queueResourceDiffScan({
- dataSourceType: SecretScanningDataSource.GitLab,
- payload,
- dataSourceId: dataSource.id
- });
- } else {
- logger.info(
- `secretScanningV2PushEvent: GitLab - ignoring due to repository not being present in config [dataSourceId=${dataSource.id}] [projectId=${payload.project.id}]`
- );
- }
+ await secretScanningV2Queue.handleResourcePushEvent({
+ dataSource,
+ dataSourceType: SecretScanningDataSource.GitLab,
+ payload
+ });
};
return {
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
index 3dfa4fdf8b..363e88139d 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
@@ -59,11 +59,19 @@ export const doesDataSourceIncludeResources = (dataSource: TSecretScanningDataSo
}
};
-// GitLab discovery relies on group-level webhooks, so a project-scoped data source cannot discover new projects.
+// GitLab only reports new projects through group webhooks, so discovery needs a group. Self-hosted system
+// hooks would also work, but are not supported.
export const doesDataSourceSupportDiscovery = ({ type, config }: { type: string; config?: unknown }) =>
- !(
- type === SecretScanningDataSource.GitLab && (config as { scope?: string })?.scope === GitLabDataSourceScope.Project
- );
+ type !== SecretScanningDataSource.GitLab || (config as { scope?: string })?.scope === GitLabDataSourceScope.Group;
+
+// Push events drive both discovery and new commit scans, so with both off there is nothing to listen for.
+export const isDataSourceWebhookRequired = ({
+ isDiscoveryEnabled,
+ isScanNewCommitsEnabled
+}: {
+ isDiscoveryEnabled?: boolean | null;
+ isScanNewCommitsEnabled?: boolean | null;
+}) => Boolean(isDiscoveryEnabled || isScanNewCommitsEnabled);
// The scanner exits 77 when it wrote findings; that is a successful scan, not a failure.
const SCAN_FINDINGS_EXIT_CODE = 77;
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
index c8102c26f6..88064b14b9 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-queue.ts
@@ -1,6 +1,11 @@
import { join } from "path";
-import { ProjectMembershipRole, TSecretScanningFindings } from "@app/db/schemas";
+import {
+ ProjectMembershipRole,
+ TSecretScanningDataSources,
+ TSecretScanningFindings,
+ TSecretScanningResourcesInsert
+} from "@app/db/schemas";
import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";
import {
createTempFolder,
@@ -1060,8 +1065,110 @@ export const secretScanningV2QueueServiceFactory = ({
}
});
+ const queueResourceFullScan = async ({
+ dataSourceId,
+ resourcePayload
+ }: {
+ dataSourceId: string;
+ resourcePayload: Pick<TSecretScanningResourcesInsert, "externalId" | "name">;
+ }) => {
+ try {
+ const scan = await secretScanningV2DAL.resources.transaction(async (tx) => {
+ const [resource] = await secretScanningV2DAL.resources.upsert(
+ [{ ...resourcePayload, dataSourceId }],
+ ["externalId", "dataSourceId"],
+ tx
+ );
+
+ return secretScanningV2DAL.scans.create(
+ { resourceId: resource.id, type: SecretScanningScanType.Historical },
+ tx
+ );
+ });
+
+ await queueService.queue(
+ QueueName.SecretScanningV2FullScan,
+ QueueJobs.SecretScanningV2FullScan,
+ { scanId: scan.id, resourceId: scan.resourceId, dataSourceId },
+ {
+ jobId: scan.id,
+ removeOnFail: true,
+ attempts: FULL_SCAN_ATTEMPTS,
+ backoff: { type: "fixed", delay: FULL_SCAN_RETRY_DELAY }
+ }
+ );
+ } catch (error) {
+ logger.error(
+ error,
+ `secretScanningV2Queue: Failed to queue full scan for discovered resource [dataSourceId=${dataSourceId}] [resourceExternalId=${resourcePayload.externalId}]`
+ );
+ }
+ };
+
+ // A push from a repository with no resource row means it was added after setup, so discovery decides
+ // whether it joins the data source. Pushes to known repositories are scanned only when new commits are.
+ const handleResourcePushEvent = async ({
+ dataSource,
+ dataSourceType,
+ payload
+ }: {
+ dataSource: Pick<
+ TSecretScanningDataSources,
+ "id" | "isDiscoveryEnabled" | "isScanNewCommitsEnabled" | "isHistoricalScanEnabled"
+ >;
+ dataSourceType: SecretScanningDataSource;
+ payload: TQueueSecretScanningResourceDiffScanPayload["payload"];
+ }) => {
+ const factory = SECRET_SCANNING_FACTORY_MAP[dataSourceType]({
+ kmsService,
+ appConnectionDAL
+ });
+
+ const resourcePayload = factory.getDiffScanResourcePayload(payload);
+ const logDetails = `[dataSourceId=${dataSource.id}] [resourceExternalId=${resourcePayload.externalId}]`;
+
+ const existingResource = await secretScanningV2DAL.resources.findOne({
+ dataSourceId: dataSource.id,
+ externalId: resourcePayload.externalId
+ });
+
+ if (existingResource) {
+ if (!dataSource.isScanNewCommitsEnabled) {
+ logger.info(`secretScanningV2PushEvent: ignoring push, scanning new commits is disabled ${logDetails}`);
+ return;
+ }
+
+ await queueResourceDiffScan({ dataSourceType, payload, dataSourceId: dataSource.id });
+ return;
+ }
+
+ if (!dataSource.isDiscoveryEnabled) {
+ logger.info(
+ `secretScanningV2PushEvent: ignoring push from unknown resource, discovery is disabled ${logDetails}`
+ );
+ return;
+ }
+
+ // The historical scan covers the pushed commits too, so it replaces the diff scan.
+ if (dataSource.isHistoricalScanEnabled) {
+ await queueResourceFullScan({ dataSourceId: dataSource.id, resourcePayload });
+ return;
+ }
+
+ if (dataSource.isScanNewCommitsEnabled) {
+ await queueResourceDiffScan({ dataSourceType, payload, dataSourceId: dataSource.id });
+ return;
+ }
+
+ await secretScanningV2DAL.resources.upsert(
+ [{ ...resourcePayload, dataSourceId: dataSource.id }],
+ ["externalId", "dataSourceId"]
+ );
+ };
+
return {
queueDataSourceFullScan,
- queueResourceDiffScan
+ queueResourceDiffScan,
+ handleResourcePushEvent
};
};
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-service.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-service.ts
index 3fd56b9959..2c4d042843 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-service.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-service.ts
@@ -22,6 +22,7 @@ import { SECRET_SCANNING_FACTORY_MAP } from "@app/ee/services/secret-scanning-v2
import {
doesDataSourceIncludeResources,
doesDataSourceSupportDiscovery,
+ isDataSourceWebhookRequired,
listSecretScanningDataSourceOptions
} from "@app/ee/services/secret-scanning-v2/secret-scanning-v2-fns";
import {
@@ -443,11 +444,49 @@ export const secretScanningV2ServiceFactory = ({
});
}
+ const dataSourceWithConnection = { ...dataSource, connection } as TSecretScanningDataSourceWithConnection;
+ const { encryptor, decryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId: dataSource.projectId
+ });
+
+ const isWebhookRequired = isDataSourceWebhookRequired({
+ isDiscoveryEnabled: payload.isDiscoveryEnabled ?? dataSource.isDiscoveryEnabled,
+ isScanNewCommitsEnabled: payload.isScanNewCommitsEnabled ?? dataSource.isScanNewCommitsEnabled
+ });
+
+ // Stored credentials are what tells us a webhook exists, so they are added or cleared alongside it.
+ let createdCredentials: TSecretScanningDataSourceCredentials;
+ let encryptedCredentials: Buffer | null | undefined;
+
+ if (isWebhookRequired && !dataSource.encryptedCredentials) {
+ createdCredentials = await factory.setupWebhook({ dataSource: dataSourceWithConnection });
+
+ if (createdCredentials) {
+ encryptedCredentials = encryptor({ plainText: Buffer.from(JSON.stringify(createdCredentials)) }).cipherTextBlob;
+ }
+ } else if (!isWebhookRequired && dataSource.encryptedCredentials) {
+ await factory.teardown({
+ dataSource: dataSourceWithConnection,
+ credentials: JSON.parse(
+ decryptor({ cipherTextBlob: dataSource.encryptedCredentials }).toString()
+ ) as TSecretScanningDataSourceCredentials
+ });
+ encryptedCredentials = null;
+ }
+
try {
- const updatedDataSource = await secretScanningV2DAL.dataSources.updateById(dataSourceId, payload);
+ const updatedDataSource = await secretScanningV2DAL.dataSources.updateById(dataSourceId, {
+ ...payload,
+ ...(encryptedCredentials !== undefined && { encryptedCredentials })
+ });
return updatedDataSource as TSecretScanningDataSource;
} catch (err) {
+ if (createdCredentials) {
+ await factory.teardown({ dataSource: dataSourceWithConnection, credentials: createdCredentials });
+ }
+
if (err instanceof DatabaseError && (err.error as { code: string })?.code === DatabaseErrorCode.UniqueViolation) {
throw new BadRequestError({
message: `A Secret Scanning Data Source with the name "${payload.name}" already exists for the project with ID "${dataSource.projectId}"`
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-types.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-types.ts
index 90e5207613..f08a539cd9 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-types.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-types.ts
@@ -215,12 +215,18 @@ export type TSecretScanningFactoryPostInitialization<
P extends TSecretScanningDataSourceInput,
T extends TSecretScanningDataSourceWithConnection["connection"] | undefined = undefined,
C extends TSecretScanningDataSourceCredentials = undefined
-> = (params: { payload: P; connection: T; credentials: C; dataSourceId: string }) => Promise<void>;
+> = (params: { payload: P; connection: T; credentials?: C; dataSourceId: string }) => Promise<void>;
export type TSecretScanningFactoryTeardown<
T extends TSecretScanningDataSourceWithConnection,
C extends TSecretScanningDataSourceCredentials = undefined
-> = (params: { dataSource: T; credentials: C }) => Promise<void>;
+> = (params: { dataSource: T; credentials?: C }) => Promise<void>;
+
+// Creates the push webhook for an existing data source; returns undefined when the provider or config has none.
+export type TSecretScanningFactorySetupWebhook<
+ T extends TSecretScanningDataSourceWithConnection,
+ C extends TSecretScanningDataSourceCredentials = undefined
+> = (params: { dataSource: T }) => Promise<C | undefined>;
export type TSecretScanningFactoryParams = {
appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">;
@@ -238,6 +244,7 @@ export type TSecretScanningFactory<
initialize: TSecretScanningFactoryInitialize<I, T["connection"] | undefined, C>;
postInitialization: TSecretScanningFactoryPostInitialization<I, T["connection"] | undefined, C>;
teardown: TSecretScanningFactoryTeardown<T, C>;
+ setupWebhook: TSecretScanningFactorySetupWebhook<T, C>;
validateConfigUpdate: TSecretScanningFactoryValidateConfigUpdate<I["config"], T>;
getDiffScanResourcePayload: TSecretScanningFactoryGetDiffScanResourcePayload<P>;
getDiffScanFindingsPayload: TSecretScanningFactoryGetDiffScanFindingsPayload<T, P>;
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index ff76b3cbe0..99b47f0015 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -4002,7 +4002,8 @@ export const SecretScanningDataSources = {
GITLAB: {
includeProjects:
'The projects to include when scanning. Use ["*"] for all projects or an empty list for none. Defaults to all projects (["*"]).',
- scope: "The GitLab scope scanning should occur at (project or group level).",
+ scope:
+ "The GitLab scope scanning should occur at: account (every project the connection can read), group, or project (deprecated, kept for existing Data Sources).",
projectId: "The ID of the project to scan.",
projectName: "The name of the project to scan.",
groupId: "The ID of the group to scan projects from.",
diff --git a/frontend/src/components/secret-scanning/forms/CreateSecretScanningDataSourceForm.tsx b/frontend/src/components/secret-scanning/forms/CreateSecretScanningDataSourceForm.tsx
index d5bf88a390..bf3bab1938 100644
--- a/frontend/src/components/secret-scanning/forms/CreateSecretScanningDataSourceForm.tsx
+++ b/frontend/src/components/secret-scanning/forms/CreateSecretScanningDataSourceForm.tsx
@@ -55,7 +55,7 @@ const getDefaultConfig = (type: SecretScanningDataSource) => {
case SecretScanningDataSource.Bitbucket:
return { workspaceSlug: "", includeRepos: [] };
case SecretScanningDataSource.GitLab:
- return { scope: GitLabDataSourceScope.Project };
+ return { scope: GitLabDataSourceScope.Account, includeProjects: [] };
default:
throw new Error(`Unhandled data source type: ${type}`);
}
@@ -87,7 +87,7 @@ const CONNECTION_STEP_INFO: Record<SecretScanningDataSource, string> = {
[SecretScanningDataSource.Bitbucket]:
"The App Connection is how Secret Scanning reads your Bitbucket repositories, and the workspace tells it where to look. The connection cannot be changed after the data source is created.",
[SecretScanningDataSource.GitLab]:
- "The App Connection is how Secret Scanning reads your GitLab projects. Group scope relies on group-level webhooks, so it is the only scope that picks up projects added later. Connection and scope cannot be changed after the data source is created."
+ "The App Connection is how Secret Scanning reads your GitLab projects. Without a group, the data source covers every project the connection can read. GitLab only reports new projects through group webhooks, so a group is required to discover them. Connection and group cannot be changed after the data source is created."
};
const getSteps = (type: SecretScanningDataSource) => {
diff --git a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/GitLabDataSourceConfigFields.tsx b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/GitLabDataSourceConfigFields.tsx
index badf366d0a..7edb0d14c5 100644
--- a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/GitLabDataSourceConfigFields.tsx
+++ b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/GitLabDataSourceConfigFields.tsx
@@ -8,19 +8,10 @@ import {
FieldDescription,
FieldError,
FieldLabel,
- FieldLegend,
- FieldSet,
- FieldTitle,
- RadioGroup,
- RadioGroupItem
+ Input
} from "@app/components/v3";
import { useDebounce } from "@app/hooks";
-import {
- TGitLabGroup,
- TGitLabProject,
- useGitLabConnectionListGroups,
- useGitLabConnectionListProjects
-} from "@app/hooks/api/appConnections/gitlab";
+import { TGitLabGroup, useGitLabConnectionListGroups } from "@app/hooks/api/appConnections/gitlab";
import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { GitLabDataSourceScope } from "@app/hooks/api/secretScanningV2/types/gitlab-data-source";
@@ -29,23 +20,6 @@ import { SecretScanningDataSourceConnectionField } from "../SecretScanningDataSo
const GITLAB_DATA_SOURCE_LIST_LIMIT = 20;
-const SEARCH_HELP =
- "Only the first results are shown. Search by name to find more. The connection must have access to it.";
-
-const SCOPE_OPTIONS = [
- {
- scope: GitLabDataSourceScope.Project,
- title: "Project",
- description: "Scan a single GitLab project."
- },
- {
- scope: GitLabDataSourceScope.Group,
- title: "Group",
- description:
- "Scan projects in a GitLab group. Required to scan projects added to the group later."
- }
-];
-
type TGitLabForm = TSecretScanningDataSourceForm & { type: SecretScanningDataSource.GitLab };
export const GitLabDataSourceConnectionFields = () => {
@@ -56,26 +30,17 @@ export const GitLabDataSourceConnectionFields = () => {
const scope = useWatch({ control, name: "config.scope" });
const groupId = useWatch({ control, name: "config.groupId" });
const groupName = useWatch({ control, name: "config.groupName" });
- const projectId = useWatch({ control, name: "config.projectId" });
const projectName = useWatch({ control, name: "config.projectName" });
+ const projectId = useWatch({ control, name: "config.projectId" });
- const [projectSearch, setProjectSearch] = useState("");
- const [debouncedProjectSearch] = useDebounce(projectSearch, 300);
const [groupSearch, setGroupSearch] = useState("");
const [debouncedGroupSearch] = useDebounce(groupSearch, 300);
- const { data: projects, isPending: isProjectsPending } = useGitLabConnectionListProjects(
- connectionId,
- debouncedProjectSearch || undefined,
- GITLAB_DATA_SOURCE_LIST_LIMIT,
- { enabled: Boolean(connectionId) && scope === GitLabDataSourceScope.Project }
- );
-
const { data: groups, isPending: isGroupsPending } = useGitLabConnectionListGroups(
connectionId,
debouncedGroupSearch || undefined,
GITLAB_DATA_SOURCE_LIST_LIMIT,
- { enabled: Boolean(connectionId) && scope === GitLabDataSourceScope.Group }
+ { enabled: Boolean(connectionId) && !isUpdate }
);
const groupOptions = useMemo(() => {
@@ -89,124 +54,51 @@ export const GitLabDataSourceConnectionFields = () => {
return results;
}, [groups, groupId, groupName]);
- const projectOptions = useMemo(() => {
- const results = projects ?? [];
- if (projectId && projectName && !results.some((project) => Number(project.id) === projectId)) {
- return [{ id: String(projectId), name: projectName }, ...results];
- }
- return results;
- }, [projects, projectId, projectName]);
-
- const clearConfig = () => {
- setValue("config.includeProjects", []);
- setValue("config.projectName", "");
- setValue("config.groupName", "");
- // @ts-expect-error rhf doesn't like this but we need to reset
- setValue("config.projectId", undefined);
+ const selectGroup = (group: TGitLabGroup | null) => {
+ setValue("config.scope", group ? GitLabDataSourceScope.Group : GitLabDataSourceScope.Account);
// @ts-expect-error rhf doesn't like this but we need to reset
- setValue("config.groupId", undefined);
- setProjectSearch("");
- setGroupSearch("");
+ setValue("config.groupId", group ? Number(group.id) : undefined);
+ setValue("config.groupName", group?.fullName ?? "");
+ setValue("config.includeProjects", []);
+ setValue("isDiscoveryEnabled", Boolean(group));
};
return (
<>
- <SecretScanningDataSourceConnectionField isUpdate={isUpdate} onChange={clearConfig} />
- <Controller
- name="config.scope"
- control={control}
- render={({ field: { value, onChange } }) => (
- <FieldSet>
- <FieldLegend variant="label">Scope</FieldLegend>
- <RadioGroup
- value={value}
- onValueChange={(newScope) => {
- onChange(newScope);
- clearConfig();
- setValue("isDiscoveryEnabled", newScope === GitLabDataSourceScope.Group);
- }}
- disabled={isUpdate}
- >
- {SCOPE_OPTIONS.map((option) => (
- <FieldLabel
- key={option.scope}
- htmlFor={`secret-scanning-gitlab-scope-${option.scope}`}
- variant="project"
- >
- <Field orientation="horizontal">
- <FieldContent>
- <FieldTitle>{option.title}</FieldTitle>
- <FieldDescription>{option.description}</FieldDescription>
- </FieldContent>
- <RadioGroupItem
- value={option.scope}
- id={`secret-scanning-gitlab-scope-${option.scope}`}
- />
- </Field>
- </FieldLabel>
- ))}
- </RadioGroup>
- {isUpdate && <FieldDescription>Cannot be updated.</FieldDescription>}
- </FieldSet>
- )}
+ <SecretScanningDataSourceConnectionField
+ isUpdate={isUpdate}
+ onChange={() => {
+ selectGroup(null);
+ setGroupSearch("");
+ }}
/>
{scope === GitLabDataSourceScope.Project ? (
- <Controller
- name="config.projectId"
- control={control}
- render={({ field: { value, onChange }, fieldState: { error } }) => (
- <Field data-invalid={Boolean(error)}>
- <FieldLabel
- id="secret-scanning-gitlab-project-label"
- htmlFor="secret-scanning-gitlab-project"
- >
- Project
- </FieldLabel>
- <FieldContent>
- <Combobox<TGitLabProject>
- id="secret-scanning-gitlab-project"
- aria-labelledby="secret-scanning-gitlab-project-label"
- aria-describedby="secret-scanning-gitlab-project-help"
- isError={Boolean(error)}
- isLoading={isProjectsPending && Boolean(connectionId)}
- isDisabled={!connectionId || isUpdate}
- isClearable={false}
- value={projectOptions.find((project) => Number(project.id) === value) ?? null}
- onValueChange={(project) => {
- onChange(Number(project.id));
- setValue("config.projectName", project.name);
- }}
- onInputValueChange={setProjectSearch}
- shouldFilter={false}
- options={projectOptions}
- placeholder="Search for a project..."
- searchAriaLabel="Search projects"
- getOptionLabel={(option) => option.name}
- getOptionValue={(option) => option.id}
- emptyMessage={(inputValue) =>
- inputValue ? "No projects found matching your search." : "No projects found."
- }
- modal
- />
- <FieldDescription id="secret-scanning-gitlab-project-help">
- {SEARCH_HELP}
- </FieldDescription>
- <FieldError errors={[error]} />
- </FieldContent>
- </Field>
- )}
- />
+ <Field data-disabled>
+ <FieldLabel htmlFor="secret-scanning-gitlab-project">Project</FieldLabel>
+ <FieldContent>
+ <Input
+ id="secret-scanning-gitlab-project"
+ value={projectName || String(projectId ?? "")}
+ aria-describedby="secret-scanning-gitlab-project-help"
+ readOnly
+ disabled
+ />
+ <FieldDescription id="secret-scanning-gitlab-project-help">
+ Project scope is no longer offered for new data sources. Cannot be updated.
+ </FieldDescription>
+ </FieldContent>
+ </Field>
) : (
<Controller
name="config.groupId"
control={control}
- render={({ field: { value, onChange }, fieldState: { error } }) => (
- <Field data-invalid={Boolean(error)}>
+ render={({ field: { value }, fieldState: { error } }) => (
+ <Field data-invalid={Boolean(error)} data-disabled={isUpdate}>
<FieldLabel
id="secret-scanning-gitlab-group-label"
htmlFor="secret-scanning-gitlab-group"
>
- Group
+ Group (Optional)
</FieldLabel>
<FieldContent>
<Combobox<TGitLabGroup>
@@ -214,20 +106,16 @@ export const GitLabDataSourceConnectionFields = () => {
aria-labelledby="secret-scanning-gitlab-group-label"
aria-describedby="secret-scanning-gitlab-group-help"
isError={Boolean(error)}
- isLoading={isGroupsPending && Boolean(connectionId)}
+ isLoading={isGroupsPending && Boolean(connectionId) && !isUpdate}
isDisabled={!connectionId || isUpdate}
- isClearable={false}
value={groupOptions.find((group) => Number(group.id) === value) ?? null}
- onValueChange={(group) => {
- onChange(Number(group.id));
- setValue("config.groupName", group.fullName);
- setValue("config.includeProjects", []);
- }}
+ onValueChange={selectGroup}
onInputValueChange={setGroupSearch}
shouldFilter={false}
options={groupOptions}
- placeholder="Search for a group..."
+ placeholder="All accessible projects"
searchAriaLabel="Search groups"
+ clearAriaLabel="Clear group"
getOptionLabel={(option) => option.fullPath}
getOptionValue={(option) => option.id}
emptyMessage={(inputValue) =>
@@ -236,7 +124,9 @@ export const GitLabDataSourceConnectionFields = () => {
modal
/>
<FieldDescription id="secret-scanning-gitlab-group-help">
- {SEARCH_HELP}
+ {isUpdate
+ ? "Cannot be updated."
+ : "Leave empty to cover every project the connection can read, including your own. Select a group to discover projects added to it later."}
</FieldDescription>
<FieldError errors={[error]} />
</FieldContent>
diff --git a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/SecretScanningDataSourceConfigFields.tsx b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/SecretScanningDataSourceConfigFields.tsx
index dccde815d3..8c68ac2c6a 100644
--- a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/SecretScanningDataSourceConfigFields.tsx
+++ b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceConfigFields/SecretScanningDataSourceConfigFields.tsx
@@ -113,7 +113,7 @@ export const SecretScanningDataSourceScanFields = () => {
description={
supportsDiscovery
? `Automatically enable scanning for new ${pluralNoun} added to ${platform}.`
- : `Requires a group scope. GitLab discovery relies on group-level webhooks, which aren't available for a single project.`
+ : `Select a group in the previous step to enable this. GitLab only reports new projects through group webhooks.`
}
/>
</FieldGroup>
diff --git a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceReviewFields/SecretScanningDataSourceReviewFields.tsx b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceReviewFields/SecretScanningDataSourceReviewFields.tsx
index 6beb6bfc80..7c12566426 100644
--- a/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceReviewFields/SecretScanningDataSourceReviewFields.tsx
+++ b/frontend/src/components/secret-scanning/forms/SecretScanningDataSourceReviewFields/SecretScanningDataSourceReviewFields.tsx
@@ -46,15 +46,19 @@ const getConnectionDetails = (
case SecretScanningDataSource.Bitbucket:
return [{ label: "Workspace", value: form.config.workspaceSlug }];
case SecretScanningDataSource.GitLab:
- return form.config?.scope === GitLabDataSourceScope.Group
- ? [
- { label: "Scope", value: "Group" },
- { label: "Group", value: form.config.groupName }
- ]
- : [
- { label: "Scope", value: "Project" },
- { label: "Project", value: form.config?.projectName }
- ];
+ if (form.config?.scope === GitLabDataSourceScope.Project) {
+ return [{ label: "Project", value: form.config.projectName }];
+ }
+
+ return [
+ {
+ label: "Group",
+ value:
+ form.config?.scope === GitLabDataSourceScope.Group
+ ? form.config.groupName
+ : "None (all accessible projects)"
+ }
+ ];
default:
throw new Error("Unhandled data source type");
}
diff --git a/frontend/src/components/secret-scanning/forms/schemas/gitlab-data-source-schema.ts b/frontend/src/components/secret-scanning/forms/schemas/gitlab-data-source-schema.ts
index 2a4e7c820a..b803c10d61 100644
--- a/frontend/src/components/secret-scanning/forms/schemas/gitlab-data-source-schema.ts
+++ b/frontend/src/components/secret-scanning/forms/schemas/gitlab-data-source-schema.ts
@@ -5,6 +5,10 @@ import { GitLabDataSourceScope } from "@app/hooks/api/secretScanningV2/types/git
import { BaseSecretScanningDataSourceSchema } from "./base-secret-scanning-data-source-schema";
+const IncludeProjectsSchema = z
+ .array(z.string().min(1).max(256))
+ .max(100, "Cannot configure more than 100 projects");
+
export const GitLabDataSourceSchema = z
.object({
type: z.literal(SecretScanningDataSource.GitLab),
@@ -13,9 +17,11 @@ export const GitLabDataSourceSchema = z
scope: z.literal(GitLabDataSourceScope.Group),
groupId: z.number(),
groupName: z.string(),
- includeProjects: z
- .array(z.string().min(1).max(256))
- .max(100, "Cannot configure more than 100 projects")
+ includeProjects: IncludeProjectsSchema
+ }),
+ z.object({
+ scope: z.literal(GitLabDataSourceScope.Account),
+ includeProjects: IncludeProjectsSchema
}),
z.object({
scope: z.literal(GitLabDataSourceScope.Project),
diff --git a/frontend/src/hooks/api/secretScanningV2/types/gitlab-data-source.ts b/frontend/src/hooks/api/secretScanningV2/types/gitlab-data-source.ts
index 82f1002e0a..c8d97ed2ab 100644
--- a/frontend/src/hooks/api/secretScanningV2/types/gitlab-data-source.ts
+++ b/frontend/src/hooks/api/secretScanningV2/types/gitlab-data-source.ts
@@ -4,6 +4,8 @@ import { SecretScanningDataSource } from "../enums";
import { TSecretScanningDataSourceBase } from "./shared";
export enum GitLabDataSourceScope {
+ Account = "account",
+ // Only existing data sources use project scope; new ones pick a group or none.
Project = "project",
Group = "group"
}
@@ -17,6 +19,10 @@ export type TGitLabDataSource = TSecretScanningDataSourceBase & {
includeProjects: string[];
scope: GitLabDataSourceScope.Group;
}
+ | {
+ includeProjects: string[];
+ scope: GitLabDataSourceScope.Account;
+ }
| {
projectName?: string;
projectId: number;
diff --git a/frontend/src/pages/secret-scanning/SecretScanningDataSourceByIdPage/components/DataSourceConfigDisplay/GitLabDataSourceConfigDisplay.tsx b/frontend/src/pages/secret-scanning/SecretScanningDataSourceByIdPage/components/DataSourceConfigDisplay/GitLabDataSourceConfigDisplay.tsx
index 2f2f63d068..4e32299890 100644
--- a/frontend/src/pages/secret-scanning/SecretScanningDataSourceByIdPage/components/DataSourceConfigDisplay/GitLabDataSourceConfigDisplay.tsx
+++ b/frontend/src/pages/secret-scanning/SecretScanningDataSourceByIdPage/components/DataSourceConfigDisplay/GitLabDataSourceConfigDisplay.tsx
@@ -23,16 +23,15 @@ export const GitLabDataSourceConfigDisplay = ({ dataSource }: Props) => {
);
}
- // group-scope
-
- const { includeProjects, groupId, groupName } = config;
+ const { includeProjects } = config;
return (
<>
- <GenericFieldLabel label="Scope" className="capitalize">
- {config.scope}
+ <GenericFieldLabel label="Group">
+ {config.scope === GitLabDataSourceScope.Group
+ ? config.groupName || config.groupId
+ : "None (all accessible projects)"}
</GenericFieldLabel>
- <GenericFieldLabel label="Group">{groupName || groupId}</GenericFieldLabel>
<GenericFieldLabel label="Scan Projects">
{includeProjects.includes("*") ? "All" : includeProjects.join(", ")}
</GenericFieldLabel>