|
| 1 | +// Copyright 2021 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package sigv4 |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "io/ioutil" |
| 21 | + "net/http" |
| 22 | + "net/textproto" |
| 23 | + "sync" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/aws/aws-sdk-go/aws" |
| 27 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 28 | + "github.com/aws/aws-sdk-go/aws/credentials/stscreds" |
| 29 | + "github.com/aws/aws-sdk-go/aws/session" |
| 30 | + signer "github.com/aws/aws-sdk-go/aws/signer/v4" |
| 31 | +) |
| 32 | + |
| 33 | +var sigv4HeaderDenylist = []string{ |
| 34 | + "uber-trace-id", |
| 35 | +} |
| 36 | + |
| 37 | +type sigV4RoundTripper struct { |
| 38 | + region string |
| 39 | + next http.RoundTripper |
| 40 | + pool sync.Pool |
| 41 | + |
| 42 | + signer *signer.Signer |
| 43 | +} |
| 44 | + |
| 45 | +// NewSigV4RoundTripper returns a new http.RoundTripper that will sign requests |
| 46 | +// using Amazon's Signature Verification V4 signing procedure. The request will |
| 47 | +// then be handed off to the next RoundTripper provided by next. If next is nil, |
| 48 | +// http.DefaultTransport will be used. |
| 49 | +// |
| 50 | +// Credentials for signing are retrieved using the the default AWS credential |
| 51 | +// chain. If credentials cannot be found, an error will be returned. |
| 52 | +func NewSigV4RoundTripper(cfg *SigV4Config, next http.RoundTripper) (http.RoundTripper, error) { |
| 53 | + if next == nil { |
| 54 | + next = http.DefaultTransport |
| 55 | + } |
| 56 | + |
| 57 | + creds := credentials.NewStaticCredentials(cfg.AccessKey, string(cfg.SecretKey), "") |
| 58 | + if cfg.AccessKey == "" && cfg.SecretKey == "" { |
| 59 | + creds = nil |
| 60 | + } |
| 61 | + |
| 62 | + sess, err := session.NewSessionWithOptions(session.Options{ |
| 63 | + Config: aws.Config{ |
| 64 | + Region: aws.String(cfg.Region), |
| 65 | + Credentials: creds, |
| 66 | + }, |
| 67 | + Profile: cfg.Profile, |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("could not create new AWS session: %w", err) |
| 71 | + } |
| 72 | + if _, err := sess.Config.Credentials.Get(); err != nil { |
| 73 | + return nil, fmt.Errorf("could not get SigV4 credentials: %w", err) |
| 74 | + } |
| 75 | + if aws.StringValue(sess.Config.Region) == "" { |
| 76 | + return nil, fmt.Errorf("region not configured in sigv4 or in default credentials chain") |
| 77 | + } |
| 78 | + |
| 79 | + signerCreds := sess.Config.Credentials |
| 80 | + if cfg.RoleARN != "" { |
| 81 | + signerCreds = stscreds.NewCredentials(sess, cfg.RoleARN) |
| 82 | + } |
| 83 | + |
| 84 | + rt := &sigV4RoundTripper{ |
| 85 | + region: cfg.Region, |
| 86 | + next: next, |
| 87 | + signer: signer.NewSigner(signerCreds), |
| 88 | + } |
| 89 | + rt.pool.New = rt.newBuf |
| 90 | + return rt, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (rt *sigV4RoundTripper) newBuf() interface{} { |
| 94 | + return bytes.NewBuffer(make([]byte, 0, 1024)) |
| 95 | +} |
| 96 | + |
| 97 | +func (rt *sigV4RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 98 | + // rt.signer.Sign needs a seekable body, so we replace the body with a |
| 99 | + // buffered reader filled with the contents of original body. |
| 100 | + buf := rt.pool.Get().(*bytes.Buffer) |
| 101 | + defer func() { |
| 102 | + buf.Reset() |
| 103 | + rt.pool.Put(buf) |
| 104 | + }() |
| 105 | + if _, err := io.Copy(buf, req.Body); err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + // Close the original body since we don't need it anymore. |
| 109 | + _ = req.Body.Close() |
| 110 | + |
| 111 | + // Ensure our seeker is back at the start of the buffer once we return. |
| 112 | + var seeker io.ReadSeeker = bytes.NewReader(buf.Bytes()) |
| 113 | + defer func() { |
| 114 | + _, _ = seeker.Seek(0, io.SeekStart) |
| 115 | + }() |
| 116 | + req.Body = ioutil.NopCloser(seeker) |
| 117 | + |
| 118 | + // Clone the request and trim out headers that we don't want to sign. |
| 119 | + signReq := req.Clone(req.Context()) |
| 120 | + for _, header := range sigv4HeaderDenylist { |
| 121 | + signReq.Header.Del(header) |
| 122 | + } |
| 123 | + |
| 124 | + headers, err := rt.signer.Sign(signReq, seeker, "aps", rt.region, time.Now().UTC()) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("failed to sign request: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Copy over signed headers. Authorization header is not returned by |
| 130 | + // rt.signer.Sign and needs to be copied separately. |
| 131 | + for k, v := range headers { |
| 132 | + req.Header[textproto.CanonicalMIMEHeaderKey(k)] = v |
| 133 | + } |
| 134 | + req.Header.Set("Authorization", signReq.Header.Get("Authorization")) |
| 135 | + |
| 136 | + return rt.next.RoundTrip(req) |
| 137 | +} |
0 commit comments