Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 8930df3

Browse files
authored
fix: copy credentials in internal config (#1052)
1 parent 178c2a9 commit 8930df3

8 files changed

Lines changed: 74 additions & 21 deletions

File tree

src/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ function initConfig(userConfig: Forceable<Config>): TopLevelConfig {
125125
},
126126
writerConfig: {
127127
[FORCE_NEW]: forceNew,
128-
projectId: lastOf<string | undefined>(
129-
mergedConfig.projectId,
130-
process.env.GCLOUD_PROJECT
131-
),
132128
onUncaughtException: mergedConfig.onUncaughtException,
133129
bufferSize: mergedConfig.bufferSize,
134130
flushDelaySeconds: mergedConfig.flushDelaySeconds,
@@ -153,6 +149,18 @@ function initConfig(userConfig: Forceable<Config>): TopLevelConfig {
153149
process.env.GAE_MINOR_VERSION
154150
),
155151
},
152+
/**
153+
* Our TypeScript interface suggests that only credentials, keyFilename,
154+
* and projectId are accepted, but by passing the entire object to the
155+
* Trace Writer, we can allow users to supply other fields that are
156+
* publicly supported by the Google Auth Library.
157+
*/
158+
authOptions: Object.assign({}, mergedConfig, {
159+
projectId: lastOf<string | undefined>(
160+
mergedConfig.projectId,
161+
process.env.GCLOUD_PROJECT
162+
),
163+
}),
156164
},
157165
pluginLoaderConfig: {
158166
[FORCE_NEW]: forceNew,

src/trace-writer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const SCOPES: string[] = ['https://www.googleapis.com/auth/trace.append'];
4040
/* The API endpoint of the Stackdriver Trace service */
4141
const TRACE_API_ENDPOINT = 'cloudtrace.googleapis.com';
4242

43-
export interface TraceWriterConfig extends common.GoogleAuthOptions {
44-
projectId?: string;
43+
export interface TraceWriterConfig {
44+
authOptions: common.GoogleAuthOptions;
4545
onUncaughtException: string;
4646
bufferSize: number;
4747
flushDelaySeconds: number;
@@ -124,7 +124,7 @@ export class TraceWriter extends common.Service {
124124
baseUrl: `https://${TRACE_API_ENDPOINT}/v1`,
125125
scopes: SCOPES,
126126
},
127-
config
127+
config.authOptions
128128
);
129129

130130
this.logger = logger;

src/tracing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ export class Tracing implements Component {
154154
.activate();
155155

156156
if (
157-
typeof this.config.writerConfig.projectId !== 'string' &&
158-
typeof this.config.writerConfig.projectId !== 'undefined'
157+
typeof this.config.writerConfig.authOptions.projectId !== 'string' &&
158+
typeof this.config.writerConfig.authOptions.projectId !== 'undefined'
159159
) {
160160
this.logger.error(
161161
'StackdriverTracer#start: config.projectId, if provided, must be a string.',

test/test-config-priority.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ describe('should respect config load order', () => {
105105
it('should respect GCLOUD_PROJECT', () => {
106106
traceTestModule.start();
107107
const config = getCapturedConfig();
108-
assert.strictEqual(config.writerConfig.projectId, '1729');
108+
assert.strictEqual(config.writerConfig.authOptions.projectId, '1729');
109109
});
110110

111111
it('should prefer env to config', () => {
112112
traceTestModule.start({ projectId: '1927' });
113113
const config = getCapturedConfig();
114-
assert.strictEqual(config.writerConfig.projectId, '1729');
114+
assert.strictEqual(config.writerConfig.authOptions.projectId, '1729');
115115
});
116116
});
117117
});

test/test-config.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as testTraceModule from './trace';
2424
import { TopLevelConfig } from '../src/tracing';
2525
import { StackdriverTracer } from '../src/trace-api';
2626
import {Logger} from '../src/logger';
27+
import { TraceWriterConfig } from '../src/trace-writer';
2728

2829
describe('Behavior set by config for CLS', () => {
2930
const useAH = semver.satisfies(process.version, '>=8');
@@ -164,3 +165,38 @@ describe('Behavior set by config for TracePolicy', () => {
164165
});
165166
});
166167

168+
describe('Behavior set by config for TraceWriter', () => {
169+
let capturedConfig: TraceWriterConfig|null;
170+
171+
class CaptureConfigTestWriter extends testTraceModule.TestTraceWriter {
172+
constructor(config: TraceWriterConfig, logger: Logger) {
173+
super(config, logger);
174+
// Capture the config object passed into this constructor.
175+
capturedConfig = config;
176+
}
177+
}
178+
179+
beforeEach(() => {
180+
capturedConfig = null;
181+
});
182+
183+
before(() => {
184+
testTraceModule.setTraceWriterForTest(CaptureConfigTestWriter);
185+
});
186+
187+
after(() => {
188+
testTraceModule.setTraceWriterForTest(testTraceModule.TestTraceWriter);
189+
});
190+
191+
it('should set auth variables passed to TraceWriter as authOptions', () => {
192+
const credentials = { private_key: 'abc' };
193+
testTraceModule.start({
194+
keyFilename: 'a',
195+
credentials
196+
});
197+
assert.ok(capturedConfig);
198+
assert.strictEqual(capturedConfig!.authOptions.keyFilename, 'a');
199+
assert.deepStrictEqual(capturedConfig!.authOptions.credentials, credentials);
200+
});
201+
});
202+

test/test-trace-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('Trace Interface', () => {
6868
return traceWriter
6969
.create(
7070
Object.assign(
71-
{ [FORCE_NEW]: true, projectId: 'project-1' },
71+
{ [FORCE_NEW]: true, authOptions: { projectId: 'project-1' } },
7272
defaultConfig
7373
),
7474
logger

test/test-trace-writer.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function createDummyTrace(numSpans: number): Trace {
8787
describe('Trace Writer', () => {
8888
const pjson = require('../../package.json');
8989
const DEFAULT_CONFIG: TraceWriterConfig = {
90+
authOptions: {},
9091
onUncaughtException: 'ignore',
9192
bufferSize: Infinity,
9293
flushDelaySeconds: 3600,
@@ -174,17 +175,21 @@ describe('Trace Writer', () => {
174175
it('should use the keyFilename field of the config object', async () => {
175176
const expectedCredentials: TestCredentials = require('./fixtures/gcloud-credentials.json');
176177
const actualCredentials = await captureCredentialsForConfig({
177-
projectId: 'my-project',
178-
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
178+
authOptions: {
179+
projectId: 'my-project',
180+
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
181+
},
179182
});
180183
assert.deepStrictEqual(actualCredentials, expectedCredentials);
181184
});
182185

183186
it('should use the credentials field of the config object', async () => {
184187
const expectedCredentials: TestCredentials = require('./fixtures/gcloud-credentials.json');
185188
const actualCredentials = await captureCredentialsForConfig({
186-
projectId: 'my-project',
187-
credentials: expectedCredentials,
189+
authOptions: {
190+
projectId: 'my-project',
191+
credentials: expectedCredentials,
192+
},
188193
});
189194
assert.deepStrictEqual(actualCredentials, expectedCredentials);
190195
});
@@ -197,9 +202,11 @@ describe('Trace Writer', () => {
197202
type: 'authorized_user',
198203
};
199204
const actualCredentials = await captureCredentialsForConfig({
200-
projectId: 'my-project',
201-
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
202-
credentials: expectedCredentials,
205+
authOptions: {
206+
projectId: 'my-project',
207+
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
208+
credentials: expectedCredentials,
209+
},
203210
});
204211
assert.deepStrictEqual(actualCredentials, expectedCredentials);
205212
});
@@ -216,7 +223,9 @@ describe('Trace Writer', () => {
216223

217224
it(`doesn't call Service#getProjectId if project ID is passed`, async () => {
218225
const writer = new TraceWriter(
219-
Object.assign({ projectId: 'my-project' }, DEFAULT_CONFIG),
226+
Object.assign({}, DEFAULT_CONFIG, {
227+
authOptions: { projectId: 'my-project' },
228+
}),
220229
logger
221230
);
222231
getProjectIdOverride = () => Promise.resolve('my-different-project');

test/trace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class TestLogger extends testLogger.TestLogger {
7575
export class TestTraceWriter extends TraceWriter {
7676
constructor(config: TraceWriterConfig, logger: logger.Logger) {
7777
super(config, logger);
78-
this.getConfig().projectId = '0';
78+
this.getConfig().authOptions.projectId = '0';
7979
}
8080

8181
async initialize(): Promise<void> {}

0 commit comments

Comments
 (0)