Skip to content

Commit

Permalink
feat(subscription): support oidcToken (#865)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <[email protected]>
Co-authored-by: Megan Potter <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2020
1 parent cf138ab commit a786ca0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,15 @@ export class PubSub {
* days.
* @property {string} [pushEndpoint] A URL to a custom endpoint that
* messages should be pushed to.
* @property {object} [oidcToken] If specified, Pub/Sub will generate and
* attach an OIDC JWT token as an `Authorization` header in the HTTP
* request for every pushed message. This object should have the same
* structure as [OidcToken]{@link google.pubsub.v1.OidcToken}
* @property {boolean} [retainAckedMessages=false] If set, acked messages
* are retained in the subscription's backlog for the length of time
* specified by `options.messageRetentionDuration`.
* @property {ExpirationPolicy} [expirationPolicy] A policy that specifies
* the conditions for this subscription's expiration.
* the conditions for this subscription's expiration.
*/
/**
* Create a subscription to a topic.
Expand Down
19 changes: 19 additions & 0 deletions src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ import {noop} from './util';

export type PushConfig = google.pubsub.v1.IPushConfig;

export type OidcToken = google.pubsub.v1.PushConfig.IOidcToken;

export type SubscriptionMetadata = {
messageRetentionDuration?: google.protobuf.IDuration | number;
pushEndpoint?: string;
oidcToken?: OidcToken;
} & Omit<google.pubsub.v1.ISubscription, 'messageRetentionDuration'>;

export type SubscriptionOptions = SubscriberOptions & {topic?: Topic};
Expand Down Expand Up @@ -739,6 +742,10 @@ export class Subscription extends EventEmitter {
* @param {string} config.pushEndpoint A URL locating the endpoint to which
* messages should be published.
* @param {object} config.attributes [PushConfig attributes](https://cloud.google.com/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PushConfig).
* @param {object} config.oidcToken If specified, Pub/Sub will generate and
* attach an OIDC JWT token as an `Authorization` header in the HTTP
* request for every pushed message. This object should have the same
* structure as [OidcToken]{@link google.pubsub.v1.OidcToken}
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {ModifyPushConfigCallback} [callback] Callback function.
Expand All @@ -755,6 +762,10 @@ export class Subscription extends EventEmitter {
* pushEndpoint: 'https://mydomain.com/push',
* attributes: {
* key: 'value'
* },
* oidcToken: {
* serviceAccountEmail: 'myproject@appspot.gserviceaccount.com',
* audience: 'myaudience'
* }
* };
*
Expand Down Expand Up @@ -1039,6 +1050,14 @@ export class Subscription extends EventEmitter {
delete formatted.pushEndpoint;
}

if (metadata.oidcToken) {
formatted.pushConfig = {
...formatted.pushConfig,
oidcToken: metadata.oidcToken,
};
delete formatted.oidcToken;
}

return formatted as google.pubsub.v1.ISubscription;
}
/*!
Expand Down
47 changes: 47 additions & 0 deletions test/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,53 @@ describe('Subscription', () => {
undefined
);
});

it('should format oidcToken', () => {
const oidcToken = {
serviceAccount: '[email protected]',
audience: 'audience',
};

const metadata = {
oidcToken,
};

const formatted = Subscription.formatMetadata_(metadata);

assert.strictEqual(formatted.pushConfig!.oidcToken, oidcToken);
assert.strictEqual(
(formatted as subby.SubscriptionMetadata).oidcToken,
undefined
);
});

it('should format both pushEndpoint and oidcToken', () => {
const pushEndpoint = 'http://noop.com/push';

const oidcToken = {
serviceAccount: '[email protected]',
audience: 'audience',
};

const metadata = {
pushEndpoint,
oidcToken,
};

const formatted = Subscription.formatMetadata_(metadata);

assert.strictEqual(formatted.pushConfig!.pushEndpoint, pushEndpoint);
assert.strictEqual(
(formatted as subby.SubscriptionMetadata).pushEndpoint,
undefined
);

assert.strictEqual(formatted.pushConfig!.oidcToken, oidcToken);
assert.strictEqual(
(formatted as subby.SubscriptionMetadata).oidcToken,
undefined
);
});
});

describe('formatName_', () => {
Expand Down

0 comments on commit a786ca0

Please sign in to comment.