git.iko.soy/typeable/oauth2-proxy
on master help sign in
Improved request errors (#286)

* worked on wrapping errors in requests.go, added defer statements * removed .idea (generated by goland) * added another require.NoError * Update pkg/requests/requests.go Co-Authored-By: Dan Bond <pm@danbond.io> * fixed out-of-order imports * changelog entry added * swapped error definitions to use fmt.Errorf rather than Wrap() * formatting changes, added new defers to requests_test.go * suppot for go1.12 pipeline removed from travis pipeline, .idea/ added to gitignore * Reorder changelog entry

commit35f2ae9a36bc979feb2377b40611fe4dd3d0c75a authorTom Deadman <tjdeadma@gmail.com> dateWed 23 Oct 2019, 17:55 (7 years ago) parent6b8d2bd
5 files changed+35−19
.gitignore+1 .travis.yml−1 CHANGELOG.md+1 pkg/requests/requests.go+15−6 pkg/requests/requests_test.go+18−12
.gitignore+1
@@ -16,6 +16,7 @@ release
1616# Folders
1717_obj
1818_test
19+.idea/
1920
2021# Architecture specific extensions/prefixes
2122*.[568vq]
.travis.yml−1
@@ -1,6 +1,5 @@
11language: go
22go:
3− - 1.12.x
43 - 1.13.x
54install:
65 # Fetch dependencies
CHANGELOG.md+1
@@ -8,6 +8,7 @@
88- [#275](https://github.com/pusher/oauth2_proxy/pull/275) docker: build from debian buster (@syscll)
99- [#258](https://github.com/pusher/oauth2_proxy/pull/258) Add IDToken for Azure provider
1010 - This PR adds the IDToken into the session for the Azure provider allowing requests to a backend to be identified as a specific user. As a consequence, if you are using a cookie to store the session the cookie will now exceed the 4kb size limit and be split into multiple cookies. This can cause problems when using nginx as a proxy, resulting in no cookie being passed at all. Either increase the proxy_buffer_size in nginx or implement the redis session storage (see https://pusher.github.io/oauth2_proxy/configuration#redis-storage)
11+- [#286](https://github.com/pusher/oauth2_proxy/pull/286) Requests.go updated with useful error messages (@biotom)
1112
1213# v4.0.0
1314
pkg/requests/requests.go+15−6
@@ -18,17 +18,23 @@ func Request(req *http.Request) (*simplejson.Json, error) {
1818 return nil, err
1919 }
2020 body, err := ioutil.ReadAll(resp.Body)
21− resp.Body.Close()
21+ if body != nil {
22+ defer resp.Body.Close()
23+ }
24+
2225 logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
26+
2327 if err != nil {
24− return nil, err
28+ return nil, fmt.Errorf("problem reading http request body: %w", err)
2529 }
30+
2631 if resp.StatusCode != 200 {
2732 return nil, fmt.Errorf("got %d %s", resp.StatusCode, body)
2833 }
34+
2935 data, err := simplejson.NewJson(body)
3036 if err != nil {
31− return nil, err
37+ return nil, fmt.Errorf("error unmarshalling json: %w", err)
3238 }
3339 return data, nil
3440}
@@ -41,10 +47,13 @@ func RequestJSON(req *http.Request, v interface{}) error {
4147 return err
4248 }
4349 body, err := ioutil.ReadAll(resp.Body)
44− resp.Body.Close()
50+ if body != nil {
51+ defer resp.Body.Close()
52+ }
53+
4554 logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
4655 if err != nil {
47− return err
56+ return fmt.Errorf("error reading body from http response: %w", err)
4857 }
4958 if resp.StatusCode != 200 {
5059 return fmt.Errorf("got %d %s", resp.StatusCode, body)
@@ -56,7 +65,7 @@ func RequestJSON(req *http.Request, v interface{}) error {
5665func RequestUnparsedResponse(url string, header http.Header) (resp *http.Response, err error) {
5766 req, err := http.NewRequest("GET", url, nil)
5867 if err != nil {
59− return nil, err
68+ return nil, fmt.Errorf("error performing get request: %w", err)
6069 }
6170 req.Header = header
6271
pkg/requests/requests_test.go+18−12
@@ -8,20 +8,21 @@ import (
88 "testing"
99
1010 "github.com/bitly/go-simplejson"
11−
1211 "github.com/stretchr/testify/assert"
12+ "github.com/stretchr/testify/require"
1313)
1414
15−func testBackend(responseCode int, payload string) *httptest.Server {
15+func testBackend(t *testing.T, responseCode int, payload string) *httptest.Server {
1616 return httptest.NewServer(http.HandlerFunc(
1717 func(w http.ResponseWriter, r *http.Request) {
1818 w.WriteHeader(responseCode)
19− w.Write([]byte(payload))
19+ _, err := w.Write([]byte(payload))
20+ require.NoError(t, err)
2021 }))
2122}
2223
2324func TestRequest(t *testing.T) {
24− backend := testBackend(200, "{\"foo\": \"bar\"}")
25+ backend := testBackend(t, 200, "{\"foo\": \"bar\"}")
2526 defer backend.Close()
2627
2728 req, _ := http.NewRequest("GET", backend.URL, nil)
@@ -35,7 +36,7 @@ func TestRequest(t *testing.T) {
3536func TestRequestFailure(t *testing.T) {
3637 // Create a backend to generate a test URL, then close it to cause a
3738 // connection error.
38− backend := testBackend(200, "{\"foo\": \"bar\"}")
39+ backend := testBackend(t, 200, "{\"foo\": \"bar\"}")
3940 backend.Close()
4041
4142 req, err := http.NewRequest("GET", backend.URL, nil)
@@ -49,7 +50,7 @@ func TestRequestFailure(t *testing.T) {
4950}
5051
5152func TestHttpErrorCode(t *testing.T) {
52− backend := testBackend(404, "{\"foo\": \"bar\"}")
53+ backend := testBackend(t, 404, "{\"foo\": \"bar\"}")
5354 defer backend.Close()
5455
5556 req, err := http.NewRequest("GET", backend.URL, nil)
@@ -60,7 +61,7 @@ func TestHttpErrorCode(t *testing.T) {
6061}
6162
6263func TestJsonParsingError(t *testing.T) {
63− backend := testBackend(200, "not well-formed JSON")
64+ backend := testBackend(t, 200, "not well-formed JSON")
6465 defer backend.Close()
6566
6667 req, err := http.NewRequest("GET", backend.URL, nil)
@@ -77,7 +78,8 @@ func TestRequestUnparsedResponseUsingAccessTokenParameter(t *testing.T) {
7778 token := r.FormValue("access_token")
7879 if r.URL.Path == "/" && token == "my_token" {
7980 w.WriteHeader(200)
80− w.Write([]byte("some payload"))
81+ _, err := w.Write([]byte("some payload"))
82+ require.NoError(t, err)
8183 } else {
8284 w.WriteHeader(403)
8385 }
@@ -86,16 +88,17 @@ func TestRequestUnparsedResponseUsingAccessTokenParameter(t *testing.T) {
8688
8789 response, err := RequestUnparsedResponse(
8890 backend.URL+"?access_token=my_token", nil)
91+ defer response.Body.Close()
92+
8993 assert.Equal(t, nil, err)
9094 assert.Equal(t, 200, response.StatusCode)
9195 body, err := ioutil.ReadAll(response.Body)
9296 assert.Equal(t, nil, err)
93− response.Body.Close()
9497 assert.Equal(t, "some payload", string(body))
9598}
9699
97100func TestRequestUnparsedResponseUsingAccessTokenParameterFailedResponse(t *testing.T) {
98− backend := testBackend(200, "some payload")
101+ backend := testBackend(t, 200, "some payload")
99102 // Close the backend now to force a request failure.
100103 backend.Close()
101104
@@ -110,7 +113,8 @@ func TestRequestUnparsedResponseUsingHeaders(t *testing.T) {
110113 func(w http.ResponseWriter, r *http.Request) {
111114 if r.URL.Path == "/" && r.Header["Auth"][0] == "my_token" {
112115 w.WriteHeader(200)
113− w.Write([]byte("some payload"))
116+ _, err := w.Write([]byte("some payload"))
117+ require.NoError(t, err)
114118 } else {
115119 w.WriteHeader(403)
116120 }
@@ -120,10 +124,12 @@ func TestRequestUnparsedResponseUsingHeaders(t *testing.T) {
120124 headers := make(http.Header)
121125 headers.Set("Auth", "my_token")
122126 response, err := RequestUnparsedResponse(backend.URL, headers)
127+ defer response.Body.Close()
128+
123129 assert.Equal(t, nil, err)
124130 assert.Equal(t, 200, response.StatusCode)
125131 body, err := ioutil.ReadAll(response.Body)
126132 assert.Equal(t, nil, err)
127− response.Body.Close()
133+
128134 assert.Equal(t, "some payload", string(body))
129135}