Merge pull request #2178 from Infisical/cert-search-filtering

Add List CAs / Certificates to Documentation + Filter Options
This commit is contained in:
BlackMagiq
2024-07-25 09:40:44 -07:00
committed by GitHub
7 changed files with 71 additions and 16 deletions
@@ -320,7 +320,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
slug: slugSchema.describe("The slug of the project to list CAs.") slug: slugSchema.describe("The slug of the project to list CAs.")
}), }),
querystring: z.object({ querystring: z.object({
status: z.enum([CaStatus.ACTIVE, CaStatus.PENDING_CERTIFICATE]).optional() status: z.enum([CaStatus.ACTIVE, CaStatus.PENDING_CERTIFICATE]).optional(),
friendlyName: z.string().optional(),
commonName: z.string().optional(),
offset: z.coerce.number().min(0).max(100).default(0),
limit: z.coerce.number().min(1).max(100).default(25)
}), }),
response: { response: {
200: z.object({ 200: z.object({
@@ -336,11 +340,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
orgId: req.permission.orgId, orgId: req.permission.orgId,
type: ProjectFilterType.SLUG type: ProjectFilterType.SLUG
}, },
status: req.query.status,
actorId: req.permission.id, actorId: req.permission.id,
actorOrgId: req.permission.orgId, actorOrgId: req.permission.orgId,
actorAuthMethod: req.permission.authMethod, actorAuthMethod: req.permission.authMethod,
actor: req.permission.type actor: req.permission.type,
...req.query
}); });
return { cas }; return { cas };
} }
@@ -357,6 +361,8 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
slug: slugSchema.describe("The slug of the project to list certificates.") slug: slugSchema.describe("The slug of the project to list certificates.")
}), }),
querystring: z.object({ querystring: z.object({
friendlyName: z.string().optional(),
commonName: z.string().optional(),
offset: z.coerce.number().min(0).max(100).default(0), offset: z.coerce.number().min(0).max(100).default(0),
limit: z.coerce.number().min(1).max(100).default(25) limit: z.coerce.number().min(1).max(100).default(25)
}), }),
@@ -8,19 +8,35 @@ export type TCertificateDALFactory = ReturnType<typeof certificateDALFactory>;
export const certificateDALFactory = (db: TDbClient) => { export const certificateDALFactory = (db: TDbClient) => {
const certificateOrm = ormify(db, TableName.Certificate); const certificateOrm = ormify(db, TableName.Certificate);
const countCertificatesInProject = async (projectId: string) => { const countCertificatesInProject = async ({
projectId,
friendlyName,
commonName
}: {
projectId: string;
friendlyName?: string;
commonName?: string;
}) => {
try { try {
interface CountResult { interface CountResult {
count: string; count: string;
} }
const count = await db let query = db
.replicaNode()(TableName.Certificate) .replicaNode()(TableName.Certificate)
.join(TableName.CertificateAuthority, `${TableName.Certificate}.caId`, `${TableName.CertificateAuthority}.id`) .join(TableName.CertificateAuthority, `${TableName.Certificate}.caId`, `${TableName.CertificateAuthority}.id`)
.join(TableName.Project, `${TableName.CertificateAuthority}.projectId`, `${TableName.Project}.id`) .join(TableName.Project, `${TableName.CertificateAuthority}.projectId`, `${TableName.Project}.id`)
.where(`${TableName.Project}.id`, projectId) .where(`${TableName.Project}.id`, projectId);
.count("*")
.first(); if (friendlyName) {
query = query.andWhere(`${TableName.Certificate}.friendlyName`, friendlyName);
}
if (commonName) {
query = query.andWhere(`${TableName.Certificate}.commonName`, commonName);
}
const count = await query.count("*").first();
return parseInt((count as unknown as CountResult).count || "0", 10); return parseInt((count as unknown as CountResult).count || "0", 10);
} catch (error) { } catch (error) {
@@ -575,6 +575,10 @@ export const projectServiceFactory = ({
*/ */
const listProjectCas = async ({ const listProjectCas = async ({
status, status,
friendlyName,
commonName,
limit = 25,
offset = 0,
actorId, actorId,
actorOrgId, actorOrgId,
actorAuthMethod, actorAuthMethod,
@@ -596,10 +600,15 @@ export const projectServiceFactory = ({
ProjectPermissionSub.CertificateAuthorities ProjectPermissionSub.CertificateAuthorities
); );
const cas = await certificateAuthorityDAL.find({ const cas = await certificateAuthorityDAL.find(
projectId: project.id, {
...(status && { status }) projectId: project.id,
}); ...(status && { status }),
...(friendlyName && { friendlyName }),
...(commonName && { commonName })
},
{ offset, limit, sort: [["updatedAt", "desc"]] }
);
return cas; return cas;
}; };
@@ -608,8 +617,10 @@ export const projectServiceFactory = ({
* Return list of certificates for project * Return list of certificates for project
*/ */
const listProjectCertificates = async ({ const listProjectCertificates = async ({
offset, limit = 25,
limit, offset = 0,
friendlyName,
commonName,
actorId, actorId,
actorOrgId, actorOrgId,
actorAuthMethod, actorAuthMethod,
@@ -634,12 +645,18 @@ export const projectServiceFactory = ({
{ {
$in: { $in: {
caId: cas.map((ca) => ca.id) caId: cas.map((ca) => ca.id)
} },
...(friendlyName && { friendlyName }),
...(commonName && { commonName })
}, },
{ offset, limit, sort: [["updatedAt", "desc"]] } { offset, limit, sort: [["updatedAt", "desc"]] }
); );
const count = await certificateDAL.countCertificatesInProject(project.id); const count = await certificateDAL.countCertificatesInProject({
projectId: project.id,
friendlyName,
commonName
});
return { return {
certificates, certificates,
@@ -89,6 +89,10 @@ export type AddUserToWsDTO = {
export type TListProjectCasDTO = { export type TListProjectCasDTO = {
status?: CaStatus; status?: CaStatus;
friendlyName?: string;
offset?: number;
limit?: number;
commonName?: string;
filter: Filter; filter: Filter;
} & Omit<TProjectPermission, "projectId">; } & Omit<TProjectPermission, "projectId">;
@@ -96,4 +100,6 @@ export type TListProjectCertsDTO = {
filter: Filter; filter: Filter;
offset: number; offset: number;
limit: number; limit: number;
friendlyName?: string;
commonName?: string;
} & Omit<TProjectPermission, "projectId">; } & Omit<TProjectPermission, "projectId">;
@@ -0,0 +1,4 @@
---
title: "List"
openapi: "GET /api/v2/workspace/{slug}/cas"
---
@@ -0,0 +1,4 @@
---
title: "List"
openapi: "GET /api/v2/workspace/{slug}/certificates"
---
+2
View File
@@ -654,6 +654,7 @@
{ {
"group": "Certificate Authorities", "group": "Certificate Authorities",
"pages": [ "pages": [
"api-reference/endpoints/certificate-authorities/list",
"api-reference/endpoints/certificate-authorities/create", "api-reference/endpoints/certificate-authorities/create",
"api-reference/endpoints/certificate-authorities/read", "api-reference/endpoints/certificate-authorities/read",
"api-reference/endpoints/certificate-authorities/update", "api-reference/endpoints/certificate-authorities/update",
@@ -669,6 +670,7 @@
{ {
"group": "Certificates", "group": "Certificates",
"pages": [ "pages": [
"api-reference/endpoints/certificates/list",
"api-reference/endpoints/certificates/read", "api-reference/endpoints/certificates/read",
"api-reference/endpoints/certificates/revoke", "api-reference/endpoints/certificates/revoke",
"api-reference/endpoints/certificates/delete", "api-reference/endpoints/certificates/delete",