forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signer.go
62 lines (49 loc) · 1.75 KB
/
signer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package authv4
import (
"github.com/aws/amazon-ecs-agent/agent/ecs_client/authv4/credentials"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/authv4/sign"
"github.com/aws/amazon-ecs-agent/agent/ecs_client/authv4/signable"
"errors"
"net/http"
)
func newDefaultSigner(region, service string, creds credentials.AWSCredentialProvider, extraHeaders []string) *DefaultSigner {
internalSigner := sign.NewSigner(region, service, creds, extraHeaders)
return &DefaultSigner{
AWSCredentialProvider: creds,
Region: region,
Service: service,
ExtraHeaders: extraHeaders,
Signer: internalSigner,
}
}
func NewSigner(region, service string, creds credentials.AWSCredentialProvider, extraHeaders []string) Signer {
return newDefaultSigner(region, service, creds, extraHeaders)
}
func NewHttpSigner(region, service string, creds credentials.AWSCredentialProvider, extraHeaders []string) HttpSigner {
return newDefaultSigner(region, service, creds, extraHeaders)
}
func NewRoundtripSigner(signer HttpSigner, Transport http.RoundTripper) RoundTripperSigner {
return &DefaultRoundTripSigner{
HttpSigner: signer,
Transport: Transport,
}
}
func (signer *DefaultSigner) Sign(s signable.Signable) error {
return signer.Signer.Sign(s)
}
func (signer *DefaultSigner) SignHttpRequest(req *http.Request) error {
signable := signable.HttpRequest{req}
return signer.Sign(signable)
}
func (signer *DefaultRoundTripSigner) RoundTrip(req *http.Request) (*http.Response, error) {
if signer.Transport == nil {
return nil, errors.New("Invalid transport provided")
}
reqCopy := http.Request(*req)
err := signer.SignHttpRequest(&reqCopy)
if err != nil {
return nil, err
}
resp, err := signer.Transport.RoundTrip(&reqCopy)
return resp, err
}