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

Commit 02e8bb4

Browse files
authored
fix: accept uniqueWriterIdentity in setMetadata (#1034)
* fix: accept uniqueWriterIdentity in setMetadata * docs: explain unique writer id flag better * chore: explain uniqueWriterIdentity field * fix: prevent topic_permission_denied errors in tests
1 parent 0b7af34 commit 02e8bb4

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface CreateSinkRequest {
8383
includeChildren?: boolean;
8484
name?: string;
8585
outputVersionFormat?: google.logging.v2.LogSink.VersionFormat;
86-
uniqueWriterIdentity?: string;
86+
uniqueWriterIdentity?: string | boolean;
8787
gaxOptions?: gax.CallOptions;
8888
}
8989

@@ -315,7 +315,7 @@ class Logging {
315315
* https://cloud.google.com/nodejs/docs/reference/pubsub/latest/Topic Topic}
316316
* @property {string} [filter] An advanced logs filter. Only log entries
317317
* matching the filter are written.
318-
* @property {string} [uniqueWriterIdentity] Determines the kind of IAM
318+
* @property {string|boolean} [uniqueWriterIdentity] Determines the kind of IAM
319319
* identity returned as `writerIdentity` in the new sink. See {@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create#query-parameters}.
320320
*/
321321
/**

src/sink.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export type SinkMetadataResponse = [LogSink];
3434

3535
export interface SetSinkMetadata extends LogSink {
3636
gaxOptions?: CallOptions;
37+
// A unique service account just for the sink
38+
// https://cloud.google.com/logging/docs/api/tasks/exporting-logs#using_a_unique_writer_identity
39+
uniqueWriterIdentity?: boolean | string;
3740
}
3841

3942
/**
@@ -289,6 +292,11 @@ class Sink {
289292
/**
290293
* Set the sink's metadata.
291294
*
295+
* Note: If the sink was previously created or updated with
296+
* uniqueWriterIdentity = true, then you must update the sink by setting
297+
* uniqueWriterIdentity = true. Read more about using a unique writer identity
298+
* here: https://cloud.google.com/logging/docs/api/tasks/exporting-logs#using_a_unique_writer_identity
299+
*
292300
* @see [Sink Resource]{@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks#LogSink}
293301
* @see [projects.sink.update API Documentation]{@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/update}
294302
*
@@ -325,11 +333,18 @@ class Sink {
325333
*/
326334
async setMetadata(metadata: SetSinkMetadata): Promise<SinkMetadataResponse> {
327335
const [currentMetadata] = await this.getMetadata();
328-
const reqOpts = {
336+
const uniqueWriterIdentity = metadata.uniqueWriterIdentity;
337+
delete metadata.uniqueWriterIdentity;
338+
let reqOpts = {
329339
sinkName: this.formattedName_,
330340
sink: extend({}, currentMetadata, metadata),
331341
};
332342
delete reqOpts.sink.gaxOptions;
343+
// Add user specified uniqueWriterIdentity boolean, if any.
344+
reqOpts = {
345+
...reqOpts,
346+
...(uniqueWriterIdentity && {uniqueWriterIdentity}),
347+
};
333348
[this.metadata] = await this.logging.configService.updateSink(
334349
reqOpts,
335350
metadata.gaxOptions

system-test/logging.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,43 @@ describe('Logging', () => {
182182
});
183183
});
184184

185+
describe('metadata with uniqueWriterIdentity', () => {
186+
it('should set metadata if uniqueWriterIdentity was true', async () => {
187+
const sink = logging.sink(generateName());
188+
const FILTER = 'severity = ALERT';
189+
await sink.create({
190+
destination: topic,
191+
uniqueWriterIdentity: true,
192+
});
193+
const metadata = {
194+
filter: FILTER,
195+
uniqueWriterIdentity: true,
196+
};
197+
const [apiResponse] = await sink.setMetadata(metadata);
198+
assert.strictEqual(apiResponse.filter, FILTER);
199+
// Sink must be deleted within this test before any logs are generated
200+
// to avoid topic_permission_denied emails.
201+
await sink.delete();
202+
});
203+
204+
it('should set uniqueWriterIdentity from false to true', async () => {
205+
const sink = logging.sink(generateName());
206+
const FILTER = 'severity = ALERT';
207+
await sink.create({
208+
destination: topic,
209+
});
210+
const metadata = {
211+
filter: FILTER,
212+
uniqueWriterIdentity: true,
213+
};
214+
const [apiResponse] = await sink.setMetadata(metadata);
215+
assert.strictEqual(apiResponse.filter, FILTER);
216+
// Sink must be deleted within this test before any logs are generated
217+
// to avoid topic_permission_denied emails.
218+
await sink.delete();
219+
});
220+
});
221+
185222
describe('listing sinks', () => {
186223
const sink = logging.sink(generateName());
187224

0 commit comments

Comments
 (0)