Update cert structure to ref correct ca cert in cert chain retrieval

This commit is contained in:
Tuan Dang
2024-08-14 15:43:49 -07:00
parent f993e4aa5c
commit 856c2423be
6 changed files with 45 additions and 16 deletions
@@ -62,6 +62,26 @@ export async function up(knex: Knex): Promise<void> {
t.dropUnique(["caId"]); t.dropUnique(["caId"]);
}); });
} }
if (await knex.schema.hasTable(TableName.Certificate)) {
await knex.schema.alterTable(TableName.Certificate, (t) => {
t.uuid("caCertId").nullable();
t.foreign("caCertId").references("id").inTable(TableName.CertificateAuthorityCert);
});
await knex.raw(`
UPDATE "${TableName.Certificate}" cert
SET "caCertId" = (
SELECT caCert.id
FROM "${TableName.CertificateAuthorityCert}" caCert
WHERE caCert."caId" = cert."caId"
)
`);
await knex.schema.alterTable(TableName.Certificate, (t) => {
t.uuid("caCertId").notNullable().alter();
});
}
} }
export async function down(knex: Knex): Promise<void> { export async function down(knex: Knex): Promise<void> {
@@ -86,4 +106,12 @@ export async function down(knex: Knex): Promise<void> {
}); });
} }
} }
if (await knex.schema.hasTable(TableName.Certificate)) {
if (await knex.schema.hasColumn(TableName.Certificate, "caCertId")) {
await knex.schema.alterTable(TableName.Certificate, (t) => {
t.dropColumn("caCertId");
});
}
}
} }
+2 -1
View File
@@ -20,7 +20,8 @@ export const CertificatesSchema = z.object({
notAfter: z.date(), notAfter: z.date(),
revokedAt: z.date().nullable().optional(), revokedAt: z.date().nullable().optional(),
revocationReason: z.number().nullable().optional(), revocationReason: z.number().nullable().optional(),
altNames: z.string().default("").nullable().optional() altNames: z.string().default("").nullable().optional(),
caCertId: z.string().uuid()
}); });
export type TCertificates = z.infer<typeof CertificatesSchema>; export type TCertificates = z.infer<typeof CertificatesSchema>;
@@ -195,20 +195,18 @@ export const getCaCertChains = async ({
/** /**
* Return the decrypted pem-encoded certificate and certificate chain * Return the decrypted pem-encoded certificate and certificate chain
* for CA with id [caId]. * corresponding to CA certificate with id [caCertId].
*/ */
export const getCaCertChain = async ({ export const getCaCertChain = async ({
caId, caCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
kmsService kmsService
}: TGetCaCertChainDTO) => { }: TGetCaCertChainDTO) => {
const ca = await certificateAuthorityDAL.findById(caId); const caCert = await certificateAuthorityCertDAL.findById(caCertId);
if (!ca) throw new BadRequestError({ message: "CA not found" }); if (!caCert) throw new BadRequestError({ message: "CA certificate not found" });
if (!ca.activeCaCertId) throw new BadRequestError({ message: "CA does not have a certificate installed" }); const ca = await certificateAuthorityDAL.findById(caCert.caId);
const caCert = await certificateAuthorityCertDAL.findById(ca.activeCaCertId);
const keyId = await getProjectKmsCertificateKeyId({ const keyId = await getProjectKmsCertificateKeyId({
projectId: ca.projectId, projectId: ca.projectId,
@@ -617,7 +617,7 @@ export const certificateAuthorityServiceFactory = ({
}); });
const { caCert: parentCaCertificate, caCertChain: parentCaCertChain } = await getCaCertChain({ const { caCert: parentCaCertificate, caCertChain: parentCaCertChain } = await getCaCertChain({
caId: parentCa.id, caCertId: parentCa.activeCaCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
@@ -704,11 +704,11 @@ export const certificateAuthorityServiceFactory = ({
/** /**
* Return current certificate and certificate chain for CA * Return current certificate and certificate chain for CA
* get latest?? ca cert
*/ */
const getCaCert = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TGetCaCertDTO) => { const getCaCert = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TGetCaCertDTO) => {
const ca = await certificateAuthorityDAL.findById(caId); const ca = await certificateAuthorityDAL.findById(caId);
if (!ca) throw new BadRequestError({ message: "CA not found" }); if (!ca) throw new BadRequestError({ message: "CA not found" });
if (!ca.activeCaCertId) throw new BadRequestError({ message: "CA does not have a certificate installed" });
const { permission } = await permissionService.getProjectPermission( const { permission } = await permissionService.getProjectPermission(
actor, actor,
@@ -724,7 +724,7 @@ export const certificateAuthorityServiceFactory = ({
); );
const { caCert, caCertChain, serialNumber } = await getCaCertChain({ const { caCert, caCertChain, serialNumber } = await getCaCertChain({
caId, caCertId: ca.activeCaCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
@@ -860,7 +860,7 @@ export const certificateAuthorityServiceFactory = ({
}); });
const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({ const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
caId, caCertId: ca.activeCaCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
@@ -1166,6 +1166,7 @@ export const certificateAuthorityServiceFactory = ({
const cert = await certificateDAL.create( const cert = await certificateDAL.create(
{ {
caId: ca.id, caId: ca.id,
caCertId: caCert.id,
status: CertStatus.ACTIVE, status: CertStatus.ACTIVE,
friendlyName: friendlyName || commonName, friendlyName: friendlyName || commonName,
commonName, commonName,
@@ -1189,7 +1190,7 @@ export const certificateAuthorityServiceFactory = ({
}); });
const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({ const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
caId: ca.id, caCertId: caCert.id,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
@@ -1369,6 +1370,7 @@ export const certificateAuthorityServiceFactory = ({
const cert = await certificateDAL.create( const cert = await certificateDAL.create(
{ {
caId: ca.id, caId: ca.id,
caCertId: caCert.id,
status: CertStatus.ACTIVE, status: CertStatus.ACTIVE,
friendlyName: friendlyName || csrObj.subject, friendlyName: friendlyName || csrObj.subject,
commonName: cn, commonName: cn,
@@ -1392,7 +1394,7 @@ export const certificateAuthorityServiceFactory = ({
}); });
const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({ const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
caId: ca.id, caCertId: ca.activeCaCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,
@@ -132,7 +132,7 @@ export type TGetCaCertChainsDTO = {
}; };
export type TGetCaCertChainDTO = { export type TGetCaCertChainDTO = {
caId: string; caCertId: string;
certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById">; certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById">;
certificateAuthorityCertDAL: Pick<TCertificateAuthorityCertDALFactory, "findById">; certificateAuthorityCertDAL: Pick<TCertificateAuthorityCertDALFactory, "findById">;
projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">; projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">;
@@ -180,7 +180,7 @@ export const certificateServiceFactory = ({
const certObj = new x509.X509Certificate(decryptedCert); const certObj = new x509.X509Certificate(decryptedCert);
const { caCert, caCertChain } = await getCaCertChain({ const { caCert, caCertChain } = await getCaCertChain({
caId: ca.id, caCertId: cert.caCertId,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateAuthorityCertDAL, certificateAuthorityCertDAL,
projectDAL, projectDAL,