Skip to content

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

use gateway in case of id set

Thiago Araujo da Silvaef21f1120709
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index ee0d9e4c57..c39568105d 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1030,12 +1030,7 @@ export const registerRoutes = async (
membershipGroupDAL
});
- const gitHubAppService = gitHubAppServiceFactory({
- gitHubAppDAL,
- permissionService,
- kmsService,
- keyStore
- });
+ // gitHubAppService is created after gatewayPoolService (below) due to dependency on gateway services
// ldapService is created after loginService (below) due to dependency on processProviderCallback
@@ -1653,6 +1648,18 @@ export const registerRoutes = async (
dynamicSecretDAL
});
+ const gitHubAppService = gitHubAppServiceFactory({
+ gitHubAppDAL,
+ permissionService,
+ kmsService,
+ keyStore,
+ licenseService,
+ gatewayService,
+ gatewayV2Service,
+ gatewayDAL,
+ gatewayV2DAL
+ });
+
const secretSyncQueue = secretSyncQueueFactory({
queueService,
cronJob,
diff --git a/backend/src/server/routes/v1/github-app-router.ts b/backend/src/server/routes/v1/github-app-router.ts
index 2d2e93d9c3..8550a2d9d1 100644
--- a/backend/src/server/routes/v1/github-app-router.ts
+++ b/backend/src/server/routes/v1/github-app-router.ts
@@ -101,6 +101,7 @@ export const registerGitHubAppRouter = async (server: FastifyZodProvider) => {
gitHubAppId: z.string().uuid().optional(),
host: z.string().trim().optional(),
instanceType: z.enum(["cloud", "server"]).optional(),
+ gatewayId: z.string().uuid().optional(),
projectId: z.string().optional()
}),
response: {
@@ -123,6 +124,7 @@ export const registerGitHubAppRouter = async (server: FastifyZodProvider) => {
gitHubAppId: req.body.gitHubAppId,
host: req.body.host,
instanceType: req.body.instanceType,
+ gatewayId: req.body.gatewayId,
projectId: req.body.projectId
});
diff --git a/backend/src/services/github-app/github-app-service.ts b/backend/src/services/github-app/github-app-service.ts
index d080c2c980..455179cc83 100644
--- a/backend/src/services/github-app/github-app-service.ts
+++ b/backend/src/services/github-app/github-app-service.ts
@@ -1,8 +1,17 @@
import { ForbiddenError } from "@casl/ability";
-import { AxiosError } from "axios";
+import { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
import { ActionProjectType, OrganizationActionScope } from "@app/db/schemas";
-import { OrgPermissionAppConnectionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
+import { TGatewayDALFactory } from "@app/ee/services/gateway/gateway-dal";
+import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
+import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
+import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
+import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
+import {
+ OrgPermissionAppConnectionActions,
+ OrgPermissionGatewayActions,
+ OrgPermissionSubjects
+} from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
ProjectPermissionAppConnectionActions,
@@ -18,13 +27,16 @@ import { safeRequest } from "@app/lib/validator/safe-request";
import {
assertPlatformGitHubHostAllowed,
buildGitHubAppJwtHeaders,
+ getGitHubGatewayConnectionDetails,
getGitHubInstanceApiUrl,
GithubTokenRespData,
isGithubErrorResponse,
listGitHubUserInstallations,
+ requestWithGitHubGateway,
resolveGitHubAppCredentials,
signGitHubAppInstallationsToken,
- signGitHubAppJwt
+ signGitHubAppJwt,
+ TGitHubUserInstallation
} from "@app/services/app-connection/github/github-connection-fns";
import { ActorAuthMethod, ActorType } from "@app/services/auth/auth-type";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
@@ -47,6 +59,11 @@ type TGitHubAppServiceFactoryDep = {
permissionService: Pick<TPermissionServiceFactory, "getOrgPermission" | "getProjectPermission">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
keyStore: Pick<TKeyStoreFactory, "setItemWithExpiryNX" | "deleteItem">;
+ licenseService: Pick<TLicenseServiceFactory, "getPlan">;
+ gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">;
+ gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
+ gatewayDAL: Pick<TGatewayDALFactory, "find">;
+ gatewayV2DAL: Pick<TGatewayV2DALFactory, "find">;
};
export type TGitHubAppServiceFactory = ReturnType<typeof gitHubAppServiceFactory>;
@@ -55,7 +72,12 @@ export const gitHubAppServiceFactory = ({
gitHubAppDAL,
permissionService,
kmsService,
- keyStore
+ keyStore,
+ licenseService,
+ gatewayService,
+ gatewayV2Service,
+ gatewayDAL,
+ gatewayV2DAL
}: TGitHubAppServiceFactoryDep) => {
const listGitHubApps = async ({ orgPermission }: TListGitHubAppsDTO): Promise<TSanitizedGitHubApp[]> => {
const { permission } = await permissionService.getOrgPermission({
@@ -481,13 +503,22 @@ export const gitHubAppServiceFactory = ({
gitHubAppId,
host,
instanceType,
+ gatewayId,
projectId,
orgPermission
}: TResolveGitHubAppInstallationsDTO): Promise<{
installations: TGitHubAppInstallation[];
installationsToken: string;
}> => {
- // mirror the permission required to create the connection this flow feeds into
+ const { permission: orgScopedPermission } = await permissionService.getOrgPermission({
+ actor: orgPermission.type,
+ actorId: orgPermission.id,
+ orgId: orgPermission.orgId,
+ actorAuthMethod: orgPermission.authMethod,
+ actorOrgId: orgPermission.orgId,
+ scope: OrganizationActionScope.Any
+ });
+
if (projectId) {
const { permission } = await permissionService.getProjectPermission({
actor: orgPermission.type,
@@ -503,21 +534,35 @@ export const gitHubAppServiceFactory = ({
ProjectPermissionSub.AppConnections
);
} else {
- const { permission } = await permissionService.getOrgPermission({
- actor: orgPermission.type,
- actorId: orgPermission.id,
- orgId: orgPermission.orgId,
- actorAuthMethod: orgPermission.authMethod,
- actorOrgId: orgPermission.orgId,
- scope: OrganizationActionScope.Any
- });
-
- ForbiddenError.from(permission).throwUnlessCan(
+ ForbiddenError.from(orgScopedPermission).throwUnlessCan(
OrgPermissionAppConnectionActions.Create,
OrgPermissionSubjects.AppConnections
);
}
+ if (gatewayId) {
+ const plan = await licenseService.getPlan(orgPermission.orgId);
+ if (!plan.gateway) {
+ throw new BadRequestError({
+ message:
+ "Your current plan does not support gateway usage with app connections. Please upgrade your plan or contact Infisical Sales for assistance."
+ });
+ }
+
+ ForbiddenError.from(orgScopedPermission).throwUnlessCan(
+ OrgPermissionGatewayActions.AttachGateways,
+ OrgPermissionSubjects.Gateway
+ );
+
+ const [gateway] = await gatewayDAL.find({ id: gatewayId, orgId: orgPermission.orgId });
+ const [gatewayV2] = await gatewayV2DAL.find({ id: gatewayId, orgId: orgPermission.orgId });
+ if (!gateway && !gatewayV2) {
+ throw new NotFoundError({
+ message: `Gateway with ID ${gatewayId} not found for org`
+ });
+ }
+ }
+
const { SITE_URL } = getConfig();
if (!SITE_URL) {
throw new BadRequestError({ message: "SITE_URL is not configured." });
@@ -546,22 +591,42 @@ export const gitHubAppServiceFactory = ({
const oauthHost = effectiveHost ?? "github.com";
+ const sendGitHubRequest = async <T>(
+ requestConfig: AxiosRequestConfig & { url: string },
+ gatewayConnectionDetails?: Awaited<ReturnType<typeof getGitHubGatewayConnectionDetails>>
+ ): Promise<AxiosResponse<T>> =>
+ gatewayId
+ ? requestWithGitHubGateway<T>(
+ { gatewayId },
+ gatewayService,
+ gatewayV2Service,
+ requestConfig,
+ gatewayConnectionDetails
+ )
+ : safeRequest.request<T>(requestConfig);
+
+ const oauthGatewayConnectionDetails = gatewayId
+ ? await getGitHubGatewayConnectionDetails(gatewayId, oauthHost, gatewayV2Service)
+ : undefined;
+
let tokenData: GithubTokenRespData;
try {
- const { data } = await safeRequest.post<GithubTokenRespData>(
- `https://${oauthHost}/login/oauth/access_token`,
- {
- client_id: clientId,
- client_secret: clientSecret,
- code,
- redirect_uri: `${SITE_URL}/organization/app-connections/github/oauth/callback`
- },
+ const { data } = await sendGitHubRequest<GithubTokenRespData>(
{
+ url: `https://${oauthHost}/login/oauth/access_token`,
+ method: "POST",
+ data: {
+ client_id: clientId,
+ client_secret: clientSecret,
+ code,
+ redirect_uri: `${SITE_URL}/organization/app-connections/github/oauth/callback`
+ },
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
- }
+ },
+ oauthGatewayConnectionDetails
);
tokenData = data;
} catch (err) {
@@ -581,8 +646,12 @@ export const gitHubAppServiceFactory = ({
credentials: { host: effectiveHost, instanceType: effectiveInstanceType }
});
+ const apiGatewayConnectionDetails = gatewayId
+ ? await getGitHubGatewayConnectionDetails(gatewayId, apiBaseUrl, gatewayV2Service)
+ : undefined;
+
const installations = await listGitHubUserInstallations(apiBaseUrl, tokenData.access_token, (requestConfig) =>
- safeRequest.request(requestConfig)
+ sendGitHubRequest<{ installations: TGitHubUserInstallation[] }>(requestConfig, apiGatewayConnectionDetails)
);
const appInstallations = installations.filter((installation) => String(installation.app_id) === appId);
diff --git a/backend/src/services/github-app/github-app-types.ts b/backend/src/services/github-app/github-app-types.ts
index b6bfa86463..eb62993eca 100644
--- a/backend/src/services/github-app/github-app-types.ts
+++ b/backend/src/services/github-app/github-app-types.ts
@@ -38,6 +38,7 @@ export type TResolveGitHubAppInstallationsDTO = {
gitHubAppId?: string;
host?: string;
instanceType?: "cloud" | "server";
+ gatewayId?: string | null;
projectId?: string;
orgPermission: OrgServiceActor;
};
diff --git a/frontend/src/hooks/api/gitHubApps/queries.tsx b/frontend/src/hooks/api/gitHubApps/queries.tsx
index 929de61fba..2a7ff28f52 100644
--- a/frontend/src/hooks/api/gitHubApps/queries.tsx
+++ b/frontend/src/hooks/api/gitHubApps/queries.tsx
@@ -19,6 +19,7 @@ export const resolveGitHubAppInstallations = async (params: {
gitHubAppId?: string;
host?: string;
instanceType?: "cloud" | "server";
+ gatewayId?: string;
projectId?: string;
}) => {
const { data } = await apiRequest.post<{
diff --git a/frontend/src/pages/organization/AppConnections/OauthCallbackPage/OauthCallbackPage.tsx b/frontend/src/pages/organization/AppConnections/OauthCallbackPage/OauthCallbackPage.tsx
index 3694b045b2..88008cb20d 100644
--- a/frontend/src/pages/organization/AppConnections/OauthCallbackPage/OauthCallbackPage.tsx
+++ b/frontend/src/pages/organization/AppConnections/OauthCallbackPage/OauthCallbackPage.tsx
@@ -430,6 +430,7 @@ export const OAuthCallbackPage = () => {
gitHubAppId: storedGitHubAppId ?? undefined,
...(credentials?.instanceType && { instanceType: credentials.instanceType }),
...(credentials?.host && { host: credentials.host }),
+ ...(gatewayId && { gatewayId }),
...(projectId && { projectId })
});
setGitHubPicker({ ...result, formData });