Skip to content

Commit

Permalink
fix: copy credentials in internal config
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Jun 24, 2019
1 parent 178c2a9 commit ceb8db7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ function initConfig(userConfig: Forceable<Config>): 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,
Expand Down
5 changes: 3 additions & 2 deletions src/trace-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,7 +125,7 @@ export class TraceWriter extends common.Service {
baseUrl: `https://${TRACE_API_ENDPOINT}/v1`,
scopes: SCOPES,
},
config
config.authOptions
);

this.logger = logger;
Expand Down
36 changes: 36 additions & 0 deletions test/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
});
});

2 changes: 1 addition & 1 deletion test/test-trace-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions test/test-trace-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit ceb8db7

Please sign in to comment.