Finish preliminary backend endpoints for GCP IAM Auth method

This commit is contained in:
Tuan Dang
2024-05-07 10:42:39 -07:00
parent 4d8965eb82
commit 4d74d264dd
14 changed files with 768 additions and 1 deletions
+13
View File
@@ -72,6 +72,8 @@ import { identityOrgDALFactory } from "@app/services/identity/identity-org-dal";
import { identityServiceFactory } from "@app/services/identity/identity-service";
import { identityAccessTokenDALFactory } from "@app/services/identity-access-token/identity-access-token-dal";
import { identityAccessTokenServiceFactory } from "@app/services/identity-access-token/identity-access-token-service";
import { identityGcpIamAuthDALFactory } from "@app/services/identity-gcp-iam-auth/identity-gcp-iam-auth-dal";
import { identityGcpIamAuthServiceFactory } from "@app/services/identity-gcp-iam-auth/identity-gcp-iam-auth-service";
import { identityProjectDALFactory } from "@app/services/identity-project/identity-project-dal";
import { identityProjectMembershipRoleDALFactory } from "@app/services/identity-project/identity-project-membership-role-dal";
import { identityProjectServiceFactory } from "@app/services/identity-project/identity-project-service";
@@ -196,6 +198,8 @@ export const registerRoutes = async (
const identityUaDAL = identityUaDALFactory(db);
const identityUaClientSecretDAL = identityUaClientSecretDALFactory(db);
const identityGcpIamAuthDAL = identityGcpIamAuthDALFactory(db);
const auditLogDAL = auditLogDALFactory(db);
const auditLogStreamDAL = auditLogStreamDALFactory(db);
const trustedIpDAL = trustedIpDALFactory(db);
@@ -662,6 +666,14 @@ export const registerRoutes = async (
identityUaDAL,
licenseService
});
const identityGcpIamAuthService = identityGcpIamAuthServiceFactory({
identityGcpIamAuthDAL,
identityOrgMembershipDAL,
identityAccessTokenDAL,
identityDAL,
permissionService,
licenseService
});
const dynamicSecretProviders = buildDynamicSecretProviders();
const dynamicSecretQueueService = dynamicSecretLeaseQueueServiceFactory({
@@ -731,6 +743,7 @@ export const registerRoutes = async (
identityAccessToken: identityAccessTokenService,
identityProject: identityProjectService,
identityUa: identityUaService,
identityGcpIamAuth: identityGcpIamAuthService,
secretApprovalPolicy: sapService,
secretApprovalRequest: sarService,
secretRotation: secretRotationService,
@@ -0,0 +1,259 @@
import { z } from "zod";
import { IdentityGcpIamAuthsSchema } 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";
export const registerIdentityGcpIamAuthRouter = async (server: FastifyZodProvider) => {
server.route({
method: "POST",
url: "/gcp-iam-auth/login",
config: {
rateLimit: writeLimit
},
schema: {
description: "Login with GCP IAM Auth",
body: z.object({
identityId: z.string(),
jwt: z.string()
}),
response: {
200: z.object({
accessToken: z.string(),
expiresIn: z.coerce.number(),
accessTokenMaxTTL: z.coerce.number(),
tokenType: z.literal("Bearer")
})
}
},
handler: async (req) => {
const { identityGcpIamAuth, accessToken, identityAccessToken, identityMembershipOrg } =
await server.services.identityGcpIamAuth.login(req.body);
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: identityMembershipOrg?.orgId,
event: {
type: EventType.LOGIN_IDENTITY_GCP_IAM_AUTH,
metadata: {
identityId: identityGcpIamAuth.identityId,
identityAccessTokenId: identityAccessToken.id,
identityGcpIamAuthId: identityGcpIamAuth.id
}
}
});
return {
accessToken,
tokenType: "Bearer" as const,
expiresIn: identityGcpIamAuth.accessTokenTTL,
accessTokenMaxTTL: identityGcpIamAuth.accessTokenMaxTTL
};
}
});
server.route({
method: "POST",
url: "/gcp-iam-auth/identities/:identityId",
config: {
rateLimit: writeLimit
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
schema: {
description: "Attach GCP IAM Auth configuration onto identity",
security: [
{
bearerAuth: []
}
],
params: z.object({
identityId: z.string().trim()
}),
body: z.object({
allowedServiceAccounts: z.string(), // TODO: better validation
allowedProjects: z.string(), // TODO: better validation
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
})
.array()
.min(1)
.default([{ ipAddress: "0.0.0.0/0" }, { ipAddress: "::/0" }]),
accessTokenTTL: z
.number()
.int()
.min(1)
.refine((value) => value !== 0, {
message: "accessTokenTTL must have a non zero number"
})
.default(2592000),
accessTokenMaxTTL: z
.number()
.int()
.refine((value) => value !== 0, {
message: "accessTokenMaxTTL must have a non zero number"
})
.default(2592000),
accessTokenNumUsesLimit: z.number().int().min(0).default(0)
}),
response: {
200: z.object({
identityGcpIamAuth: IdentityGcpIamAuthsSchema
})
}
},
handler: async (req) => {
const identityGcpIamAuth = await server.services.identityGcpIamAuth.attachGcpIamAuth({
actor: req.permission.type,
actorId: req.permission.id,
actorAuthMethod: req.permission.authMethod,
actorOrgId: req.permission.orgId,
...req.body,
identityId: req.params.identityId
});
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: identityGcpIamAuth.orgId,
event: {
type: EventType.ADD_IDENTITY_GCP_IAM_AUTH,
metadata: {
identityId: identityGcpIamAuth.identityId,
allowedServiceAccounts: identityGcpIamAuth.allowedServiceAccounts,
allowedProjects: identityGcpIamAuth.allowedProjects,
accessTokenTTL: identityGcpIamAuth.accessTokenTTL,
accessTokenMaxTTL: identityGcpIamAuth.accessTokenMaxTTL,
accessTokenTrustedIps: identityGcpIamAuth.accessTokenTrustedIps as TIdentityTrustedIp[],
accessTokenNumUsesLimit: identityGcpIamAuth.accessTokenNumUsesLimit
}
}
});
return { identityGcpIamAuth };
}
});
server.route({
method: "PATCH",
url: "/gcp-iam-auth/identities/:identityId",
config: {
rateLimit: writeLimit
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
schema: {
description: "Update GCP IAM Auth configuration on identity",
security: [
{
bearerAuth: []
}
],
params: z.object({
identityId: z.string().trim()
}),
body: z.object({
allowedServiceAccounts: z.string().trim().optional(),
allowedProjects: z.string().trim().optional(),
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
})
.array()
.min(1)
.optional(),
accessTokenTTL: z.number().int().min(0).optional(),
accessTokenNumUsesLimit: z.number().int().min(0).optional(),
accessTokenMaxTTL: z
.number()
.int()
.refine((value) => value !== 0, {
message: "accessTokenMaxTTL must have a non zero number"
})
.optional()
}),
response: {
200: z.object({
identityGcpIamAuth: IdentityGcpIamAuthsSchema
})
}
},
handler: async (req) => {
const identityGcpIamAuth = await server.services.identityGcpIamAuth.updateGcpIamAuth({
actor: req.permission.type,
actorId: req.permission.id,
actorOrgId: req.permission.orgId,
actorAuthMethod: req.permission.authMethod,
...req.body,
identityId: req.params.identityId
});
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: identityGcpIamAuth.orgId,
event: {
type: EventType.UPDATE_IDENTITY_GCP_IAM_AUTH,
metadata: {
identityId: identityGcpIamAuth.identityId,
allowedServiceAccounts: identityGcpIamAuth.allowedServiceAccounts,
allowedProjects: identityGcpIamAuth.allowedProjects,
accessTokenTTL: identityGcpIamAuth.accessTokenTTL,
accessTokenMaxTTL: identityGcpIamAuth.accessTokenMaxTTL,
accessTokenTrustedIps: identityGcpIamAuth.accessTokenTrustedIps as TIdentityTrustedIp[],
accessTokenNumUsesLimit: identityGcpIamAuth.accessTokenNumUsesLimit
}
}
});
return { identityGcpIamAuth };
}
});
server.route({
method: "GET",
url: "/gcp-iam-auth/identities/:identityId",
config: {
rateLimit: readLimit
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
schema: {
description: "Retrieve GCP IAM Auth configuration on identity",
security: [
{
bearerAuth: []
}
],
params: z.object({
identityId: z.string()
}),
response: {
200: z.object({
identityGcpIamAuth: IdentityGcpIamAuthsSchema
})
}
},
handler: async (req) => {
const identityGcpIamAuth = await server.services.identityGcpIamAuth.getGcpIamAuth({
identityId: req.params.identityId,
actor: req.permission.type,
actorId: req.permission.id,
actorOrgId: req.permission.orgId,
actorAuthMethod: req.permission.authMethod
});
await server.services.auditLog.createAuditLog({
...req.auditLogInfo,
orgId: identityGcpIamAuth.orgId,
event: {
type: EventType.GET_IDENTITY_GCP_IAM_AUTH,
metadata: {
identityId: identityGcpIamAuth.identityId
}
}
});
return { identityGcpIamAuth };
}
});
};
+2
View File
@@ -2,6 +2,7 @@ import { registerAdminRouter } from "./admin-router";
import { registerAuthRoutes } from "./auth-router";
import { registerProjectBotRouter } from "./bot-router";
import { registerIdentityAccessTokenRouter } from "./identity-access-token-router";
import { registerIdentityGcpIamAuthRouter } from "./identity-gcp-iam-auth-router";
import { registerIdentityRouter } from "./identity-router";
import { registerIdentityUaRouter } from "./identity-ua";
import { registerIntegrationAuthRouter } from "./integration-auth-router";
@@ -27,6 +28,7 @@ export const registerV1Routes = async (server: FastifyZodProvider) => {
async (authRouter) => {
await authRouter.register(registerAuthRoutes);
await authRouter.register(registerIdentityUaRouter);
await authRouter.register(registerIdentityGcpIamAuthRouter);
await authRouter.register(registerIdentityAccessTokenRouter);
},
{ prefix: "/auth" }