Skip to content

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

feat(pam): connection test for rest of accounts

xfab247bf76a0
diff --git a/backend/src/ee/services/pam-account/pam-account-connection-test.ts b/backend/src/ee/services/pam-account/pam-account-connection-test.ts
index dcc2368af5..d05eb7b751 100644
--- a/backend/src/ee/services/pam-account/pam-account-connection-test.ts
+++ b/backend/src/ee/services/pam-account/pam-account-connection-test.ts
@@ -1,7 +1,17 @@
import { PamAccountType, PamSshAuthMethod } from "../pam/pam-enums";
+import {
+ AWS_STS_MIN_DURATION_SECONDS,
+ generateAwsIamSessionCredentials
+} from "../pam-session/aws-iam/aws-iam-federation";
+import { AZURE_SCOPES, getAzureAccessToken } from "../pam-session/azure/azure-federation";
+import { mintGcpAccessToken } from "../pam-session/gcp/gcp-federation";
+import { extractGatewayTarget, isCredentialConfigured } from "./pam-account-schemas";
export enum TestConnectionMode {
- Postgres = "postgres",
+ SQL = "sql",
+ MongoDB = "mongodb",
+ LDAP = "ldap",
+ Kubernetes = "kubernetes",
SSH = "ssh",
Tcp = "tcp"
}
@@ -9,7 +19,8 @@ export enum TestConnectionMode {
// the request the gateway's /v1/test-connection handler expects, discriminated by `mode`
export type TestConnectionRequest =
| {
- mode: TestConnectionMode.Postgres;
+ mode: TestConnectionMode.SQL;
+ dialect: "postgres" | "mysql" | "mssql";
username: string;
password?: string;
database: string;
@@ -18,50 +29,194 @@ export type TestConnectionRequest =
sslCertificate?: string;
}
| {
- mode: TestConnectionMode.SSH;
- authMethod: string;
+ mode: TestConnectionMode.MongoDB;
username: string;
password?: string;
- privateKey?: string;
- certificate?: string;
+ database: string;
+ sslEnabled?: boolean;
+ sslRejectUnauthorized?: boolean;
+ sslCertificate?: string;
+ }
+ | {
+ mode: TestConnectionMode.LDAP;
+ username: string;
+ password?: string;
+ useLdaps?: boolean;
+ ldapRejectUnauthorized?: boolean;
+ ldapCaCert?: string;
+ ldapTlsServerName?: string;
}
+ | { mode: TestConnectionMode.Kubernetes; token: string; sslRejectUnauthorized?: boolean; sslCertificate?: string }
+ | { mode: TestConnectionMode.SSH; authMethod: string; username: string; password?: string; privateKey?: string }
| { mode: TestConnectionMode.Tcp };
-type ConnectionTestBuilder = (
+const SQL_DIALECTS = {
+ [PamAccountType.Postgres]: "postgres",
+ [PamAccountType.MySQL]: "mysql",
+ [PamAccountType.MsSQL]: "mssql"
+} as const;
+
+const tcp = (host: string, port: number) => ({ host, port, request: { mode: TestConnectionMode.Tcp } as const });
+
+// resolves the gateway target and the per-type auth request
+export const buildGatewayConnectionTest = async (
+ accountType: PamAccountType,
connectionDetails: Record<string, unknown>,
- credentials: Record<string, unknown>
-) => TestConnectionRequest;
+ credentials: Record<string, unknown> | null
+): Promise<{ host: string; port: number; request: TestConnectionRequest } | null> => {
+ const creds = credentials && isCredentialConfigured(accountType, credentials) ? credentials : null;
-// single source of truth for which account types support a connection test and how each is tested
-export const PAM_CONNECTION_TEST_BUILDERS: Partial<Record<PamAccountType, ConnectionTestBuilder>> = {
- [PamAccountType.Postgres]: (connectionDetails, credentials) => {
- const cd = connectionDetails as {
- database: string;
- sslEnabled?: boolean;
- sslRejectUnauthorized?: boolean;
- sslCertificate?: string;
- };
- const creds = credentials as { username: string; password?: string };
- return {
- mode: TestConnectionMode.Postgres,
- username: creds.username,
- password: creds.password,
- database: cd.database,
- sslEnabled: cd.sslEnabled,
- sslRejectUnauthorized: cd.sslRejectUnauthorized,
- sslCertificate: cd.sslCertificate
- };
+ const target =
+ accountType === PamAccountType.WindowsAd
+ ? {
+ host: (connectionDetails as { dcAddress: string }).dcAddress,
+ port: (connectionDetails as { port: number }).port
+ }
+ : await extractGatewayTarget(accountType, connectionDetails);
+ if (target.port === undefined) return null;
+ const { host, port } = target as { host: string; port: number };
+
+ switch (accountType) {
+ case PamAccountType.Postgres:
+ case PamAccountType.MySQL:
+ case PamAccountType.MsSQL: {
+ const cd = connectionDetails as {
+ database: string;
+ sslEnabled?: boolean;
+ sslRejectUnauthorized?: boolean;
+ sslCertificate?: string;
+ };
+ const c = creds as { authMethod?: string; username: string; password?: string } | null;
+ if (!c || (accountType === PamAccountType.MsSQL && c.authMethod !== "sql-login")) return tcp(host, port);
+ return {
+ host,
+ port,
+ request: {
+ mode: TestConnectionMode.SQL,
+ dialect: SQL_DIALECTS[accountType],
+ username: c.username,
+ password: c.password,
+ database: cd.database,
+ sslEnabled: cd.sslEnabled,
+ sslRejectUnauthorized: cd.sslRejectUnauthorized,
+ sslCertificate: cd.sslCertificate
+ }
+ };
+ }
+ case PamAccountType.MongoDB: {
+ const cd = connectionDetails as {
+ database: string;
+ sslEnabled?: boolean;
+ sslRejectUnauthorized?: boolean;
+ sslCertificate?: string;
+ };
+ const c = creds as { username: string; password?: string } | null;
+ if (!c) return tcp(host, port);
+ return {
+ host,
+ port,
+ request: {
+ mode: TestConnectionMode.MongoDB,
+ username: c.username,
+ password: c.password,
+ database: cd.database,
+ sslEnabled: cd.sslEnabled,
+ sslRejectUnauthorized: cd.sslRejectUnauthorized,
+ sslCertificate: cd.sslCertificate
+ }
+ };
+ }
+ case PamAccountType.Kubernetes: {
+ const cd = connectionDetails as { sslRejectUnauthorized?: boolean; sslCertificate?: string };
+ const c = creds as { authMethod: string; serviceAccountToken?: string } | null;
+ if (!c || c.authMethod !== "service-account-token" || !c.serviceAccountToken) return tcp(host, port);
+ return {
+ host,
+ port,
+ request: {
+ mode: TestConnectionMode.Kubernetes,
+ token: c.serviceAccountToken,
+ sslRejectUnauthorized: cd.sslRejectUnauthorized,
+ sslCertificate: cd.sslCertificate
+ }
+ };
+ }
+ case PamAccountType.WindowsAd: {
+ const cd = connectionDetails as {
+ useLdaps?: boolean;
+ ldapRejectUnauthorized?: boolean;
+ ldapCaCert?: string;
+ ldapTlsServerName?: string;
+ };
+ const c = creds as { username: string; password?: string } | null;
+ if (!c) return tcp(host, port);
+ return {
+ host,
+ port,
+ request: {
+ mode: TestConnectionMode.LDAP,
+ username: c.username,
+ password: c.password,
+ useLdaps: cd.useLdaps,
+ ldapRejectUnauthorized: cd.ldapRejectUnauthorized,
+ ldapCaCert: cd.ldapCaCert,
+ ldapTlsServerName: cd.ldapTlsServerName
+ }
+ };
+ }
+ case PamAccountType.SSH: {
+ const c = creds as { authMethod: string; username: string; password?: string; privateKey?: string } | null;
+ if (!c || c.authMethod === PamSshAuthMethod.Certificate) return tcp(host, port);
+ return {
+ host,
+ port,
+ request: {
+ mode: TestConnectionMode.SSH,
+ authMethod: c.authMethod,
+ username: c.username,
+ password: c.password,
+ privateKey: c.privateKey
+ }
+ };
+ }
+ case PamAccountType.Windows:
+ return tcp(host, port);
+ default:
+ return null;
+ }
+};
+
+// cloud accounts have no gateway-reachable host; the test validates the brokering credentials instead
+type CloudConnectionValidator = (params: {
+ connectionDetails: Record<string, unknown>;
+ credentials: Record<string, unknown> | null;
+ orgId: string;
+}) => Promise<void>;
+
+export const CLOUD_CONNECTION_VALIDATORS: Partial<Record<PamAccountType, CloudConnectionValidator>> = {
+ [PamAccountType.AwsIam]: async ({ connectionDetails, orgId }) => {
+ await generateAwsIamSessionCredentials({
+ roleArn: connectionDetails.roleArn as string,
+ externalId: orgId,
+ roleSessionName: "infisical-pam-connection-test",
+ sessionDuration: AWS_STS_MIN_DURATION_SECONDS
+ });
},
- [PamAccountType.SSH]: (_connectionDetails, credentials) => {
- const creds = credentials as { authMethod: string; username: string; password?: string; privateKey?: string };
- if (creds.authMethod === PamSshAuthMethod.Certificate) return { mode: TestConnectionMode.Tcp };
- return {
- mode: TestConnectionMode.SSH,
- authMethod: creds.authMethod,
- username: creds.username,
- password: creds.password,
- privateKey: creds.privateKey
- };
+ [PamAccountType.AzureCli]: async ({ connectionDetails, credentials }) => {
+ if (!credentials) return;
+ await getAzureAccessToken({
+ tenantId: connectionDetails.tenantId as string,
+ clientId: credentials.clientId as string,
+ clientSecret: credentials.clientSecret as string,
+ scope: AZURE_SCOPES.arm
+ });
},
- [PamAccountType.Windows]: () => ({ mode: TestConnectionMode.Tcp })
+ [PamAccountType.GcpServiceAccount]: async ({ connectionDetails, credentials }) => {
+ if (!credentials) return;
+ await mintGcpAccessToken({
+ serviceAccountEmail: connectionDetails.serviceAccountEmail as string,
+ authMethod: credentials.authMethod as string,
+ serviceAccountKeyJson: credentials.serviceAccountKeyJson as string | undefined
+ });
+ }
};
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.ts b/backend/src/ee/services/pam-account/pam-account-schemas.ts
index fc0e058baf..9da9260cb3 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.ts
@@ -665,7 +665,7 @@ export const extractGatewayTarget = async (
case PamAccountType.Kubernetes: {
const { url } = validated as { url: string };
const parsed = new URL(url);
- return { host: parsed.hostname };
+ return { host: parsed.hostname, port: parsed.port ? Number(parsed.port) : 443 };
}
case PamAccountType.MongoDB: {
const { connectionString } = validated as { connectionString: string };
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index a5b2d74394..44e74a0eaf 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -43,7 +43,7 @@ import { TPamAccessRequestServiceFactory } from "../pam-access-request/pam-acces
import { TPamAccountTemplateDALFactory } from "../pam-account-template/pam-account-template-dal";
import { PamTemplateSettingsSchema } from "../pam-account-template/pam-account-template-schemas";
import { TPamFolderDALFactory } from "../pam-folder/pam-folder-dal";
-import { PAM_CONNECTION_TEST_BUILDERS, TestConnectionMode } from "./pam-account-connection-test";
+import { buildGatewayConnectionTest, CLOUD_CONNECTION_VALIDATORS } from "./pam-account-connection-test";
import { TPamAccountDALFactory } from "./pam-account-dal";
import {
getAccountAccessibilityIssues,
@@ -293,10 +293,23 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
gatewayPoolId?: string | null;
templateGatewayId?: string | null;
templateGatewayPoolId?: string | null;
- }
+ },
+ orgId: string
): Promise<void> => {
- const buildRequest = PAM_CONNECTION_TEST_BUILDERS[accountType];
- if (!buildRequest) return;
+ const validateCloud = CLOUD_CONNECTION_VALIDATORS[accountType];
+ if (validateCloud) {
+ try {
+ await validateCloud({ connectionDetails, credentials, orgId });
+ } catch (err) {
+ throw new BadRequestError({
+ message: `Connection test failed: ${err instanceof Error ? err.message : "unable to validate credentials"}`
+ });
+ }
+ return;
+ }
+
+ const test = await buildGatewayConnectionTest(accountType, connectionDetails, credentials);
+ if (!test) return;
const effectiveGatewayId = gateway.gatewayId ?? gateway.templateGatewayId;
const gatewayId = await gatewayPoolService.resolveEffectiveGatewayId({
@@ -308,18 +321,12 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
throw new BadRequestError({ message: "A gateway must be attached to this account." });
}
- const cd = connectionDetails as { host: string; port: number };
- const request =
- credentials === null || !isCredentialConfigured(accountType, credentials)
- ? { mode: TestConnectionMode.Tcp }
- : buildRequest(connectionDetails, credentials);
-
const result = await testConnectionWithGateway(
- cd.host,
- cd.port,
+ test.host,
+ test.port,
gatewayId,
gatewayV2Service,
- request,
+ test.request,
CONNECTION_TEST_TIMEOUT_MS
);
@@ -416,12 +423,18 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
// discovery import creates accounts in bulk from a scan that already reached them, so it skips the test
if (!skipConnectionTest) {
- await assertConnectionOk(accountType, validatedConnectionDetails, validatedCredentials, {
- gatewayId,
- gatewayPoolId,
- templateGatewayId: template.gatewayId,
- templateGatewayPoolId: template.gatewayPoolId
- });
+ await assertConnectionOk(
+ accountType,
+ validatedConnectionDetails,
+ validatedCredentials,
+ {
+ gatewayId,
+ gatewayPoolId,
+ templateGatewayId: template.gatewayId,
+ templateGatewayPoolId: template.gatewayPoolId
+ },
+ ctx.actorOrgId
+ );
}
const encryptedConnectionDetails = await encrypt(projectId, validatedConnectionDetails);
@@ -632,12 +645,18 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
// only test with credentials supplied in this request to prevent exfiltration
const testCredentials = credentials ? validateCredentials(accountType, credentials) : null;
- await assertConnectionOk(accountType, effectiveConnectionDetails, testCredentials, {
- gatewayId: gatewayId !== undefined ? gatewayId : existing.gatewayId,
- gatewayPoolId: gatewayPoolId !== undefined ? gatewayPoolId : existing.gatewayPoolId,
- templateGatewayId: template ? template.gatewayId : existing.templateGatewayId,
- templateGatewayPoolId: template ? template.gatewayPoolId : existing.templateGatewayPoolId
- });
+ await assertConnectionOk(
+ accountType,
+ effectiveConnectionDetails,
+ testCredentials,
+ {
+ gatewayId: gatewayId !== undefined ? gatewayId : existing.gatewayId,
+ gatewayPoolId: gatewayPoolId !== undefined ? gatewayPoolId : existing.gatewayPoolId,
+ templateGatewayId: template ? template.gatewayId : existing.templateGatewayId,
+ templateGatewayPoolId: template ? template.gatewayPoolId : existing.templateGatewayPoolId
+ },
+ ctx.actorOrgId
+ );
}
try {
diff --git a/backend/src/ee/services/pam-session/gcp/gcp-federation.ts b/backend/src/ee/services/pam-session/gcp/gcp-federation.ts
new file mode 100644
index 0000000000..cb3ae43a50
--- /dev/null
+++ b/backend/src/ee/services/pam-session/gcp/gcp-federation.ts
@@ -0,0 +1,81 @@
+import { Impersonated, JWT } from "google-auth-library";
+
+import { getConfig } from "@app/lib/config/env";
+import { BadRequestError, InternalServerError } from "@app/lib/errors";
+import { buildGcpSourceCredential } from "@app/services/app-connection/gcp/gcp-connection-fns";
+
+import { GcpServiceAccountAuthMethod } from "../../pam/pam-enums";
+
+const CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+const IMPERSONATION_SCOPES = [CLOUD_PLATFORM_SCOPE, "https://www.googleapis.com/auth/iam"];
+
+const tokenError = (serviceAccountEmail: string, err: unknown) =>
+ new BadRequestError({
+ message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]: ${
+ err instanceof Error ? err.message : String(err)
+ }`
+ });
+
+// mints a short-lived access token for the target service account
+export const mintGcpAccessToken = async ({
+ serviceAccountEmail,
+ authMethod,
+ serviceAccountKeyJson,
+ ttlSeconds = 3600
+}: {
+ serviceAccountEmail: string;
+ authMethod: string;
+ serviceAccountKeyJson?: string;
+ ttlSeconds?: number;
+}): Promise<string> => {
+ let tokenResponse;
+
+ if (authMethod === GcpServiceAccountAuthMethod.StaticKey) {
+ const keyJson = JSON.parse(serviceAccountKeyJson as string) as { client_email: string; private_key: string };
+ const jwtClient = new JWT({
+ email: keyJson.client_email,
+ key: keyJson.private_key,
+ scopes: [CLOUD_PLATFORM_SCOPE]
+ });
+ try {
+ tokenResponse =
+ keyJson.client_email === serviceAccountEmail
+ ? await jwtClient.getAccessToken()
+ : await new Impersonated({
+ sourceClient: jwtClient,
+ targetPrincipal: serviceAccountEmail,
+ lifetime: ttlSeconds,
+ delegates: [],
+ targetScopes: IMPERSONATION_SCOPES
+ }).getAccessToken();
+ } catch (err) {
+ throw tokenError(serviceAccountEmail, err);
+ }
+ } else {
+ const appCfg = getConfig();
+ if (!appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL) {
+ throw new InternalServerError({
+ message: "Environment variable has not been configured: INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL"
+ });
+ }
+ const sourceClient = buildGcpSourceCredential(appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL);
+ try {
+ tokenResponse = await new Impersonated({
+ sourceClient,
+ targetPrincipal: serviceAccountEmail,
+ lifetime: ttlSeconds,
+ delegates: [],
+ targetScopes: IMPERSONATION_SCOPES
+ }).getAccessToken();
+ } catch (err) {
+ throw tokenError(serviceAccountEmail, err);
+ }
+ }
+
+ if (!tokenResponse?.token) {
+ throw new BadRequestError({
+ message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]`
+ });
+ }
+ return tokenResponse.token;
+};
diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.ts
index 896aa08462..8670cac5cb 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -1,4 +1,3 @@
-import { Impersonated, JWT } from "google-auth-library";
import RE2 from "re2";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
@@ -7,10 +6,8 @@ import { TPermissionServiceFactory } from "@app/ee/services/permission/permissio
import { createSshCert, createSshKeyPair } from "@app/ee/services/ssh/ssh-certificate-authority-fns";
import { SshCertType } from "@app/ee/services/ssh/ssh-certificate-authority-types";
import { SshCertKeyAlgorithm } from "@app/ee/services/ssh-certificate/ssh-certificate-types";
-import { getConfig } from "@app/lib/config/env";
-import { BadRequestError, ForbiddenRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";
+import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { ms } from "@app/lib/ms";
-import { buildGcpSourceCredential } from "@app/services/app-connection/gcp/gcp-connection-fns";
import { ActorType } from "@app/services/auth/auth-type";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
@@ -20,13 +17,7 @@ import { TMfaSessionServiceFactory } from "@app/services/mfa-session/mfa-session
import { TOrgDALFactory } from "@app/services/org/org-dal";
import { TUserDALFactory } from "@app/services/user/user-dal";
-import {
- GcpServiceAccountAuthMethod,
- PamAccessMethod,
- PamAccessStatus,
- PamAccountType,
- PamSessionStatus
-} from "../pam/pam-enums";
+import { PamAccessMethod, PamAccessStatus, PamAccountType, PamSessionStatus } from "../pam/pam-enums";
import { resolveAccountByPath as resolveAccountByPathFn } from "../pam/pam-fns";
import { enforceMfa } from "../pam/pam-mfa";
import {
@@ -67,6 +58,7 @@ import {
generateAwsIamSessionCredentials
} from "./aws-iam/aws-iam-federation";
import { getAzureAccessTokens } from "./azure/azure-federation";
+import { mintGcpAccessToken } from "./gcp/gcp-federation";
import { DEFAULT_SESSION_DURATION_MS } from "./pam-session-constants";
import { TPamSessionDALFactory } from "./pam-session-dal";
import { TPamSessionExpirationServiceFactory } from "./pam-session-expiration-queue";
@@ -232,81 +224,13 @@ export const pamSessionServiceFactory = ({
}
if (account.accountType === PamAccountType.GcpServiceAccount) {
- const serviceAccountEmail = connectionDetails.serviceAccountEmail as string;
const remainingSeconds = Math.max(1, Math.floor((new Date(session.expiresAt).getTime() - Date.now()) / 1000));
- const sessionTtlSeconds = Math.min(remainingSeconds, 3600);
-
- let tokenResponse;
-
- if (credentials.authMethod === GcpServiceAccountAuthMethod.StaticKey) {
- const keyJson = JSON.parse(credentials.serviceAccountKeyJson as string) as {
- client_email: string;
- private_key: string;
- };
- const jwtClient = new JWT({
- email: keyJson.client_email,
- key: keyJson.private_key,
- scopes: ["https://www.googleapis.com/auth/cloud-platform"]
- });
-
- if (keyJson.client_email === serviceAccountEmail) {
- try {
- tokenResponse = await jwtClient.getAccessToken();
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- throw new BadRequestError({
- message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]: ${msg}`
- });
- }
- } else {
- const impersonated = new Impersonated({
- sourceClient: jwtClient,
- targetPrincipal: serviceAccountEmail,
- lifetime: sessionTtlSeconds,
- delegates: [],
- targetScopes: ["https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/iam"]
- });
- try {
- tokenResponse = await impersonated.getAccessToken();
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- throw new BadRequestError({
- message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]: ${msg}`
- });
- }
- }
- } else {
- const appCfg = getConfig();
- if (!appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL) {
- throw new InternalServerError({
- message: "Environment variable has not been configured: INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL"
- });
- }
- const sourceClient = buildGcpSourceCredential(appCfg.INF_APP_CONNECTION_GCP_SERVICE_ACCOUNT_CREDENTIAL);
- const impersonated = new Impersonated({
- sourceClient,
- targetPrincipal: serviceAccountEmail,
- lifetime: sessionTtlSeconds,
- delegates: [],
- targetScopes: ["https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/iam"]
- });
- try {
- tokenResponse = await impersonated.getAccessToken();
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- throw new BadRequestError({
- message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]: ${msg}`
- });
- }
- }
-
- if (!tokenResponse?.token) {
- throw new BadRequestError({
- message: `Failed to obtain GCP access token for [serviceAccountEmail=${serviceAccountEmail}]`
- });
- }
-
- credentials.token = tokenResponse.token;
+ credentials.token = await mintGcpAccessToken({
+ serviceAccountEmail: connectionDetails.serviceAccountEmail as string,
+ authMethod: credentials.authMethod as string,
+ serviceAccountKeyJson: credentials.serviceAccountKeyJson as string | undefined,
+ ttlSeconds: Math.min(remainingSeconds, 3600)
+ });
delete credentials.serviceAccountKeyJson;
}
diff --git a/backend/src/ee/services/pam/CLAUDE.md b/backend/src/ee/services/pam/CLAUDE.md
index 36cf6d623c..c802c73642 100644
--- a/backend/src/ee/services/pam/CLAUDE.md
+++ b/backend/src/ee/services/pam/CLAUDE.md
@@ -63,12 +63,14 @@ frontend components**. Adding a type is mostly a config entry + an icon; the gat
`extractGatewayTarget` and `buildSessionGatewayConnectionDetails` in the same file.
**Connection test** (`assertConnectionOk`) runs inside account `create`/`update` and **throws to block the write**
-if the target can't be reached/authenticated (verified from the gateway, never the backend). Which types are
-testable and how each builds its request is one registry — `PAM_CONNECTION_TEST_BUILDERS` in
-`pam-account-connection-test.ts` (a type absent from it isn't tested; add a testable type by adding an entry).
-One gateway RPC runs it all — `testConnectionWithGateway` → the gateway's `/v1/test-connection` handler
-(`packages/gateway-v2/test_connection_handler.go`), which dispatches on a `mode`: `postgres` (pgx auth + `SELECT 1`),
-`ssh` (login via the shared exec path), or `tcp` (reachability).
+if the target can't be reached/authenticated. Per-type behaviour lives in `pam-account-connection-test.ts`:
+`buildGatewayConnectionTest` resolves the gateway target + a `mode`-tagged request, and `CLOUD_CONNECTION_VALIDATORS`
+handles the host-less cloud accounts (AWS/GCP/Azure) by minting credentials backend-side via the federation helpers.
+Gateway-routed types run through one RPC — `testConnectionWithGateway` → the gateway's `/v1/test-connection` handler
+(`packages/gateway-v2/test_connection_handler.go`), dispatched on `mode`: `sql` (postgres/mysql/mssql auth), `mongodb`,
+`ldap` (Windows AD, against `dcAddress`), `kubernetes` (token), `ssh`, or `tcp` (reachability — the ceiling for
+Windows RDP, SSH cert auth, MsSQL NTLM/Kerberos, and K8s gateway-auth). A null test result (gateway offline / missing
+the connection-test protocol) skips rather than blocks.
Cloud types use one of two brokering models instead of a plain gateway TCP proxy: **gateway-less** (`AwsIam`,
`requiresGateway: false`) mints short-lived STS credentials in `access()` and returns them in session