Skip to content
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

Implement Amazon's IMDSv2 #249

Merged
merged 4 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ChangeLog

## Unreleased

* To keep up with the latest security protocols implemented by Amazon Web
Services, the agent now uses AWS IMDSv2 to find utilization data.

## 3.9.0

### Changes
Expand Down
40 changes: 36 additions & 4 deletions v3/internal/utilization/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
)

const (
awsHostname = "169.254.169.254"
awsEndpointPath = "/2016-09-02/dynamic/instance-identity/document"
awsEndpoint = "http://" + awsHostname + awsEndpointPath
awsHostname = "169.254.169.254"
awsEndpointPath = "/2016-09-02/dynamic/instance-identity/document"
awsTokenEndpointPath = "/latest/api/token"
awsEndpoint = "http://" + awsHostname + awsEndpointPath
awsTokenEndpoint = "http://" + awsHostname + awsTokenEndpointPath
awsTokenTTL = "60" // seconds this AWS utiliation session will last
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
awsTokenTTL = "60" // seconds this AWS utiliation session will last
awsTokenTTL = "60" // seconds this AWS utilization session will last

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

)

type aws struct {
Expand Down Expand Up @@ -44,6 +47,24 @@ func (e unexpectedAWSErr) Error() string {
return fmt.Sprintf("unexpected AWS error: %v", e.e)
}

// getAWSToken attempts to get the IMDSv2 token within the providerTimeout set
// provider.go.
func getAWSToken(client *http.Client) (token string, err error) {
request, err := http.NewRequest("PUT", awsTokenEndpoint, nil)
request.Header.Add("X-aws-ec2-metadata-token-ttl-seconds", awsTokenTTL)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}

return string(body), nil
}

func getAWS(client *http.Client) (ret *aws, err error) {
// In some cases, 3rd party providers might block requests to metadata
// endpoints in such a way that causes a panic in the underlying
Expand All @@ -56,7 +77,18 @@ func getAWS(client *http.Client) (ret *aws, err error) {
}
}()

response, err := client.Get(awsEndpoint)
// AWS' IMDSv2 requires us to get a token before requesting metadata.
awsToken, err := getAWSToken(client)
if err != nil {
ret = nil
err = unexpectedAWSErr{e: fmt.Errorf("error contacting AWS IMDSv2 token endpoint, %v", err)}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return here, if an error happened, right?

Suggested change
ret = nil
err = unexpectedAWSErr{e: fmt.Errorf("error contacting AWS IMDSv2 token endpoint, %v", err)}
return nil, unexpectedAWSErr{e: fmt.Errorf("error contacting AWS IMDSv2 token endpoint, %v", err)}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, maybe you could create a request to awsEndpoint before calling getAWSToken and add the token to the request's headers only if err != nil. This would make IMDSv2 optional in case someone wants to stick with IMDSv1 (if that is even possible).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing this! After thinking harder about it, we shouldn't throw an unExpectedAWSErr at all, we're expecting a timeout unless they're in AWS. Fixing this and the test.

A more thorough implementation would raise errors on other HTTP errors, but I'm deferring that work in the interest of time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the extra request to awsEndpoint, I'd rather avoid making three calls to AWS and increasing the overhead by another 0.5 second on startup. As of now, IMDSv2 endpoints are always available -- the only control the customer has is to turn IMDSv1 off to help their security posture.

}

//Add the header to the outbound request.
request, err := http.NewRequest("GET", awsEndpoint, nil)
request.Header.Add("X-aws-ec2-metadata-token", awsToken)

response, err := client.Do(request)
if err != nil {
// No unexpectedAWSErr here: A timeout is usually going to
// happen.
Expand Down
4 changes: 2 additions & 2 deletions v3/internal/utilization/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

// Constants from the spec.
const (
maxFieldValueSize = 255 // The maximum value size, in bytes.
providerTimeout = 1 * time.Second // The maximum time a HTTP provider may block.
maxFieldValueSize = 255 // The maximum value size, in bytes.
providerTimeout = 500 * time.Millisecond // The maximum time a HTTP provider may block.
lookupAddrTimeout = 500 * time.Millisecond
)

Expand Down