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: support sigv4 signing #169

Merged
merged 14 commits into from
Oct 28, 2024
Merged

Conversation

obanby
Copy link
Contributor

@obanby obanby commented Aug 30, 2024

  • Added Tripper to enable signing requests
  • Added Signer to implement the AWS sigv4 signing standard

I based my PR on this comment #161 (comment) and https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html

Note: I don't have deep knowledge about the sigv4, so please provide as much feedback as possible, and I will be happy to address it :)

Addresses: #93

- Added Tripper to enable signing requests
- Added Signer to implement the aws sigv4 signing standard

Signed-off-by: obanby <obanby@gmail.com>
@obanby obanby requested a review from a team as a code owner August 30, 2024 20:08
@obanby obanby requested review from olegbespalov and joanlopez and removed request for a team August 30, 2024 20:08
@CLAassistant
Copy link

CLAassistant commented Aug 30, 2024

CLA assistant check
All committers have signed the CLA.

@mike-sol
Copy link

This is great, enables easy export to Amazon Managed Prometheus. 👍

@mike-sol
Copy link

Can I buy someone a coffee / beer / etc to get this reviewed and merged?

@olegbespalov
Copy link
Contributor

Hey @mike-sol !

The pull request is on our radar, but the team is busy with the upcoming K6 release. We'll return to it once we finish. Sorry for the delay!

Copy link
Contributor

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

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

Hi @obanby!

Thanks for the PR, and sorry for the delay in reviewing!

First of all, I'd like to say that, like you, I don't have a deep knowledge of sigv4. That's why I checked the AWS Go SDK to familiarize myself with it and made some of my comments based on the logic that I found implemented there.

Another question is, have you tested these changes if they work?

pkg/sigv4/tripper.go Outdated Show resolved Hide resolved
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
pkg/sigv4/tripper_test.go Show resolved Hide resolved
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
@obanby
Copy link
Contributor Author

obanby commented Sep 26, 2024

Hi @obanby!

Thanks for the PR, and sorry for the delay in reviewing!

First of all, I'd like to say that, like you, I don't have a deep knowledge of sigv4. That's why I checked the AWS Go SDK to familiarize myself with it and made some of my comments based on the logic that I found implemented there.

Another question is, have you tested these changes if they work?

Thanks @olegbespalov, yes I have tested with our aws managed prometheus.

- Ported code from aws-sdk-go for proper query string handling
- Refactored code to reduce methods
- Added validations for tripper
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
pkg/remotewrite/config.go Outdated Show resolved Hide resolved
pkg/sigv4/tripper.go Outdated Show resolved Hide resolved
pkg/sigv4/const.go Outdated Show resolved Hide resolved
pkg/sigv4/utils.go Show resolved Hide resolved
pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
@obanby obanby force-pushed the feateure-sigv4 branch 2 times, most recently from 16523ee to f36001e Compare September 27, 2024 15:11
- Added config validations for tripper
- Added comment for aps
- Removed noEscape to signer internal state
Copy link

@joanlopez joanlopez left a comment

Choose a reason for hiding this comment

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

Hey @obanby, thanks for your contribution! 🙌🏻

I have reviewed this PR as well, and I don't have any other comments beyond what @olegbespalov has already mentioned. So, feel free to try to address them all and I'll be happy to give my 👍🏻 once ready!

@obanby
Copy link
Contributor Author

obanby commented Oct 8, 2024

Thanks @olegbespalov @joanlopez, I addressed all the comments.

One thing I noticed, but didn't know how to go about it is: erroring out if only partial config is passed in. Say a user sets the the SIGV4_REGION without setting the access key. Any ideas how to fail with an informative message?

@olegbespalov
Copy link
Contributor

Hey @obanby

Thanks for the update; I'll go through it at the beginning of next week!

One thing I noticed, but didn't know how to go about it is: erroring out if only partial config is passed in. Say a user sets the the SIGV4_REGION without setting the access key. Any ideas how to fail with an informative message?

You could try collecting the validation errors in the slice, something like:

errs := make([]string, 0)

if len(strings.TrimSpace(config.Region)) == 0 {
	errs = append(errs, "sigV4 config `Region` must be set")
}

if len(strings.TrimSpace(config.AwsSecretAccessKey)) == 0 {
	errs = append(errs, "sigV4 config `AwsSecretAccessKey` must be set")
}

if len(strings.TrimSpace(config.AwsAccessKeyID)) == 0 {
	errs = append(errs, "sigV4 config `AwsAccessKeyID` must be set")
}

if len(errs) > 0 {
	return nil, errors.New("can't initialize a sigv4 round tripper:" + strings.Join(errs, ", "))
}

@obanby
Copy link
Contributor Author

obanby commented Oct 11, 2024

Thanks @olegbespalov, but to clarify. The current error handling should have caught that config.AwsSecretAccessKey is not set, and returns the error to the tripper client. However, that error doesn't correctly get propagated to console. So the user end up with a 403 unauthorized, because they didn't see any errors about sigv4. So my question is, how can this error propagate all the way up (preferably also short circuit the test)

@olegbespalov
Copy link
Contributor

olegbespalov commented Oct 11, 2024

Right, then I believe we should add validation somewhere around RemoteConfig e.g. https://github.com/grafana/xk6-output-prometheus-remote/pull/169/files#diff-35f036a9d95de5edce2e31d56e65f748e31a64950ca6af842f241bf8dc01bf7fR126

sigvcfg := &sigv4.Config{
	Region:             conf.SigV4Region.String,
	AwsAccessKeyID:     conf.SigV4AccessKey.String,
	AwsSecretAccessKey: conf.SigV4SecretKey.String,
}

err := sigvcfg.Validate()
if err != nil {
    return nil, err
}

hc.SigV4 = sigvcfg

@obanby
Copy link
Contributor Author

obanby commented Oct 11, 2024

Sounds good, I will try that.

I will leave it till your review next week and address it then with any other comments you have :)

pkg/sigv4/sigv4.go Outdated Show resolved Hide resolved
@olegbespalov
Copy link
Contributor

Hey @obanby !

Just in case, I've reviewed the rest of the changes, and it looks good; maybe the only missing part is the validation that we discussed before to advise customers faster about misconfiguration.

So, thanks for implementing that!

@obanby
Copy link
Contributor Author

obanby commented Oct 15, 2024

Thanks @olegbespalov was just taking sometime to see how to go about it. My last commit I addressed the partial config issue, that's kinda what I was pointing out, see if you like that and we can go from there :)

@obanby obanby requested a review from olegbespalov October 15, 2024 15:57
@obanby obanby requested a review from olegbespalov October 16, 2024 14:25
Copy link
Contributor

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

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

Looks good to me! 🚀 Thanks for the contribution! 🙇

Let's wait till @joanlopez will give a final look/feedback!

@obanby obanby requested a review from olegbespalov October 18, 2024 15:28
@obanby
Copy link
Contributor Author

obanby commented Oct 18, 2024

I addressed some of the issues flagged by the CI lint job.

@olegbespalov @joanlopez please have a look. I hope we can get this in soon :)

@obanby
Copy link
Contributor Author

obanby commented Oct 22, 2024

@olegbespalov @joanlopez, when will we be able to merge this please?

@olegbespalov
Copy link
Contributor

Hey @obanby, you have my approval, but we still need the review from @joanlopez for merging.

In the meantime, could you please address comments from the linter? Most of them look like auto-fixes, so it's just a matter of running golangci-lint run --fix against the files. You could also validate if that helps by running the make lint command locally.

@obanby
Copy link
Contributor Author

obanby commented Oct 24, 2024

Thanks all, I have address the changes flagged by make lint.

There are two missing, one is not related to my changes, and the other will require adding another function to breeak down the method, but doesn't seem like the approach to take.

pkg/remotewrite/config.go:289    funlen  Function 'parseEnvs' is too long (84 > 80)
pkg/remotewrite/trend.go:117:21  gosec   G115: integer overflow conversion int -> uint16

@olegbespalov
Copy link
Contributor

@obanby, no worries. We will take care of these linter issues. We'll merge this PR soon. Thanks again for your contribution! 🙇 🎉

@olegbespalov olegbespalov merged commit 166752f into grafana:main Oct 28, 2024
9 of 10 checks passed
@joanlopez
Copy link

Thank you @obanby for your contribution! 🙇🏻
Thanks @olegbespalov for driving this to getting it merged! 💟

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.

5 participants