Compare commits

...

1 Commits

Author SHA1 Message Date
1a93f882f1 improvement: optimize rotation queries 2025-02-26 09:11:29 +09:00
3 changed files with 38 additions and 28 deletions

View File

@ -6,7 +6,7 @@
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import axios from "axios"; import axios from "axios";
import jmespath from "jmespath"; import jmespath from "jmespath";
import knex from "knex"; import knex, { Knex } from "knex";
import { getConfig } from "@app/lib/config/env"; import { getConfig } from "@app/lib/config/env";
import { getDbConnectionHost } from "@app/lib/knex"; import { getDbConnectionHost } from "@app/lib/knex";
@ -80,12 +80,11 @@ export const secretRotationDbFn = async ({
ca, ca,
host, host,
port, port,
query,
database, database,
password, password,
username, username,
client, client,
variables, getQuery,
options options
}: TSecretRotationDbFn) => { }: TSecretRotationDbFn) => {
const appCfg = getConfig(); const appCfg = getConfig();
@ -122,6 +121,9 @@ export const secretRotationDbFn = async ({
options options
} }
}); });
const { query, variables } = await getQuery(db);
const data = await db.raw(query, variables); const data = await db.raw(query, variables);
return data; return data;
}; };
@ -148,24 +150,27 @@ export const secretRotationHttpSetFn = async (func: THttpProviderFunction, varia
}); });
}; };
export const getDbSetQuery = (db: TDbProviderClients, variables: { username: string; password: string }) => { export const getDbSetQuery =
if (db === TDbProviderClients.Pg) { (db: TDbProviderClients, variables: { username: string; password: string }) => async (knx: Knex) => {
const sanitizedPassword = await knx.raw("select ?", [variables.password]);
if (db === TDbProviderClients.Pg) {
return {
query: `ALTER USER ?? WITH PASSWORD '${sanitizedPassword}'`,
variables: [variables.username]
};
}
if (db === TDbProviderClients.MsSqlServer) {
return {
query: `ALTER LOGIN ?? WITH PASSWORD = '${sanitizedPassword}'`,
variables: [variables.username]
};
}
// add more based on client
return { return {
query: `ALTER USER ?? WITH PASSWORD '${variables.password}'`, query: `ALTER USER ?? IDENTIFIED BY '${sanitizedPassword}'`,
variables: [variables.username] variables: [variables.username]
}; };
}
if (db === TDbProviderClients.MsSqlServer) {
return {
query: `ALTER LOGIN ?? WITH PASSWORD = '${variables.password}'`,
variables: [variables.username]
};
}
// add more based on client
return {
query: `ALTER USER ?? IDENTIFIED BY '${variables.password}'`,
variables: [variables.username]
}; };
};

View File

@ -1,3 +1,5 @@
import { Knex } from "knex";
import { TDbProviderClients } from "../templates/types"; import { TDbProviderClients } from "../templates/types";
export type TSecretRotationEncData = { export type TSecretRotationEncData = {
@ -21,8 +23,7 @@ export type TSecretRotationDbFn = {
host: string; host: string;
database: string; database: string;
port: number; port: number;
query: string; getQuery: (db: Knex) => Promise<{ query: string; variables: unknown[] }>;
variables: unknown[];
ca?: string; ca?: string;
options?: Record<string, unknown>; options?: Record<string, unknown>;
}; };

View File

@ -188,13 +188,15 @@ export const secretRotationQueueFactory = ({
options options
} as TSecretRotationDbFn; } as TSecretRotationDbFn;
const getQuery = getDbSetQuery(provider.template.client, {
password: newCredential.internal.rotated_password as string,
username: newCredential.internal.username as string
});
// set function // set function
await secretRotationDbFn({ await secretRotationDbFn({
...dbFunctionArg, ...dbFunctionArg,
...getDbSetQuery(provider.template.client, { getQuery
password: newCredential.internal.rotated_password as string,
username: newCredential.internal.username as string
})
}); });
// test function // test function
@ -203,8 +205,10 @@ export const secretRotationQueueFactory = ({
await secretRotationDbFn({ await secretRotationDbFn({
...dbFunctionArg, ...dbFunctionArg,
query: testQuery, getQuery: async () => ({
variables: [] query: testQuery,
variables: []
})
}); });
newCredential.outputs.db_username = newCredential.internal.username; newCredential.outputs.db_username = newCredential.internal.username;