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: Forward original authorization header when using remote (json) authorizer #554

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pipeline/authz/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
return errors.WithStack(err)
}
req.Header.Add("Content-Type", r.Header.Get("Content-Type"))
req.Header.Add("Authorization", r.Header.Get("Authorization"))

for hdr, templateString := range c.Headers {
var tmpl *template.Template
Expand Down
3 changes: 2 additions & 1 deletion pipeline/authz/remote_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (a *AuthorizerRemoteJSON) GetID() string {
}

// Authorize implements the Authorizer interface.
func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
func (a *AuthorizerRemoteJSON) Authorize(r *http.Request, session *authn.AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
c, err := a.Config(config)
if err != nil {
return err
Expand Down Expand Up @@ -84,6 +84,7 @@ func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.Authent
return errors.WithStack(err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", r.Header.Get("Authorization"))
Copy link
Member

Choose a reason for hiding this comment

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

What happens when there is no Authorization header in the original request? Looks like it will still be set, then to an empty string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mmm, that's not so nice i guess. changed to only forward if authz header is non-empty

Copy link
Member

Choose a reason for hiding this comment

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

can you maybe also add a test case for that? thanks 😉

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!


res, err := a.client.Do(req)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pipeline/authz/remote_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.Header, "Content-Type")
assert.Contains(t, r.Header["Content-Type"], "application/json")
assert.Contains(t, r.Header, "Authorization")
assert.Contains(t, r.Header["Authorization"], "Bearer token")
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, string(body), "{}")
Expand Down Expand Up @@ -139,7 +141,11 @@ func TestAuthorizerRemoteJSONAuthorize(t *testing.T) {

p := configuration.NewViperProvider(logrusx.New("", ""))
a := NewAuthorizerRemoteJSON(p)
if err := a.Authorize(&http.Request{}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
if err := a.Authorize(&http.Request{
Header: map[string][]string{
"Authorization": {"Bearer token"},
},
}, tt.session, tt.config, &rule.Rule{}); (err != nil) != tt.wantErr {
t.Errorf("Authorize() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
7 changes: 5 additions & 2 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Contains(t, r.Header, "Content-Type")
assert.Contains(t, r.Header["Content-Type"], "text/plain")
assert.Contains(t, r.Header, "Authorization")
assert.Contains(t, r.Header["Authorization"], "Bearer token")
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, "testtest", string(body))
Expand Down Expand Up @@ -145,8 +147,9 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
a := NewAuthorizerRemote(p)
r := &http.Request{
Header: map[string][]string{
"Content-Type": {"text/plain"},
"User-Agent": {"Fancy Browser 5.1"},
"Content-Type": {"text/plain"},
"User-Agent": {"Fancy Browser 5.1"},
"Authorization": {"Bearer token"},
},
}
if tt.body != "" {
Expand Down