diff --git a/backend/src/db/migrations/20240509235819_identity-gcp-auth.ts b/backend/src/db/migrations/20240509235819_identity-gcp-auth.ts index 2de275045..8c80fed84 100644 --- a/backend/src/db/migrations/20240509235819_identity-gcp-auth.ts +++ b/backend/src/db/migrations/20240509235819_identity-gcp-auth.ts @@ -14,9 +14,6 @@ export async function up(knex: Knex): Promise { t.timestamps(true, true, true); t.uuid("identityId").notNullable().unique(); t.foreign("identityId").references("id").inTable(TableName.Identity).onDelete("CASCADE"); - t.text("encryptedCredentials").notNullable(); - t.string("credentialsIV").notNullable(); - t.string("credentialsTag").notNullable(); t.string("type").notNullable(); t.string("allowedServiceAccounts").notNullable(); t.string("allowedProjects").notNullable(); diff --git a/backend/src/db/schemas/identity-gcp-auths.ts b/backend/src/db/schemas/identity-gcp-auths.ts index 5eda85463..65c7db837 100644 --- a/backend/src/db/schemas/identity-gcp-auths.ts +++ b/backend/src/db/schemas/identity-gcp-auths.ts @@ -16,9 +16,6 @@ export const IdentityGcpAuthsSchema = z.object({ createdAt: z.date(), updatedAt: z.date(), identityId: z.string().uuid(), - encryptedCredentials: z.string(), - credentialsIV: z.string(), - credentialsTag: z.string(), type: z.string(), allowedServiceAccounts: z.string(), allowedProjects: z.string(), diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts index 5eb46e861..57d78778e 100644 --- a/backend/src/server/routes/index.ts +++ b/backend/src/server/routes/index.ts @@ -711,7 +711,6 @@ export const registerRoutes = async ( identityOrgMembershipDAL, identityAccessTokenDAL, identityDAL, - orgBotDAL, permissionService, licenseService }); diff --git a/backend/src/server/routes/v1/identity-gcp-auth-router.ts b/backend/src/server/routes/v1/identity-gcp-auth-router.ts index 2f7b0240c..58654f220 100644 --- a/backend/src/server/routes/v1/identity-gcp-auth-router.ts +++ b/backend/src/server/routes/v1/identity-gcp-auth-router.ts @@ -1,14 +1,12 @@ import { z } from "zod"; +import { IdentityGcpAuthsSchema } from "@app/db/schemas"; import { EventType } from "@app/ee/services/audit-log/audit-log-types"; import { readLimit, writeLimit } from "@app/server/config/rateLimiter"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { AuthMode } from "@app/services/auth/auth-type"; import { TIdentityTrustedIp } from "@app/services/identity/identity-types"; -import { - ModifiedIdentityGcpAuthsSchema, - validateGcpAuthField -} from "@app/services/identity-gcp-auth/identity-gcp-auth-validators"; +import { validateGcpAuthField } from "@app/services/identity-gcp-auth/identity-gcp-auth-validators"; export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) => { server.route({ @@ -76,7 +74,6 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) identityId: z.string().trim() }), body: z.object({ - credentials: z.string().trim(), type: z.enum(["iam", "gce"]), allowedServiceAccounts: validateGcpAuthField, allowedProjects: validateGcpAuthField, @@ -107,7 +104,7 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) }), response: { 200: z.object({ - identityGcpAuth: ModifiedIdentityGcpAuthsSchema + identityGcpAuth: IdentityGcpAuthsSchema }) } }, @@ -162,7 +159,6 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) identityId: z.string().trim() }), body: z.object({ - credentials: z.string().trim().optional(), type: z.enum(["iam", "gce"]).optional(), allowedServiceAccounts: validateGcpAuthField, allowedProjects: validateGcpAuthField, @@ -186,7 +182,7 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) }), response: { 200: z.object({ - identityGcpAuth: ModifiedIdentityGcpAuthsSchema + identityGcpAuth: IdentityGcpAuthsSchema }) } }, @@ -242,7 +238,7 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider) }), response: { 200: z.object({ - identityGcpAuth: ModifiedIdentityGcpAuthsSchema + identityGcpAuth: IdentityGcpAuthsSchema }) } }, diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts index ad3095b71..16ad991ca 100644 --- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts +++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts @@ -1,6 +1,5 @@ import axios from "axios"; -import { JWTInput, OAuth2Client } from "google-auth-library"; -import { google } from "googleapis"; +import { OAuth2Client } from "google-auth-library"; import jwt from "jsonwebtoken"; import { UnauthorizedError } from "@app/lib/errors"; @@ -8,45 +7,13 @@ import { UnauthorizedError } from "@app/lib/errors"; import { TDecodedGcpIamAuthJwt, TGcpGceIdTokenPayload } from "./identity-gcp-auth-types"; /** - * Return the full details of the service account corresponding to the service account email or unique ID [serviceAccount]. - * @param {string} serviceAccount - The email or unique ID of the service account. - * @param {string} credentials - The credentials in the GCP Auth configuration for Infisical. - * @returns - */ -const getGcpServiceAccountDetails = async (serviceAccount: string, credentials: string) => { - const auth = new google.auth.GoogleAuth({ - credentials: JSON.parse(credentials) as JWTInput, - scopes: ["https://www.googleapis.com/auth/iam"] - }); - - const iam = google.iam({ - version: "v1", - auth - }); - - const { data } = await iam.projects.serviceAccounts.get({ - name: `projects/-/serviceAccounts/${serviceAccount}` - }); - - return data; -}; - -/** - * Validates that the identity token [jwt] sent in from a client GCE instance as part of GCP GCE authentication + * Validates that the identity token [jwt] sent in from a client GCE instance as part of GCP ID Token authentication * is valid. * @param {string} identityId - The ID of the identity in Infisical that is being authenticated against (used as audience). * @param {string} jwt - The identity token to validate. * @param {string} credentials - The credentials in the GCP Auth configuration for Infisical. */ -export const validateGceIdentity = async ({ - identityId, - jwt: identityToken, - credentials -}: { - identityId: string; - jwt: string; - credentials: string; -}) => { +export const validateGceIdentity = async ({ identityId, jwt: identityToken }: { identityId: string; jwt: string }) => { const oAuth2Client = new OAuth2Client(); const response = await oAuth2Client.getFederatedSignonCerts(); const ticket = await oAuth2Client.verifySignedJwtWithCertsAsync( @@ -57,9 +24,8 @@ export const validateGceIdentity = async ({ ); const payload = ticket.getPayload() as TGcpGceIdTokenPayload; if (!payload || !payload.email) throw new UnauthorizedError(); - const serviceAccountDetails = await getGcpServiceAccountDetails(payload.email, credentials); - return { serviceAccountDetails, gceInstanceDetails: payload }; + return { email: payload.email, computeEngineDetails: payload.google?.compute_engine }; }; /** @@ -71,12 +37,10 @@ export const validateGceIdentity = async ({ */ export const validateIamIdentity = async ({ identityId, - jwt: serviceAccountJwt, - credentials + jwt: serviceAccountJwt }: { identityId: string; jwt: string; - credentials: string; }) => { const decodedJwt = jwt.decode(serviceAccountJwt, { complete: true }) as TDecodedGcpIamAuthJwt; const { sub, aud } = decodedJwt.payload; @@ -96,6 +60,5 @@ export const validateIamIdentity = async ({ }); if (aud !== identityId) throw new UnauthorizedError(); - const serviceAccountDetails = await getGcpServiceAccountDetails(sub, credentials); - return { serviceAccountDetails }; + return { email: sub }; }; diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts index e2fb37386..8c4fa8b49 100644 --- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts +++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts @@ -1,23 +1,13 @@ import { ForbiddenError } from "@casl/ability"; -import { iam_v1 } from "googleapis"; import jwt from "jsonwebtoken"; -import { IdentityAuthMethod, SecretKeyEncoding, TIdentityGcpAuthsUpdate } from "@app/db/schemas"; +import { IdentityAuthMethod } from "@app/db/schemas"; import { TLicenseServiceFactory } from "@app/ee/services/license/license-service"; import { OrgPermissionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; import { getConfig } from "@app/lib/config/env"; -import { - decryptSymmetric, - encryptSymmetric, - generateAsymmetricKeyPair, - generateSymmetricKey, - infisicalSymmetricDecrypt, - infisicalSymmetricEncypt -} from "@app/lib/crypto/encryption"; import { BadRequestError, UnauthorizedError } from "@app/lib/errors"; import { extractIPDetails, isValidIpOrCidr } from "@app/lib/ip"; -import { TOrgBotDALFactory } from "@app/services/org/org-bot-dal"; import { AuthTokenType } from "../auth/auth-type"; import { TIdentityDALFactory } from "../identity/identity-dal"; @@ -28,7 +18,7 @@ import { TIdentityGcpAuthDALFactory } from "./identity-gcp-auth-dal"; import { validateGceIdentity, validateIamIdentity } from "./identity-gcp-auth-fns"; import { TAttachGcpAuthDTO, - TGcpGceIdTokenPayload, + TGcpIdentityDetails, TGetGcpAuthDTO, TLoginGcpAuthDTO, TUpdateGcpAuthDTO @@ -39,7 +29,6 @@ type TIdentityGcpAuthServiceFactoryDep = { identityOrgMembershipDAL: Pick; identityAccessTokenDAL: Pick; identityDAL: Pick; - orgBotDAL: Pick; permissionService: Pick; licenseService: Pick; }; @@ -51,7 +40,6 @@ export const identityGcpAuthServiceFactory = ({ identityOrgMembershipDAL, identityAccessTokenDAL, identityDAL, - orgBotDAL, permissionService, licenseService }: TIdentityGcpAuthServiceFactoryDep) => { @@ -62,47 +50,20 @@ export const identityGcpAuthServiceFactory = ({ const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityGcpAuth.identityId }); if (!identityMembershipOrg) throw new UnauthorizedError(); - const orgBot = await orgBotDAL.findOne({ orgId: identityMembershipOrg.orgId }); - if (!orgBot) throw new BadRequestError({ message: "Org bot not found", name: "OrgBotNotFound" }); - - const key = infisicalSymmetricDecrypt({ - ciphertext: orgBot.encryptedSymmetricKey, - iv: orgBot.symmetricKeyIV, - tag: orgBot.symmetricKeyTag, - keyEncoding: orgBot.symmetricKeyKeyEncoding as SecretKeyEncoding - }); - - const { encryptedCredentials, credentialsIV, credentialsTag } = identityGcpAuth; - let credentials = ""; - if (encryptedCredentials && credentialsIV && credentialsTag) { - credentials = decryptSymmetric({ - ciphertext: encryptedCredentials, - key, - tag: credentialsTag, - iv: credentialsIV - }); - } - - let serviceAccountDetails: iam_v1.Schema$ServiceAccount; - let gceInstanceDetails: TGcpGceIdTokenPayload | undefined; + let gcpIdentityDetails: TGcpIdentityDetails; switch (identityGcpAuth.type) { case "gce": { - const gceIdentity = await validateGceIdentity({ + gcpIdentityDetails = await validateGceIdentity({ identityId, - jwt: serviceAccountJwt, - credentials + jwt: serviceAccountJwt }); - serviceAccountDetails = gceIdentity.serviceAccountDetails; - gceInstanceDetails = gceIdentity.gceInstanceDetails; break; } case "iam": { - const iamIdentity = await validateIamIdentity({ + gcpIdentityDetails = await validateIamIdentity({ identityId, - jwt: serviceAccountJwt, - credentials + jwt: serviceAccountJwt }); - serviceAccountDetails = iamIdentity.serviceAccountDetails; break; } default: { @@ -116,30 +77,29 @@ export const identityGcpAuthServiceFactory = ({ const isServiceAccountAllowed = identityGcpAuth.allowedServiceAccounts .split(",") .map((serviceAccount) => serviceAccount.trim()) - .some( - (serviceAccount) => - serviceAccount === serviceAccountDetails.email || serviceAccount === serviceAccountDetails.uniqueId - ); + .some((serviceAccount) => serviceAccount === gcpIdentityDetails.email); if (!isServiceAccountAllowed) throw new UnauthorizedError(); } - if (identityGcpAuth.allowedProjects) { + // note that only possible to apply allowed projects/zones filters for GCE instances + + if (identityGcpAuth.type === "gce" && identityGcpAuth.allowedProjects && gcpIdentityDetails.computeEngineDetails) { // validate if the project that the service account belongs to is in the list of allowed projects const isProjectAllowed = identityGcpAuth.allowedProjects .split(",") .map((project) => project.trim()) - .some((project) => project === serviceAccountDetails.projectId); + .some((project) => project === gcpIdentityDetails.computeEngineDetails?.project_id); if (!isProjectAllowed) throw new UnauthorizedError(); } - if (identityGcpAuth.type === "gce" && gceInstanceDetails && identityGcpAuth.allowedZones) { + if (identityGcpAuth.type === "gce" && identityGcpAuth.allowedZones && gcpIdentityDetails.computeEngineDetails) { const isZoneAllowed = identityGcpAuth.allowedZones .split(",") .map((zone) => zone.trim()) - .some((zone) => zone === gceInstanceDetails!.google.compute_engine.zone); + .some((zone) => zone === gcpIdentityDetails.computeEngineDetails?.zone); if (!isZoneAllowed) throw new UnauthorizedError(); } @@ -180,7 +140,6 @@ export const identityGcpAuthServiceFactory = ({ const attachGcpAuth = async ({ identityId, - credentials, type, allowedServiceAccounts, allowedProjects, @@ -232,60 +191,6 @@ export const identityGcpAuthServiceFactory = ({ return extractIPDetails(accessTokenTrustedIp.ipAddress); }); - const orgBot = await orgBotDAL.transaction(async (tx) => { - const doc = await orgBotDAL.findOne({ orgId: identityMembershipOrg.orgId }, tx); - if (doc) return doc; - - const { privateKey, publicKey } = generateAsymmetricKeyPair(); - const key = generateSymmetricKey(); - const { - ciphertext: encryptedPrivateKey, - iv: privateKeyIV, - tag: privateKeyTag, - encoding: privateKeyKeyEncoding, - algorithm: privateKeyAlgorithm - } = infisicalSymmetricEncypt(privateKey); - const { - ciphertext: encryptedSymmetricKey, - iv: symmetricKeyIV, - tag: symmetricKeyTag, - encoding: symmetricKeyKeyEncoding, - algorithm: symmetricKeyAlgorithm - } = infisicalSymmetricEncypt(key); - - return orgBotDAL.create( - { - name: "Infisical org bot", - publicKey, - privateKeyIV, - encryptedPrivateKey, - symmetricKeyIV, - symmetricKeyTag, - encryptedSymmetricKey, - symmetricKeyAlgorithm, - orgId: identityMembershipOrg.orgId, - privateKeyTag, - privateKeyAlgorithm, - privateKeyKeyEncoding, - symmetricKeyKeyEncoding - }, - tx - ); - }); - - const key = infisicalSymmetricDecrypt({ - ciphertext: orgBot.encryptedSymmetricKey, - iv: orgBot.symmetricKeyIV, - tag: orgBot.symmetricKeyTag, - keyEncoding: orgBot.symmetricKeyKeyEncoding as SecretKeyEncoding - }); - - const { - ciphertext: encryptedCredentials, - iv: credentialsIV, - tag: credentialsTag - } = encryptSymmetric(credentials, key); - const identityGcpAuth = await identityGcpAuthDAL.transaction(async (tx) => { const doc = await identityGcpAuthDAL.create( { @@ -294,9 +199,6 @@ export const identityGcpAuthServiceFactory = ({ allowedServiceAccounts, allowedProjects, allowedZones, - encryptedCredentials, - credentialsIV, - credentialsTag, accessTokenMaxTTL, accessTokenTTL, accessTokenNumUsesLimit, @@ -313,13 +215,12 @@ export const identityGcpAuthServiceFactory = ({ ); return doc; }); - return { ...identityGcpAuth, credentials, orgId: identityMembershipOrg.orgId }; + return { ...identityGcpAuth, orgId: identityMembershipOrg.orgId }; }; const updateGcpAuth = async ({ identityId, type, - credentials, allowedServiceAccounts, allowedProjects, allowedZones, @@ -375,7 +276,7 @@ export const identityGcpAuthServiceFactory = ({ return extractIPDetails(accessTokenTrustedIp.ipAddress); }); - const updateQuery: TIdentityGcpAuthsUpdate = { + const updatedGcpAuth = await identityGcpAuthDAL.updateById(identityGcpAuth.id, { type, allowedServiceAccounts, allowedProjects, @@ -386,38 +287,10 @@ export const identityGcpAuthServiceFactory = ({ accessTokenTrustedIps: reformattedAccessTokenTrustedIps ? JSON.stringify(reformattedAccessTokenTrustedIps) : undefined - }; - - const orgBot = await orgBotDAL.findOne({ orgId: identityMembershipOrg.orgId }); - if (!orgBot) throw new BadRequestError({ message: "Org bot not found", name: "OrgBotNotFound" }); - const key = infisicalSymmetricDecrypt({ - ciphertext: orgBot.encryptedSymmetricKey, - iv: orgBot.symmetricKeyIV, - tag: orgBot.symmetricKeyTag, - keyEncoding: orgBot.symmetricKeyKeyEncoding as SecretKeyEncoding }); - if (credentials !== undefined) { - const { - ciphertext: encryptedCredentials, - iv: credentialsIV, - tag: credentialsTag - } = encryptSymmetric(credentials, key); - updateQuery.encryptedCredentials = encryptedCredentials; - updateQuery.credentialsIV = credentialsIV; - updateQuery.credentialsTag = credentialsTag; - } - - const updatedGcpAuth = await identityGcpAuthDAL.updateById(identityGcpAuth.id, updateQuery); - return { ...updatedGcpAuth, - credentials: decryptSymmetric({ - ciphertext: updatedGcpAuth.encryptedCredentials, - iv: updatedGcpAuth.credentialsIV, - tag: updatedGcpAuth.credentialsTag, - key - }), orgId: identityMembershipOrg.orgId }; }; @@ -441,28 +314,7 @@ export const identityGcpAuthServiceFactory = ({ ); ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Read, OrgPermissionSubjects.Identity); - const orgBot = await orgBotDAL.findOne({ orgId: identityMembershipOrg.orgId }); - if (!orgBot) throw new BadRequestError({ message: "Org bot not found", name: "OrgBotNotFound" }); - - const key = infisicalSymmetricDecrypt({ - ciphertext: orgBot.encryptedSymmetricKey, - iv: orgBot.symmetricKeyIV, - tag: orgBot.symmetricKeyTag, - keyEncoding: orgBot.symmetricKeyKeyEncoding as SecretKeyEncoding - }); - - const { encryptedCredentials, credentialsIV, credentialsTag } = identityGcpAuth; - let credentials = ""; - if (encryptedCredentials && credentialsIV && credentialsTag) { - credentials = decryptSymmetric({ - ciphertext: encryptedCredentials, - key, - tag: credentialsTag, - iv: credentialsIV - }); - } - - return { ...identityGcpAuth, credentials, orgId: identityMembershipOrg.orgId }; + return { ...identityGcpAuth, orgId: identityMembershipOrg.orgId }; }; return { diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-types.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-types.ts index c01ab2edb..931de78e0 100644 --- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-types.ts +++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-types.ts @@ -7,7 +7,6 @@ export type TLoginGcpAuthDTO = { export type TAttachGcpAuthDTO = { identityId: string; - credentials: string; type: "iam" | "gce"; allowedServiceAccounts: string; allowedProjects: string; @@ -20,7 +19,6 @@ export type TAttachGcpAuthDTO = { export type TUpdateGcpAuthDTO = { identityId: string; - credentials?: string; type?: "iam" | "gce"; allowedServiceAccounts?: string; allowedProjects?: string; @@ -35,21 +33,28 @@ export type TGetGcpAuthDTO = { identityId: string; } & Omit; +type TComputeEngineDetails = { + instance_creation_timestamp: number; + instance_id: string; + instance_name: string; + project_id: string; + project_number: number; + zone: string; +}; + +export type TGcpIdentityDetails = { + email: string; + computeEngineDetails?: TComputeEngineDetails; +}; + export type TGcpGceIdTokenPayload = { aud: string; azp: string; email: string; email_verified: boolean; exp: number; - google: { - compute_engine: { - instance_creation_timestamp: number; - instance_id: string; - instance_name: string; - project_id: string; - project_number: number; - zone: string; - }; + google?: { + compute_engine: TComputeEngineDetails; }; iat: number; iss: string; diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-validators.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-validators.ts index 7e3ac9439..a49cee417 100644 --- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-validators.ts +++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-validators.ts @@ -1,15 +1,5 @@ import { z } from "zod"; -import { IdentityGcpAuthsSchema } from "@app/db/schemas"; - -export const ModifiedIdentityGcpAuthsSchema = IdentityGcpAuthsSchema.omit({ - encryptedCredentials: true, - credentialsIV: true, - credentialsTag: true -}).extend({ - credentials: z.string() -}); - export const validateGcpAuthField = z .string() .trim() diff --git a/docs/documentation/platform/identities/gcp-auth.mdx b/docs/documentation/platform/identities/gcp-auth.mdx index b98b1a00c..7b5e60600 100644 --- a/docs/documentation/platform/identities/gcp-auth.mdx +++ b/docs/documentation/platform/identities/gcp-auth.mdx @@ -3,12 +3,174 @@ title: GCP Auth description: "Learn how to authenticate with Infisical for services on Google Cloud Platform" --- -**GCP Auth** is a GCP-native authentication method for GCP resources to access Infisical. +**GCP Auth** is a GCP-native authentication method for GCP resources to access Infisical. It consists of two sub-methods/approaches: +- GCP ID Token Auth: For GCP services including [Compute Engine](https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature), [App Engine standard environment](https://cloud.google.com/appengine/docs/standard/python3/runtime#metadata_server), [App Engine flexible environment](https://cloud.google.com/appengine/docs/flexible/python/runtime#metadata_server), [Cloud Functions](https://cloud.google.com/functions/docs/securing/function-identity#using_the_metadata_server_to_acquire_tokens), [Cloud Run](https://cloud.google.com/run/docs/container-contract#metadata-server), [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/docs/concepts/workload-identity#instance_metadata), and [Cloud Build](https://cloud.google.com/kubernetes-engine/docs/concepts/workload-identity#instance_metadata) to authenticate with Infisical. - GCP IAM Auth: For Google Cloud Platform (GCP) service accounts to authenticate with Infisical. -- GCP GCE Auth: For GCP services including [Compute Engine](https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature), [App Engine standard environment](https://cloud.google.com/appengine/docs/standard/python3/runtime#metadata_server), [App Engine flexible environment](https://cloud.google.com/appengine/docs/flexible/python/runtime#metadata_server), [Cloud Functions](https://cloud.google.com/functions/docs/securing/function-identity#using_the_metadata_server_to_acquire_tokens), [Cloud Run](https://cloud.google.com/run/docs/container-contract#metadata-server), [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/docs/concepts/workload-identity#instance_metadata), and [Cloud Build](https://cloud.google.com/kubernetes-engine/docs/concepts/workload-identity#instance_metadata) to authenticate with Infisical. + + + ## Diagram + + The following sequence digram illustrates the GCP ID Token Auth workflow for authenticating GCP resources with Infisical. + +```mermaid +sequenceDiagram + participant GCE as GCP Service + participant Infis as Infisical + participant Google as OAuth2 API + + Note over GCE,Google: Step 1: Instance Identity Token Retrieval + GCE->>Google: Request instance identity metadata token + Google-->>GCE: Return JWT token with RS256 signature + + Note over GCE,Infis: Step 2: Identity Token Login Operation + GCE->>Infis: Send JWT token to /api/v1/auth/gcp-auth/login + Infis->>Google: Request OAuth2 certificates + Google-->>Infis: Return certificates + + Note over Infis: Step 3: Identity Token Verification + Note over Infis: Step 4: Identity Property Validation + Infis->>GCE: Return short-lived access token + + Note over GCE,Infis: Step 4: Access Infisical API with Token + GCE->>Infis: Make authenticated requests using the short-lived access token +``` + + ## Concept + +At a high-level, Infisical authenticates a GCP resource by verifying its identity and checking that it meets specific requirements (e.g. it is an allowed GCE instance) at the `/api/v1/auth/gcp-auth/login` endpoint. If successful, +then Infisical returns a short-lived access token that can be used to make authenticated requests to the Infisical API. + +To be more specific: + +1. The client running on a GCP service obtains an [ID token](https://cloud.google.com/compute/docs/instances/verifying-instance-identity) constituting the identity for a GCP resource such as a GCE instance or Cloud Function; this is a unique JWT token that includes details about the instance as well as Google's [RS256 signature](https://datatracker.ietf.org/doc/html/rfc7518#section-3.3). +2. The client sends the ID token to Infisical. +3. Infisical verifies the token against Google's [public OAuth2 certificates](https://www.googleapis.com/oauth2/v3/certs). +4. Infisical fetches more details about the identity behind the token and checks if it is allowed to authenticate with Infisical based on set criteria. +5. If all is well, Infisical returns a short-lived access token that the client can use to make authenticated requests to the Infisical API. + + +We recommend using one of Infisical's clients like SDKs or the Infisical Agent +to authenticate with Infisical using GCP ID Token Auth as they handle the +authentication process including generating the instance ID token for you. + +Also, note that Infisical needs network-level access to send requests to the Google Cloud API +as part of the GCP Auth workflow. + + + +## Guide + +In the following steps, we explore how to create and use identities for your workloads and applications on GCP to +access the Infisical API using the GCP ID Token authentication method. + + + + To create an identity, head to your Organization Settings > Access Control > Machine Identities and press **Create identity**. + + ![identities organization](/images/platform/identities/identities-org.png) + + When creating an identity, you specify an organization level [role](/documentation/platform/role-based-access-controls) for it to assume; you can configure roles in Organization Settings > Access Control > Organization Roles. + + ![identities organization create](/images/platform/identities/identities-org-create.png) + + Now input a few details for your new identity. Here's some guidance for each field: + + - Name (required): A friendly name for the identity. + - Role (required): A role from the **Organization Roles** tab for the identity to assume. The organization role assigned will determine what organization level resources this identity can have access to. + + Once you've created an identity, you'll be prompted to configure the authentication method for it. Here, select **GCP Auth** and set the **Type** to **GCP ID Token Auth**. + + ![identities create gcp auth method](/images/platform/identities/identities-org-create-gcp-gce-auth-method.png) + + Here's some more guidance on each field: + + - Allowed Service Account Emails: A comma-separated list of trusted service account emails corresponding to the GCE resource(s) allowed to authenticate with Infisical; this could be something like `test@project.iam.gserviceaccount.com`, `12345-compute@developer.gserviceaccount.com`, etc. + - Allowed Projects: A comma-separated list of trusted GCP projects that the GCE instance must belong to authenticate with Infisical. Note that this validation property will only work for GCE instances. + - Allowed Zones: A comma-separated list of trusted zones that the GCE instances must belong to authenticate with Infisical; this should be the fully-qualified zone name in the format `-`like `us-central1-a`, `us-west1-b`, etc. Note that this validation property will only work for GCE instances. + - Access Token TTL (default is `2592000` equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time. + - Access Token Max TTL (default is `2592000` equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time. + - Access Token Max Number of Uses (default is `0`): The maximum number of times that an access token can be used; a value of `0` implies infinite number of uses. + - Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the `0.0.0.0/0`, allowing usage from any network address. + + + + To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project. + + To do this, head over to the project you want to add the identity to and go to Project Settings > Access Control > Machine Identities and press **Add identity**. + + Next, select the identity you want to add to the project and the project level role you want to allow it to assume. The project role assigned will determine what project level resources this identity can have access to. + + ![identities project](/images/platform/identities/identities-project.png) + + ![identities project create](/images/platform/identities/identities-project-create.png) + + + To access the Infisical API as the identity, you need to generate an [ID token](https://cloud.google.com/compute/docs/instances/verifying-instance-identity) constituting the identity of the present GCE instance and make a request to the `/api/v1/auth/gcp-auth/login` endpoint containing the token in exchange for an access token. + + We provide a few code examples below of how you can authenticate with Infisical to access the [Infisical API](/api-reference/overview/introduction). + + + + Start by making a request from the GCE instance to obtain the ID token. + For more examples of how to obtain the token in Java, Go, Node.js, etc. refer to the [official documentation](https://cloud.google.com/docs/authentication/get-id-token#curl). + + #### Sample request + + ```bash curl + curl -H "Metadata-Flavor: Google" \ + 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=' + ``` + + + + Note that you should replace `` with the ID of the identity you created in step 1. + + + Next use send the obtained JWT token along to authenticate with Infisical and obtain an access token. + + #### Sample request + + ```bash Request + curl --location --request POST 'https://app.infisical.com/api/v1/auth/gcp-auth/login' \ + --header 'Content-Type: application/x-www-form-urlencoded' \ + --data-urlencode 'identityId=...' \ + --data-urlencode 'jwt=...' + ``` + + #### Sample response + + ```bash Response + { + "accessToken": "...", + "expiresIn": 7200, + "accessTokenMaxTTL": 43244 + "tokenType": "Bearer" + } + ``` + + Next, you can use the access token to access the [Infisical API](/api-reference/overview/introduction) + + + + + We recommend using one of Infisical's clients like SDKs or the Infisical Agent to authenticate with Infisical using GCP IAM Auth as they handle the authentication process including generating the signed JWT token. + + + Each identity access token has a time-to-live (TLL) which you can infer from the response of the login operation; + the default TTL is `7200` seconds which can be adjusted. + If an identity access token expires, it can no longer authenticate with the Infisical API. In this case, + a new access token should be obtained by performing another login operation. + + + + + + ## Diagram @@ -87,18 +249,12 @@ access the Infisical API using the GCP IAM authentication method. Here's some more guidance on each field: - - Service Account JSON: The JSON key file for the GCP service account that will be used by Infisical to look-up other service account details; this service account should have the `iam.serviceAccounts.get` permission that can be found on the `roles/iam.serviceAccountKeyAdmin` role. - Allowed Service Account Emails: A comma-separated list of trusted IAM service account emails that are allowed to authenticate with Infisical; this could be something like `test@project.iam.gserviceaccount.com`, `12345-compute@developer.gserviceaccount.com`, etc. - - Allowed Projects: A comma-separated list of trusted GCP projects that the IAM service accounts must belong to authenticate with Infisical. - Access Token TTL (default is `2592000` equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time. - Access Token Max TTL (default is `2592000` equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time. - Access Token Max Number of Uses (default is `0`): The maximum number of times that an access token can be used; a value of `0` implies infinite number of uses. - Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the `0.0.0.0/0`, allowing usage from any network address. - - Note that you should enable the [IAM Service Account Credentials API](https://console.developers.google.com/apis/api/iamcredentials.googleapis.com/overview) - in your project. - To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project. @@ -191,172 +347,5 @@ access the Infisical API using the GCP IAM authentication method. - - - ## Diagram - - The following sequence digram illustrates the GCP GCE Auth workflow for authenticating a GCE instance with Infisical. - -```mermaid -sequenceDiagram - participant GCE as GCP Service - participant Infis as Infisical - participant Google as OAuth2 API - - Note over GCE,Google: Step 1: Instance Identity Token Retrieval - GCE->>Google: Request instance identity metadata token - Google-->>GCE: Return JWT token with RS256 signature - - Note over GCE,Infis: Step 2: Identity Token Login Operation - GCE->>Infis: Send JWT token to /api/v1/auth/gcp-auth/login - Infis->>Google: Request OAuth2 certificates - Google-->>Infis: Return certificates - - Note over Infis: Step 3: Identity Token Verification - Note over Infis: Step 4: Identity Property Validation - Infis->>GCE: Return short-lived access token - - Note over GCE,Infis: Step 4: Access Infisical API with Token - GCE->>Infis: Make authenticated requests using the short-lived access token -``` - - ## Concept - -At a high-level, Infisical authenticates a GCE instance by verifying its identity and checking that it meets specific requirements (e.g. it is an allowed GCE instance) at the `/api/v1/auth/gcp-auth/login` endpoint. If successful, -then Infisical returns a short-lived access token that can be used to make authenticated requests to the Infisical API. - -To be more specific: - -1. The client running on a GCP service obtains an [ID token](https://cloud.google.com/compute/docs/instances/verifying-instance-identity) constituting the identity for a GCE instance; this is a unique JWT token that includes details about the instance as well as Google's [RS256 signature](https://datatracker.ietf.org/doc/html/rfc7518#section-3.3). -2. The client sends the ID token to Infisical. -3. Infisical verifies the token against Google's [public OAuth2 certificates](https://www.googleapis.com/oauth2/v3/certs). -4. Infisical fetches more details about the identity behind the token and checks if it is allowed to authenticate with Infisical based on set criteria. -5. If all is well, Infisical returns a short-lived access token that the client can use to make authenticated requests to the Infisical API. - - -We recommend using one of Infisical's clients like SDKs or the Infisical Agent -to authenticate with Infisical using GCP GCE Auth as they handle the -authentication process including generating the instance ID token for you. - -Also, note that Infisical needs network-level access to send requests to the Google Cloud API -as part of the GCP Auth workflow. - - - -## Guide - -In the following steps, we explore how to create and use identities for your workloads and applications on GCP to -access the Infisical API using the GCP GCE authentication method. - - - - To create an identity, head to your Organization Settings > Access Control > Machine Identities and press **Create identity**. - - ![identities organization](/images/platform/identities/identities-org.png) - - When creating an identity, you specify an organization level [role](/documentation/platform/role-based-access-controls) for it to assume; you can configure roles in Organization Settings > Access Control > Organization Roles. - - ![identities organization create](/images/platform/identities/identities-org-create.png) - - Now input a few details for your new identity. Here's some guidance for each field: - - - Name (required): A friendly name for the identity. - - Role (required): A role from the **Organization Roles** tab for the identity to assume. The organization role assigned will determine what organization level resources this identity can have access to. - - Once you've created an identity, you'll be prompted to configure the authentication method for it. Here, select **GCP Auth** and set the **Type** to **GCP GCE Auth**. - - ![identities create gcp auth method](/images/platform/identities/identities-org-create-gcp-gce-auth-method.png) - - Here's some more guidance on each field: - - - Service Account JSON: The JSON key file for the GCP service account that will be used by Infisical to look-up authenticated service account details. - - Allowed Service Account Emails: A comma-separated list of trusted service account emails corresponding to the GCE instance(s) allowed to authenticate with Infisical; this could be something like `test@project.iam.gserviceaccount.com`, `12345-compute@developer.gserviceaccount.com`, etc. - - Allowed Projects: A comma-separated list of trusted GCP projects that the GCE instance must belong to authenticate with Infisical. - - Allowed Zones: A comma-separated list of trusted zones that the GCE instances must belong to authenticate with Infisical; this should be the fully-qualified zone name in the format `-`like `us-central1-a`, `us-west1-b`, etc. - - Access Token TTL (default is `2592000` equivalent to 30 days): The lifetime for an acccess token in seconds. This value will be referenced at renewal time. - - Access Token Max TTL (default is `2592000` equivalent to 30 days): The maximum lifetime for an acccess token in seconds. This value will be referenced at renewal time. - - Access Token Max Number of Uses (default is `0`): The maximum number of times that an access token can be used; a value of `0` implies infinite number of uses. - - Access Token Trusted IPs: The IPs or CIDR ranges that access tokens can be used from. By default, each token is given the `0.0.0.0/0`, allowing usage from any network address. - - - Note that you should enable the [IAM Service Account Credentials API](https://console.developers.google.com/apis/api/iamcredentials.googleapis.com/overview) - in your project. - - - - To enable the identity to access project-level resources such as secrets within a specific project, you should add it to that project. - - To do this, head over to the project you want to add the identity to and go to Project Settings > Access Control > Machine Identities and press **Add identity**. - - Next, select the identity you want to add to the project and the project level role you want to allow it to assume. The project role assigned will determine what project level resources this identity can have access to. - - ![identities project](/images/platform/identities/identities-project.png) - - ![identities project create](/images/platform/identities/identities-project-create.png) - - - To access the Infisical API as the identity, you need to generate an [ID token](https://cloud.google.com/compute/docs/instances/verifying-instance-identity) constituting the identity of the present GCE instance and make a request to the `/api/v1/auth/gcp-auth/login` endpoint containing the token in exchange for an access token. - - We provide a few code examples below of how you can authenticate with Infisical to access the [Infisical API](/api-reference/overview/introduction). - - - - Start by making a request from the GCE instance to obtain the ID token. - For more examples of how to obtain the token in Java, Go, Node.js, etc. refer to the [official documentation](https://cloud.google.com/docs/authentication/get-id-token#curl). - - #### Sample request - - ```bash curl - curl -H "Metadata-Flavor: Google" \ - 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=' - ``` - - - - Note that you should replace `` with the ID of the identity you created in step 1. - - - Next use send the obtained JWT token along to authenticate with Infisical and obtain an access token. - - #### Sample request - - ```bash Request - curl --location --request POST 'https://app.infisical.com/api/v1/auth/gcp-auth/login' \ - --header 'Content-Type: application/x-www-form-urlencoded' \ - --data-urlencode 'identityId=...' \ - --data-urlencode 'jwt=...' - ``` - - #### Sample response - - ```bash Response - { - "accessToken": "...", - "expiresIn": 7200, - "accessTokenMaxTTL": 43244 - "tokenType": "Bearer" - } - ``` - - Next, you can use the access token to access the [Infisical API](/api-reference/overview/introduction) - - - - - We recommend using one of Infisical's clients like SDKs or the Infisical Agent to authenticate with Infisical using GCP IAM Auth as they handle the authentication process including generating the signed JWT token. - - - Each identity access token has a time-to-live (TLL) which you can infer from the response of the login operation; - the default TTL is `7200` seconds which can be adjusted. - If an identity access token expires, it can no longer authenticate with the Infisical API. In this case, - a new access token should be obtained by performing another login operation. - - - - - - diff --git a/docs/images/platform/identities/identities-org-create-gcp-gce-auth-method.png b/docs/images/platform/identities/identities-org-create-gcp-gce-auth-method.png index 779c26884..899130c42 100644 Binary files a/docs/images/platform/identities/identities-org-create-gcp-gce-auth-method.png and b/docs/images/platform/identities/identities-org-create-gcp-gce-auth-method.png differ diff --git a/docs/images/platform/identities/identities-org-create-gcp-iam-auth-method.png b/docs/images/platform/identities/identities-org-create-gcp-iam-auth-method.png index f26e86473..9dacf9f89 100644 Binary files a/docs/images/platform/identities/identities-org-create-gcp-iam-auth-method.png and b/docs/images/platform/identities/identities-org-create-gcp-iam-auth-method.png differ diff --git a/frontend/src/hooks/api/identities/mutations.tsx b/frontend/src/hooks/api/identities/mutations.tsx index 527f74f16..5c2e20ff0 100644 --- a/frontend/src/hooks/api/identities/mutations.tsx +++ b/frontend/src/hooks/api/identities/mutations.tsx @@ -181,7 +181,6 @@ export const useAddIdentityGcpAuth = () => { return useMutation({ mutationFn: async ({ identityId, - credentials, type, allowedServiceAccounts, allowedProjects, @@ -196,7 +195,6 @@ export const useAddIdentityGcpAuth = () => { } = await apiRequest.post<{ identityGcpAuth: IdentityGcpAuth }>( `/api/v1/auth/gcp-auth/identities/${identityId}`, { - credentials, type, allowedServiceAccounts, allowedProjects, @@ -221,7 +219,6 @@ export const useUpdateIdentityGcpAuth = () => { return useMutation({ mutationFn: async ({ identityId, - credentials, type, allowedServiceAccounts, allowedProjects, @@ -236,7 +233,6 @@ export const useUpdateIdentityGcpAuth = () => { } = await apiRequest.patch<{ identityGcpAuth: IdentityGcpAuth }>( `/api/v1/auth/gcp-auth/identities/${identityId}`, { - credentials, type, allowedServiceAccounts, allowedProjects, diff --git a/frontend/src/hooks/api/identities/types.ts b/frontend/src/hooks/api/identities/types.ts index a63096562..c1d8e09fa 100644 --- a/frontend/src/hooks/api/identities/types.ts +++ b/frontend/src/hooks/api/identities/types.ts @@ -115,7 +115,6 @@ export type UpdateIdentityUniversalAuthDTO = { export type IdentityGcpAuth = { identityId: string; - credentials: string; type: "iam" | "gce"; allowedServiceAccounts: string; allowedProjects: string; @@ -129,7 +128,6 @@ export type IdentityGcpAuth = { export type AddIdentityGcpAuthDTO = { organizationId: string; identityId: string; - credentials: string; type: "iam" | "gce"; allowedServiceAccounts: string; allowedProjects: string; @@ -145,7 +143,6 @@ export type AddIdentityGcpAuthDTO = { export type UpdateIdentityGcpAuthDTO = { organizationId: string; identityId: string; - credentials?: string; type?: "iam" | "gce"; allowedServiceAccounts?: string; allowedProjects?: string; diff --git a/frontend/src/views/Org/MembersPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx b/frontend/src/views/Org/MembersPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx index ecfac5f45..430115d35 100644 --- a/frontend/src/views/Org/MembersPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx +++ b/frontend/src/views/Org/MembersPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx @@ -6,15 +6,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; -import { - Button, - FormControl, - IconButton, - Input, - Select, - SelectItem, - TextArea -} from "@app/components/v2"; +import { Button, FormControl, IconButton, Input, Select, SelectItem } from "@app/components/v2"; import { useOrganization, useSubscription } from "@app/context"; import { useAddIdentityGcpAuth, @@ -27,7 +19,6 @@ import { UsePopUpState } from "@app/hooks/usePopUp"; const schema = z .object({ - credentials: z.string(), type: z.enum(["iam", "gce"]), allowedServiceAccounts: z.string(), allowedProjects: z.string(), @@ -83,8 +74,7 @@ export const IdentityGcpAuthForm = ({ } = useForm({ resolver: zodResolver(schema), defaultValues: { - credentials: "", - type: "iam", + type: "gce", allowedServiceAccounts: "", allowedProjects: "", allowedZones: "", @@ -106,7 +96,6 @@ export const IdentityGcpAuthForm = ({ useEffect(() => { if (data) { reset({ - credentials: data.credentials, type: data.type, allowedServiceAccounts: data.allowedServiceAccounts, allowedProjects: data.allowedProjects, @@ -137,7 +126,6 @@ export const IdentityGcpAuthForm = ({ }, [data]); const onFormSubmit = async ({ - credentials, type, allowedServiceAccounts, allowedProjects, @@ -154,7 +142,6 @@ export const IdentityGcpAuthForm = ({ await updateMutateAsync({ identityId: identityAuthMethodData.identityId, organizationId: orgId, - credentials, type, allowedServiceAccounts, allowedProjects, @@ -168,7 +155,6 @@ export const IdentityGcpAuthForm = ({ await addMutateAsync({ identityId: identityAuthMethodData.identityId, organizationId: orgId, - credentials, type, allowedServiceAccounts: allowedServiceAccounts || "", allowedProjects: allowedProjects || "", @@ -211,36 +197,23 @@ export const IdentityGcpAuthForm = ({ onValueChange={(e) => onChange(e)} className="w-full" > + + GCP ID Token Auth (Recommended) + GCP IAM Auth - - GCP GCE Auth - )} /> - ( - -