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
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
@@ -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
6 changes: 5 additions & 1 deletion pipeline/authz/remote_json.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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 {
8 changes: 7 additions & 1 deletion pipeline/authz/remote_json_test.go
Original file line number Diff line number Diff line change
@@ -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), "{}")
@@ -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)
}
})
1 change: 1 addition & 0 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
@@ -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))