From 67396ec100a506d63c4fae2a0857ae699cf44466 Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Tue, 21 Aug 2018 15:58:38 +0100 Subject: [PATCH] Move to use provided config --- server/config/index.js | 6 +++--- server/security/index.js | 16 ++++++++-------- server/server.js | 21 ++++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 150dfe8..7f940d8 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -28,19 +28,19 @@ if (process.env.PLAYGROUND_VARIABLES_FILE) { } } -const topLevelGraphqlPath = '/graphql' +const graphqlEndpoint = '/graphql' const port = process.env.HTTP_PORT || '8000' const config = { server: { - apiPath: topLevelGraphqlPath, port }, graphQLConfig: { + graphqlEndpoint, tracing: true }, playgroundConfig: { - endpoint: topLevelGraphqlPath, // if you want GraphiQL enabled + endpoint: graphqlEndpoint, // if you want GraphiQL enabled query: playgroundQueryFileContent, variables: playgroundVariableFileContent, subscriptionEndpoint: process.env.PLAYGROUND_SUBS_ENDPOINT || `ws://${hostname()}:${port}/subscriptions` diff --git a/server/security/index.js b/server/security/index.js index 1ea26d6..4b88e25 100644 --- a/server/security/index.js +++ b/server/security/index.js @@ -2,19 +2,19 @@ const { log } = require('../lib/util/logger') const Keycloak = require('keycloak-connect') var session = require('express-session') -const config = require('../config') - /** * Create keycloak middleware if needed. * - * @param {*} expressRouter + * @param {*} keycloakConfig keycloak server specific configuration + * @param {*} expressRouter express router that should be used to attach auth + * @param {string} apiPath location of the protected api */ -exports.applyAuthMiddleware = (expressRouter, apiPath) => { - if (config.keycloakConfig) { +exports.applyAuthMiddleware = (keycloakConfig, expressRouter, apiPath) => { + if (keycloakConfig) { log.info('Initializing Keycloak authentication') const memoryStore = new session.MemoryStore() expressRouter.use(session({ - secret: config.secret || 'secret', + secret: keycloakConfig.secret || 'secret', resave: false, saveUninitialized: true, store: memoryStore @@ -22,13 +22,13 @@ exports.applyAuthMiddleware = (expressRouter, apiPath) => { var keycloak = new Keycloak({ store: memoryStore - }, config.keycloakConfig) + }, keycloakConfig) // Install general keycloak middleware expressRouter.use(keycloak.middleware()) // Protect the main route for all graphql services - // (disable unauthenticated access) + // Disable unauthenticated access expressRouter.use(apiPath, keycloak.protect()) } else { log.info('Keycloak authentication is not configured') diff --git a/server/server.js b/server/server.js index a6f9757..4d73674 100644 --- a/server/server.js +++ b/server/server.js @@ -10,20 +10,19 @@ const {getMetrics, responseLoggingMetric} = require('./metrics') const {applyAuthMiddleware} = require('./security') const schemaParser = require('./lib/schemaParser') const schemaListenerCreator = require('./lib/schemaListeners/schemaListenerCreator') -const { server } = require('./config') -function newExpressApp () { +function newExpressApp (keycloakConfig, graphqlEndpoint) { let app = express() app.use(responseLoggingMetric) app.use('*', cors()) app.use(expressPino) - applyAuthMiddleware(app, server.apiPath) + applyAuthMiddleware(keycloakConfig, app, graphqlEndpoint) return app } -function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) { +function newApolloServer (app, schema, httpServer, tracing, playgroundConfig, graphqlEndpoint) { let apolloServer = new ApolloServer({ schema, @@ -43,21 +42,25 @@ function newApolloServer (app, schema, httpServer, tracing, playgroundConfig) { ] } }) - apolloServer.applyMiddleware({ app, disableHealthCheck: true, path: server.apiPath }) + apolloServer.applyMiddleware({ app, disableHealthCheck: true, path: graphqlEndpoint }) apolloServer.installSubscriptionHandlers(httpServer) return apolloServer } -module.exports = async ({graphQLConfig, playgroundConfig, schemaListenerConfig}, models, pubsub) => { +module.exports = async ({graphQLConfig, + playgroundConfig, + schemaListenerConfig, + keycloakConfig}, models, pubsub) => { const { tracing } = graphQLConfig let { schema, dataSources } = await buildSchema(models, pubsub) await connectDataSources(dataSources) let server = http.createServer() - let app = newExpressApp() - let apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig) + let graphqlEndpoint = graphQLConfig.graphqlEndpoint + let app = newExpressApp(keycloakConfig, graphqlEndpoint) + let apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig, graphqlEndpoint) server.on('request', app) app.get('/healthz', async (req, res) => { @@ -97,7 +100,7 @@ module.exports = async ({graphQLConfig, playgroundConfig, schemaListenerConfig}, server.removeListener('request', app) // reinitialize the server objects schema = newSchema.schema - app = newExpressApp() + app = newExpressApp(keycloakConfig, graphqlEndpoint) apolloServer = newApolloServer(app, schema, server, tracing, playgroundConfig) server.on('request', app)