mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-24 17:51:14 +02:00
chore: AWS IAM DB auth migrator, logs
This commit is contained in:
parent
4a00792f1e
commit
7cec27a75a
31
src/lib/db/aws-iam.ts
Normal file
31
src/lib/db/aws-iam.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Signer } from '@aws-sdk/rds-signer';
|
||||
import type { IDBOption } from '../types/option.js';
|
||||
|
||||
type PasswordResolver = () => Promise<string>;
|
||||
|
||||
export const getDBPasswordResolver = (db: IDBOption): PasswordResolver => {
|
||||
if (db.awsIamAuth) {
|
||||
if (!db.awsRegion)
|
||||
throw new Error(
|
||||
'AWS_REGION is required when DATABASE_AWS_IAM=true',
|
||||
);
|
||||
|
||||
const signer = new Signer({
|
||||
region: db.awsRegion,
|
||||
hostname: db.host,
|
||||
port: db.port,
|
||||
username: db.user,
|
||||
});
|
||||
return async () => {
|
||||
console.log('[AWS RDS SIGNER] Getting token...');
|
||||
const token = await signer.getAuthToken();
|
||||
console.log(`[AWS RDS SIGNER] Got token: ${token}`);
|
||||
return token;
|
||||
};
|
||||
}
|
||||
|
||||
return async () => db.password;
|
||||
};
|
||||
|
||||
export const getDBPassword = (db: IDBOption): Promise<string> =>
|
||||
getDBPasswordResolver(db)();
|
@ -2,8 +2,7 @@ import type { Knex } from 'knex';
|
||||
import knexpkg from 'knex';
|
||||
const { knex } = knexpkg;
|
||||
import type { IUnleashConfig } from '../types/option.js';
|
||||
|
||||
import { Signer } from '@aws-sdk/rds-signer';
|
||||
import { getDBPasswordResolver } from './aws-iam.js';
|
||||
|
||||
export function createDb({
|
||||
db,
|
||||
@ -11,53 +10,19 @@ export function createDb({
|
||||
}: Pick<IUnleashConfig, 'db' | 'getLogger'>): Knex {
|
||||
const logger = getLogger('db-pool.js');
|
||||
|
||||
const {
|
||||
host,
|
||||
port,
|
||||
user,
|
||||
database,
|
||||
ssl,
|
||||
applicationName,
|
||||
password,
|
||||
awsIamAuth,
|
||||
awsRegion,
|
||||
pool,
|
||||
} = db;
|
||||
|
||||
let resolvedPassword: string | (() => Promise<string>) | undefined =
|
||||
password;
|
||||
|
||||
if (awsIamAuth) {
|
||||
if (!awsRegion) {
|
||||
throw new Error(
|
||||
'AWS_REGION is required when DATABASE_AWS_IAM=true',
|
||||
);
|
||||
}
|
||||
const signer = new Signer({
|
||||
region: awsRegion,
|
||||
hostname: host,
|
||||
port,
|
||||
username: user,
|
||||
});
|
||||
|
||||
resolvedPassword = async () => signer.getAuthToken();
|
||||
}
|
||||
|
||||
const connection = {
|
||||
host,
|
||||
port,
|
||||
user,
|
||||
database,
|
||||
ssl,
|
||||
application_name: applicationName,
|
||||
password: resolvedPassword,
|
||||
};
|
||||
logger.info(
|
||||
`createDb: iam=${Boolean(db.awsIamAuth)} host=${db.host} port=${db.port} db=${db.database} user=${db.user} ssl=${Boolean(db.ssl)}`,
|
||||
);
|
||||
|
||||
return knex({
|
||||
client: 'pg',
|
||||
version: db.version,
|
||||
connection,
|
||||
pool,
|
||||
connection: {
|
||||
...db,
|
||||
application_name: db.applicationName,
|
||||
password: getDBPasswordResolver(db),
|
||||
},
|
||||
pool: db.pool,
|
||||
searchPath: db.schema,
|
||||
asyncStackTraces: true,
|
||||
log: {
|
||||
|
@ -20,7 +20,7 @@ export interface ISSLOption {
|
||||
|
||||
export interface IDBOption {
|
||||
user: string;
|
||||
password?: string;
|
||||
password: string;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
|
@ -6,6 +6,7 @@ import type { IUnleashConfig } from './lib/types/option.js';
|
||||
import { secondsToMilliseconds } from 'date-fns';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { getDBPassword } from './lib/db/aws-iam.js';
|
||||
|
||||
log.setLogLevel('error');
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
@ -25,6 +26,7 @@ export async function migrateDb(
|
||||
return noDatabaseUrl(async () => {
|
||||
const custom = {
|
||||
...db,
|
||||
password: await getDBPassword(db),
|
||||
connectionTimeoutMillis: secondsToMilliseconds(10),
|
||||
};
|
||||
|
||||
@ -46,6 +48,7 @@ export async function requiresMigration({
|
||||
return noDatabaseUrl(async () => {
|
||||
const custom = {
|
||||
...db,
|
||||
password: await getDBPassword(db),
|
||||
connectionTimeoutMillis: secondsToMilliseconds(10),
|
||||
};
|
||||
|
||||
@ -67,6 +70,7 @@ export async function resetDb({ db }: IUnleashConfig): Promise<void> {
|
||||
return noDatabaseUrl(async () => {
|
||||
const custom = {
|
||||
...db,
|
||||
password: await getDBPassword(db),
|
||||
connectionTimeoutMillis: secondsToMilliseconds(10),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user