From ab9f6c50367b0a0834348017c1c4c29109492e3d Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 17 May 2019 17:18:23 -0400 Subject: [PATCH 1/3] feat(apollo-server-core): provide hash of engine key Make hash of Engine API key available to plugins via GraphQLServiceContext.engine.apiKeyHash This will allow plugins to access a service's storage key --- .../apollo-server-core/src/ApolloServer.ts | 26 ++++++++++++------- .../src/requestPipelineAPI.ts | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index e8304035ded..8a27f0b49b6 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -51,6 +51,7 @@ import { } from './playground'; import { generateSchemaHash } from './utils/schemaHash'; +import createSHA from './utils/createSHA'; import { processGraphQLRequest, GraphQLRequestContext, @@ -74,20 +75,18 @@ const NoIntrospection = (context: ValidationContext) => ({ }, }); -function getEngineServiceId(engine: Config['engine']): string | undefined { +function getEngineApiKey(engine: Config['engine']): string | undefined { const keyFromEnv = process.env.ENGINE_API_KEY || ''; - if (!(engine || (engine !== false && keyFromEnv))) { - return; - } - - let engineApiKey: string = ''; - if (typeof engine === 'object' && engine.apiKey) { - engineApiKey = engine.apiKey; + return engine.apiKey; } else if (keyFromEnv) { - engineApiKey = keyFromEnv; + return keyFromEnv; } + return; +} +function getEngineServiceId(engine: Config['engine']): string | undefined { + const engineApiKey = getEngineApiKey(engine); if (engineApiKey) { return engineApiKey.split(':', 2)[1]; } @@ -110,6 +109,7 @@ export class ApolloServerBase { private context?: Context | ContextFunction; private engineReportingAgent?: import('apollo-engine-reporting').EngineReportingAgent; private engineServiceId?: string; + private engineApiKeyHash?: string; private extensions: Array<() => GraphQLExtension>; private schemaHash: string; protected plugins: ApolloServerPlugin[] = []; @@ -335,6 +335,13 @@ export class ApolloServerBase { // related to Engine, as is the case with EngineReportingAgent just below. this.engineServiceId = getEngineServiceId(engine); + const apiKey = getEngineApiKey(engine); + if (apiKey) { + this.engineApiKeyHash = createSHA('sha256') + .update(apiKey) + .digest('hex'); + } + if (this.engineServiceId) { const { EngineReportingAgent } = require('apollo-engine-reporting'); this.engineReportingAgent = new EngineReportingAgent( @@ -400,6 +407,7 @@ export class ApolloServerBase { schemaHash: this.schemaHash, engine: { serviceID: this.engineServiceId, + apiKeyHash: this.engineApiKeyHash, }, persistedQueries: this.requestOptions.persistedQueries, }), diff --git a/packages/apollo-server-core/src/requestPipelineAPI.ts b/packages/apollo-server-core/src/requestPipelineAPI.ts index 359d0007b4d..8eeea9d14b1 100644 --- a/packages/apollo-server-core/src/requestPipelineAPI.ts +++ b/packages/apollo-server-core/src/requestPipelineAPI.ts @@ -24,6 +24,7 @@ export interface GraphQLServiceContext { schemaHash: string; engine: { serviceID?: string; + apiKeyHash?: string; }; persistedQueries?: { cache: KeyValueCache; From a1653e6ac32cc8e1bc187d5535a96e2bb8642573 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 17 May 2019 17:26:26 -0400 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5396fb4875f..9147e5ac8f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # vNEXT - extensions: add setParsed to ensure we get an operation signature and capture errors in extensions when they occur in unexpected locations [PR#2659](https://github.com/apollographql/apollo-server/pull/2659) +- core: make hash of Engine API key available to plugins via `GraphQLServiceContext.engine.apiKeyHash` [PR#2685](https://github.com/apollographql/apollo-server/pull/2685) ### v2.5.0 From d577a242b50bcc92da73bea15880425be155334c Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Wed, 22 May 2019 18:25:57 +0300 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9147e5ac8f1..d459dc7ea45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ # vNEXT -- extensions: add setParsed to ensure we get an operation signature and capture errors in extensions when they occur in unexpected locations [PR#2659](https://github.com/apollographql/apollo-server/pull/2659) -- core: make hash of Engine API key available to plugins via `GraphQLServiceContext.engine.apiKeyHash` [PR#2685](https://github.com/apollographql/apollo-server/pull/2685) +- extensions: Add setParsed to ensure we get an operation signature and capture errors in extensions when they occur in unexpected locations [PR#2659](https://github.com/apollographql/apollo-server/pull/2659) +- core: Expose SHA-256 hex hash digest of the Engine API key to plugins, when available, as `engine.apiKeyHash`. [PR# 2685](https://github.com/apollographql/apollo-server/pull/2685) ### v2.5.0