Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate AWS metadata to use IMDSv2 #965

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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