mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 07:27:43 +00:00
Fix: Failing to create admin config on first run
This commit is contained in:
@@ -2,6 +2,7 @@ import { Knex } from "knex";
|
|||||||
|
|
||||||
import { TDbClient } from "@app/db";
|
import { TDbClient } from "@app/db";
|
||||||
import { TableName, TSuperAdmin, TSuperAdminUpdate } from "@app/db/schemas";
|
import { TableName, TSuperAdmin, TSuperAdminUpdate } from "@app/db/schemas";
|
||||||
|
import { DatabaseError } from "@app/lib/errors";
|
||||||
import { ormify } from "@app/lib/knex";
|
import { ormify } from "@app/lib/knex";
|
||||||
|
|
||||||
export type TSuperAdminDALFactory = ReturnType<typeof superAdminDALFactory>;
|
export type TSuperAdminDALFactory = ReturnType<typeof superAdminDALFactory>;
|
||||||
@@ -32,7 +33,17 @@ export const superAdminDALFactory = (db: TDbClient) => {
|
|||||||
const updateById = async (id: string, data: TSuperAdminUpdate, tx?: Knex) => {
|
const updateById = async (id: string, data: TSuperAdminUpdate, tx?: Knex) => {
|
||||||
const updatedConfig = await (superAdminOrm || tx).transaction(async (trx: Knex) => {
|
const updatedConfig = await (superAdminOrm || tx).transaction(async (trx: Knex) => {
|
||||||
await superAdminOrm.updateById(id, data, trx);
|
await superAdminOrm.updateById(id, data, trx);
|
||||||
return findById(id, trx);
|
const config = await findById(id, trx);
|
||||||
|
|
||||||
|
if (!config) {
|
||||||
|
throw new DatabaseError({
|
||||||
|
error: "Failed to find updated super admin config",
|
||||||
|
message: "Failed to update super admin config",
|
||||||
|
name: "UpdateById"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
});
|
});
|
||||||
|
|
||||||
return updatedConfig;
|
return updatedConfig;
|
||||||
|
|||||||
@@ -47,9 +47,11 @@ export const superAdminServiceFactory = ({
|
|||||||
if (!config) {
|
if (!config) {
|
||||||
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
|
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
|
||||||
|
|
||||||
if (serverCfg) {
|
if (!serverCfg) {
|
||||||
await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(serverCfg)); // insert it back to keystore
|
throw new BadRequestError({ name: "Admin config", message: "Admin config not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(serverCfg)); // insert it back to keystore
|
||||||
return serverCfg;
|
return serverCfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +69,13 @@ export const superAdminServiceFactory = ({
|
|||||||
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
|
const serverCfg = await serverCfgDAL.findById(ADMIN_CONFIG_DB_UUID);
|
||||||
if (serverCfg) return;
|
if (serverCfg) return;
|
||||||
|
|
||||||
// @ts-expect-error id is kept as fixed for idempotence and to avoid race condition
|
const newCfg = await serverCfgDAL.create({
|
||||||
const newCfg = await serverCfgDAL.create({ initialized: false, allowSignUp: true, id: ADMIN_CONFIG_DB_UUID });
|
// @ts-expect-error id is kept as fixed for idempotence and to avoid race condition
|
||||||
|
id: ADMIN_CONFIG_DB_UUID,
|
||||||
|
initialized: false,
|
||||||
|
allowSignUp: true,
|
||||||
|
defaultAuthOrgId: null
|
||||||
|
});
|
||||||
return newCfg;
|
return newCfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -76,6 +83,7 @@ export const superAdminServiceFactory = ({
|
|||||||
const updatedServerCfg = await serverCfgDAL.updateById(ADMIN_CONFIG_DB_UUID, data);
|
const updatedServerCfg = await serverCfgDAL.updateById(ADMIN_CONFIG_DB_UUID, data);
|
||||||
|
|
||||||
await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(updatedServerCfg));
|
await keyStore.setItemWithExpiry(ADMIN_CONFIG_KEY, ADMIN_CONFIG_KEY_EXP, JSON.stringify(updatedServerCfg));
|
||||||
|
|
||||||
return updatedServerCfg;
|
return updatedServerCfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user