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

fix(sns): allow tokens to be used in UrlSubscription #2938

Merged
merged 11 commits into from
Jul 4, 2019

Conversation

spg
Copy link
Contributor

@spg spg commented Jun 19, 2019

Allows to pass in a dynamic reference as the url, like so:

myTopic.addSubscription(
  new UrlSubscription("{{resolve:secretsmanager:some-url}}", sns.SubscriptionProtocol.Https)
);

without having the CDK complain:

Error: URL must start with either http:// or https://
    at new UrlSubscription (/Users/spgingras/myProject/cdk-app/node_modules/@aws-cdk/aws-sns-subscriptions/lib/url.ts:28:13)
...

Pull Request Checklist

  • Testing
    • Unit test added (prefer not to modify an existing test, otherwise, it's probably a breaking change)
    • CLI change?: coordinate update of integration tests with team
    • cdk-init template change?: coordinated update of integration tests with team
  • Docs
    • jsdocs: All public APIs documented
    • README: README and/or documentation topic updated
    • Design: For significant features, design document added to design folder
  • Title and Description
    • Change type: title prefixed with fix, feat and module name in parens, which will appear in changelog
    • Title: use lower-case and doesn't end with a period
    • Breaking?: last paragraph: "BREAKING CHANGE: <describe what changed + link for details>"
    • Issues: Indicate issues fixed via: "Fixes #xxx" or "Closes #xxx"
  • Sensitive Modules (requires 2 PR approvers)
    • IAM Policy Document (in @aws-cdk/aws-iam)
    • EC2 Security Groups and ACLs (in @aws-cdk/aws-ec2)
    • Grant APIs (only if not based on official documentation with a reference)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

@spg spg requested a review from a team as a code owner June 19, 2019 22:16
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

You're right that this needs a fix, but I would like to see the fix done differently.

Can you change it to only check if (!cdk.Token.isUnresolved(url)) { ...do the check ... }.

You will be able to plug in SecretsManager values by going:

const url = secretsmanager.Secret.fromSecretAttributes(stack, 'Secret', { ... }).secretValue;

Or, if you really want to use the direct string construction, Token.asString('{{resolve:...}}').

https://github.com/awslabs/aws-cdk/blob/master/packages/%40aws-cdk/aws-secretsmanager/test/test.secret.ts#L299

@rix0rrr rix0rrr self-assigned this Jun 20, 2019
@spg spg force-pushed the fix-sns-dont-check-protocol-url-subscription branch from 11929e2 to fb7d856 Compare June 20, 2019 19:04
throw new Error('URL must start with either http:// or https://');
}
}

public bind(scope: Construct, topic: sns.ITopic): void {
new sns.Subscription(scope, this.url, {
new sns.Subscription(scope, topic.node.uniqueId + 'Url', {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This avoids an error:

Error: Cannot use tokens in construct ID: ${Token[TOKEN.11]}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, so it does. But now you can only add one URL subscription for any given topic.

Can you use the following expression instead?

Token.isUnresolved(this.url) ? 'UnresolvedUrl' : this.url

So we use the literal one if available, and only fall back to the reuse-breaking identifier if the URL is unresolved?

@spg spg force-pushed the fix-sns-dont-check-protocol-url-subscription branch from e6269e0 to a0c71e6 Compare June 21, 2019 13:31
@spg spg force-pushed the fix-sns-dont-check-protocol-url-subscription branch from a0c71e6 to e48f892 Compare June 21, 2019 13:32
@@ -23,17 +23,17 @@ export interface UrlSubscriptionProps extends SubscriptionProps {
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html
*/
export class UrlSubscription implements sns.ITopicSubscription {
constructor(private readonly url: string, private readonly props: UrlSubscriptionProps = {}) {
if (!url.startsWith('http://') && !url.startsWith('https://')) {
constructor(private readonly url: string, private readonly protocol: sns.SubscriptionProtocol, private readonly props: UrlSubscriptionProps = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather have the protocol optionally inside the props, and derive it from the literal URL (if available).

Only force passing it if the URL is unresolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rix0rrr fixed in d524d12

@spg spg changed the title fix(sns) UrlSubscription: don't validate protocol fix(sns) UrlSubscription: allow Tokens as URL Jun 21, 2019
@spg
Copy link
Contributor Author

spg commented Jun 21, 2019

@rix0rrr not sure what is happening with CodeBuild

@spg spg force-pushed the fix-sns-dont-check-protocol-url-subscription branch from a6c350c to 938bee4 Compare June 25, 2019 21:28
}

if (this.unresolvedUrl) {
this.protocol = props.protocol!;
Copy link
Contributor

Choose a reason for hiding this comment

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

The “!” shouldn’t be required here. Try to use !props.protocol above

@eladb eladb changed the title fix(sns) UrlSubscription: allow Tokens as URL fix(sns): allow tokens to be used in UrlSubscription Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants