Skip to content

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

13.5 KB
Raw Blame
/* eslint-disable no-await-in-loop */
import { GitbeakerRequestError, GitbeakerRetryError, Gitlab } from "@gitbeaker/rest";
import { AxiosError, HttpStatusCode } from "axios";

import { getConfig } from "@app/lib/config/env";
import { request } from "@app/lib/config/request";
import { BadRequestError, InternalServerError, RateLimitError } from "@app/lib/errors";
import { removeTrailingSlash } from "@app/lib/fn";
import { logger } from "@app/lib/logger";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
import { encryptAppConnectionCredentials } from "@app/services/app-connection/app-connection-fns";
import { IntegrationUrls } from "@app/services/integration-auth/integration-list";
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";

interface GitLabOAuthTokenResponse {
  access_token: string;
  token_type: string;
  expires_in: number;
  refresh_token: string;
  created_at: number;
  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();

  return {
    name: "GitLab" as const,
    app: AppConnection.GitLab as const,
    methods: Object.values(GitLabConnectionMethod) as [
      GitLabConnectionMethod.AccessToken,
      GitLabConnectionMethod.OAuth
    ],
    oauthClientId: INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID
  };
};

export const getGitLabInstanceUrl = async (instanceUrl?: string) => {
  const gitLabInstanceUrl = instanceUrl ? removeTrailingSlash(instanceUrl) : IntegrationUrls.GITLAB_URL;

  await blockLocalAndPrivateIpAddresses(gitLabInstanceUrl);

  return gitLabInstanceUrl;
};

export const getGitLabClient = async (accessToken: string, instanceUrl?: string, isOAuth = false) => {
  const host = await getGitLabInstanceUrl(instanceUrl);

  const client = new Gitlab<true>({
    host,
    ...(isOAuth ? { oauthToken: accessToken } : { token: accessToken }),
    camelize: true
  });

  return client;
};

export const refreshGitLabToken = async (
  refreshToken: string,
  appId: string,
  orgId: string,
  projectId: string | undefined | null,
  appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">,
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">,
  instanceUrl?: string
): Promise<string> => {
  const {
    INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID,
    INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET,
    SITE_URL,
    isCloud
  } = getConfig();
  if (
    !INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET ||
    !INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID ||
    !SITE_URL ||
    // Cloud instances do not support OAuth authentication
    isCloud
  ) {
    throw new InternalServerError({
      message: `GitLab environment variables have not been configured`
    });
  }

  const payload = new URLSearchParams({
    grant_type: "refresh_token",
    refresh_token: refreshToken,
    client_id: INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID,
    client_secret: INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET,
    redirect_uri: `${SITE_URL}/organization/app-connections/gitlab/oauth/callback`
  });

  try {
    const url = await getGitLabInstanceUrl(instanceUrl);
    const { data } = await request.post<GitLabOAuthTokenResponse>(`${url}/oauth/token`, payload.toString(), {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        Accept: "application/json"
      }
    });

    const expiresAt = new Date(Date.now() + data.expires_in * 1000 - 600000);

    const encryptedCredentials = await encryptAppConnectionCredentials({
      credentials: {
        instanceUrl,
        tokenType: data.token_type,
        createdAt: new Date(data.created_at * 1000).toISOString(),
        refreshToken: data.refresh_token,
        accessToken: data.access_token,
        expiresAt
      },
      orgId,
      kmsService,
      projectId
    });

    await appConnectionDAL.updateById(appId, { encryptedCredentials });
    return data.access_token;
  } catch (error: unknown) {
    if (error instanceof AxiosError) {
      throw new BadRequestError({
        message: `Failed to refresh GitLab token: ${error.message}`
      });
    }
    throw new BadRequestError({
      message: "Unable to refresh GitLab token"
    });
  }
};

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 exchangeGitLabOAuthCode = async (
  code: string,
  instanceUrl?: string
): Promise<GitLabOAuthTokenResponse> => {
  const {
    INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID,
    INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET,
    SITE_URL,
    isCloud
  } = getConfig();
  if (
    !INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET ||
    !INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID ||
    !SITE_URL ||
    isCloud
  ) {
    throw new InternalServerError({
      message: `GitLab environment variables have not been configured`
    });
  }

  try {
    const payload = new URLSearchParams({
      grant_type: "authorization_code",
      code,
      client_id: INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_ID,
      client_secret: INF_APP_CONNECTION_GITLAB_OAUTH_CLIENT_SECRET,
      redirect_uri: `${SITE_URL}/organization/app-connections/gitlab/oauth/callback`
    });
    const url = await getGitLabInstanceUrl(instanceUrl);

    const response = await request.post<GitLabOAuthTokenResponse>(`${url}/oauth/token`, payload.toString(), {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        Accept: "application/json"
      }
    });

    if (!response.data) {
      throw new InternalServerError({
        message: "Failed to exchange OAuth code: Empty response"
      });
    }

    return response.data;
  } catch (error: unknown) {
    if (error instanceof AxiosError) {
      throw new BadRequestError({
        message: `Failed to exchange OAuth code: ${error.message}`
      });
    }
    throw new BadRequestError({
      message: "Unable to exchange OAuth code"
    });
  }
};

export const validateGitLabConnectionCredentials = async (config: TGitLabConnectionConfig) => {
  const { credentials: inputCredentials, method } = config;

  let accessToken: string;
  let oauthData: GitLabOAuthTokenResponse | null = null;

  if (method === GitLabConnectionMethod.OAuth && "code" in inputCredentials) {
    oauthData = await exchangeGitLabOAuthCode(inputCredentials.code, inputCredentials.instanceUrl);
    accessToken = oauthData.access_token;
  } else if (method === GitLabConnectionMethod.AccessToken && "accessToken" in inputCredentials) {
    accessToken = inputCredentials.accessToken;
  } else {
    throw new BadRequestError({
      message: "Invalid credentials for the selected connection method"
    });
  }

  try {
    const client = await getGitLabClient(
      accessToken,
      inputCredentials.instanceUrl,
      method === GitLabConnectionMethod.OAuth
    );
    await client.Users.showCurrentUser();
  } catch (error: unknown) {
    logger.error(error, "Error validating GitLab connection credentials");

    if (error instanceof GitbeakerRequestError) {
      throw new BadRequestError({
        message: `Failed to validate credentials: ${error.message ?? "Unknown error"}${error.cause?.description && error.message !== "Unauthorized" ? `. Cause: ${error.cause.description}` : ""}`
      });
    }

    throw new BadRequestError({
      message: `Failed to validate credentials: ${(error as Error)?.message || "verify credentials"}`
    });
  }

  if (method === GitLabConnectionMethod.OAuth && oauthData) {
    return {
      accessToken,
      instanceUrl: inputCredentials.instanceUrl,
      refreshToken: oauthData.refresh_token,
      expiresAt: new Date(Date.now() + oauthData.expires_in * 1000 - 60000),
      tokenType: oauthData.token_type,
      createdAt: new Date(oauthData.created_at * 1000)
    };
  }

  return inputCredentials;
};

export const getGitLabConnectionClient = async (
  appConnection: TGitLabConnection,
  appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">,
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
) => {
  let { accessToken } = appConnection.credentials;

  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
    );
  }

  const client = await getGitLabClient(
    accessToken,
    appConnection.credentials.instanceUrl,
    appConnection.method === GitLabConnectionMethod.OAuth
  );

  return client;
};

const throwGitLabListError = (error: unknown, resource: "groups" | "projects"): never => {
  // Gitbeaker retries 429 responses and exposes the final status only in the retry error's message.
  const isRateLimited =
    (error instanceof GitbeakerRequestError && error.cause?.response.status === HttpStatusCode.TooManyRequests) ||
    (error instanceof GitbeakerRetryError && /last status code: 429\b/.test(error.message));

  if (isRateLimited) {
    throw new RateLimitError({
      message: `GitLab rate limit reached while loading ${resource}. Wait a moment and try again.`
    });
  }

  if (error instanceof GitbeakerRequestError) {
    throw new BadRequestError({
      message: `Failed to fetch GitLab ${resource}: ${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 ${resource}`
  });
};

export const listGitLabProjects = async ({
  appConnection,
  appConnectionDAL,
  kmsService,
  search,
  limit
}: {
  appConnection: TGitLabConnection;
  appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  search?: string;
  limit?: number;
}): Promise<TGitLabProject[]> => {
  let { accessToken } = appConnection.credentials;

  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
    );
  }

  try {
    const client = await getGitLabClient(
      accessToken,
      appConnection.credentials.instanceUrl,
      appConnection.method === GitLabConnectionMethod.OAuth
    );
    const projects = await client.Projects.all({
      pagination: "offset",
      ...(limit !== undefined ? { perPage: limit } : {}),
      ...(search ? { search } : {}),
      maxPages: 1,
      archived: false,
      includePendingDelete: false,
      membership: true,
      includeHidden: false,
      imported: false,
      searchNamespaces: !!search
    });

    return projects.map((project) => ({
      name: project.pathWithNamespace,
      id: project.id.toString()
    }));
  } catch (error: unknown) {
    return throwGitLabListError(error, "projects");
  }
};

export const listGitLabGroups = async ({
  appConnection,
  appConnectionDAL,
  kmsService,
  search,
  limit
}: {
  appConnection: TGitLabConnection;
  appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">;
  kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
  search?: string;
  limit?: number;
}): Promise<TGitLabGroup[]> => {
  const client = await getNavigationClient({ appConnection, appConnectionDAL, kmsService });
  if (!client) return [];

  try {
    const groups = await client.Groups.all({
      pagination: "offset",
      ...(limit !== undefined ? { perPage: limit } : {}),
      maxPages: 1,
      orderBy: "name",
      sort: "asc",
      minAccessLevel: 50,
      ...(search ? { search } : {})
    });

    return groups.map((group) => ({
      id: group.id.toString(),
      name: group.name,
      fullName: group.fullName,
      fullPath: group.fullPath
    }));
  } catch (error: unknown) {
    return throwGitLabListError(error, "groups");
  }
};