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: refresh AWS credentials if marked as expired #634

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
20 changes: 20 additions & 0 deletions pkg/roundtripper/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (

type AWSSigningTransport struct {
t http.RoundTripper
cfg aws.Config
creds aws.Credentials
region string
log log.Logger
Expand All @@ -57,12 +58,17 @@ func NewAWSSigningTransport(transport http.RoundTripper, region string, log log.
return &AWSSigningTransport{
t: transport,
region: region,
cfg: cfg,
creds: creds,
log: log,
}, err
}

func (a *AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if err := a.refreshCredentialsIfNeeded(); err != nil {
_ = level.Error(a.log).Log("msg", "fail to refresh aws credentials", "err", err)
}

signer := v4.NewSigner()
payloadHash, newReader, err := hashPayload(req.Body)
if err != nil {
Expand All @@ -78,6 +84,20 @@ func (a *AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, erro
return a.t.RoundTrip(req)
}

func (a *AWSSigningTransport) refreshCredentialsIfNeeded() error {
if a.creds.Expired() {
creds, err := a.cfg.Credentials.Retrieve(context.Background())

if err != nil {
return err
}

a.creds = creds
}

return nil
}

func hashPayload(r io.ReadCloser) (string, io.ReadCloser, error) {
var newReader io.ReadCloser
payload := []byte("")
Expand Down