forked from tdeebswihart/aws-signing
-
Notifications
You must be signed in to change notification settings - Fork 4
/
aws.go
66 lines (59 loc) · 1.36 KB
/
aws.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
63
64
65
66
package config
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"github.com/BishopFox/aws-signing/signing"
"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
awscfg "github.com/aws/aws-sdk-go-v2/config"
)
var (
DefaultsAws = Aws{
Use: false,
Endpoint: "http://localhost:9200",
Service: "es",
}
EnvAws = Aws{
Use: hasEnvVar("AWS_SIGNING"),
Endpoint: os.Getenv("AWS_ENDPOINT"),
Service: os.Getenv("AWS_SERVICE"),
}
)
type Aws struct {
Use bool `hcl:"enabled"`
Service string `hcl:"service"`
Endpoint string `hcl:"endpoint"`
}
func (a Aws) EndpointUrl() *url.URL {
return parseUrl(a.Endpoint, nil)
}
func MergeAws(cfgs ...Aws) Aws {
rv := Aws{}
for _, cur := range cfgs {
rv.Use = rv.Use || cur.Use
if cur.Endpoint != "" {
rv.Endpoint = cur.Endpoint
}
if cur.Service != "" {
rv.Service = cur.Service
}
}
return rv
}
func (a Aws) Transport(ctx context.Context) (http.RoundTripper, error) {
cfg, err := awscfg.LoadDefaultConfig(ctx)
if err != nil {
return nil, fmt.Errorf("error loading aws config: %s", err)
}
if region := os.Getenv("AWS_REGION"); region != "" {
cfg.Region = region
}
signer := v4.NewSigner()
creds, err := cfg.Credentials.Retrieve(ctx)
if err != nil {
return nil, fmt.Errorf("failed to retrieve credentials: %w", err)
}
return signing.NewTransport(signer, creds, a.Service, cfg.Region), nil
}