Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

ec2rolecreds: Add method for getting instance identity doc - #590

Merged
jasdel merged 1 commit into
aws:masterfrom
radeksimko:f-ec2-metadata-verification
Mar 16, 2016
Merged

jasdel merged 1 commit into
aws:masterfrom
radeksimko:f-ec2-metadata-verification

Conversation

@radeksimko

Copy link
Copy Markdown
Contributor

This is an implementation of a very basic check for "authenticity" of an EC2 metadata API as discussed in #543

This is how I'd expect people to use it:

func getCreds(key, secret, token, profile, credsfile string) *awsCredentials.Credentials {
    providers := []awsCredentials.Provider{
        &awsCredentials.StaticProvider{Value: awsCredentials.Value{
            AccessKeyID:     key,
            SecretAccessKey: secret,
            SessionToken:    token,
        }},
        &awsCredentials.EnvProvider{},
        &awsCredentials.SharedCredentialsProvider{
            Filename: credsfile,
            Profile:  profile,
        },
    }

    cfg := &aws.Config{}
    endpoint := os.Getenv("AWS_METADATA_ENDPOINT")
    if endpoint != "" {
        cfg.Endpoint = aws.String(getMetadataEndpoint())
    }

    ec2Provider := &ec2rolecreds.EC2RoleProvider{
        Client: ec2metadata.New(session.New(cfg)),
    }

    // Unless custom endpoint was given, verify it
    if endpoint == "" {
        _, err := ec2Provider.GetIdentityDocument()
        if err == nil {
            providers = append(providers, ec2Provider)
        } else {
            log.Printf("Failed to get instance identity document: %s", err)
        }
    }

    return awsCredentials.NewChainCredentials(providers)
}

Is there any standardised name for the ENV variable (AWS_METADATA_ENDPOINT)?

The PKCS7 signature verification can be a next iteration in a separate PR - I tried to keep this simple for the beginning.

@jasdel

jasdel commented Mar 14, 2016

Copy link
Copy Markdown
Contributor

Thanks for submitting this PR @radeksimko I'll review the change, and check if there is a standard for the metadata endpoint.

if err != nil {
return EC2InstanceIdentityDocument{},
awserr.New("EC2RoleRequestError",
fmt.Sprint("failed to get EC2 instance identity document"), err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably can drop the fmt.Sprintf since not needed. Here and the error case below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, that's useless there, I will remove it.

@jasdel jasdel added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. Review Needed and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 14, 2016
@jasdel jasdel self-assigned this Mar 15, 2016
@radeksimko

Copy link
Copy Markdown
Contributor Author

Updated according to feedback.

Also when I'm now looking into ec2metadata, I'm thinking there's already one handy function which may be good enough for the use-case we discussed in #543

Would something like this be considered "stable" or recommended way to verify EC2 metadata endpoint?

metadataSvc := ec2metadata.New(session.New(cfg))
if metadataSvc.Available() {
    // it is verified
}

which doesn't mean the time implementing this PR was wasted as I believe it may be useful anyway, but probably not in the context I thought it would be.

Comment thread aws/ec2metadata/api.go
// GetDynamicData uses the path provided to request
func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
op := &request.Operation{
Name: "GetMetadata",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Name's value should be GetDynamicData in this case. Its not directly used in the HTTP request, but is useful for logging.

@jasdel

jasdel commented Mar 15, 2016

Copy link
Copy Markdown
Contributor

Good point, I think its fair to expect Available's check for instance-id is valid and stable. If this changes in the future via a version rev Available could be updated to correspond to that new method. But In either case the way available checks for instance-id is expected to be stable.

Adding the retrieval for the ident doc is a good addition too :) so not a waste of time.

When initially created I was thinking you were looking for a more detailed way of verifying the ec2's host's identity. I should of mentioned available. But i think your right Available probably is enough to verify ec2 instance in this usecase.

@jasdel

jasdel commented Mar 15, 2016

Copy link
Copy Markdown
Contributor

The change looks good, thanks for taking the time to investigate this and implement the PR. Even though Available also exists, this will be a helpful update.

jasdel added a commit that referenced this pull request Mar 16, 2016
ec2rolecreds: Add method for getting instance identity doc
@jasdel
jasdel merged commit 9b3f54c into aws:master Mar 16, 2016
@radeksimko
radeksimko deleted the f-ec2-metadata-verification branch March 16, 2016 07:02
jasdel added a commit that referenced this pull request Mar 18, 2016
xibz pushed a commit to xibz/aws-sdk-go that referenced this pull request Apr 18, 2016
xibz pushed a commit to xibz/aws-sdk-go that referenced this pull request Apr 18, 2016
skotambkar pushed a commit to skotambkar/aws-sdk-go that referenced this pull request May 20, 2021
Fix for deserialization code generation to use time.ParseDateTime.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants