Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide hash of engine API key to plugins via GraphQLServiceContext #2685

Merged
merged 4 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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)
- 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

Expand Down
26 changes: 17 additions & 9 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from './playground';

import { generateSchemaHash } from './utils/schemaHash';
import createSHA from './utils/createSHA';
import {
processGraphQLRequest,
GraphQLRequestContext,
Expand All @@ -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];
}
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -400,6 +407,7 @@ export class ApolloServerBase {
schemaHash: this.schemaHash,
engine: {
serviceID: this.engineServiceId,
apiKeyHash: this.engineApiKeyHash,
},
persistedQueries: this.requestOptions.persistedQueries,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/requestPipelineAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface GraphQLServiceContext {
schemaHash: string;
engine: {
serviceID?: string;
apiKeyHash?: string;
};
persistedQueries?: {
cache: KeyValueCache;
Expand Down