misc: migrated internal CA to use new CA endpoint

This commit is contained in:
Sheen Capadngan
2025-05-22 04:21:54 +08:00
parent 77de085ffc
commit df75b3b8d3
22 changed files with 325 additions and 331 deletions
@@ -127,7 +127,8 @@ export const certificateAuthorityServiceFactory = ({
...(configuration as TCreateInternalCertificateAuthorityDTO["configuration"]),
isInternal: true,
projectId: finalProjectId,
requireTemplateForIssuance: !enableDirectIssuance
enableDirectIssuance,
name
});
if (!ca.internalCa) {
@@ -312,8 +313,9 @@ export const certificateAuthorityServiceFactory = ({
const updatedCa = await internalCertificateAuthorityService.updateCaById({
...configuration,
isInternal: true,
requireTemplateForIssuance: !enableDirectIssuance,
enableDirectIssuance,
caId: certificateAuthority.id,
status,
name
});
@@ -23,14 +23,12 @@ const InternalCertificateAuthorityConfigurationSchema = z
locality: z.string().trim().describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.locality),
notBefore: validateCaDateField.optional().describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.notBefore),
notAfter: validateCaDateField.optional().describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.notAfter),
maxPathLength: z.number().min(-1).describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.maxPathLength),
maxPathLength: z.number().min(-1).nullish().describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.maxPathLength),
keyAlgorithm: z.nativeEnum(CertKeyAlgorithm).describe(CertificateAuthorities.CONFIGURATIONS.INTERNAL.keyAlgorithm),
// no need for descriptions of the following fields because they are not exposed to the API user
dn: z.string().trim(),
dn: z.string().trim().nullish(),
parentCaId: z.string().uuid().nullish(),
serialNumber: z.string().trim().optional(),
activeCaCertId: z.string().uuid().optional()
serialNumber: z.string().trim().nullish(),
activeCaCertId: z.string().uuid().nullish()
})
.refine(
(data) => {
@@ -143,7 +143,8 @@ export const internalCertificateAuthorityServiceFactory = ({
notAfter,
maxPathLength,
keyAlgorithm,
requireTemplateForIssuance,
enableDirectIssuance,
name,
...dto
}: TCreateCaDTO) => {
let projectId: string;
@@ -202,8 +203,8 @@ export const internalCertificateAuthorityServiceFactory = ({
const ca = await certificateAuthorityDAL.create(
{
projectId,
enableDirectIssuance: !requireTemplateForIssuance,
name: slugify(`${friendlyName || dn}-${alphaNumericNanoId(8)}`),
enableDirectIssuance,
name: name || slugify(`${(friendlyName || dn).slice(0, 16)}-${alphaNumericNanoId(8)}`),
status: type === InternalCaType.ROOT ? CaStatus.ACTIVE : CaStatus.PENDING_CERTIFICATE
},
tx
@@ -360,7 +361,7 @@ export const internalCertificateAuthorityServiceFactory = ({
* Update CA with id [caId].
* Note: Used to enable/disable CA
*/
const updateCaById = async ({ caId, status, requireTemplateForIssuance, name, ...dto }: TUpdateCaDTO) => {
const updateCaById = async ({ caId, status, enableDirectIssuance, name, ...dto }: TUpdateCaDTO) => {
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });
@@ -381,12 +382,8 @@ export const internalCertificateAuthorityServiceFactory = ({
}
const updatedCa = await certificateAuthorityDAL.transaction(async (tx) => {
if (requireTemplateForIssuance !== undefined || status !== undefined || name !== undefined) {
await certificateAuthorityDAL.updateById(
ca.id,
{ enableDirectIssuance: !requireTemplateForIssuance, status, name },
tx
);
if (enableDirectIssuance !== undefined || status !== undefined || name !== undefined) {
await certificateAuthorityDAL.updateById(ca.id, { enableDirectIssuance, status, name }, tx);
}
return certificateAuthorityDAL.findByIdWithAssociatedCa(caId, tx);
@@ -31,6 +31,7 @@ export type TCreateCaDTO =
projectId: string;
type: InternalCaType;
friendlyName?: string;
name?: string;
commonName: string;
organization: string;
ou: string;
@@ -39,15 +40,16 @@ export type TCreateCaDTO =
locality: string;
notBefore?: string;
notAfter?: string;
maxPathLength: number;
maxPathLength?: number | null;
keyAlgorithm: CertKeyAlgorithm;
requireTemplateForIssuance: boolean;
enableDirectIssuance: boolean;
}
| ({
isInternal: false;
projectSlug: string;
type: InternalCaType;
friendlyName?: string;
name?: string;
commonName: string;
organization: string;
ou: string;
@@ -56,9 +58,9 @@ export type TCreateCaDTO =
locality: string;
notBefore?: string;
notAfter?: string;
maxPathLength: number;
maxPathLength?: number | null;
keyAlgorithm: CertKeyAlgorithm;
requireTemplateForIssuance: boolean;
enableDirectIssuance: boolean;
} & Omit<TProjectPermission, "projectId">);
export type TGetCaDTO = {
@@ -71,14 +73,14 @@ export type TUpdateCaDTO =
caId: string;
name?: string;
status?: CaStatus;
requireTemplateForIssuance?: boolean;
enableDirectIssuance?: boolean;
}
| ({
isInternal: false;
caId: string;
name?: string;
status?: CaStatus;
requireTemplateForIssuance?: boolean;
enableDirectIssuance?: boolean;
} & Omit<TProjectPermission, "projectId">);
export type TDeleteCaDTO = {
@@ -920,7 +920,7 @@ export const projectServiceFactory = ({
{
[`${TableName.CertificateAuthority}.projectId` as "projectId"]: projectId,
$notNull: [`${TableName.InternalCertificateAuthority}.id` as "id"],
...(status && { [`${TableName.InternalCertificateAuthority}.status` as "status"]: status }),
...(status && { [`${TableName.CertificateAuthority}.status` as "status"]: status }),
...(friendlyName && {
[`${TableName.InternalCertificateAuthority}.friendlyName` as "friendlyName"]: friendlyName
}),