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 all commits
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
4 changes: 4 additions & 0 deletions pipeline/authz/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
return errors.WithStack(err)
}
req.Header.Add("Content-Type", r.Header.Get("Content-Type"))
authz := r.Header.Get("Authorization")
if authz != "" {
req.Header.Add("Authorization", authz)
}

for hdr, templateString := range c.Headers {
var tmpl *template.Template
Expand Down
6 changes: 5 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,10 @@ func (a *AuthorizerRemoteJSON) Authorize(_ *http.Request, session *authn.Authent
return errors.WithStack(err)
}
req.Header.Add("Content-Type", "application/json")
authz := r.Header.Get("Authorization")
if authz != "" {
req.Header.Add("Authorization", authz)
}

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
1 change: 1 addition & 0 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ 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.Nil(t, r.Header["Authorization"])
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, "testtest", string(body))
Expand Down