From 9bee7c7092dc643e184ffe4eb8d7736f65acda3a Mon Sep 17 00:00:00 2001 From: snaerseljan Date: Mon, 23 Sep 2024 20:58:42 +0000 Subject: [PATCH] Update config and redis dev setup --- apps/services/bff/infra/admin-portal.infra.ts | 1 + apps/services/bff/src/app/bff.config.ts | 47 ++++++++++++------- .../bff/src/app/modules/auth/auth.service.ts | 2 +- .../bff/src/app/modules/cache/cache.module.ts | 16 ++----- .../bff/src/environment/environment.ts | 2 +- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/apps/services/bff/infra/admin-portal.infra.ts b/apps/services/bff/infra/admin-portal.infra.ts index cbc85c7a49c8..b5b1e5fa16a6 100644 --- a/apps/services/bff/infra/admin-portal.infra.ts +++ b/apps/services/bff/infra/admin-portal.infra.ts @@ -50,6 +50,7 @@ export const serviceSetup = (): ServiceBuilder<'services-bff-admin-portal'> => BFF_CLIENT_KEY_PATH: '/stjornbord', BFF_CALLBACKS_BASE_PATH: generateWebBaseUrls('/stjornbord/bff/callbacks'), BFF_CLIENT_BASE_URL: generateWebBaseUrls(), + BFF_LOGOUT_REDIRECT_URI: generateWebBaseUrls(), BFF_PROXY_API_ENDPOINT: generateWebBaseUrls('/api/graphql'), BFF_ALLOWED_EXTERNAL_API_URLS: { dev: json(['https://api.dev01.devland.is']), diff --git a/apps/services/bff/src/app/bff.config.ts b/apps/services/bff/src/app/bff.config.ts index a4c76123d530..15ec16f0694c 100644 --- a/apps/services/bff/src/app/bff.config.ts +++ b/apps/services/bff/src/app/bff.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from '@island.is/nest/config' import { z } from 'zod' -import { environment } from '../environment' +import { isProduction } from '../environment' import { removeTrailingSlash } from '../utils/removeTrailingSlash' export const idsSchema = z.strictObject({ @@ -12,11 +12,19 @@ export const idsSchema = z.strictObject({ }) const BffConfigSchema = z.object({ - redis: z.object({ - nodes: z.array(z.string()), - ssl: z.boolean(), - }), + redis: z + .object({ + name: z.string(), + nodes: z.array(z.string()), + ssl: z.boolean(), + }) + // Only required in production + .optional(), graphqlApiEndpont: z.string(), + /** + * The URL to redirect to after logging out + */ + logoutRedirectUri: z.string(), /** * Bff client base URL */ @@ -48,25 +56,32 @@ export const BffConfig = defineConfig({ const callbacksBaseRedirectPath = removeTrailingSlash( env.required('BFF_CALLBACKS_BASE_PATH'), ) + // Redis nodes are only required in production + // In development, we can use a local Redis server or + // rely on the default in-memory cache provided by CacheModule + const redisNodes = env.optionalJSON('BFF_REDIS_URL_NODES') return { parSupportEnabled: env.optionalJSON('BFF_PAR_SUPPORT_ENABLED') ?? false, clientBaseUrl: env.required('BFF_CLIENT_BASE_URL'), + logoutRedirectUri: env.required('BFF_LOGOUT_REDIRECT_URI'), /** * Our main GraphQL API endpoint */ graphqlApiEndpont: env.required('BFF_PROXY_API_ENDPOINT'), - redis: { - nodes: env.requiredJSON('REDIS_URL_NODE_01', [ - 'localhost:7000', - 'localhost:7001', - 'localhost:7002', - 'localhost:7003', - 'localhost:7004', - 'localhost:7005', - ]), - ssl: environment.production, - }, + redis: isProduction + ? { + name: env.required('BFF_REDIS_NAME'), + nodes: env.requiredJSON('BFF_REDIS_URL_NODES'), + ssl: true, + } + : redisNodes + ? { + name: env.optional('BFF_REDIS_NAME') ?? 'unnamed-bff', + nodes: redisNodes, + ssl: false, + } + : undefined, ids: { issuer: env.required('IDENTITY_SERVER_ISSUER_URL'), clientId: env.required('IDENTITY_SERVER_CLIENT_ID'), diff --git a/apps/services/bff/src/app/modules/auth/auth.service.ts b/apps/services/bff/src/app/modules/auth/auth.service.ts index 38f0398b177b..7f5df4421d10 100644 --- a/apps/services/bff/src/app/modules/auth/auth.service.ts +++ b/apps/services/bff/src/app/modules/auth/auth.service.ts @@ -244,6 +244,6 @@ export class AuthService { // Delete session cookie res.clearCookie('sid') - return res.redirect(this.config.clientBaseUrl) + return res.redirect(this.config.logoutRedirectUri) } } diff --git a/apps/services/bff/src/app/modules/cache/cache.module.ts b/apps/services/bff/src/app/modules/cache/cache.module.ts index ccd96e595a42..dc3fe66967b9 100644 --- a/apps/services/bff/src/app/modules/cache/cache.module.ts +++ b/apps/services/bff/src/app/modules/cache/cache.module.ts @@ -11,20 +11,14 @@ import { CacheService } from './cache.service' export class CacheModule { static register(): DynamicModule { const imports = - process.env.NODE_ENV === 'test' || process.env.INIT_SCHEMA === 'true' + process.env.NODE_ENV === 'test' ? [NestCacheModule.register()] : [ NestCacheModule.registerAsync({ - useFactory: ({ - redis: { ssl, nodes }, - }: ConfigType) => ({ - store: redisInsStore( - createRedisCluster({ - name: 'bff', - ssl, - nodes, - }), - ), + useFactory: ({ redis }: ConfigType) => ({ + store: redis + ? redisInsStore(createRedisCluster(redis)) + : undefined, }), inject: [BffConfig.KEY], }), diff --git a/apps/services/bff/src/environment/environment.ts b/apps/services/bff/src/environment/environment.ts index 5505ee19d36d..8c44d8eb6ca2 100644 --- a/apps/services/bff/src/environment/environment.ts +++ b/apps/services/bff/src/environment/environment.ts @@ -1,6 +1,6 @@ import { BffEnvironment, environmentSchema } from './environment.schema' -const isProduction = process.env.NODE_ENV === 'production' +export const isProduction = process.env.NODE_ENV === 'production' const parsedEnvironment = environmentSchema.parse({ production: isProduction,