Skip to content

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

add endpoints to search groups

adilsonfd62e7f4f4d4
diff --git a/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts b/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
index b932088e99..851890f294 100644
--- a/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
+++ b/backend/src/server/routes/v1/app-connection-routers/gitlab-connection-router.ts
@@ -10,6 +10,7 @@ import {
TGitLabProject,
UpdateGitLabConnectionSchema
} from "@app/services/app-connection/gitlab";
+import { TGitLabGroupTreeItem } from "@app/services/app-connection/gitlab/gitlab-connection-types";
import { AuthMode } from "@app/services/auth/auth-type";
import { registerAppConnectionEndpoints } from "./app-connection-endpoints";
@@ -89,4 +90,140 @@ export const registerGitLabConnectionRouter = async (server: FastifyZodProvider)
return groups;
}
});
+
+ server.route({
+ method: "GET",
+ url: `/:connectionId/search-groups`,
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ operationId: "searchGitLabGroups",
+ params: z.object({
+ connectionId: z.string().uuid()
+ }),
+ querystring: z.object({
+ search: z.string().trim().min(1).max(255)
+ }),
+ response: {
+ 200: z
+ .object({
+ id: z.string(),
+ name: z.string(),
+ fullPath: z.string()
+ })
+ .array()
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ const { connectionId } = req.params;
+ const { search } = req.query;
+ const groups: TGitLabGroupTreeItem[] = await server.services.appConnection.gitlab.searchGroups(
+ connectionId,
+ search,
+ req.permission
+ );
+ return groups;
+ }
+ });
+
+ server.route({
+ method: "GET",
+ url: `/:connectionId/root-groups`,
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ operationId: "listGitLabRootGroups",
+ params: z.object({
+ connectionId: z.string().uuid()
+ }),
+ response: {
+ 200: z
+ .object({
+ id: z.string(),
+ name: z.string(),
+ fullPath: z.string()
+ })
+ .array()
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ const { connectionId } = req.params;
+ const groups: TGitLabGroupTreeItem[] = await server.services.appConnection.gitlab.listRootGroups(
+ connectionId,
+ req.permission
+ );
+ return groups;
+ }
+ });
+
+ server.route({
+ method: "GET",
+ url: `/:connectionId/groups/:groupId/subgroups`,
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ operationId: "listGitLabGroupSubgroups",
+ params: z.object({
+ connectionId: z.string().uuid(),
+ groupId: z.string().min(1)
+ }),
+ response: {
+ 200: z
+ .object({
+ id: z.string(),
+ name: z.string(),
+ fullPath: z.string()
+ })
+ .array()
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ const { connectionId, groupId } = req.params;
+ const subgroups: TGitLabGroupTreeItem[] = await server.services.appConnection.gitlab.listSubgroups(
+ connectionId,
+ groupId,
+ req.permission
+ );
+ return subgroups;
+ }
+ });
+
+ server.route({
+ method: "GET",
+ url: `/:connectionId/groups/:groupId/projects`,
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ operationId: "listGitLabGroupProjects",
+ params: z.object({
+ connectionId: z.string().uuid(),
+ groupId: z.string().min(1)
+ }),
+ response: {
+ 200: z
+ .object({
+ id: z.string(),
+ name: z.string()
+ })
+ .array()
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async (req) => {
+ const { connectionId, groupId } = req.params;
+ const projects: TGitLabProject[] = await server.services.appConnection.gitlab.listGroupProjects(
+ connectionId,
+ groupId,
+ req.permission
+ );
+ return projects;
+ }
+ });
};
diff --git a/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts b/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
index 37eeeaeb15..ce0a355768 100644
--- a/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
+++ b/backend/src/services/app-connection/gitlab/gitlab-connection-fns.ts
@@ -15,7 +15,13 @@ import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TAppConnectionDALFactory } from "../app-connection-dal";
import { GitLabAccessTokenType, GitLabConnectionMethod } from "./gitlab-connection-enums";
-import { TGitLabConnection, TGitLabConnectionConfig, TGitLabGroup, TGitLabProject } from "./gitlab-connection-types";
+import {
+ TGitLabConnection,
+ TGitLabConnectionConfig,
+ TGitLabGroup,
+ TGitLabGroupTreeItem,
+ TGitLabProject
+} from "./gitlab-connection-types";
interface GitLabOAuthTokenResponse {
access_token: string;
@@ -26,6 +32,12 @@ interface GitLabOAuthTokenResponse {
scope?: string;
}
+type TNavigationParams = {
+ appConnection: TGitLabConnection;
+ appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">;
+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
+};
+
export const getGitLabConnectionListItem = () => {
const { INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID } = getConfig();
@@ -404,3 +416,132 @@ export const listGitLabGroups = async ({
});
}
};
+
+const getNavigationClient = async ({ appConnection, appConnectionDAL, kmsService }: TNavigationParams) => {
+ let { accessToken } = appConnection.credentials;
+
+ if (
+ appConnection.method === GitLabConnectionMethod.AccessToken &&
+ appConnection.credentials.accessTokenType === GitLabAccessTokenType.Project
+ ) {
+ return null;
+ }
+
+ if (
+ appConnection.method === GitLabConnectionMethod.OAuth &&
+ appConnection.credentials.refreshToken &&
+ new Date(appConnection.credentials.expiresAt) < new Date()
+ ) {
+ accessToken = await refreshGitLabToken(
+ appConnection.credentials.refreshToken,
+ appConnection.id,
+ appConnection.orgId,
+ appConnection.projectId,
+ appConnectionDAL,
+ kmsService,
+ appConnection.credentials.instanceUrl
+ );
+ }
+
+ return getGitLabClient(
+ accessToken,
+ appConnection.credentials.instanceUrl,
+ appConnection.method === GitLabConnectionMethod.OAuth
+ );
+};
+
+export const listGitLabRootGroups = async (params: TNavigationParams): Promise<TGitLabGroupTreeItem[]> => {
+ const client = await getNavigationClient(params);
+ if (!client) return [];
+
+ try {
+ const groups = await client.Groups.all({
+ topLevelOnly: true,
+ orderBy: "name",
+ sort: "asc",
+ minAccessLevel: 20
+ });
+
+ return groups.filter((g) => !g.parentId).map((g) => ({ id: g.id.toString(), name: g.name, fullPath: g.fullPath }));
+ } catch (error: unknown) {
+ if (error instanceof GitbeakerRequestError) {
+ throw new BadRequestError({
+ message: `Failed to fetch GitLab groups: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
+ });
+ }
+ if (error instanceof InternalServerError) throw error;
+ throw new InternalServerError({ message: "Unable to fetch GitLab root groups" });
+ }
+};
+
+export const listGitLabSubgroups = async (
+ groupId: string,
+ params: TNavigationParams
+): Promise<TGitLabGroupTreeItem[]> => {
+ const client = await getNavigationClient(params);
+ if (!client) return [];
+
+ try {
+ const subgroups = await client.Groups.allSubgroups(Number(groupId), {
+ orderBy: "name",
+ sort: "asc"
+ });
+
+ return subgroups.map((g) => ({ id: g.id.toString(), name: g.name, fullPath: g.fullPath }));
+ } catch (error: unknown) {
+ if (error instanceof GitbeakerRequestError) {
+ throw new BadRequestError({
+ message: `Failed to fetch GitLab subgroups: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
+ });
+ }
+ if (error instanceof InternalServerError) throw error;
+ throw new InternalServerError({ message: "Unable to fetch GitLab subgroups" });
+ }
+};
+
+export const listGitLabGroupProjects = async (
+ groupId: string,
+ params: TNavigationParams
+): Promise<TGitLabProject[]> => {
+ const client = await getNavigationClient(params);
+ if (!client) return [];
+
+ try {
+ const projects = await client.Groups.allProjects(Number(groupId), {
+ archived: false,
+ includeSubgroups: false
+ });
+
+ return projects.map((p) => ({ id: p.id.toString(), name: p.pathWithNamespace }));
+ } catch (error: unknown) {
+ if (error instanceof GitbeakerRequestError) {
+ throw new BadRequestError({
+ message: `Failed to fetch GitLab group projects: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
+ });
+ }
+ if (error instanceof InternalServerError) throw error;
+ throw new InternalServerError({ message: "Unable to fetch GitLab group projects" });
+ }
+};
+
+export const searchGitLabGroups = async (
+ search: string,
+ params: TNavigationParams
+): Promise<TGitLabGroupTreeItem[]> => {
+ const client = await getNavigationClient(params);
+ if (!client) return [];
+
+ try {
+ const groups = await client.Groups.search(search);
+
+ return groups.map((g) => ({ id: g.id.toString(), name: g.name, fullPath: g.fullPath }));
+ } catch (error: unknown) {
+ if (error instanceof GitbeakerRequestError) {
+ throw new BadRequestError({
+ message: `Failed to search GitLab groups: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
+ });
+ }
+ if (error instanceof InternalServerError) throw error;
+ throw new InternalServerError({ message: "Unable to search GitLab groups" });
+ }
+};
diff --git a/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts b/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
index 818697be3c..45a96bf30b 100644
--- a/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
+++ b/backend/src/services/app-connection/gitlab/gitlab-connection-service.ts
@@ -4,7 +4,14 @@ import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TAppConnectionDALFactory } from "../app-connection-dal";
import { AppConnection } from "../app-connection-enums";
-import { listGitLabGroups, listGitLabProjects } from "./gitlab-connection-fns";
+import {
+ listGitLabGroupProjects,
+ listGitLabGroups,
+ listGitLabProjects,
+ listGitLabRootGroups,
+ listGitLabSubgroups,
+ searchGitLabGroups
+} from "./gitlab-connection-fns";
import { TGitLabConnection } from "./gitlab-connection-types";
type TGetAppConnectionFunc = (
@@ -40,8 +47,57 @@ export const gitlabConnectionService = (
}
};
+ const listRootGroups = async (connectionId: string, actor: OrgServiceActor) => {
+ try {
+ const appConnection = await getAppConnection(AppConnection.GitLab, connectionId, actor);
+ return await listGitLabRootGroups({ appConnection, appConnectionDAL, kmsService });
+ } catch (error) {
+ logger.error(error, `Failed to list root groups for GitLab connection ${connectionId}`);
+ return [];
+ }
+ };
+
+ const searchGroups = async (connectionId: string, search: string, actor: OrgServiceActor) => {
+ try {
+ const appConnection = await getAppConnection(AppConnection.GitLab, connectionId, actor);
+ return await searchGitLabGroups(search, { appConnection, appConnectionDAL, kmsService });
+ } catch (error) {
+ logger.error(error, `Failed to search groups for GitLab connection ${connectionId}`);
+ return [];
+ }
+ };
+
+ const listSubgroups = async (connectionId: string, groupId: string, actor: OrgServiceActor) => {
+ try {
+ const appConnection = await getAppConnection(AppConnection.GitLab, connectionId, actor);
+ const subgroups = await listGitLabSubgroups(groupId, {
+ appConnection,
+ appConnectionDAL,
+ kmsService
+ });
+ return subgroups;
+ } catch (error) {
+ logger.error(error, `Failed to list subgroups for GitLab group ${groupId}`);
+ return [];
+ }
+ };
+
+ const listGroupProjects = async (connectionId: string, groupId: string, actor: OrgServiceActor) => {
+ try {
+ const appConnection = await getAppConnection(AppConnection.GitLab, connectionId, actor);
+ return await listGitLabGroupProjects(groupId, { appConnection, appConnectionDAL, kmsService });
+ } catch (error) {
+ logger.error(error, `Failed to list projects for GitLab group ${groupId}`);
+ return [];
+ }
+ };
+
return {
listProjects,
- listGroups
+ listGroups,
+ listRootGroups,
+ searchGroups,
+ listSubgroups,
+ listGroupProjects
};
};
diff --git a/backend/src/services/app-connection/gitlab/gitlab-connection-types.ts b/backend/src/services/app-connection/gitlab/gitlab-connection-types.ts
index 27d29ec056..8f8f130e71 100644
--- a/backend/src/services/app-connection/gitlab/gitlab-connection-types.ts
+++ b/backend/src/services/app-connection/gitlab/gitlab-connection-types.ts
@@ -26,6 +26,13 @@ export type TGitLabProject = {
id: string;
};
+export type TGitLabListProjectsParams = {
+ owned?: boolean;
+ search?: string;
+ page?: number;
+ perPage?: number;
+};
+
export type TGitLabAccessTokenCredentials = {
accessToken: string;
instanceUrl: string;
@@ -54,3 +61,9 @@ export interface TGitLabGroup {
id: string;
fullName: string;
}
+
+export type TGitLabGroupTreeItem = {
+ id: string;
+ name: string;
+ fullPath: string;
+};