From 574872ba79d1b1aa4942665d0d961d596934dd6d Mon Sep 17 00:00:00 2001 From: Mayur Deshmukh Date: Wed, 24 May 2023 14:37:07 +0530 Subject: [PATCH] fix: load config.yaml at runtime Changed the import strategy for the router config options to dynamic i.e. at runtime, instead of importing at build time --- .env.example | 5 ++- config/routes.yaml => config.yaml | 0 package.json | 5 ++- pnpm-lock.yaml | 60 ++++--------------------------- rollup.config.ts | 3 +- src/@types/config.d.ts | 17 ++++----- src/resolvers/apikeys.ts | 2 +- src/resolvers/proxy.ts | 2 -- src/server.ts | 9 +++-- src/setup/env.ts | 3 +- 10 files changed, 30 insertions(+), 76 deletions(-) rename config/routes.yaml => config.yaml (100%) diff --git a/.env.example b/.env.example index 7120e3d..9914343 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,10 @@ ## MongoDB connection MONGODB_URI= +## Proxy Route configurations +# ROUTE_CONFIG_PATH=config.yaml + ## API Token options -# API_TOKEN_PREFIX=apik +# API_TOKEN_PREFIX=apik # `_` is automatically appended to the prefix # ENABLE_API_KEY_AUTH=false diff --git a/config/routes.yaml b/config.yaml similarity index 100% rename from config/routes.yaml rename to config.yaml diff --git a/package.json b/package.json index 48fa4d6..891f0f2 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "license": "Apache-2.0", "devDependencies": { "@rollup/plugin-typescript": "^11.1.0", - "@rollup/plugin-yaml": "^4.0.1", "@types/body-parser": "^1.19.2", "@types/cookie-parser": "^1.4.3", "@types/cors": "^2.8.13", @@ -52,8 +51,8 @@ "mongodb": "^5.4.0", "mongoose": "^7.1.0", "nanoid": "^4.0.2", - "passport": "^0.6.0", "pino": "^8.14.1", - "pino-http": "^8.3.3" + "pino-http": "^8.3.3", + "yaml": "^2.3.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e5b205a..95f225b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,7 +2,6 @@ lockfileVersion: 5.3 specifiers: '@rollup/plugin-typescript': ^11.1.0 - '@rollup/plugin-yaml': ^4.0.1 '@types/body-parser': ^1.19.2 '@types/cookie-parser': ^1.4.3 '@types/cors': ^2.8.13 @@ -24,13 +23,13 @@ specifiers: mongoose: ^7.1.0 nanoid: ^4.0.2 nodemon: ^2.0.22 - passport: ^0.6.0 pino: ^8.14.1 pino-http: ^8.3.3 rimraf: ^5.0.0 rollup: ^3.21.5 supertest: ^6.3.3 typescript: ^5.0.4 + yaml: ^2.3.0 dependencies: base62-token: 1.1.1 @@ -43,13 +42,12 @@ dependencies: mongodb: 5.4.0 mongoose: 7.1.1 nanoid: 4.0.2 - passport: 0.6.0 pino: 8.14.1 pino-http: 8.3.3 + yaml: 2.3.0 devDependencies: '@rollup/plugin-typescript': 11.1.0_rollup@3.21.6+typescript@5.0.4 - '@rollup/plugin-yaml': 4.0.1_rollup@3.21.6 '@types/body-parser': 1.19.2 '@types/cookie-parser': 1.4.3 '@types/cors': 2.8.13 @@ -714,21 +712,6 @@ packages: typescript: 5.0.4 dev: true - /@rollup/plugin-yaml/4.0.1_rollup@3.21.6: - resolution: {integrity: sha512-eyftkLWrwaGhgad+gXmisPYXeW3hP1s+lz63mgbur+F/8aKZhPG1Bf8RFNnz0Vhnf3uBimFebZBDwwz6X4KqUQ==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^1.20.0||^2.0.0||^3.0.0 - peerDependenciesMeta: - rollup: - optional: true - dependencies: - '@rollup/pluginutils': 5.0.2_rollup@3.21.6 - js-yaml: 4.1.0 - rollup: 3.21.6 - tosource: 2.0.0-alpha.3 - dev: true - /@rollup/pluginutils/5.0.2_rollup@3.21.6: resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==} engines: {node: '>=14.0.0'} @@ -1019,10 +1002,6 @@ packages: sprintf-js: 1.0.3 dev: true - /argparse/2.0.1: - resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==} - dev: true - /array-flatten/1.1.1: resolution: {integrity: sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==} dev: false @@ -2492,13 +2471,6 @@ packages: esprima: 4.0.1 dev: true - /js-yaml/4.1.0: - resolution: {integrity: sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==} - hasBin: true - dependencies: - argparse: 2.0.1 - dev: true - /jsesc/2.5.2: resolution: {integrity: sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==} engines: {node: '>=4'} @@ -2875,20 +2847,6 @@ packages: engines: {node: '>= 0.8'} dev: false - /passport-strategy/1.0.0: - resolution: {integrity: sha512-CB97UUvDKJde2V0KDWWB3lyf6PC3FaZP7YxZ2G8OAtn9p4HI9j9JLP9qjOGZFvyl8uwNT8qM+hGnz/n16NI7oA==} - engines: {node: '>= 0.4.0'} - dev: false - - /passport/0.6.0: - resolution: {integrity: sha512-0fe+p3ZnrWRW74fe8+SvCyf4a3Pb2/h7gFkQ8yTJpAO50gDzlfjZUZTO1k5Eg9kUct22OxHLqDZoKUWRHOh9ug==} - engines: {node: '>= 0.4.0'} - dependencies: - passport-strategy: 1.0.0 - pause: 0.0.1 - utils-merge: 1.0.1 - dev: false - /path-exists/4.0.0: resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==} engines: {node: '>=8'} @@ -2920,10 +2878,6 @@ packages: resolution: {integrity: sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==} dev: false - /pause/0.0.1: - resolution: {integrity: sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg==} - dev: false - /picocolors/1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} dev: true @@ -3494,11 +3448,6 @@ packages: engines: {node: '>=0.6'} dev: false - /tosource/2.0.0-alpha.3: - resolution: {integrity: sha512-KAB2lrSS48y91MzFPFuDg4hLbvDiyTjOVgaK7Erw+5AmZXNq4sFRVn8r6yxSLuNs15PaokrDRpS61ERY9uZOug==} - engines: {node: '>=10'} - dev: true - /touch/3.1.0: resolution: {integrity: sha512-WBx8Uy5TLtOSRtIq+M03/sKDrXCLHxwDcquSP2c43Le03/9serjQBIztjRz6FkJez9D/hleyAXTBGLwwZUw9lA==} hasBin: true @@ -3655,6 +3604,11 @@ packages: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} dev: true + /yaml/2.3.0: + resolution: {integrity: sha512-8/1wgzdKc7bc9E6my5wZjmdavHLvO/QOmLG1FBugblEvY4IXrLjlViIOmL24HthU042lWTDRO90Fz1Yp66UnMw==} + engines: {node: '>= 14', npm: '>= 7'} + dev: false + /yargs-parser/21.1.1: resolution: {integrity: sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==} engines: {node: '>=12'} diff --git a/rollup.config.ts b/rollup.config.ts index af70fad..d734db6 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -1,10 +1,9 @@ import typescript from "@rollup/plugin-typescript"; -import yaml from "@rollup/plugin-yaml"; export default { input: "./src/index.ts", output: { dir: "./dist", }, - plugins: [typescript(), yaml()], + plugins: [typescript()], }; diff --git a/src/@types/config.d.ts b/src/@types/config.d.ts index f7a3781..cf1b326 100644 --- a/src/@types/config.d.ts +++ b/src/@types/config.d.ts @@ -1,14 +1,9 @@ /// -declare module '*/config/routes.yaml' { - const config: YamlConfig; - export default config; +declare type YamlConfig = { + routes: Routes; +}; - type Routes = { - [path: string]: Options; - }; - - export type YamlConfig = { - routes: Routes; - }; -} +type Routes = { + [path: string]: Options; +}; diff --git a/src/resolvers/apikeys.ts b/src/resolvers/apikeys.ts index 8d5ed87..affcde7 100644 --- a/src/resolvers/apikeys.ts +++ b/src/resolvers/apikeys.ts @@ -19,7 +19,7 @@ const getKey = async (req: Request, res: Response) => { const createKey = async (req: Request, res: Response) => { /* Generate a new key */ - const key = generateToken(API_TOKEN_PREFIX); + const key = generateToken(API_TOKEN_PREFIX + '_'); const hashKey = generateHash(key); const body = req.body; diff --git a/src/resolvers/proxy.ts b/src/resolvers/proxy.ts index 054f0cc..1f3765a 100644 --- a/src/resolvers/proxy.ts +++ b/src/resolvers/proxy.ts @@ -5,8 +5,6 @@ export default function getProxyResolver(options: Options) { const resolver = (req: Request, res: Response, next: NextFunction) => { const proxy = createProxyMiddleware({ ...options, - logLevel: 'debug', - secure: false, headers: { ...options.headers, 'x-forwarded-user': res.locals.userId, diff --git a/src/server.ts b/src/server.ts index 5b24755..2321ad1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,11 +3,13 @@ import cors from 'cors'; import bodyParser from 'body-parser'; import cookieParser from 'cookie-parser'; import apiKeyRouter from './resolvers/apikeys'; -import { ENABLE_API_KEY_AUTH } from './setup/env'; +import { ENABLE_API_KEY_AUTH, ROUTE_CONFIG_PATH } from './setup/env'; import { pinoMiddleware } from './setup/logger'; import { auth } from './middlewares/auth'; -import config from '../config/routes.yaml'; import getProxyResolver from './resolvers/proxy'; +import { readFileSync } from 'fs'; +import { resolve } from 'path'; +import { parse } from 'yaml'; const server = express(); @@ -28,6 +30,9 @@ if (ENABLE_API_KEY_AUTH) { } /* Creating proxy handlers for all the routes defined in config.routes */ +const config: YamlConfig = parse( + readFileSync(resolve(ROUTE_CONFIG_PATH)).toString() +); Object.entries(config.routes).forEach((route) => { const [path, options] = route; server.use(path, [auth], getProxyResolver(options)); diff --git a/src/setup/env.ts b/src/setup/env.ts index b82d677..6c212ba 100644 --- a/src/setup/env.ts +++ b/src/setup/env.ts @@ -6,7 +6,8 @@ export const { NODE_ENV = 'development', PORT = '8080', MONGODB_URI = 'mongodb://localhost:27017/auth-proxy', - API_TOKEN_PREFIX = 'apik' + API_TOKEN_PREFIX = 'apik', + ROUTE_CONFIG_PATH = 'config.yaml', } = process.env; export const ENABLE_API_KEY_AUTH = process.env.ENABLE_API_KEY_AUTH === 'true';