Skip to content

Commit

Permalink
Merge DB Gateway Changes (#72)
Browse files Browse the repository at this point in the history
* Add Schema Qualifier to SQL statements

- Write to Permissions schema instead of Metadata

* Update `POSTGRES_AERIE_MERLIN_DB` envvars (BREAKING)

- Rename envvars to reflect DB Merge Changes (Breaking)
- Point at `aerie` instead of `aerie_merlin`
- Remove unneeded variable in `parseArray`
  • Loading branch information
Mythicaeda authored Apr 15, 2024
1 parent 196ec9d commit a03842d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
11 changes: 5 additions & 6 deletions docs/ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | {} |
Expand All @@ -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 |

Expand Down
39 changes: 17 additions & 22 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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: '',
Expand All @@ -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',
Expand All @@ -64,8 +62,7 @@ export const defaultEnv: Env = {
function parseArray<T = string>(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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/packages/auth/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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;
Expand All @@ -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],
Expand Down
10 changes: 2 additions & 8 deletions src/packages/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -25,7 +19,7 @@ export class DbMerlin {
static async init(): Promise<void> {
try {
const config: PoolConfig = {
database: POSTGRES_AERIE_MERLIN_DB,
database: 'aerie',
host,
password,
port: parseInt(port, 10),
Expand Down
4 changes: 2 additions & 2 deletions src/packages/files/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`,
Expand Down Expand Up @@ -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;
`,
Expand Down

0 comments on commit a03842d

Please sign in to comment.