Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(subscription): support oidcToken #865

Merged
merged 4 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: '[email protected]',
* 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm relatively green in this codebase, but mind clarifying why we need to delete the key off the formatted object?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem to already be happening above it for formatted.pushEndpoint and as well...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was my take as well, oidcToken is just being treated like what's above. My main question is on the sort of systemic goodness of the patch - I don't understand the gestalt of the library enough to say yay or nay yet.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the changes here just expose access to the underlying API call, and is already exposed in other client libraries. With the exception of deleting the key (is this for memory management?) this is LGTM.

}

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