Skip to content

Commit 4a5ffe1

Browse files
committed
tool: Add source for Go SDK cred provider with custom endpoint
When a custom endpoint is configured, AWS SDK for Go sets "Accept: application/json" in request, although not set Accept header by default. This will be used in an integration test to be added in the next commit. Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
1 parent 75375f2 commit 4a5ffe1

File tree

2 files changed

+139
-1
lines changed
  • resources

2 files changed

+139
-1
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
"net/http/httputil"
10+
"os"
11+
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"github.com/aws/aws-sdk-go-v2/credentials/endpointcreds"
14+
)
15+
16+
const mmdsBaseUrl = "http://169.254.169.254"
17+
18+
func main() {
19+
// Get MMDS token
20+
token, err := getMmdsToken()
21+
if err != nil {
22+
log.Fatalf("Failed to get MMDS token: %v", err)
23+
}
24+
25+
// Construct a client
26+
client := &http.Client{
27+
Transport: &tokenInjector{
28+
token: token,
29+
next: &loggingRoundTripper{
30+
next: http.DefaultTransport,
31+
},
32+
},
33+
}
34+
35+
// Construct a credential provider
36+
endpoint := fmt.Sprintf("%s/latest/meta-data/iam/security-credentials/role", mmdsBaseUrl)
37+
provider := endpointcreds.New(endpoint, func(o *endpointcreds.Options) {
38+
o.HTTPClient = client
39+
})
40+
41+
// Load config with the custom provider
42+
cfg, err := config.LoadDefaultConfig(
43+
context.TODO(),
44+
config.WithCredentialsProvider(provider),
45+
)
46+
if err != nil {
47+
log.Fatalf("Unable to load config: %v", err)
48+
}
49+
50+
// Retrieve credentials
51+
cred, err := cfg.Credentials.Retrieve(context.TODO())
52+
if err != nil {
53+
log.Fatalf("Unable to retrieve credentials: %v", err)
54+
}
55+
56+
fmt.Printf("%v,%v,%v\n", cred.AccessKeyID, cred.SecretAccessKey, cred.SessionToken)
57+
}
58+
59+
func getMmdsToken() (string, error) {
60+
client := &http.Client{}
61+
62+
// Construct a request
63+
req, err := http.NewRequest("PUT", mmdsBaseUrl + "/latest/api/token", nil)
64+
if err != nil {
65+
return "", err
66+
}
67+
req.Header.Set("x-aws-ec2-metadata-token-ttl-seconds", "21600")
68+
69+
// Log the request
70+
dumpReq, err := httputil.DumpRequest(req, true)
71+
if err != nil {
72+
return "", err
73+
}
74+
fmt.Fprintf(os.Stderr, "REQUEST:\n%s\n", dumpReq)
75+
76+
// Perform the request
77+
resp, err := client.Do(req)
78+
if err != nil {
79+
return "", err
80+
}
81+
defer resp.Body.Close()
82+
83+
// Log the response
84+
dumpResp, err := httputil.DumpResponse(resp, true)
85+
if err != nil {
86+
return "", err
87+
}
88+
fmt.Fprintf(os.Stderr, "RESPONSE:\n%s\n", dumpResp)
89+
90+
// Check the response status code.
91+
if resp.StatusCode != http.StatusOK {
92+
return "", fmt.Errorf("Status: %s", resp.Status)
93+
}
94+
95+
// Read the body
96+
body, _ := ioutil.ReadAll(resp.Body)
97+
return string(body), nil
98+
}
99+
100+
// tokenInjector adds the token header on every metadata request
101+
type tokenInjector struct {
102+
token string
103+
next http.RoundTripper
104+
}
105+
106+
func (t *tokenInjector) RoundTrip(req *http.Request) (*http.Response, error) {
107+
req.Header.Set("x-aws-ec2-metadata-token", t.token)
108+
return t.next.RoundTrip(req)
109+
}
110+
111+
// logginRoundTripper logs requests and responses
112+
type loggingRoundTripper struct {
113+
next http.RoundTripper
114+
}
115+
116+
func (l *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
117+
// Log the request
118+
dumpReq, err := httputil.DumpRequest(req, true)
119+
if err != nil {
120+
return nil, err
121+
}
122+
fmt.Fprintf(os.Stderr, "REQUEST:\n%s\n", dumpReq)
123+
124+
// Perform the request
125+
resp, err := l.next.RoundTrip(req)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
// Log the response
131+
dumpResp, err := httputil.DumpResponse(resp, true)
132+
if err != nil {
133+
return nil, err
134+
}
135+
fmt.Fprintf(os.Stderr, "RESPONSE:\n%s\n", dumpResp)
136+
137+
return resp, nil
138+
}

resources/rebuild.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function build_al_kernel {
200200
function prepare_and_build_rootfs {
201201
BIN_DIR=overlay/usr/local/bin
202202

203-
SRCS=(init.c fillmem.c fast_page_fault_helper.c readmem.c go_sdk_cred_provider.go)
203+
SRCS=(init.c fillmem.c fast_page_fault_helper.c readmem.c go_sdk_cred_provider.go go_sdk_cred_provider_with_custom_endpoint.go)
204204
if [ $ARCH == "aarch64" ]; then
205205
SRCS+=(devmemread.c)
206206
fi

0 commit comments

Comments
 (0)