Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
test: add unit tests for GCP identity validation functions
Victor Hugo dos Santosee69cc0d3c1c
diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.tsnew file mode 100644index 0000000000..c583e9dfa3--- /dev/null+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts@@ -0,0 +1,258 @@+import crypto from "node:crypto";++import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";++import { crypto as infisicalCrypto } from "@app/lib/crypto";++import { validateIamIdentity, validateIdTokenIdentity } from "./identity-gcp-auth-fns";++const { getFederatedSignonCerts, verifySignedJwtWithCertsAsync, requestGet } = vi.hoisted(() => ({+ getFederatedSignonCerts: vi.fn(),+ verifySignedJwtWithCertsAsync: vi.fn(),+ requestGet: vi.fn()+}));++vi.mock("@app/lib/logger", () => ({+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }+}));++vi.mock("google-auth-library", () => ({+ OAuth2Client: vi.fn(() => ({+ getFederatedSignonCerts,+ verifySignedJwtWithCertsAsync+ }))+}));++vi.mock("@app/lib/config/request", async (importOriginal) => {+ const actual = await importOriginal<typeof import("@app/lib/config/request")>();+ return { ...actual, request: { ...actual.request, get: requestGet } };+});++const IDENTITY_ID = "bc2d5b6c-1f0a-4f7d-9a9f-1c19d8f0b111";+const SERVICE_ACCOUNT = "[email protected]";+const KEY_ID = "d0f1e2a3b4c5";++const generateRsaKeyPair = () =>+ crypto.generateKeyPairSync("rsa", {+ modulusLength: 2048,+ publicKeyEncoding: { type: "spki", format: "pem" },+ privateKeyEncoding: { type: "pkcs8", format: "pem" }+ });++let signingKeyPair: ReturnType<typeof generateRsaKeyPair>;+let otherKeyPair: ReturnType<typeof generateRsaKeyPair>;+let previousFipsEnabled: string | undefined;++const signIamJwt = ({+ privateKey = signingKeyPair.privateKey,+ payload = { sub: SERVICE_ACCOUNT, aud: IDENTITY_ID },+ omitKid = false,+ expiresIn = "1h"+}: {+ privateKey?: string;+ payload?: Record<string, unknown>;+ omitKid?: boolean;+ expiresIn?: string;+} = {}) =>+ infisicalCrypto.jwt().sign(payload, privateKey, {+ algorithm: "RS256",+ expiresIn,+ ...(omitKid ? {} : { keyid: KEY_ID })+ });++beforeAll(async () => {+ previousFipsEnabled = process.env.FIPS_ENABLED;+ process.env.FIPS_ENABLED = "false";+ await infisicalCrypto.initialize({} as never, {} as never, {} as never);+ signingKeyPair = generateRsaKeyPair();+ otherKeyPair = generateRsaKeyPair();+});++afterAll(() => {+ if (previousFipsEnabled === undefined) {+ delete process.env.FIPS_ENABLED;+ } else {+ process.env.FIPS_ENABLED = previousFipsEnabled;+ }+});++beforeEach(() => {+ vi.clearAllMocks();+});++describe("validateIdTokenIdentity", () => {+ beforeEach(() => {+ getFederatedSignonCerts.mockResolvedValue({ certs: { "cert-kid": "cert-value" } });+ });++ const computeEngine = {+ instance_creation_timestamp: 1700000000,+ instance_id: "3802628075545820873",+ instance_name: "test-instance",+ project_id: "my-project",+ project_number: 123456789,+ zone: "us-central1-a"+ };++ const mockPayload = (payload: unknown) => {+ verifySignedJwtWithCertsAsync.mockResolvedValue({ getPayload: () => payload });+ };++ test("returns the email and Compute Engine details for a GCE token", async () => {+ mockPayload({ email: SERVICE_ACCOUNT, google: { compute_engine: computeEngine } });++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" })).resolves.toEqual({+ email: SERVICE_ACCOUNT,+ computeEngineDetails: computeEngine+ });+ });++ test("verifies the token against the identity ID as audience and the Google issuer", async () => {+ mockPayload({ email: SERVICE_ACCOUNT, google: { compute_engine: computeEngine } });++ await validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" });++ expect(verifySignedJwtWithCertsAsync).toHaveBeenCalledWith("id-token", { "cert-kid": "cert-value" }, IDENTITY_ID, [+ "https://accounts.google.com"+ ]);+ });++ // A token minted outside a GCE instance (for example by a service account key) verifies+ // fine but carries no google.compute_engine claim. The service, not this function, decides+ // whether that is allowed.+ test("returns undefined Compute Engine details when the token carries no GCE claim", async () => {+ mockPayload({ email: SERVICE_ACCOUNT });++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" })).resolves.toEqual({+ email: SERVICE_ACCOUNT,+ computeEngineDetails: undefined+ });+ });++ test("rejects a token that fails signature or audience verification", async () => {+ verifySignedJwtWithCertsAsync.mockRejectedValue(new Error("Wrong recipient, payload audience != requiredAudience"));++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" })).rejects.toThrow(+ "Invalid GCP ID token"+ );+ });++ // google-auth-library embeds the full token in its error messages, so the cause must not+ // ride along into a logged error.+ test("does not leak the token in the rejection", async () => {+ verifySignedJwtWithCertsAsync.mockRejectedValue(new Error("Invalid token signature: super-secret-token"));++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "super-secret-token" })).rejects.toSatisfy(+ (error: Error) => !JSON.stringify({ message: error.message, cause: error.cause }).includes("super-secret-token")+ );+ });++ test("rejects a token with no payload", async () => {+ mockPayload(undefined);++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" })).rejects.toThrow(+ "GCP ID token is missing an email claim"+ );+ });++ test("rejects a token with no email claim", async () => {+ mockPayload({ google: { compute_engine: computeEngine } });++ await expect(validateIdTokenIdentity({ identityId: IDENTITY_ID, jwt: "id-token" })).rejects.toThrow(+ "GCP ID token is missing an email claim"+ );+ });+});++describe("validateIamIdentity", () => {+ beforeEach(() => {+ requestGet.mockResolvedValue({ data: { [KEY_ID]: signingKeyPair.publicKey } });+ });++ test("returns the service account email for a correctly signed token", async () => {+ await expect(validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt() })).resolves.toEqual({+ email: SERVICE_ACCOUNT+ });+ });++ test("fetches the signing keys for the service account named in the token", async () => {+ await validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt() });++ expect(requestGet).toHaveBeenCalledWith(+ `https://www.googleapis.com/service_accounts/v1/metadata/x509/${encodeURIComponent(SERVICE_ACCOUNT)}`+ );+ });++ test("rejects a malformed token", async () => {+ await expect(validateIamIdentity({ identityId: IDENTITY_ID, jwt: "not-a-jwt" })).rejects.toThrow(+ "Invalid GCP IAM token"+ );+ expect(requestGet).not.toHaveBeenCalled();+ });++ // Without a kid there is no way to pick a signing key, and indexing the key map with+ // undefined must not be allowed to select one.+ test("rejects a token whose header carries no kid", async () => {+ await expect(validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt({ omitKid: true }) })).rejects.toThrow(+ "Invalid GCP IAM token"+ );+ expect(requestGet).not.toHaveBeenCalled();+ });++ test("rejects a token whose subject is not a service account email", async () => {+ await expect(+ validateIamIdentity({+ identityId: IDENTITY_ID,+ jwt: signIamJwt({ payload: { sub: "[email protected]", aud: IDENTITY_ID } })+ })+ ).rejects.toThrow("Invalid service account identifier");+ expect(requestGet).not.toHaveBeenCalled();+ });++ test("rejects a token with no subject", async () => {+ await expect(+ validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt({ payload: { aud: IDENTITY_ID } }) })+ ).rejects.toThrow("Invalid service account identifier");+ expect(requestGet).not.toHaveBeenCalled();+ });++ test("rejects a token whose kid matches none of the published keys", async () => {+ requestGet.mockResolvedValue({ data: { "some-other-kid": signingKeyPair.publicKey } });++ await expect(validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt() })).rejects.toThrow(+ "No matching signing key found for the GCP IAM token"+ );+ });++ test("rejects a token signed by a key other than the published one", async () => {+ await expect(+ validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt({ privateKey: otherKeyPair.privateKey }) })+ ).rejects.toThrow("Invalid GCP IAM token signature");+ });++ test("rejects an expired token", async () => {+ await expect(+ validateIamIdentity({ identityId: IDENTITY_ID, jwt: signIamJwt({ expiresIn: "-1h" }) })+ ).rejects.toThrow("Invalid GCP IAM token signature");+ });++ // An unsigned token must never verify, otherwise anyone able to name a service account+ // could mint an Infisical login.+ test("rejects an alg: none token", async () => {+ const [, payload] = signIamJwt().split(".");+ const header = Buffer.from(JSON.stringify({ alg: "none", typ: "JWT", kid: KEY_ID })).toString("base64url");++ await expect(validateIamIdentity({ identityId: IDENTITY_ID, jwt: `${header}.${payload}.` })).rejects.toThrow(+ "Invalid GCP IAM token signature"+ );+ });++ test("rejects a token minted for a different identity", async () => {+ await expect(+ validateIamIdentity({+ identityId: IDENTITY_ID,+ jwt: signIamJwt({ payload: { sub: SERVICE_ACCOUNT, aud: "another-identity-id" } })+ })+ ).rejects.toThrow("Invalid audience in GCP IAM Token");+ });+});diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.tsnew file mode 100644index 0000000000..a73b21afb5--- /dev/null+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.ts@@ -0,0 +1,183 @@+import { beforeEach, describe, expect, test, vi } from "vitest";++import { identityGcpAuthServiceFactory } from "./identity-gcp-auth-service";+import { TGcpIdentityDetails } from "./identity-gcp-auth-types";++const { validateIamIdentity, validateIdTokenIdentity } = vi.hoisted(() => ({+ validateIamIdentity: vi.fn(),+ validateIdTokenIdentity: vi.fn()+}));++vi.mock("./identity-gcp-auth-fns", () => ({ validateIamIdentity, validateIdTokenIdentity }));++vi.mock("@app/lib/config/env", () => ({+ getConfig: () => ({ OTEL_TELEMETRY_COLLECTION_ENABLED: false })+}));++vi.mock("@fastify/request-context", () => ({+ requestContext: { get: vi.fn(() => undefined) }+}));++const IDENTITY_ID = "5d3a8b2e-7c61-4b0f-9f3e-2a1c4d6e8f90";+const ORG_ID = "org-id";+const ACCESS_TOKEN = "access-token";++const SERVICE_ACCOUNT = "[email protected]";+const OTHER_SERVICE_ACCOUNT = "[email protected]";++const computeEngineDetails = {+ instance_creation_timestamp: 1700000000,+ instance_id: "3802628075545820873",+ instance_name: "test-instance",+ project_id: "my-project",+ project_number: 123456789,+ zone: "us-central1-a"+};++type TGcpAuthRow = {+ type: "gce" | "iam";+ allowedServiceAccounts: string | null;+ allowedProjects: string | null;+ allowedZones: string | null;+};++const makeService = (gcpAuth: Partial<TGcpAuthRow> = {}) => {+ const issueIdentityAccessToken = vi.fn().mockResolvedValue({+ accessToken: ACCESS_TOKEN,+ identityAccessToken: { id: "access-token-id" }+ });++ const service = identityGcpAuthServiceFactory({+ identityGcpAuthDAL: {+ findOne: vi.fn().mockResolvedValue({+ identityId: IDENTITY_ID,+ type: "gce",+ allowedServiceAccounts: null,+ allowedProjects: null,+ allowedZones: null,+ accessTokenTTL: 3600,+ accessTokenMaxTTL: 7200,+ accessTokenNumUsesLimit: 0,+ accessTokenPeriod: 0,+ accessTokenTrustedIps: [{ ipAddress: "0.0.0.0/0" }],+ ...gcpAuth+ })+ } as never,+ identityDAL: {+ findById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, orgId: ORG_ID, name: "gce-identity" })+ } as never,+ orgDAL: {+ findById: vi.fn().mockResolvedValue({ id: ORG_ID, name: "org", slug: "org", rootOrgId: null, parentOrgId: null })+ } as never,+ membershipIdentityDAL: { update: vi.fn().mockResolvedValue(undefined) } as never,+ keyStore: { setItemWithExpiryNX: vi.fn().mockResolvedValue(null) } as never,+ identityAccessTokenDAL: {} as never,+ permissionService: {} as never,+ licenseService: {} as never,+ identityAccessTokenService: { issueIdentityAccessToken } as never+ });++ return { service, issueIdentityAccessToken };+};++const login = (gcpAuth: Partial<TGcpAuthRow>, details: TGcpIdentityDetails) => {+ const { service, issueIdentityAccessToken } = makeService(gcpAuth);+ validateIdTokenIdentity.mockResolvedValue(details);+ validateIamIdentity.mockResolvedValue(details);+ return { result: service.login({ identityId: IDENTITY_ID, jwt: "gcp-jwt" }), issueIdentityAccessToken };+};++beforeEach(() => {+ vi.clearAllMocks();+});++// A GCE ID token only carries google.compute_engine when it was minted by the metadata server on+// an instance. A token minted any other way by an allowed service account would otherwise sail+// past the project and zone allow lists, because `undefined === undefined` never matches an entry+// but the checks below read through an optional chain. Refusing the login is what keeps the+// allow lists from silently becoming a service-account-only check.+describe("gcpAuthService.login GCE Compute Engine detail requirement", () => {+ test("rejects a token with no Compute Engine details when projects are restricted", async () => {+ const { result, issueIdentityAccessToken } = login(+ { type: "gce", allowedProjects: "my-project" },+ { email: SERVICE_ACCOUNT, computeEngineDetails: undefined }+ );++ await expect(result).rejects.toThrow("Access denied: GCP identity token is missing the Compute Engine details");+ await expect(result).rejects.toMatchObject({+ detail: { reasonCode: "compute_engine_details_missing", identityId: IDENTITY_ID, orgId: ORG_ID }+ });+ expect(issueIdentityAccessToken).not.toHaveBeenCalled();+ });++ test("rejects a token with no Compute Engine details when zones are restricted", async () => {+ const { result } = login(+ { type: "gce", allowedZones: "us-central1-a" },+ { email: SERVICE_ACCOUNT, computeEngineDetails: undefined }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "compute_engine_details_missing" } });+ });++ // Nothing about the identity depends on where the token came from, so requiring GCE details+ // here would break every unrestricted GCE configuration in the field.+ test("allows a token with no Compute Engine details when neither projects nor zones are restricted", async () => {+ const { result } = login({ type: "gce" }, { email: SERVICE_ACCOUNT, computeEngineDetails: undefined });++ await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });+ });++ // IAM tokens are signed service account JWTs and never carry GCE details, so the guard must+ // stay scoped to the gce type even when the stale project and zone columns are populated.+ test("allows an IAM login even while project and zone columns are populated", async () => {+ const { result } = login(+ { type: "iam", allowedProjects: "my-project", allowedZones: "us-central1-a" },+ { email: SERVICE_ACCOUNT }+ );++ await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });+ });+});++describe("gcpAuthService.login GCE allow lists", () => {+ test("allows a token whose project and zone are both allowed", async () => {+ const { result } = login(+ {+ type: "gce",+ allowedServiceAccounts: `${OTHER_SERVICE_ACCOUNT}, ${SERVICE_ACCOUNT}`,+ allowedProjects: "other-project, my-project",+ allowedZones: "us-east1-b, us-central1-a"+ },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });+ });++ test("rejects a service account outside the allow list", async () => {+ const { result } = login(+ { type: "gce", allowedServiceAccounts: OTHER_SERVICE_ACCOUNT },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "service_account_not_allowed" } });+ });++ test("rejects an instance running in a project outside the allow list", async () => {+ const { result } = login(+ { type: "gce", allowedProjects: "other-project" },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "project_not_allowed" } });+ });++ test("rejects an instance running in a zone outside the allow list", async () => {+ const { result } = login(+ { type: "gce", allowedZones: "us-east1-b" },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "zone_not_allowed" } });+ });+});