From 6e87e48607a15b8cc4377ffefeba2e3ae9ec611c Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 25 Sep 2018 12:54:13 -0400 Subject: [PATCH] grpc-js-core: delay composing credentials The channel and stream credentials may change between the time a CallCredentialsFilterFactory is created and the time that the metadata is sent. This commit delays composing the credentials until the time metadata is sent. --- .../src/call-credentials-filter.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/grpc-js-core/src/call-credentials-filter.ts b/packages/grpc-js-core/src/call-credentials-filter.ts index 95aee592..b9f1d6bf 100644 --- a/packages/grpc-js-core/src/call-credentials-filter.ts +++ b/packages/grpc-js-core/src/call-credentials-filter.ts @@ -7,10 +7,11 @@ import {Metadata} from './metadata'; export class CallCredentialsFilter extends BaseFilter implements Filter { private serviceUrl: string; constructor( - private readonly credentials: CallCredentials, - private readonly host: string, private readonly path: string) { + private readonly channel: Http2Channel, private readonly stream: Call) { super(); - const splitPath: string[] = path.split('/'); + this.channel = channel; + this.stream = stream; + const splitPath: string[] = stream.getMethod().split('/'); let serviceName = ''; /* The standard path format is "/{serviceName}/{methodName}", so if we split * by '/', the first item should be empty and the second should be the @@ -20,12 +21,15 @@ export class CallCredentialsFilter extends BaseFilter implements Filter { } /* Currently, call credentials are only allowed on HTTPS connections, so we * can assume that the scheme is "https" */ - this.serviceUrl = `https://${host}/${serviceName}`; + this.serviceUrl = `https://${stream.getHost()}/${serviceName}`; } async sendMetadata(metadata: Promise): Promise { + const channelCredentials = this.channel.credentials._getCallCredentials(); + const streamCredentials = this.stream.getCredentials(); + const credentials = channelCredentials.compose(streamCredentials); const credsMetadata = - this.credentials.generateMetadata({service_url: this.serviceUrl}); + credentials.generateMetadata({service_url: this.serviceUrl}); const resultMetadata = await metadata; resultMetadata.merge(await credsMetadata); return resultMetadata; @@ -34,14 +38,12 @@ export class CallCredentialsFilter extends BaseFilter implements Filter { export class CallCredentialsFilterFactory implements FilterFactory { - private readonly credentials: CallCredentials; + private readonly channel: Http2Channel; constructor(channel: Http2Channel) { - this.credentials = channel.credentials._getCallCredentials(); + this.channel = channel; } createFilter(callStream: Call): CallCredentialsFilter { - return new CallCredentialsFilter( - this.credentials.compose(callStream.getCredentials()), - callStream.getHost(), callStream.getMethod()); + return new CallCredentialsFilter(this.channel, callStream); } }