feature: gitlab secret scanning

This commit is contained in:
Scott Wilson
2025-07-28 15:03:23 -07:00
parent 7d1bc86702
commit f2b0e4ae37
68 changed files with 1711 additions and 94 deletions
@@ -0,0 +1,274 @@
import { useEffect } from "react";
import { Controller, useFormContext, useWatch } from "react-hook-form";
import { MultiValue, SingleValue } from "react-select";
import { faCircleInfo } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { FilterableSelect, FormControl, Select, SelectItem, Tooltip } from "@app/components/v2";
import {
TGitLabGroup,
TGitLabProject,
useGitLabConnectionListGroups,
useGitLabConnectionListProjects
} from "@app/hooks/api/appConnections/gitlab";
import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { GitLabDataSourceScope } from "@app/hooks/api/secretScanningV2/types/gitlab-data-source";
import { TSecretScanningDataSourceForm } from "../schemas";
import { SecretScanningDataSourceConnectionField } from "../SecretScanningDataSourceConnectionField";
enum ScanMethod {
AllProjects = "all-projects",
SelectProjects = "select-projects"
}
export const GitLabDataSourceConfigFields = () => {
const { control, watch, setValue, formState } = useFormContext<
TSecretScanningDataSourceForm & {
type: SecretScanningDataSource.GitLab;
}
>();
console.log(formState);
const connectionId = useWatch({ control, name: "connection.id" });
const isUpdate = Boolean(watch("id"));
const scope = watch("config.scope");
const groupName = watch("config.groupName");
const includeProjects = watch("config.includeProjects");
const { data: projects, isPending: isProjectsPending } = useGitLabConnectionListProjects(
connectionId,
{ enabled: Boolean(connectionId) }
);
const { data: groups, isPending: isGroupsPending } = useGitLabConnectionListGroups(connectionId, {
enabled: Boolean(connectionId) && scope === GitLabDataSourceScope.Group
});
const scanMethod =
!includeProjects || includeProjects.includes("*")
? ScanMethod.AllProjects
: ScanMethod.SelectProjects;
useEffect(() => {
if (!includeProjects) {
setValue("config.includeProjects", ["*"]);
}
}, [includeProjects]);
const clearAllFields = () => {
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);
// @ts-expect-error rhf doesn't like this but we need to reset
setValue("config.groupId", undefined);
};
return (
<>
<SecretScanningDataSourceConnectionField isUpdate={isUpdate} onChange={clearAllFields} />
<Controller
name="config.scope"
control={control}
defaultValue={GitLabDataSourceScope.Project}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<FormControl
isError={Boolean(error)}
errorText={error?.message}
label="Scope"
helperText={isUpdate ? "Cannot be updated" : undefined}
tooltipText={
<div className="flex flex-col gap-3">
<p>Specify the GitLab scope scanning should be performed at:</p>
<ul className="flex list-disc flex-col gap-3 pl-4">
<li>
<p className="text-mineshaft-300">
<span className="font-medium text-bunker-200">Project</span>: Scan an
individual GitLab project.
</p>
</li>
<li>
<p className="text-mineshaft-300">
<span className="font-medium text-bunker-200">Group</span>: Scan one or more
projects belonging to a GitLab group.
</p>
</li>
</ul>
</div>
}
>
<Select
value={value}
onValueChange={(v) => {
onChange(v);
clearAllFields();
}}
className="w-full border border-mineshaft-500 capitalize"
position="popper"
isDisabled={isUpdate}
dropdownContainerClassName="max-w-none"
>
{Object.values(GitLabDataSourceScope).map((method) => {
return (
<SelectItem className="capitalize" value={method} key={method}>
{method}
</SelectItem>
);
})}
</Select>
</FormControl>
)}
/>
{scope === GitLabDataSourceScope.Project ? (
<Controller
name="config.projectId"
control={control}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<FormControl
isError={Boolean(error)}
errorText={error?.message}
label="Project"
helperText={
<Tooltip
className="max-w-md"
content={<>Ensure that your connection has the correct permissions.</>}
>
<div>
<span>Don&#39;t see the project you&#39;re looking for?</span>{" "}
<FontAwesomeIcon icon={faCircleInfo} className="text-mineshaft-400" />
</div>
</Tooltip>
}
>
<FilterableSelect
isLoading={isProjectsPending && Boolean(connectionId)}
isDisabled={!connectionId}
value={projects?.find((project) => value === Number.parseInt(project.id, 10))}
onChange={(newValue) => {
const project = newValue as SingleValue<TGitLabProject>;
onChange(project ? Number.parseInt(project.id, 10) : null);
setValue("config.projectName", project?.name ?? "");
}}
options={projects}
placeholder="Select project..."
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.name}
/>
</FormControl>
)}
/>
) : (
<>
<Controller
name="config.groupId"
control={control}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<FormControl
isError={Boolean(error)}
errorText={error?.message}
label="Group"
helperText={
<Tooltip
className="max-w-md"
content={<>Ensure that your connection has the correct permissions.</>}
>
<div>
<span>Don&#39;t see the group you&#39;re looking for?</span>{" "}
<FontAwesomeIcon icon={faCircleInfo} className="text-mineshaft-400" />
</div>
</Tooltip>
}
>
<FilterableSelect
isLoading={isGroupsPending && Boolean(connectionId)}
isDisabled={!connectionId}
value={groups?.find((group) => value === Number.parseInt(group.id, 10))}
onChange={(newValue) => {
const group = newValue as SingleValue<TGitLabGroup>;
onChange(group ? Number.parseInt(group.id, 10) : null);
setValue("config.groupName", group?.name ?? "");
setValue("config.includeProjects", ["*"]);
}}
options={groups}
placeholder="Select group..."
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.name}
/>
</FormControl>
)}
/>
<FormControl label="Scan Projects">
<Select
value={scanMethod}
onValueChange={(val) => {
setValue("config.includeProjects", val === ScanMethod.AllProjects ? ["*"] : []);
}}
className="w-full border border-mineshaft-500 capitalize"
position="popper"
dropdownContainerClassName="max-w-none"
isDisabled={!connectionId}
>
{Object.values(ScanMethod).map((method) => {
return (
<SelectItem className="capitalize" value={method} key={method}>
{method.replace("-", " ")}
</SelectItem>
);
})}
</Select>
</FormControl>
{scanMethod === ScanMethod.SelectProjects && (
<Controller
name="config.includeProjects"
defaultValue={["*"]}
control={control}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<FormControl
isError={Boolean(error)}
errorText={error?.message}
label="Include Projects"
helperText={
<Tooltip
className="max-w-md"
content={<>Ensure that your connection has the correct permissions.</>}
>
<div>
<span>Don&#39;t see the project you&#39;re looking for?</span>{" "}
<FontAwesomeIcon icon={faCircleInfo} className="text-mineshaft-400" />
</div>
</Tooltip>
}
>
<FilterableSelect
menuPlacement="top"
isLoading={isProjectsPending && Boolean(connectionId)}
isDisabled={!connectionId || !groupName}
isMulti
value={projects?.filter((project) => value.includes(project.name))}
onChange={(newValue) => {
onChange(
newValue
? (newValue as MultiValue<TGitLabProject>).map((p) => p.name)
: null
);
}}
options={projects?.filter((project) => project.name.startsWith(groupName))}
placeholder="Select projects..."
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.name}
/>
</FormControl>
)}
/>
)}
</>
)}
</>
);
};
@@ -7,10 +7,12 @@ import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { TSecretScanningDataSourceForm } from "../schemas";
import { BitbucketDataSourceConfigFields } from "./BitbucketDataSourceConfigFields";
import { GitHubDataSourceConfigFields } from "./GitHubDataSourceConfigFields";
import { GitLabDataSourceConfigFields } from "./GitLabDataSourceConfigFields";
const COMPONENT_MAP: Record<SecretScanningDataSource, React.FC> = {
[SecretScanningDataSource.GitHub]: GitHubDataSourceConfigFields,
[SecretScanningDataSource.Bitbucket]: BitbucketDataSourceConfigFields
[SecretScanningDataSource.Bitbucket]: BitbucketDataSourceConfigFields,
[SecretScanningDataSource.GitLab]: GitLabDataSourceConfigFields
};
export const SecretScanningDataSourceConfigFields = () => {
@@ -0,0 +1,49 @@
import { useFormContext } from "react-hook-form";
import { GenericFieldLabel } from "@app/components/v2";
import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { GitLabDataSourceScope } from "@app/hooks/api/secretScanningV2/types/gitlab-data-source";
import { TSecretScanningDataSourceForm } from "../schemas";
import { SecretScanningDataSourceConfigReviewSection } from "./shared";
export const GitLabDataSourceReviewFields = () => {
const { watch } = useFormContext<
TSecretScanningDataSourceForm & {
type: SecretScanningDataSource.GitLab;
}
>();
const [config, connection] = watch(["config", "connection"]);
if (config.scope === GitLabDataSourceScope.Project) {
const { projectName, projectId } = config;
return (
<SecretScanningDataSourceConfigReviewSection>
{connection && <GenericFieldLabel label="Connection">{connection.name}</GenericFieldLabel>}
<GenericFieldLabel label="Scope" className="capitalize">
{config.scope}
</GenericFieldLabel>
<GenericFieldLabel label="Project">{projectName || projectId}</GenericFieldLabel>
</SecretScanningDataSourceConfigReviewSection>
);
}
// group-scope
const { includeProjects, groupName, groupId } = config;
const shouldScanAll = includeProjects.includes("*");
return (
<SecretScanningDataSourceConfigReviewSection>
{connection && <GenericFieldLabel label="Connection">{connection.name}</GenericFieldLabel>}
<GenericFieldLabel label="Scope" className="capitalize">
{config.scope}
</GenericFieldLabel>
<GenericFieldLabel label="Group">{groupName || groupId}</GenericFieldLabel>
<GenericFieldLabel label="Scan Projects">
{shouldScanAll ? "All" : includeProjects.join(", ")}
</GenericFieldLabel>
</SecretScanningDataSourceConfigReviewSection>
);
};
@@ -6,10 +6,12 @@ import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { TSecretScanningDataSourceForm } from "../schemas";
import { BitbucketDataSourceReviewFields } from "./BitbucketDataSourceReviewFields";
import { GitHubDataSourceReviewFields } from "./GitHubDataSourceReviewFields";
import { GitLabDataSourceReviewFields } from "./GitLabDataSourceReviewFields";
const COMPONENT_MAP: Record<SecretScanningDataSource, React.FC> = {
[SecretScanningDataSource.GitHub]: GitHubDataSourceReviewFields,
[SecretScanningDataSource.Bitbucket]: BitbucketDataSourceReviewFields
[SecretScanningDataSource.Bitbucket]: BitbucketDataSourceReviewFields,
[SecretScanningDataSource.GitLab]: GitLabDataSourceReviewFields
};
export const SecretScanningDataSourceReviewFields = () => {
@@ -0,0 +1,29 @@
import { z } from "zod";
import { SecretScanningDataSource } from "@app/hooks/api/secretScanningV2";
import { GitLabDataSourceScope } from "@app/hooks/api/secretScanningV2/types/gitlab-data-source";
import { BaseSecretScanningDataSourceSchema } from "./base-secret-scanning-data-source-schema";
export const GitLabDataSourceSchema = z
.object({
type: z.literal(SecretScanningDataSource.GitLab),
config: z.discriminatedUnion("scope", [
z.object({
scope: z.literal(GitLabDataSourceScope.Group),
groupId: z.number(),
groupName: z.string(),
includeProjects: z
.array(z.string().min(1).max(256))
.min(1, "One or more projects required")
.max(100, "Cannot configure more than 100 projects")
.default(["*"])
}),
z.object({
scope: z.literal(GitLabDataSourceScope.Project),
projectId: z.number(),
projectName: z.string()
})
])
})
.merge(BaseSecretScanningDataSourceSchema({ isConnectionRequired: true }));
@@ -2,10 +2,12 @@ import { z } from "zod";
import { BitbucketDataSourceSchema } from "./bitbucket-data-source-schema";
import { GitHubDataSourceSchema } from "./github-data-source-schema";
import { GitLabDataSourceSchema } from "./gitlab-data-source-schema";
export const SecretScanningDataSourceSchema = z.discriminatedUnion("type", [
GitHubDataSourceSchema,
BitbucketDataSourceSchema
BitbucketDataSourceSchema,
GitLabDataSourceSchema
]);
export type TSecretScanningDataSourceForm = z.infer<typeof SecretScanningDataSourceSchema>;
@@ -16,8 +16,8 @@ import {
import {
TGitLabGroup,
TGitLabProject,
useGitlabConnectionListGroups,
useGitlabConnectionListProjects
useGitLabConnectionListGroups,
useGitLabConnectionListProjects
} from "@app/hooks/api/appConnections/gitlab";
import { SecretSync } from "@app/hooks/api/secretSyncs";
import { GitLabSyncScope } from "@app/hooks/api/secretSyncs/types/gitlab-sync";
@@ -70,11 +70,11 @@ export const GitLabSyncFields = () => {
const scope = useWatch({ name: "destinationConfig.scope", control });
const shouldMaskSecrets = useWatch({ name: "destinationConfig.shouldMaskSecrets", control });
const { data: groups, isLoading: isGroupsLoading } = useGitlabConnectionListGroups(connectionId, {
const { data: groups, isLoading: isGroupsLoading } = useGitLabConnectionListGroups(connectionId, {
enabled: Boolean(connectionId) && scope === GitLabSyncScope.Group
});
const { data: projects, isLoading: isProjectsLoading } = useGitlabConnectionListProjects(
const { data: projects, isLoading: isProjectsLoading } = useGitLabConnectionListProjects(
connectionId,
{
enabled: Boolean(connectionId)