Skip to content

Commit

Permalink
cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Spacca committed Oct 13, 2021
1 parent 5106c05 commit fc33e85
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions libbeat/processors/add_cloud_metadata/provider_aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ import (

"github.com/elastic/beats/v7/libbeat/logp"

"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"

"github.com/elastic/beats/v7/libbeat/common"
s "github.com/elastic/beats/v7/libbeat/common/schema"
c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
)

const ec2InstanceIdentityURI = "/2014-02-25/dynamic/instance-identity/document"
const ec2InstanceIMDSv2TokenValueHeader = "X-aws-ec2-metadata-token"
const ec2InstanceIMDSv2TokenTTLHeader = "X-aws-ec2-metadata-token-ttl-seconds"
const ec2InstanceIMDSv2TokenTTLValue = "21600"
const ec2InstanceIMDSv2TokenURI = "/latest/api/token"
const (
ec2InstanceIdentityURI = "/2014-02-25/dynamic/instance-identity/document"
ec2InstanceIMDSv2TokenValueHeader = "X-aws-ec2-metadata-token"
ec2InstanceIMDSv2TokenTTLHeader = "X-aws-ec2-metadata-token-ttl-seconds"
ec2InstanceIMDSv2TokenTTLValue = "21600"
ec2InstanceIMDSv2TokenURI = "/latest/api/token"
)

// fetches IMDSv2 token, returns empty one on errors
func getIMDSv2Token(c *common.Config) string {
logger := logp.NewLogger("add_cloud_metadata")

config := defaultConfig()
if err := c.Unpack(&config); err != nil {
logger.Warnf("error while getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
logger.Warnf("error when load config for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
}

tlsConfig, err := tlscommon.LoadTLSConfig(config.TLS)
if err != nil {
logger.Warnf("error while getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
logger.Warnf("error when load TLS config for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
}

Expand All @@ -69,7 +70,7 @@ func getIMDSv2Token(c *common.Config) string {

tokenReq, err := http.NewRequest("PUT", fmt.Sprintf("http://%s%s", metadataHost, ec2InstanceIMDSv2TokenURI), nil)
if err != nil {
logger.Warnf("error while getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
logger.Warnf("error when make token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
}

Expand All @@ -82,18 +83,18 @@ func getIMDSv2Token(c *common.Config) string {
}(rsp.Body)

if err != nil {
logger.Warnf("error while getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
logger.Warnf("error when read token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
}

if rsp.StatusCode != http.StatusOK {
logger.Warnf("error while getting IMDSv2 token: http request status %d. No token in the metadata request will be used.", rsp.StatusCode)
logger.Warnf("error when check request status for getting IMDSv2 token: http request status %d. No token in the metadata request will be used.", rsp.StatusCode)
return ""
}

all, err := ioutil.ReadAll(rsp.Body)
if err != nil {
logger.Warnf("error while getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
logger.Warnf("error when reading token request for getting IMDSv2 token: %s. No token in the metadata request will be used.", err)
return ""
}

Expand Down

0 comments on commit fc33e85

Please sign in to comment.