Skip to content

Commit

Permalink
Migrate AWS metadata to use IMDSv2 (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffinito authored Feb 22, 2022
1 parent 60d375a commit 7fd84cf
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Agent/NewRelic/Agent/Core/Utilization/VendorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public class VendorInfo
private const string DockerName = @"docker";
private const string KubernetesName = @"kubernetes";

private readonly string AwsUri = @"http://169.254.169.254/2016-09-02/dynamic/instance-identity/document";
private readonly string AwsTokenUri = @"http://169.254.169.254/latest/api/token";
private readonly string AwsMetadataUri = @"http://169.254.169.254/latest/dynamic/instance-identity/document";
private const string AwsTokenDurationHeader = "X-aws-ec2-metadata-token-ttl-seconds: 10";

private readonly string AzureUri = @"http://169.254.169.254/metadata/instance/compute?api-version=2017-03-01";
private const string AzureHeader = @"Metadata: true";
Expand Down Expand Up @@ -114,13 +116,19 @@ public IDictionary<string, IVendorModel> GetVendors()

private IVendorModel GetAwsVendorInfo()
{
var responseString = _vendorHttpApiRequestor.CallVendorApi(new Uri(AwsUri), GetMethod, AwsName);
if (responseString != null)
var token = _vendorHttpApiRequestor.CallVendorApi(new Uri(AwsTokenUri), PutMethod, AwsName, new List<string> { AwsTokenDurationHeader });
if (string.IsNullOrWhiteSpace(token))
{
return ParseAwsVendorInfo(responseString);
return null;
}

return null;
var responseString = _vendorHttpApiRequestor.CallVendorApi(new Uri(AwsMetadataUri), GetMethod, AwsName, new List<string> { "X-aws-ec2-metadata-token: " + token });
if (string.IsNullOrWhiteSpace(responseString))
{
return null;
}

return ParseAwsVendorInfo(responseString);
}

public IVendorModel ParseAwsVendorInfo(string json)
Expand Down

0 comments on commit 7fd84cf

Please sign in to comment.