api.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package ec2metadata
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path"
  6. "strings"
  7. "time"
  8. "github.com/aws/aws-sdk-go/aws/awserr"
  9. "github.com/aws/aws-sdk-go/aws/request"
  10. )
  11. // GetMetadata uses the path provided to request information from the EC2
  12. // instance metdata service. The content will be returned as a string, or
  13. // error if the request failed.
  14. func (c *EC2Metadata) GetMetadata(p string) (string, error) {
  15. op := &request.Operation{
  16. Name: "GetMetadata",
  17. HTTPMethod: "GET",
  18. HTTPPath: path.Join("/", "meta-data", p),
  19. }
  20. output := &metadataOutput{}
  21. req := c.NewRequest(op, nil, output)
  22. return output.Content, req.Send()
  23. }
  24. // GetDynamicData uses the path provided to request information from the EC2
  25. // instance metadata service for dynamic data. The content will be returned
  26. // as a string, or error if the request failed.
  27. func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
  28. op := &request.Operation{
  29. Name: "GetDynamicData",
  30. HTTPMethod: "GET",
  31. HTTPPath: path.Join("/", "dynamic", p),
  32. }
  33. output := &metadataOutput{}
  34. req := c.NewRequest(op, nil, output)
  35. return output.Content, req.Send()
  36. }
  37. // GetInstanceIdentityDocument retrieves an identity document describing an
  38. // instance. Error is returned if the request fails or is unable to parse
  39. // the response.
  40. func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) {
  41. resp, err := c.GetDynamicData("instance-identity/document")
  42. if err != nil {
  43. return EC2InstanceIdentityDocument{},
  44. awserr.New("EC2MetadataRequestError",
  45. "failed to get EC2 instance identity document", err)
  46. }
  47. doc := EC2InstanceIdentityDocument{}
  48. if err := json.NewDecoder(strings.NewReader(resp)).Decode(&doc); err != nil {
  49. return EC2InstanceIdentityDocument{},
  50. awserr.New("SerializationError",
  51. "failed to decode EC2 instance identity document", err)
  52. }
  53. return doc, nil
  54. }
  55. // IAMInfo retrieves IAM info from the metadata API
  56. func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
  57. resp, err := c.GetMetadata("iam/info")
  58. if err != nil {
  59. return EC2IAMInfo{},
  60. awserr.New("EC2MetadataRequestError",
  61. "failed to get EC2 IAM info", err)
  62. }
  63. info := EC2IAMInfo{}
  64. if err := json.NewDecoder(strings.NewReader(resp)).Decode(&info); err != nil {
  65. return EC2IAMInfo{},
  66. awserr.New("SerializationError",
  67. "failed to decode EC2 IAM info", err)
  68. }
  69. if info.Code != "Success" {
  70. errMsg := fmt.Sprintf("failed to get EC2 IAM Info (%s)", info.Code)
  71. return EC2IAMInfo{},
  72. awserr.New("EC2MetadataError", errMsg, nil)
  73. }
  74. return info, nil
  75. }
  76. // Region returns the region the instance is running in.
  77. func (c *EC2Metadata) Region() (string, error) {
  78. resp, err := c.GetMetadata("placement/availability-zone")
  79. if err != nil {
  80. return "", err
  81. }
  82. // returns region without the suffix. Eg: us-west-2a becomes us-west-2
  83. return resp[:len(resp)-1], nil
  84. }
  85. // Available returns if the application has access to the EC2 Metadata service.
  86. // Can be used to determine if application is running within an EC2 Instance and
  87. // the metadata service is available.
  88. func (c *EC2Metadata) Available() bool {
  89. if _, err := c.GetMetadata("instance-id"); err != nil {
  90. return false
  91. }
  92. return true
  93. }
  94. // An EC2IAMInfo provides the shape for unmarshalling
  95. // an IAM info from the metadata API
  96. type EC2IAMInfo struct {
  97. Code string
  98. LastUpdated time.Time
  99. InstanceProfileArn string
  100. InstanceProfileID string
  101. }
  102. // An EC2InstanceIdentityDocument provides the shape for unmarshalling
  103. // an instance identity document
  104. type EC2InstanceIdentityDocument struct {
  105. DevpayProductCodes []string `json:"devpayProductCodes"`
  106. AvailabilityZone string `json:"availabilityZone"`
  107. PrivateIP string `json:"privateIp"`
  108. Version string `json:"version"`
  109. Region string `json:"region"`
  110. InstanceID string `json:"instanceId"`
  111. BillingProducts []string `json:"billingProducts"`
  112. InstanceType string `json:"instanceType"`
  113. AccountID string `json:"accountId"`
  114. PendingTime time.Time `json:"pendingTime"`
  115. ImageID string `json:"imageId"`
  116. KernelID string `json:"kernelId"`
  117. RamdiskID string `json:"ramdiskId"`
  118. Architecture string `json:"architecture"`
  119. }