mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-18 17:41:16 +00:00
PKI revamp - backend initial changes
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import { TableName } from "@app/db/schemas";
|
||||
import { DatabaseError } from "@app/lib/errors";
|
||||
import { ormify } from "@app/lib/knex";
|
||||
|
||||
import { TApiEnrollmentConfigInsert, TApiEnrollmentConfigUpdate } from "./enrollment-config-types";
|
||||
|
||||
export type TApiEnrollmentConfigDALFactory = ReturnType<typeof apiEnrollmentConfigDALFactory>;
|
||||
|
||||
export const apiEnrollmentConfigDALFactory = (db: TDbClient) => {
|
||||
const apiEnrollmentConfigOrm = ormify(db, TableName.ApiEnrollmentConfig);
|
||||
|
||||
const create = async (data: TApiEnrollmentConfigInsert, tx?: Knex) => {
|
||||
try {
|
||||
const [apiConfig] = await (tx || db)(TableName.ApiEnrollmentConfig).insert(data).returning("*");
|
||||
|
||||
return apiConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Create API enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const updateById = async (id: string, data: TApiEnrollmentConfigUpdate, tx?: Knex) => {
|
||||
try {
|
||||
const [apiConfig] = await (tx || db)(TableName.ApiEnrollmentConfig).where({ id }).update(data).returning("*");
|
||||
|
||||
return apiConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Update API enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const deleteById = async (id: string, tx?: Knex) => {
|
||||
try {
|
||||
const [apiConfig] = await (tx || db)(TableName.ApiEnrollmentConfig).where({ id }).del().returning("*");
|
||||
|
||||
return apiConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Delete API enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const findById = async (id: string, tx?: Knex) => {
|
||||
try {
|
||||
const apiConfig = await (tx || db)(TableName.ApiEnrollmentConfig).where({ id }).first();
|
||||
|
||||
return apiConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find API enrollment config by id" });
|
||||
}
|
||||
};
|
||||
|
||||
const findProfilesForAutoRenewal = async (renewalThresholdDays: number = 30, tx?: Knex) => {
|
||||
try {
|
||||
const now = new Date();
|
||||
const renewalDate = new Date();
|
||||
renewalDate.setDate(now.getDate() + renewalThresholdDays);
|
||||
|
||||
const profiles = await (tx || db)(TableName.CertificateProfile)
|
||||
.join(
|
||||
TableName.ApiEnrollmentConfig,
|
||||
`${TableName.CertificateProfile}.apiConfigId`,
|
||||
`${TableName.ApiEnrollmentConfig}.id`
|
||||
)
|
||||
.where(`${TableName.ApiEnrollmentConfig}.autoRenew`, true)
|
||||
.where((query) => {
|
||||
void query
|
||||
.whereNull(`${TableName.ApiEnrollmentConfig}.autoRenewDays`)
|
||||
.orWhere(`${TableName.ApiEnrollmentConfig}.autoRenewDays`, "<=", renewalThresholdDays);
|
||||
})
|
||||
.select((tx || db).ref("id").withSchema(TableName.CertificateProfile))
|
||||
.select((tx || db).ref("name").withSchema(TableName.CertificateProfile))
|
||||
.select((tx || db).ref("projectId").withSchema(TableName.CertificateProfile))
|
||||
.select((tx || db).ref("autoRenewDays").withSchema(TableName.CertificateProfile));
|
||||
|
||||
return profiles;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find profiles for auto renewal" });
|
||||
}
|
||||
};
|
||||
|
||||
const isConfigInUse = async (configId: string, tx?: Knex) => {
|
||||
try {
|
||||
const doc = await (tx || db)(TableName.CertificateProfile).where({ apiConfigId: configId }).count("*").first();
|
||||
|
||||
return parseInt(doc || "0", 10);
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Check if API enrollment config is in use" });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...apiEnrollmentConfigOrm,
|
||||
create,
|
||||
updateById,
|
||||
deleteById,
|
||||
findById,
|
||||
findProfilesForAutoRenewal,
|
||||
isConfigInUse
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
TApiEnrollmentConfigs,
|
||||
TApiEnrollmentConfigsInsert,
|
||||
TApiEnrollmentConfigsUpdate
|
||||
} from "@app/db/schemas/api-enrollment-configs";
|
||||
import {
|
||||
TEstEnrollmentConfigs,
|
||||
TEstEnrollmentConfigsInsert,
|
||||
TEstEnrollmentConfigsUpdate
|
||||
} from "@app/db/schemas/est-enrollment-configs";
|
||||
|
||||
export type TEstEnrollmentConfig = TEstEnrollmentConfigs;
|
||||
export type TEstEnrollmentConfigInsert = TEstEnrollmentConfigsInsert;
|
||||
export type TEstEnrollmentConfigUpdate = TEstEnrollmentConfigsUpdate;
|
||||
|
||||
export type TApiEnrollmentConfig = TApiEnrollmentConfigs;
|
||||
export type TApiEnrollmentConfigInsert = TApiEnrollmentConfigsInsert;
|
||||
export type TApiEnrollmentConfigUpdate = TApiEnrollmentConfigsUpdate;
|
||||
|
||||
export interface TEstConfigData {
|
||||
disableBootstrapCaValidation: boolean;
|
||||
passphrase: string;
|
||||
encryptedCaChain: string;
|
||||
}
|
||||
|
||||
export interface TApiConfigData {
|
||||
autoRenew: boolean;
|
||||
autoRenewDays?: number;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import { TableName } from "@app/db/schemas";
|
||||
import { DatabaseError } from "@app/lib/errors";
|
||||
import { ormify } from "@app/lib/knex";
|
||||
|
||||
import { TEstEnrollmentConfigInsert, TEstEnrollmentConfigUpdate } from "./enrollment-config-types";
|
||||
|
||||
export type TEstEnrollmentConfigDALFactory = ReturnType<typeof estEnrollmentConfigDALFactory>;
|
||||
|
||||
export const estEnrollmentConfigDALFactory = (db: TDbClient) => {
|
||||
const estEnrollmentConfigOrm = ormify(db, TableName.EstEnrollmentConfig);
|
||||
|
||||
const create = async (data: TEstEnrollmentConfigInsert, tx?: Knex) => {
|
||||
try {
|
||||
const [estConfig] = await (tx || db)(TableName.EstEnrollmentConfig).insert(data).returning("*");
|
||||
|
||||
return estConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Create EST enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const updateById = async (id: string, data: TEstEnrollmentConfigUpdate, tx?: Knex) => {
|
||||
try {
|
||||
const [estConfig] = await (tx || db)(TableName.EstEnrollmentConfig).where({ id }).update(data).returning("*");
|
||||
|
||||
return estConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Update EST enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const deleteById = async (id: string, tx?: Knex) => {
|
||||
try {
|
||||
const [estConfig] = await (tx || db)(TableName.EstEnrollmentConfig).where({ id }).del().returning("*");
|
||||
|
||||
return estConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Delete EST enrollment config" });
|
||||
}
|
||||
};
|
||||
|
||||
const findById = async (id: string, tx?: Knex) => {
|
||||
try {
|
||||
const estConfig = await (tx || db)(TableName.EstEnrollmentConfig).where({ id }).first();
|
||||
|
||||
return estConfig;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find EST enrollment config by id" });
|
||||
}
|
||||
};
|
||||
|
||||
const isConfigInUse = async (configId: string, tx?: Knex) => {
|
||||
try {
|
||||
const profileCount = await (tx || db)(TableName.CertificateProfile)
|
||||
.where({ estConfigId: configId })
|
||||
.count("* as count")
|
||||
.first();
|
||||
|
||||
return parseInt(profileCount || "0", 10) > 0;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Check if EST enrollment config is in use" });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...estEnrollmentConfigOrm,
|
||||
create,
|
||||
updateById,
|
||||
deleteById,
|
||||
findById,
|
||||
isConfigInUse
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user