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

#201 AWSEKSResourceDetector - Validate ClusterName/ContainerID independently before adding it to the resource #205

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,15 @@ public class AWSEKSResourceDetector : IResourceDetector
/// <returns>List of key-value pairs of resource attributes.</returns>
public IEnumerable<KeyValuePair<string, object>> Detect()
{
List<KeyValuePair<string, object>> resourceAttributes = null;

try
{
var clusterName = this.GetEKSClusterName(AWSEKSCredentialPath);
var containerId = this.GetEKSContainerId(AWSEKSMetadataFilePath);

if (clusterName == null && containerId == null)
{
return resourceAttributes;
}

resourceAttributes = this.ExtractResourceAttributes(clusterName, containerId);
}
catch (Exception ex)
var credentials = this.GetEKSCredentials(AWSEKSCredentialPath);
if (credentials == null || !this.IsEKSProcess(credentials))
{
AWSXRayEventSource.Log.ResourceAttributesExtractException(nameof(AWSEKSResourceDetector), ex);
return null;
}

return resourceAttributes;
return this.ExtractResourceAttributes(
this.GetEKSClusterName(credentials),
this.GetEKSContainerId(AWSEKSMetadataFilePath));
}

internal List<KeyValuePair<string, object>> ExtractResourceAttributes(string clusterName, string containerId)
Expand All @@ -68,73 +57,103 @@ internal List<KeyValuePair<string, object>> ExtractResourceAttributes(string clu
{
new KeyValuePair<string, object>(AWSSemanticConventions.AttributeCloudProvider, "aws"),
new KeyValuePair<string, object>(AWSSemanticConventions.AttributeCloudPlatform, "aws_eks"),
new KeyValuePair<string, object>(AWSSemanticConventions.AttributeK8SClusterName, clusterName),
new KeyValuePair<string, object>(AWSSemanticConventions.AttributeContainerID, containerId),
};

if (!string.IsNullOrEmpty(clusterName))
{
resourceAttributes.Add(new KeyValuePair<string, object>(AWSSemanticConventions.AttributeK8SClusterName, clusterName));
}

if (!string.IsNullOrEmpty(containerId))
{
resourceAttributes.Add(new KeyValuePair<string, object>(AWSSemanticConventions.AttributeContainerID, containerId));
}

return resourceAttributes;
}

internal string GetEKSCredentials(string path)
{
StringBuilder stringBuilder = new StringBuilder();

using (var streamReader = ResourceDetectorUtils.GetStreamReader(path))
try
{
while (!streamReader.EndOfStream)
StringBuilder stringBuilder = new StringBuilder();

using (var streamReader = ResourceDetectorUtils.GetStreamReader(path))
{
stringBuilder.Append(streamReader.ReadLine().Trim());
while (!streamReader.EndOfStream)
{
stringBuilder.Append(streamReader.ReadLine().Trim());
}
}

return "Bearer " + stringBuilder.ToString();
}
catch (Exception ex)
{
AWSXRayEventSource.Log.ResourceAttributesExtractException($"{nameof(AWSEKSResourceDetector)} : Failed to load client token", ex);
}

return "Bearer " + stringBuilder.ToString();
return null;
}

internal string GetEKSContainerId(string path)
{
string containerId = null;

using (var streamReader = ResourceDetectorUtils.GetStreamReader(path))
try
{
while (!streamReader.EndOfStream)
using (var streamReader = ResourceDetectorUtils.GetStreamReader(path))
{
var trimmedLine = streamReader.ReadLine().Trim();
if (trimmedLine.Length > 64)
while (!streamReader.EndOfStream)
{
containerId = trimmedLine.Substring(trimmedLine.Length - 64);
return containerId;
var trimmedLine = streamReader.ReadLine().Trim();
if (trimmedLine.Length > 64)
{
return trimmedLine.Substring(trimmedLine.Length - 64);
}
}
}
}
catch (Exception ex)
{
AWSXRayEventSource.Log.ResourceAttributesExtractException($"{nameof(AWSEKSResourceDetector)} : Failed to get Container Id", ex);
}

return containerId;
return null;
}

internal AWSEKSClusterInformationModel DeserializeResponse(string response)
{
return ResourceDetectorUtils.DeserializeFromString<AWSEKSClusterInformationModel>(response);
}

private string GetEKSClusterName(string path)
private string GetEKSClusterName(string credentials)
{
var credentials = this.GetEKSCredentials(path);

if (!this.IsEKSProcess(credentials))
try
{
return null;
var clusterInfo = this.GetEKSClusterInfo(credentials);
return this.DeserializeResponse(clusterInfo)?.Data?.ClusterName;
}
catch (Exception ex)
{
AWSXRayEventSource.Log.ResourceAttributesExtractException($"{nameof(AWSEKSResourceDetector)} : Failed to get cluster information", ex);
}

var clusterInfo = this.GetEKSClusterInfo(credentials);
var clusterInfoObject = this.DeserializeResponse(clusterInfo);

return clusterInfoObject.Data?.ClusterName;
return null;
}

private bool IsEKSProcess(string credentials)
{
var httpClientHandler = this.CreateHttpClientHandler();
var awsAuth = ResourceDetectorUtils.SendOutRequest(AWSAuthUrl, "GET", new KeyValuePair<string, string>("Authorization", credentials), httpClientHandler).Result;
return string.IsNullOrEmpty(awsAuth);
string awsAuth = null;
try
{
var httpClientHandler = this.CreateHttpClientHandler();
awsAuth = ResourceDetectorUtils.SendOutRequest(AWSAuthUrl, "GET", new KeyValuePair<string, string>("Authorization", credentials), httpClientHandler).Result;
}
catch (Exception ex)
{
AWSXRayEventSource.Log.ResourceAttributesExtractException($"{nameof(AWSEKSResourceDetector)} : Failed to get EKS information", ex);
}

return !string.IsNullOrEmpty(awsAuth);
}

private string GetEKSClusterInfo(string credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,43 @@ public void TestExtractResourceAttributes()

var resourceAttributes = eksResourceDetector.ExtractResourceAttributes(clusterName, containerId).ToDictionary(x => x.Key, x => x.Value);

Assert.Equal(4, resourceAttributes.Count);
Assert.Equal("aws", resourceAttributes[AWSSemanticConventions.AttributeCloudProvider]);
Assert.Equal("aws_eks", resourceAttributes[AWSSemanticConventions.AttributeCloudPlatform]);
Assert.Equal("Test cluster name", resourceAttributes[AWSSemanticConventions.AttributeK8SClusterName]);
Assert.Equal("Test container id", resourceAttributes[AWSSemanticConventions.AttributeContainerID]);
}

[Fact]
public void TestExtractResourceAttributesWithEmptyClusterName()
{
var eksResourceDetector = new AWSEKSResourceDetector();
var containerId = "Test container id";

var resourceAttributes = eksResourceDetector.ExtractResourceAttributes(string.Empty, containerId).ToDictionary(x => x.Key, x => x.Value);

// Validate the count of resourceAttributes -> Excluding cluster name, there will be only three resourceAttributes
Assert.Equal(3, resourceAttributes.Count);
Assert.Equal("aws", resourceAttributes[AWSSemanticConventions.AttributeCloudProvider]);
Assert.Equal("aws_eks", resourceAttributes[AWSSemanticConventions.AttributeCloudPlatform]);
Assert.Equal("Test container id", resourceAttributes[AWSSemanticConventions.AttributeContainerID]);
}

[Fact]
public void TestExtractResourceAttributesWithEmptyContainerId()
{
var eksResourceDetector = new AWSEKSResourceDetector();
var clusterName = "Test cluster name";

var resourceAttributes = eksResourceDetector.ExtractResourceAttributes(clusterName, string.Empty).ToDictionary(x => x.Key, x => x.Value);

// Validate the count of resourceAttributes -> Excluding container id, there will be only three resourceAttributes
Assert.Equal(3, resourceAttributes.Count);
Assert.Equal("aws", resourceAttributes[AWSSemanticConventions.AttributeCloudProvider]);
Assert.Equal("aws_eks", resourceAttributes[AWSSemanticConventions.AttributeCloudPlatform]);
Assert.Equal("Test cluster name", resourceAttributes[AWSSemanticConventions.AttributeK8SClusterName]);
}

[Fact]
public void TestGetEKSCredentials()
{
Expand Down