github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/apmcloudutil/aws.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package apmcloudutil
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"errors"
    24  	"io/ioutil"
    25  	"net/http"
    26  
    27  	"github.com/waldiirawan/apm-agent-go/v2/model"
    28  )
    29  
    30  const (
    31  	ec2TokenURL    = "http://169.254.169.254/latest/api/token"
    32  	ec2MetadataURL = "http://169.254.169.254/latest/dynamic/instance-identity/document"
    33  )
    34  
    35  // See: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
    36  func getAWSCloudMetadata(ctx context.Context, client *http.Client, out *model.Cloud) error {
    37  	token, err := getAWSToken(ctx, client)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	req, err := http.NewRequest("GET", ec2MetadataURL, nil)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	if token != "" {
    47  		req.Header.Set("X-aws-ec2-metadata-token", token)
    48  	}
    49  
    50  	resp, err := client.Do(req.WithContext(ctx))
    51  	if err != nil {
    52  		return err
    53  	}
    54  	defer resp.Body.Close()
    55  	if resp.StatusCode != http.StatusOK {
    56  		return errors.New(resp.Status)
    57  	}
    58  
    59  	var ec2Metadata struct {
    60  		AccountID        string `json:"accountId"`
    61  		AvailabilityZone string `json:"availabilityZone"`
    62  		Region           string `json:"region"`
    63  		InstanceID       string `json:"instanceId"`
    64  		InstanceType     string `json:"instanceType"`
    65  	}
    66  	if err := json.NewDecoder(resp.Body).Decode(&ec2Metadata); err != nil {
    67  		return err
    68  	}
    69  
    70  	out.Region = ec2Metadata.Region
    71  	out.AvailabilityZone = ec2Metadata.AvailabilityZone
    72  	if ec2Metadata.InstanceID != "" {
    73  		out.Instance = &model.CloudInstance{ID: ec2Metadata.InstanceID}
    74  	}
    75  	if ec2Metadata.InstanceType != "" {
    76  		out.Machine = &model.CloudMachine{Type: ec2Metadata.InstanceType}
    77  	}
    78  	if ec2Metadata.AccountID != "" {
    79  		out.Account = &model.CloudAccount{ID: ec2Metadata.AccountID}
    80  	}
    81  	return nil
    82  }
    83  
    84  func getAWSToken(ctx context.Context, client *http.Client) (string, error) {
    85  	req, err := http.NewRequest("PUT", ec2TokenURL, nil)
    86  	if err != nil {
    87  		return "", err
    88  	}
    89  	req.Header.Set("X-aws-ec2-metadata-token-ttl-seconds", "300")
    90  	resp, err := client.Do(req.WithContext(ctx))
    91  	if err != nil {
    92  		return "", err
    93  	}
    94  	defer resp.Body.Close()
    95  	token, err := ioutil.ReadAll(resp.Body)
    96  	if err != nil {
    97  		return "", err
    98  	}
    99  	return string(token), nil
   100  }