Skip to content

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

fix(gateway): scope the Kubernetes review proxy to the caller's org

bernie-g5d5c1a517509
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index f534efd39e..a7d366be58 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1222,7 +1222,7 @@ export const gatewayV2ServiceFactory = ({
// the network, so they run before the transaction is opened rather than inside it.
let authMethodArg = authMethod;
if (authMethod.method === "kubernetes") {
- await resourceAuthMethodService.preflightKubernetesConfig(authMethod.config);
+ await resourceAuthMethodService.preflightKubernetesConfig(authMethod.config, orgId);
authMethodArg = {
method: authMethod.method,
config: {
diff --git a/backend/src/ee/services/resource-auth-method/resource-auth-method-service.ts b/backend/src/ee/services/resource-auth-method/resource-auth-method-service.ts
index fbff9326c2..119a390c94 100644
--- a/backend/src/ee/services/resource-auth-method/resource-auth-method-service.ts
+++ b/backend/src/ee/services/resource-auth-method/resource-auth-method-service.ts
@@ -8,6 +8,7 @@ import { TIdentityDALFactory } from "@app/services/identity/identity-dal";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
+import { TGatewayPoolDALFactory } from "../gateway-pool/gateway-pool-dal";
import { TGatewayV2DALFactory } from "../gateway-v2/gateway-v2-dal";
import { TKmipServerDALFactory } from "../kmip-server/kmip-server-dal";
import {
@@ -79,6 +80,7 @@ type TResourceAuthMethodServiceFactoryDep = {
resourceTokenAuthDAL: TResourceTokenAuthDALFactory;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
gatewayV2DAL: Pick<TGatewayV2DALFactory, "findById" | "updateById">;
+ gatewayPoolDAL: Pick<TGatewayPoolDALFactory, "findById">;
relayDAL: Pick<TRelayDALFactory, "findById" | "updateById">;
kmipServerDAL: Pick<TKmipServerDALFactory, "findById" | "updateById">;
identityDAL: Pick<TIdentityDALFactory, "findById">;
@@ -122,6 +124,7 @@ export const resourceAuthMethodServiceFactory = ({
resourceTokenAuthDAL,
kmsService,
gatewayV2DAL,
+ gatewayPoolDAL,
relayDAL,
kmipServerDAL,
identityDAL,
@@ -358,14 +361,17 @@ export const resourceAuthMethodServiceFactory = ({
// Picks the route to the API server: straight out from Infisical, or tunnelled through a gateway.
// Makes network calls when proxied, so call it outside a transaction.
- const $buildKubernetesExecutor = async (config: {
- kubernetesHost?: string | null;
- caCertificate?: string;
- verifyTlsCertificate: boolean;
- tokenReviewMode?: string | null;
- gatewayV2Id?: string | null;
- gatewayPoolId?: string | null;
- }): Promise<{ executor: TKubernetesRequestExecutor; target: string; isGatewayReviewer: boolean }> => {
+ const $buildKubernetesExecutor = async (
+ config: {
+ kubernetesHost?: string | null;
+ caCertificate?: string;
+ verifyTlsCertificate: boolean;
+ tokenReviewMode?: string | null;
+ gatewayV2Id?: string | null;
+ gatewayPoolId?: string | null;
+ },
+ orgId: string
+ ): Promise<{ executor: TKubernetesRequestExecutor; target: string; isGatewayReviewer: boolean }> => {
const proxyId = config.gatewayV2Id ?? config.gatewayPoolId;
const isGatewayReviewer = config.tokenReviewMode === KubernetesTokenReviewMode.Gateway;
@@ -392,6 +398,22 @@ export const resourceAuthMethodServiceFactory = ({
});
}
+ // Without this an editor could name another tenant's gateway UUID and have us mint proxy
+ // credentials for it, sending requests into that tenant's network. The resolvers below look
+ // their argument up by id alone, so ownership has to be established here.
+ if (config.gatewayV2Id) {
+ const proxyGateway = await gatewayV2DAL.findById(config.gatewayV2Id);
+ if (!proxyGateway || proxyGateway.orgId !== orgId) {
+ throw new NotFoundError({ message: `Gateway with ID '${config.gatewayV2Id}' not found` });
+ }
+ }
+ if (config.gatewayPoolId) {
+ const proxyPool = await gatewayPoolDAL.findById(config.gatewayPoolId);
+ if (!proxyPool || proxyPool.orgId !== orgId) {
+ throw new NotFoundError({ message: `Gateway pool with ID '${config.gatewayPoolId}' not found` });
+ }
+ }
+
const { targetHost, targetPort } = resolveKubernetesProxyTarget(proxiedHost ?? undefined);
const connectionDetails = await gatewayProxyRegistry.resolve({
gatewayV2Id: config.gatewayV2Id,
@@ -432,6 +454,7 @@ export const resourceAuthMethodServiceFactory = ({
| "gatewayV2Id"
| "gatewayPoolId"
>,
+ orgId: string,
// Absent when the resource does not exist yet, where it cannot be its own proxy.
resource?: ResourceRef
) => {
@@ -442,14 +465,17 @@ export const resourceAuthMethodServiceFactory = ({
await assertKubernetesHostAllowed(config.kubernetesHost);
}
- const { executor, target, isGatewayReviewer } = await $buildKubernetesExecutor({
- kubernetesHost: config.kubernetesHost,
- caCertificate: config.caCertificate,
- verifyTlsCertificate: config.verifyTlsCertificate,
- tokenReviewMode: config.tokenReviewMode,
- gatewayV2Id: config.gatewayV2Id,
- gatewayPoolId: config.gatewayPoolId
- });
+ const { executor, target, isGatewayReviewer } = await $buildKubernetesExecutor(
+ {
+ kubernetesHost: config.kubernetesHost,
+ caCertificate: config.caCertificate,
+ verifyTlsCertificate: config.verifyTlsCertificate,
+ tokenReviewMode: config.tokenReviewMode,
+ gatewayV2Id: config.gatewayV2Id,
+ gatewayPoolId: config.gatewayPoolId
+ },
+ orgId
+ );
await validateKubernetesConfigReachable({
executor,
@@ -625,14 +651,17 @@ export const resourceAuthMethodServiceFactory = ({
assertKubernetesProxyNotSelf(resource, authMethod.gatewayV2Id);
- const validation = await $buildKubernetesExecutor({
- kubernetesHost: authMethod.kubernetesHost,
- caCertificate: effectiveCa,
- verifyTlsCertificate: authMethod.verifyTlsCertificate,
- tokenReviewMode: authMethod.tokenReviewMode,
- gatewayV2Id: authMethod.gatewayV2Id,
- gatewayPoolId: authMethod.gatewayPoolId
- });
+ const validation = await $buildKubernetesExecutor(
+ {
+ kubernetesHost: authMethod.kubernetesHost,
+ caCertificate: effectiveCa,
+ verifyTlsCertificate: authMethod.verifyTlsCertificate,
+ tokenReviewMode: authMethod.tokenReviewMode,
+ gatewayV2Id: authMethod.gatewayV2Id,
+ gatewayPoolId: authMethod.gatewayPoolId
+ },
+ actor.orgId
+ );
await validateKubernetesConfigReachable({
executor: validation.executor,
@@ -945,14 +974,17 @@ export const resourceAuthMethodServiceFactory = ({
}
}
- const login = await $buildKubernetesExecutor({
- kubernetesHost: config.kubernetesHost,
- caCertificate,
- verifyTlsCertificate: config.verifyTlsCertificate,
- tokenReviewMode: config.tokenReviewMode,
- gatewayV2Id: config.gatewayV2Id,
- gatewayPoolId: config.gatewayPoolId
- });
+ const login = await $buildKubernetesExecutor(
+ {
+ kubernetesHost: config.kubernetesHost,
+ caCertificate,
+ verifyTlsCertificate: config.verifyTlsCertificate,
+ tokenReviewMode: config.tokenReviewMode,
+ gatewayV2Id: config.gatewayV2Id,
+ gatewayPoolId: config.gatewayPoolId
+ },
+ resourceOrgId
+ );
const { namespace, serviceAccountName, audiences } = await reviewServiceAccountToken({
jwt,
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 3d46f2d516..0c08ba8dd4 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1798,6 +1798,7 @@ export const registerRoutes = async (
resourceTokenAuthDAL,
kmsService,
gatewayV2DAL,
+ gatewayPoolDAL,
relayDAL,
kmipServerDAL,
identityDAL,