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: add support for compressed hydrator. Closes #988 #1018

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion pipeline/mutate/mutator_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package mutate

import (
"bytes"
"compress/gzip"
"compress/zlib"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
Expand All @@ -33,6 +36,8 @@ const (
ErrNoCredentialsProvided = "No credentials were provided in mutator configuration"
contentTypeHeaderKey = "Content-Type"
contentTypeJSONHeaderValue = "application/json"
acceptEncodingHeaderKey = "Accept-Encoding"
acceptEncodingHeaderValue = "gzip, deflate"
)

type MutatorHydrator struct {
Expand Down Expand Up @@ -168,6 +173,7 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
req.SetBasicAuth(credentials.Username, credentials.Password)
}
req.Header.Set(contentTypeHeaderKey, contentTypeJSONHeaderValue)
req.Header.Set(acceptEncodingHeaderKey, acceptEncodingHeaderValue)

client := http.DefaultClient
if a.d.Tracer() != nil {
Expand Down Expand Up @@ -215,8 +221,21 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
return errors.New(ErrNon200ResponseFromAPI)
}

// Handle compressed data
Copy link
Member

Choose a reason for hiding this comment

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

The go stdlib HTTP client should do that automatically, does it not?

Copy link
Author

Choose a reason for hiding this comment

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

From the stdlib HTTP docs:

// If the Transport requests gzip on
// its own and gets a gzipped response, it's transparently
// decoded in the Response.Body. However, if the user
// explicitly requested gzip it is not automatically
// uncompressed.

So because we set the Accept-Encoding header, we must decompress ourselves.
Based on what I and Juan131 observed, the transport was not decoding the response prior to that either.

It's been over 2 years since I touched Go and we've been running this code in production all this time.
However, I have not yet tested again with the latest version of Go.

var reader io.ReadCloser
switch res.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(res.Body)
defer reader.Close()
case "deflate":
reader, err = zlib.NewReader(res.Body)
defer reader.Close()
default:
reader = res.Body
}

sessionFromUpstream := authn.AuthenticationSession{}
err = json.NewDecoder(res.Body).Decode(&sessionFromUpstream)
err = json.NewDecoder(reader).Decode(&sessionFromUpstream)
if err != nil {
return errors.WithStack(err)
}
Expand Down
Loading