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

Add support for token passed Authorization Bearer header #5397

Merged
merged 8 commits into from
Oct 1, 2018

Conversation

uepoch
Copy link
Contributor

@uepoch uepoch commented Sep 25, 2018

Fixes: #5396

Similar to what has been done in hashicorp/consul#4502
Most credits go to @mbag for the consul PR

This let users pass their token as an Authorization: Bearer XXX header RFC-6750, which is an Oauth standard.

Some software only can be configured to use Authorization headers, and don't let users specify arbitrary (i.e X-Vault-Token) header names.

Doc will be further updated by this week in the PR

@briankassouf briankassouf added this to the 0.11.2 milestone Sep 26, 2018
@uepoch uepoch force-pushed the rfc-6750 branch 2 times, most recently from 045a6db to 0dac0b0 Compare September 27, 2018 13:40
briankassouf
briankassouf previously approved these changes Sep 27, 2018
Copy link
Contributor

@briankassouf briankassouf left a comment

Choose a reason for hiding this comment

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

Looks great!

Copy link
Contributor

@tyrannosaurus-becks tyrannosaurus-becks left a comment

Choose a reason for hiding this comment

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

@uepoch this is great, thank you for doing it! Just a couple of little things.

http/handler.go Outdated
if v := r.Header.Get("Authorization"); v != "" {
// Reference for Authorization header format: https://tools.ietf.org/html/rfc7236#section-3

if !strings.HasPrefix(v, "Bearer") {
Copy link
Contributor

Choose a reason for hiding this comment

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

RFC 6750 gives only one example of value v: Bearer mF_9.B5f-4.1JqM. I wonder if it would be safe to check for "Bearer " here.

http/handler.go Outdated
if !strings.HasPrefix(v, "Bearer") {
return "", fmt.Errorf("the Authorization header scheme should be Bearer")
}
vals := strings.Split(strings.TrimSpace(strings.TrimPrefix(v, "Bearer")), " ")
Copy link
Contributor

Choose a reason for hiding this comment

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

If we checked for the full "Bearer " prefix above with a space, could this just become return v[7:], nil? https://play.golang.org/p/Nttq9bx15lu

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One might send space separated value (not that it would make sense tho)
Still have to split(s, ' ')[0]
but if the aim is to optimize, we can probably check for the presence of a space in the string without allocation, return an error if present, return value if not

https://play.golang.org/p/NgRoBv-8Y1N
This address the comment of Calvin too I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the PR with a new version, with less allocations

}

rWithVault, err := http.NewRequest("GET", "v1/test/path", nil)
rWithVault.Header.Set(consts.AuthHeaderName, token)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be moved down to after the above error is checked? In case rWithVault is nil due to an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

http/handler_test.go Show resolved Hide resolved
http/handler_test.go Show resolved Hide resolved
http/handler.go Outdated
return "", fmt.Errorf("the Authorization header scheme should be Bearer")
}
vals := strings.Split(strings.TrimSpace(strings.TrimPrefix(v, "Bearer")), " ")
if len(vals) < 1 {
Copy link
Contributor

@calvn calvn Sep 27, 2018

Choose a reason for hiding this comment

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

Checking with len(vals) != 1 would also guard against (invalid formatted) tokens that might contain spaces.

Copy link
Contributor

@briankassouf briankassouf left a comment

Choose a reason for hiding this comment

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

Thanks for making those changes, would you mind adding Docs in a separate PR?

@briankassouf briankassouf merged commit 4c3d421 into hashicorp:master Oct 1, 2018
@uepoch
Copy link
Contributor Author

uepoch commented Oct 2, 2018

Ok, I'll make the documentation PR this week, I've been busy on another project lately; sorry for the delay!

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.

4 participants