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

Verify sns message signature #198

Closed

Conversation

WtfJoke
Copy link
Contributor

@WtfJoke WtfJoke commented Nov 2, 2021

Fixes gh-176

📢 Type of change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring

📜 Description

Verify the signature of a SNS Message by utilizing SnsMessageManager#parseMessage which also validates the Signature using SignatureVerifier. The downside of it, it does more than just validate the Signature, but I think it doesnt hurt.

As an alternative we could also validate the Signature using SignatureChecker (aws sdk v1), but the class is deprecated and they suggest using SnsMessageManager.

In aws sdk 2 there would be a more optimal approach using SignatureChecker (aws sdk v2), but we still have aws sdk 1 here.

💡 Motivation and Context

#176

💚 How did you test it?

I'll added two junit tests, which tests two cases (invalid/valid signature).
For that I needed to add an additional constructor in order to be able to mock SnsMessageManager. I hope thats ok (appreciate comments for better ideas :)).

I also verified it, by publishing some messages in my sns topic through the aws management console.

📝 Checklist

  • I reviewed submitted code
  • I added tests to verify changes
  • [N/A] I updated reference documentation to reflect the change
  • All tests passing
  • No breaking changes

🔮 Next steps

@maciejwalkowiak maciejwalkowiak added this to the 2.4.0 milestone Dec 14, 2021
@maciejwalkowiak maciejwalkowiak added the component: sns SNS integration related issue label Dec 14, 2021
Copy link
Contributor

@maciejwalkowiak maciejwalkowiak left a comment

Choose a reason for hiding this comment

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

Thanks! We will include it in 2.4

Copy link
Member

@MatejNedic MatejNedic left a comment

Choose a reason for hiding this comment

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

@WtfJoke thanks for contribution!
I have an issue that we should address to have PR ready for Merge

@@ -46,8 +51,15 @@

private final MessageConverter payloadConverter;

private final SnsMessageManager snsMessageManager;
Copy link
Member

Choose a reason for hiding this comment

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

This will cause an issue if .aws/config has different region than properties for sns specific.
this.regionProvider = properties.getRegion() == null ? regionProvider.getIfAvailable() : new StaticRegionProvider(properties.getRegion());
Is used while configurating SNSclient, (meaning region in properties takes precedence over config file). Which means if we have in a file eu-central-1 and in properties if we define cloud.aws.sns.region: us-east-1 verification will always fail. (Client will use us-east-1 and SnsMessageManager will use eu-central-1)
My recommendation would be create SnsMessageManager and pass it through constructor (will require refactoring in some other classes as well since it will have to be passed through few constructors).
Or second option pass region and use properties region to configure SnsMessageManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@maciejwalkowiak First of nice find.

I know its been a while, but I dont understand the recommended approach fully 😅. SnsMessageManager has the default constructor (which uses the wrong region) and the one where I pass the region. So I should use that one.

How and where can I inject the correct region? I suppose I shouldnt duplicate the code in the SnsAutoConfiguration, right? Or should I simply inject SNSProperties and use this region (but which region provider do I use if the property is null, the default one)?

Copy link
Member

@MatejNedic MatejNedic Jan 18, 2022

Choose a reason for hiding this comment

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

Sorry, will try to explain the approach in more detail.
Personally, I would create SnsMessageManager bean in SnsAutoConfiguration and pass it to NotificationRequestConverter.
For a region use regionProvider located in SnsAutoConfiguration.

@maciejwalkowiak @eddumelendez WDYT? Could you assist further?

Copy link
Contributor Author

@WtfJoke WtfJoke Jan 29, 2022

Choose a reason for hiding this comment

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

Your explaination helped, thank you. I pushed the changes.

I always struggle a bit with formatting issues 😅 I've only managed to do this on my linux machine (and only when using the fully qualified plugin name eg /mvnw io.spring.javaformat:spring-javaformat-maven-plugin:0.0.31:apply
Havent yet tested the changes, but I think you still can take a look at it.

Just noticed the test failures, will take a look. Sorry 😅

Copy link
Contributor Author

@WtfJoke WtfJoke Jan 29, 2022

Choose a reason for hiding this comment

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

Strangely enough, all tests are green on my machine :S
EDIT: Found the cause, its the default region provided in my .aws/config

@WtfJoke
Copy link
Contributor Author

WtfJoke commented Jan 13, 2022

Just wanted to let you know that I will soon take care of the requested changes.

@WtfJoke WtfJoke force-pushed the feature/verifySNSMessageSignature branch from 19e6448 to 7386791 Compare January 29, 2022 15:29
- Create snsMessageManager with region provided by the region provider (spring properties)
- Pass snsMessageManager down to NotificationRequestConverter where it's used to verify the sns message signature
@WtfJoke WtfJoke force-pushed the feature/verifySNSMessageSignature branch from 7386791 to 9a5d8bb Compare February 4, 2022 22:07
Copy link
Contributor

@maciejwalkowiak maciejwalkowiak left a comment

Choose a reason for hiding this comment

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

I've had another look and I think we approached in from a wrong side.

Signature verification should happen for messages sent to an HTTP endpoint, not ones sent to SQS queue. This means, we do not need to verify signature in QueueMessageHandler but rather somewhere in NotificationMessageHandlerMethodArgumentResolver. SQS related code should ideally not need to change.

I know it's been a while, so is this something @WtfJoke you would be still interested in doing?

@maciejwalkowiak maciejwalkowiak added the status: waiting-for-feedback Waiting for feedback from issuer label Feb 7, 2022
@WtfJoke
Copy link
Contributor Author

WtfJoke commented Feb 8, 2022

@maciejwalkowiak no worries, I know the struggle of the life of a maintainer :D

However currently im quite occupied, so I dont want to block anybody which is interested in doing that. Feel free to close the PR.

Thank you both for taking a look to the PR in the past.

@WtfJoke WtfJoke closed this Feb 8, 2022
@maciejwalkowiak maciejwalkowiak removed this from the 2.4.0 milestone Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: sns SNS integration related issue status: waiting-for-feedback Waiting for feedback from issuer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Verifying the Signatures of Amazon SNS Messages
3 participants