-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(mmds): Set token TTL header in response to PUT /latest/api/token #5328
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
Merged
zulinx86
merged 12 commits into
firecracker-microvm:main
from
zulinx86:mmds_ttl_header_in_response
Jul 25, 2025
+313
−51
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
81f0d26
fix(mmds): Set token TTL header in response to PUT /latest/api/token
zulinx86 186cc59
test: Enable debug log in test for AWS SDK for Python
zulinx86 1e3e11e
fix(tool): Do NOT install AWS CLI v2
zulinx86 ef1e868
refactor(tool): Delete binaries in prepare_and_build_rootfs()
zulinx86 2f3f46d
refactor(tool): Delete unused variable declaration
zulinx86 018460f
tool: Install Go to build Go projects
zulinx86 69be282
refactor(tool): Prelimiary refactor to support go compilation
zulinx86 4b35a14
tool: Support Go compilation
zulinx86 215e542
tool: Add source of Go SDK's credential provider
zulinx86 a37ea35
test: Add test for Go SDK credential provider
zulinx86 fd37440
tool: Add source for Go SDK cred provider with custom endpoint
zulinx86 24404fb
test: Add test for Go SDK credential provider with custom endpoint
zulinx86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
resources/overlay/usr/local/bin/go_sdk_cred_provider.go/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
) | ||
|
||
func main() { | ||
cfg, err := config.LoadDefaultConfig( | ||
context.TODO(), | ||
config.WithClientLogMode( | ||
aws.LogSigning| | ||
aws.LogRetries| | ||
aws.LogRequest| | ||
aws.LogRequestWithBody| | ||
aws.LogResponse| | ||
aws.LogResponseWithBody, | ||
), | ||
) | ||
if err != nil { | ||
log.Fatalf("Unable to load config: %v", err) | ||
} | ||
|
||
cred, err := cfg.Credentials.Retrieve(context.TODO()) | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve credentials: %v", err) | ||
} | ||
|
||
fmt.Printf("%v,%v,%v\n", cred.AccessKeyID, cred.SecretAccessKey, cred.SessionToken) | ||
} |
138 changes: 138 additions & 0 deletions
138
resources/overlay/usr/local/bin/go_sdk_cred_provider_with_custom_endpoint.go/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials/endpointcreds" | ||
) | ||
|
||
const mmdsBaseUrl = "http://169.254.169.254" | ||
|
||
func main() { | ||
// Get MMDS token | ||
token, err := getMmdsToken() | ||
if err != nil { | ||
log.Fatalf("Failed to get MMDS token: %v", err) | ||
} | ||
|
||
// Construct a client | ||
client := &http.Client{ | ||
Transport: &tokenInjector{ | ||
token: token, | ||
next: &loggingRoundTripper{ | ||
next: http.DefaultTransport, | ||
}, | ||
}, | ||
} | ||
|
||
// Construct a credential provider | ||
endpoint := fmt.Sprintf("%s/latest/meta-data/iam/security-credentials/role", mmdsBaseUrl) | ||
provider := endpointcreds.New(endpoint, func(o *endpointcreds.Options) { | ||
o.HTTPClient = client | ||
}) | ||
|
||
// Load config with the custom provider | ||
cfg, err := config.LoadDefaultConfig( | ||
context.TODO(), | ||
config.WithCredentialsProvider(provider), | ||
) | ||
if err != nil { | ||
log.Fatalf("Unable to load config: %v", err) | ||
} | ||
|
||
// Retrieve credentials | ||
cred, err := cfg.Credentials.Retrieve(context.TODO()) | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve credentials: %v", err) | ||
} | ||
|
||
fmt.Printf("%v,%v,%v\n", cred.AccessKeyID, cred.SecretAccessKey, cred.SessionToken) | ||
} | ||
|
||
func getMmdsToken() (string, error) { | ||
client := &http.Client{} | ||
|
||
// Construct a request | ||
req, err := http.NewRequest("PUT", mmdsBaseUrl + "/latest/api/token", nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
req.Header.Set("x-aws-ec2-metadata-token-ttl-seconds", "21600") | ||
|
||
// Log the request | ||
dumpReq, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Fprintf(os.Stderr, "REQUEST:\n%s\n", dumpReq) | ||
|
||
// Perform the request | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Log the response | ||
dumpResp, err := httputil.DumpResponse(resp, true) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Fprintf(os.Stderr, "RESPONSE:\n%s\n", dumpResp) | ||
|
||
// Check the response status code. | ||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("Status: %s", resp.Status) | ||
} | ||
|
||
// Read the body | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
return string(body), nil | ||
} | ||
|
||
// tokenInjector adds the token header on every metadata request | ||
type tokenInjector struct { | ||
token string | ||
next http.RoundTripper | ||
} | ||
|
||
func (t *tokenInjector) RoundTrip(req *http.Request) (*http.Response, error) { | ||
req.Header.Set("x-aws-ec2-metadata-token", t.token) | ||
return t.next.RoundTrip(req) | ||
} | ||
|
||
// logginRoundTripper logs requests and responses | ||
type loggingRoundTripper struct { | ||
next http.RoundTripper | ||
} | ||
|
||
func (l *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
// Log the request | ||
dumpReq, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Fprintf(os.Stderr, "REQUEST:\n%s\n", dumpReq) | ||
|
||
// Perform the request | ||
resp, err := l.next.RoundTrip(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Log the response | ||
dumpResp, err := httputil.DumpResponse(resp, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Fprintf(os.Stderr, "RESPONSE:\n%s\n", dumpResp) | ||
|
||
return resp, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.