|
@@ -8,6 +8,7 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/aws/aws-sdk-go/aws"
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
|
|
"github.com/aws/aws-sdk-go/internal/sdkuri"
|
|
@@ -15,7 +16,7 @@ import (
|
|
|
|
|
|
// getToken uses the duration to return a token for EC2 metadata service,
|
|
|
// or an error if the request failed.
|
|
|
-func (c *EC2Metadata) getToken(duration time.Duration) (tokenOutput, error) {
|
|
|
+func (c *EC2Metadata) getToken(ctx aws.Context, duration time.Duration) (tokenOutput, error) {
|
|
|
op := &request.Operation{
|
|
|
Name: "GetToken",
|
|
|
HTTPMethod: "PUT",
|
|
@@ -24,6 +25,7 @@ func (c *EC2Metadata) getToken(duration time.Duration) (tokenOutput, error) {
|
|
|
|
|
|
var output tokenOutput
|
|
|
req := c.NewRequest(op, nil, &output)
|
|
|
+ req.SetContext(ctx)
|
|
|
|
|
|
// remove the fetch token handler from the request handlers to avoid infinite recursion
|
|
|
req.Handlers.Sign.RemoveByName(fetchTokenHandlerName)
|
|
@@ -50,6 +52,13 @@ func (c *EC2Metadata) getToken(duration time.Duration) (tokenOutput, error) {
|
|
|
// instance metadata service. The content will be returned as a string, or
|
|
|
// error if the request failed.
|
|
|
func (c *EC2Metadata) GetMetadata(p string) (string, error) {
|
|
|
+ return c.GetMetadataWithContext(aws.BackgroundContext(), p)
|
|
|
+}
|
|
|
+
|
|
|
+// GetMetadataWithContext uses the path provided to request information from the EC2
|
|
|
+// instance metadata service. The content will be returned as a string, or
|
|
|
+// error if the request failed.
|
|
|
+func (c *EC2Metadata) GetMetadataWithContext(ctx aws.Context, p string) (string, error) {
|
|
|
op := &request.Operation{
|
|
|
Name: "GetMetadata",
|
|
|
HTTPMethod: "GET",
|
|
@@ -59,6 +68,8 @@ func (c *EC2Metadata) GetMetadata(p string) (string, error) {
|
|
|
|
|
|
req := c.NewRequest(op, nil, output)
|
|
|
|
|
|
+ req.SetContext(ctx)
|
|
|
+
|
|
|
err := req.Send()
|
|
|
return output.Content, err
|
|
|
}
|
|
@@ -67,6 +78,13 @@ func (c *EC2Metadata) GetMetadata(p string) (string, error) {
|
|
|
// there is no user-data setup for the EC2 instance a "NotFoundError" error
|
|
|
// code will be returned.
|
|
|
func (c *EC2Metadata) GetUserData() (string, error) {
|
|
|
+ return c.GetUserDataWithContext(aws.BackgroundContext())
|
|
|
+}
|
|
|
+
|
|
|
+// GetUserDataWithContext returns the userdata that was configured for the service. If
|
|
|
+// there is no user-data setup for the EC2 instance a "NotFoundError" error
|
|
|
+// code will be returned.
|
|
|
+func (c *EC2Metadata) GetUserDataWithContext(ctx aws.Context) (string, error) {
|
|
|
op := &request.Operation{
|
|
|
Name: "GetUserData",
|
|
|
HTTPMethod: "GET",
|
|
@@ -75,6 +93,7 @@ func (c *EC2Metadata) GetUserData() (string, error) {
|
|
|
|
|
|
output := &metadataOutput{}
|
|
|
req := c.NewRequest(op, nil, output)
|
|
|
+ req.SetContext(ctx)
|
|
|
|
|
|
err := req.Send()
|
|
|
return output.Content, err
|
|
@@ -84,6 +103,13 @@ func (c *EC2Metadata) GetUserData() (string, error) {
|
|
|
// instance metadata service for dynamic data. The content will be returned
|
|
|
// as a string, or error if the request failed.
|
|
|
func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
|
|
|
+ return c.GetDynamicDataWithContext(aws.BackgroundContext(), p)
|
|
|
+}
|
|
|
+
|
|
|
+// GetDynamicDataWithContext uses the path provided to request information from the EC2
|
|
|
+// instance metadata service for dynamic data. The content will be returned
|
|
|
+// as a string, or error if the request failed.
|
|
|
+func (c *EC2Metadata) GetDynamicDataWithContext(ctx aws.Context, p string) (string, error) {
|
|
|
op := &request.Operation{
|
|
|
Name: "GetDynamicData",
|
|
|
HTTPMethod: "GET",
|
|
@@ -92,6 +118,7 @@ func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
|
|
|
|
|
|
output := &metadataOutput{}
|
|
|
req := c.NewRequest(op, nil, output)
|
|
|
+ req.SetContext(ctx)
|
|
|
|
|
|
err := req.Send()
|
|
|
return output.Content, err
|
|
@@ -101,7 +128,14 @@ func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
|
|
|
// instance. Error is returned if the request fails or is unable to parse
|
|
|
// the response.
|
|
|
func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) {
|
|
|
- resp, err := c.GetDynamicData("instance-identity/document")
|
|
|
+ return c.GetInstanceIdentityDocumentWithContext(aws.BackgroundContext())
|
|
|
+}
|
|
|
+
|
|
|
+// GetInstanceIdentityDocumentWithContext retrieves an identity document describing an
|
|
|
+// instance. Error is returned if the request fails or is unable to parse
|
|
|
+// the response.
|
|
|
+func (c *EC2Metadata) GetInstanceIdentityDocumentWithContext(ctx aws.Context) (EC2InstanceIdentityDocument, error) {
|
|
|
+ resp, err := c.GetDynamicDataWithContext(ctx, "instance-identity/document")
|
|
|
if err != nil {
|
|
|
return EC2InstanceIdentityDocument{},
|
|
|
awserr.New("EC2MetadataRequestError",
|
|
@@ -120,7 +154,12 @@ func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument
|
|
|
|
|
|
// IAMInfo retrieves IAM info from the metadata API
|
|
|
func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
|
|
|
- resp, err := c.GetMetadata("iam/info")
|
|
|
+ return c.IAMInfoWithContext(aws.BackgroundContext())
|
|
|
+}
|
|
|
+
|
|
|
+// IAMInfoWithContext retrieves IAM info from the metadata API
|
|
|
+func (c *EC2Metadata) IAMInfoWithContext(ctx aws.Context) (EC2IAMInfo, error) {
|
|
|
+ resp, err := c.GetMetadataWithContext(ctx, "iam/info")
|
|
|
if err != nil {
|
|
|
return EC2IAMInfo{},
|
|
|
awserr.New("EC2MetadataRequestError",
|
|
@@ -145,7 +184,12 @@ func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
|
|
|
|
|
|
// Region returns the region the instance is running in.
|
|
|
func (c *EC2Metadata) Region() (string, error) {
|
|
|
- ec2InstanceIdentityDocument, err := c.GetInstanceIdentityDocument()
|
|
|
+ return c.RegionWithContext(aws.BackgroundContext())
|
|
|
+}
|
|
|
+
|
|
|
+// RegionWithContext returns the region the instance is running in.
|
|
|
+func (c *EC2Metadata) RegionWithContext(ctx aws.Context) (string, error) {
|
|
|
+ ec2InstanceIdentityDocument, err := c.GetInstanceIdentityDocumentWithContext(ctx)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
@@ -162,7 +206,14 @@ func (c *EC2Metadata) Region() (string, error) {
|
|
|
// Can be used to determine if application is running within an EC2 Instance and
|
|
|
// the metadata service is available.
|
|
|
func (c *EC2Metadata) Available() bool {
|
|
|
- if _, err := c.GetMetadata("instance-id"); err != nil {
|
|
|
+ return c.AvailableWithContext(aws.BackgroundContext())
|
|
|
+}
|
|
|
+
|
|
|
+// AvailableWithContext returns if the application has access to the EC2 Metadata service.
|
|
|
+// Can be used to determine if application is running within an EC2 Instance and
|
|
|
+// the metadata service is available.
|
|
|
+func (c *EC2Metadata) AvailableWithContext(ctx aws.Context) bool {
|
|
|
+ if _, err := c.GetMetadataWithContext(ctx, "instance-id"); err != nil {
|
|
|
return false
|
|
|
}
|
|
|
|