From ceb8db7dffce99495198f9548e0e19fbf3425141 Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Mon, 24 Jun 2019 16:43:27 -0700 Subject: [PATCH] fix: copy credentials in internal config --- src/index.ts | 7 +++++++ src/trace-writer.ts | 5 +++-- test/test-config.ts | 36 ++++++++++++++++++++++++++++++++++++ test/test-trace-api.ts | 2 +- test/test-trace-writer.ts | 15 +++++++++++---- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 929ba8e54..187606fe2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -153,6 +153,13 @@ function initConfig(userConfig: Forceable): TopLevelConfig { process.env.GAE_MINOR_VERSION ), }, + /** + * Our TypeScript interface suggests that only keyFilename and credentials + * are accepted, but by passing the entire object to the Trace Writer, + * we can allow users to supply other fields that are publicly supported + * by the Google Auth Library. + */ + authOptions: mergedConfig, }, pluginLoaderConfig: { [FORCE_NEW]: forceNew, diff --git a/src/trace-writer.ts b/src/trace-writer.ts index c1ca80162..4162e0878 100644 --- a/src/trace-writer.ts +++ b/src/trace-writer.ts @@ -40,7 +40,8 @@ const SCOPES: string[] = ['https://www.googleapis.com/auth/trace.append']; /* The API endpoint of the Stackdriver Trace service */ const TRACE_API_ENDPOINT = 'cloudtrace.googleapis.com'; -export interface TraceWriterConfig extends common.GoogleAuthOptions { +export interface TraceWriterConfig { + authOptions: common.GoogleAuthOptions; projectId?: string; onUncaughtException: string; bufferSize: number; @@ -124,7 +125,7 @@ export class TraceWriter extends common.Service { baseUrl: `https://${TRACE_API_ENDPOINT}/v1`, scopes: SCOPES, }, - config + config.authOptions ); this.logger = logger; diff --git a/test/test-config.ts b/test/test-config.ts index 5af8efedf..709160483 100644 --- a/test/test-config.ts +++ b/test/test-config.ts @@ -24,6 +24,7 @@ import * as testTraceModule from './trace'; import { TopLevelConfig } from '../src/tracing'; import { StackdriverTracer } from '../src/trace-api'; import {Logger} from '../src/logger'; +import { TraceWriterConfig } from '../src/trace-writer'; describe('Behavior set by config for CLS', () => { const useAH = semver.satisfies(process.version, '>=8'); @@ -164,3 +165,38 @@ describe('Behavior set by config for TracePolicy', () => { }); }); +describe('Behavior set by config for TraceWriter', () => { + let capturedConfig: TraceWriterConfig|null; + + class CaptureConfigTestWriter extends testTraceModule.TestTraceWriter { + constructor(config: TraceWriterConfig, logger: Logger) { + super(config, logger); + // Capture the config object passed into this constructor. + capturedConfig = config; + } + } + + beforeEach(() => { + capturedConfig = null; + }); + + before(() => { + testTraceModule.setTraceWriterForTest(CaptureConfigTestWriter); + }); + + after(() => { + testTraceModule.setTraceWriterForTest(testTraceModule.TestTraceWriter); + }); + + it('should set auth variables passed to TraceWriter as authOptions', () => { + const credentials = { private_key: 'abc' }; + testTraceModule.start({ + keyFilename: 'a', + credentials + }); + assert.ok(capturedConfig); + assert.strictEqual(capturedConfig!.authOptions.keyFilename, 'a'); + assert.deepStrictEqual(capturedConfig!.authOptions.credentials, credentials); + }); +}); + diff --git a/test/test-trace-api.ts b/test/test-trace-api.ts index 12a920f4a..269e8af62 100644 --- a/test/test-trace-api.ts +++ b/test/test-trace-api.ts @@ -68,7 +68,7 @@ describe('Trace Interface', () => { return traceWriter .create( Object.assign( - { [FORCE_NEW]: true, projectId: 'project-1' }, + { [FORCE_NEW]: true, projectId: 'project-1', authOptions: {} }, defaultConfig ), logger diff --git a/test/test-trace-writer.ts b/test/test-trace-writer.ts index 610bf61a5..e4efddf8e 100644 --- a/test/test-trace-writer.ts +++ b/test/test-trace-writer.ts @@ -87,6 +87,7 @@ function createDummyTrace(numSpans: number): Trace { describe('Trace Writer', () => { const pjson = require('../../package.json'); const DEFAULT_CONFIG: TraceWriterConfig = { + authOptions: {}, onUncaughtException: 'ignore', bufferSize: Infinity, flushDelaySeconds: 3600, @@ -175,7 +176,9 @@ describe('Trace Writer', () => { const expectedCredentials: TestCredentials = require('./fixtures/gcloud-credentials.json'); const actualCredentials = await captureCredentialsForConfig({ projectId: 'my-project', - keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), + authOptions: { + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), + }, }); assert.deepStrictEqual(actualCredentials, expectedCredentials); }); @@ -184,7 +187,9 @@ describe('Trace Writer', () => { const expectedCredentials: TestCredentials = require('./fixtures/gcloud-credentials.json'); const actualCredentials = await captureCredentialsForConfig({ projectId: 'my-project', - credentials: expectedCredentials, + authOptions: { + credentials: expectedCredentials, + }, }); assert.deepStrictEqual(actualCredentials, expectedCredentials); }); @@ -198,8 +203,10 @@ describe('Trace Writer', () => { }; const actualCredentials = await captureCredentialsForConfig({ projectId: 'my-project', - keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), - credentials: expectedCredentials, + authOptions: { + keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'), + credentials: expectedCredentials, + }, }); assert.deepStrictEqual(actualCredentials, expectedCredentials); });