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

fix: use pipe to pass body remote authorizer #426

Merged
merged 1 commit into from
Apr 30, 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
14 changes: 7 additions & 7 deletions pipeline/authz/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"text/template"

Expand Down Expand Up @@ -54,13 +54,13 @@ func (a *AuthorizerRemote) Authorize(r *http.Request, session *authn.Authenticat
return err
}

var body bytes.Buffer
err = pipeRequestBody(r, &body)
if err != nil {
return errors.Wrapf(err, `could not pipe request body in rule "%s"`, rl.GetID())
}
read, write := io.Pipe()
go func() {
err := pipeRequestBody(r, write)
write.CloseWithError(errors.Wrapf(err, `could not pipe request body in rule "%s"`, rl.GetID()))
}()

req, err := http.NewRequest("POST", c.Remote, ioutil.NopCloser(&body))
req, err := http.NewRequest("POST", c.Remote, read)
if err != nil {
return errors.WithStack(err)
}
Expand Down
20 changes: 17 additions & 3 deletions pipeline/authz/remote_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package authz_test

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -71,6 +70,21 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
config: json.RawMessage(`{}`),
wantErr: true,
},
{
name: "nobody",
setup: func(t *testing.T) *httptest.Server {
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")
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, "", string(body))
w.WriteHeader(http.StatusOK)
}))
},
body: "",
config: json.RawMessage(`{}`),
},
{
name: "ok",
setup: func(t *testing.T) *httptest.Server {
Expand All @@ -92,11 +106,11 @@ func TestAuthorizerRemoteAuthorize(t *testing.T) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, bytes.Repeat([]byte("1"), 1024*1024*50), body)
assert.True(t, strings.Repeat("1", 1024*1024) == string(body))
w.WriteHeader(http.StatusOK)
}))
},
body: strings.Repeat("1", 1024*1024*50),
body: strings.Repeat("1", 1024*1024),
config: json.RawMessage(`{}`),
},
{
Expand Down
4 changes: 4 additions & 0 deletions pipeline/authz/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func pipeRequestBody(r *http.Request, w io.Writer) error {
}

var body bytes.Buffer
defer r.Body.Close()
_, err := io.Copy(w, io.TeeReader(r.Body, &body))
if err != nil {
return err
}
r.Body = ioutil.NopCloser(&body)
return err
}