diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md index 07808df..6788796 100644 --- a/docs/ENVIRONMENT.md +++ b/docs/ENVIRONMENT.md @@ -3,7 +3,7 @@ This document provides detailed information about environment variables for the gateway. | Name | Description | Type | Default | -| --------------------------- | ---------------------------------------------------------------------------------------------------- | -------- | ---------------------------------------------- | +|-----------------------------|------------------------------------------------------------------------------------------------------|----------|------------------------------------------------| | `ALLOWED_ROLES` | Allowed roles when authentication is enabled. | `array` | ["user", "viewer"] | | `ALLOWED_ROLES_NO_AUTH` | Allowed roles when authentication is disabled. | `array` | ["aerie_admin", "user", "viewer"] | | `AUTH_GROUP_ROLE_MAPPINGS` | JSON object that maps auth provider groups to Aerie roles. See [SSO authentication docs][SSO authn] | `JSON` | {} | @@ -21,11 +21,10 @@ This document provides detailed information about environment variables for the | `LOG_FILE` | Either an output filepath to log to, or 'console'. | `string` | console | | `LOG_LEVEL` | Logging level for filtering logs. | `string` | warn | | `PORT` | Port the Gateway server listens on. | `number` | 9000 | -| `POSTGRES_AERIE_MERLIN_DB` | Name of Merlin Postgres database. | `string` | aerie_merlin | -| `POSTGRES_HOST` | Hostname of Postgres instance. | `string` | localhost | -| `POSTGRES_PASSWORD` | Password of Postgres instance. | `string` | | -| `POSTGRES_PORT` | Port of Postgres instance. | `number` | 5432 | -| `POSTGRES_USER` | User of Postgres instance. | `string` | | +| `AERIE_DB_HOST` | Hostname of the Aerie Posgres Database. | `string` | localhost | +| `AERIE_DB_PORT` | Port of the Aerie Posgres Database. | `number` | 5432 | +| `GATEWAY_DB_USER` | Username of the Gateway DB User. | `string` | | +| `GATEWAY_DB_PASSWORD` | Password of the Gateway DB User. | `string` | | | `RATE_LIMITER_FILES_MAX` | Max requests allowed every 15 minutes to file endpoints | `number` | 1000 | | `RATE_LIMITER_LOGIN_MAX` | Max requests allowed every 15 minutes to login endpoints | `number` | 1000 | diff --git a/src/env.ts b/src/env.ts index 1e21ced..048bdae 100644 --- a/src/env.ts +++ b/src/env.ts @@ -19,17 +19,18 @@ export type Env = { LOG_FILE: string; LOG_LEVEL: string; PORT: string; - POSTGRES_AERIE_MERLIN_DB: string; - POSTGRES_HOST: string; - POSTGRES_PASSWORD: string; - POSTGRES_PORT: string; - POSTGRES_USER: string; + AERIE_DB_HOST: string; + AERIE_DB_PORT: string; + GATEWAY_DB_USER: string; + GATEWAY_DB_PASSWORD: string; RATE_LIMITER_FILES_MAX: number; RATE_LIMITER_LOGIN_MAX: number; VERSION: string; }; export const defaultEnv: Env = { + AERIE_DB_HOST: 'localhost', + AERIE_DB_PORT: '5432', ALLOWED_ROLES: ['user', 'viewer'], ALLOWED_ROLES_NO_AUTH: ['aerie_admin', 'user', 'viewer'], AUTH_GROUP_ROLE_MAPPINGS: {}, @@ -39,6 +40,8 @@ export const defaultEnv: Env = { AUTH_URL: 'https://atb-ocio-12b.jpl.nasa.gov:8443/cam-api', DEFAULT_ROLE: ['user'], DEFAULT_ROLE_NO_AUTH: 'aerie_admin', + GATEWAY_DB_PASSWORD: '', + GATEWAY_DB_USER: '', GQL_API_URL: 'http://localhost:8080/v1/graphql', GQL_API_WS_URL: 'ws://localhost:8080/v1/graphql', HASURA_GRAPHQL_JWT_SECRET: '', @@ -47,11 +50,6 @@ export const defaultEnv: Env = { LOG_FILE: 'console', LOG_LEVEL: 'info', PORT: '9000', - POSTGRES_AERIE_MERLIN_DB: 'aerie_merlin', - POSTGRES_HOST: 'localhost', - POSTGRES_PASSWORD: '', - POSTGRES_PORT: '5432', - POSTGRES_USER: '', RATE_LIMITER_FILES_MAX: 1000, RATE_LIMITER_LOGIN_MAX: 1000, VERSION: '2.7.0', @@ -64,8 +62,7 @@ export const defaultEnv: Env = { function parseArray(value: string | undefined, defaultValue: T[]): T[] { if (typeof value === 'string') { try { - const parsedValue = JSON.parse(value); - return parsedValue; + return JSON.parse(value); } catch (e) { console.error(e); return defaultValue; @@ -124,16 +121,17 @@ export function getEnv(): Env { const LOG_FILE = env['LOG_FILE'] ?? defaultEnv.LOG_FILE; const LOG_LEVEL = env['LOG_LEVEL'] ?? defaultEnv.LOG_LEVEL; const PORT = env['PORT'] ?? defaultEnv.PORT; - const POSTGRES_AERIE_MERLIN_DB = env['POSTGRES_AERIE_MERLIN_DB'] ?? defaultEnv.POSTGRES_AERIE_MERLIN_DB; - const POSTGRES_HOST = env['POSTGRES_HOST'] ?? defaultEnv.POSTGRES_HOST; - const POSTGRES_PASSWORD = env['POSTGRES_PASSWORD'] ?? defaultEnv.POSTGRES_PASSWORD; - const POSTGRES_PORT = env['POSTGRES_PORT'] ?? defaultEnv.POSTGRES_PORT; - const POSTGRES_USER = env['POSTGRES_USER'] ?? defaultEnv.POSTGRES_USER; + const AERIE_DB_HOST = env['AERIE_DB_HOST'] ?? defaultEnv.AERIE_DB_HOST; + const AERIE_DB_PORT = env['AERIE_DB_PORT'] ?? defaultEnv.AERIE_DB_PORT; + const GATEWAY_DB_USER = env['GATEWAY_DB_USER'] ?? defaultEnv.GATEWAY_DB_USER; + const GATEWAY_DB_PASSWORD = env['GATEWAY_DB_PASSWORD'] ?? defaultEnv.GATEWAY_DB_PASSWORD; const RATE_LIMITER_FILES_MAX = parseNumber(env['RATE_LIMITER_FILES_MAX'], defaultEnv.RATE_LIMITER_FILES_MAX); const RATE_LIMITER_LOGIN_MAX = parseNumber(env['RATE_LIMITER_LOGIN_MAX'], defaultEnv.RATE_LIMITER_LOGIN_MAX); const VERSION = env['npm_package_version'] ?? defaultEnv.VERSION; return { + AERIE_DB_HOST, + AERIE_DB_PORT, ALLOWED_ROLES, ALLOWED_ROLES_NO_AUTH, AUTH_GROUP_ROLE_MAPPINGS, @@ -143,6 +141,8 @@ export function getEnv(): Env { AUTH_URL, DEFAULT_ROLE, DEFAULT_ROLE_NO_AUTH, + GATEWAY_DB_PASSWORD, + GATEWAY_DB_USER, GQL_API_URL, GQL_API_WS_URL, HASURA_GRAPHQL_JWT_SECRET, @@ -151,11 +151,6 @@ export function getEnv(): Env { LOG_FILE, LOG_LEVEL, PORT, - POSTGRES_AERIE_MERLIN_DB, - POSTGRES_HOST, - POSTGRES_PASSWORD, - POSTGRES_PORT, - POSTGRES_USER, RATE_LIMITER_FILES_MAX, RATE_LIMITER_LOGIN_MAX, VERSION, diff --git a/src/packages/auth/functions.ts b/src/packages/auth/functions.ts index ec38e6e..0f70e2d 100644 --- a/src/packages/auth/functions.ts +++ b/src/packages/auth/functions.ts @@ -44,7 +44,7 @@ export async function getUserRoles( const { rows, rowCount } = await db.query( ` select hasura_default_role, hasura_allowed_roles - from metadata.users_and_roles + from permissions.users_and_roles where username = $1; `, [username], @@ -66,7 +66,7 @@ export async function deleteUserAllowedRoles(username: string) { await db.query( ` - delete from metadata.users_allowed_roles + delete from permissions.users_allowed_roles where username = $1; `, [username], @@ -78,7 +78,7 @@ export async function upsertUserRoles(username: string, default_role: string, al await db.query( ` - insert into metadata.users (username, default_role) + insert into permissions.users (username, default_role) values ($1, $2) on conflict (username) do update set default_role = excluded.default_role; @@ -89,7 +89,7 @@ export async function upsertUserRoles(username: string, default_role: string, al for (const allowed_role of allowed_roles) { await db.query( ` - insert into metadata.users_allowed_roles (username, allowed_role) + insert into permissions.users_allowed_roles (username, allowed_role) values ($1, $2) `, [username, allowed_role], diff --git a/src/packages/db/db.ts b/src/packages/db/db.ts index ee1489a..c264c14 100644 --- a/src/packages/db/db.ts +++ b/src/packages/db/db.ts @@ -5,13 +5,7 @@ import getLogger from '../../logger.js'; const { Pool: DbPool } = pg; -const { - POSTGRES_AERIE_MERLIN_DB, - POSTGRES_HOST: host, - POSTGRES_PASSWORD: password, - POSTGRES_PORT: port, - POSTGRES_USER: user, -} = getEnv(); +const { AERIE_DB_HOST: host, AERIE_DB_PORT: port, GATEWAY_DB_USER: user, GATEWAY_DB_PASSWORD: password } = getEnv(); const logger = getLogger('packages/db/db'); @@ -25,7 +19,7 @@ export class DbMerlin { static async init(): Promise { try { const config: PoolConfig = { - database: POSTGRES_AERIE_MERLIN_DB, + database: 'aerie', host, password, port: parseInt(port, 10), diff --git a/src/packages/files/files.ts b/src/packages/files/files.ts index 85faa13..d418b85 100644 --- a/src/packages/files/files.ts +++ b/src/packages/files/files.ts @@ -69,7 +69,7 @@ export default (app: Express) => { const deleted_date = new Date(); const { rowCount } = await db.query( ` - update uploaded_file + update merlin.uploaded_file set deleted_date = $1 where id = $2; `, @@ -123,7 +123,7 @@ export default (app: Express) => { // twice so the query casts it appropriately to each type. const { rowCount, rows } = await db.query( ` - insert into uploaded_file (name, path) + insert into merlin.uploaded_file (name, path) values ($1, $2) returning id; `,