From af0621eefca3adc0bddd10e9871da5e22a8f5d2c Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Sat, 18 Mar 2023 02:03:48 -0400 Subject: [PATCH 01/12] [Extensions.AWSXRay] Add net6.0 TFM Add a .NET 6.0 build target to improve the transitive dependency graph for consumers targeting newer runtimes. * Add a netcoreapp3.1 test target to exercise the library's netstandard2.0 build. * In the tests project, suppress the build warning generated by System.Diagnostics.DiagnosticSource 7.0.0 when targeting out-of-support frameworks. --- .../.publicApi/net6.0/PublicAPI.Shipped.txt | 0 .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 27 +++++++++++++++++++ ...elemetry.Contrib.Extensions.AWSXRay.csproj | 2 +- ...ry.Contrib.Extensions.AWSXRay.Tests.csproj | 7 ++++- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Shipped.txt create mode 100644 src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Shipped.txt b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Shipped.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt new file mode 100644 index 0000000000..14d2df82af --- /dev/null +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -0,0 +1,27 @@ +#nullable enable +OpenTelemetry.Contrib.Extensions.AWSXRay.AWSXRayIdGenerator +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector.AWSEBSResourceDetector() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector.AWSEC2ResourceDetector() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector.AWSECSResourceDetector() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector.AWSEKSResourceDetector() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector.AWSLambdaResourceDetector() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.ResourceDetectorUtils +OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.ResourceDetectorUtils.ResourceDetectorUtils() -> void +OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator +OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.AWSXRayPropagator() -> void +OpenTelemetry.Trace.TracerProviderBuilderExtensions +override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Extract(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func!>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext +override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet! +override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Inject(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action! setter) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder! builder, OpenTelemetry.Trace.Sampler! sampler) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj index 7dbba22c44..3e792b508a 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj @@ -1,7 +1,7 @@ - netstandard2.0;$(NetFrameworkMinimumSupportedVersion) + net6.0;netstandard2.0;$(NetFrameworkMinimumSupportedVersion) OpenTelemetry extensions for AWS X-Ray. Extensions.AWSXRay- enable diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj index 68fa51593c..69fccb4e99 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj @@ -1,10 +1,15 @@ - net7.0;net6.0 + net7.0;net6.0;netcoreapp3.1 $(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion) + + + true + + From 6837b429e315c980493adcf609e0b397a1c85e52 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Sat, 18 Mar 2023 12:28:41 -0400 Subject: [PATCH 02/12] [Extensions.AWSXray] Fix nullable warnings * Previously, constructing the Resource in AWSLambdaResourceDetector would throw should any of the environment variables be null. Make that condition explicit by pro-actively throwing a descriptive exception. * Change the type of ValidationCallback to match HttpClientHandler's callback delegate type, which clarifies that the certificate is an instance of X509Certificate2. * Check that 'cert' and 'chain' are non-null before performing validation steps that require them. --- .../Resources/AWSECSResourceDetector.cs | 2 +- .../Resources/AWSEKSResourceDetector.cs | 4 +- .../Resources/AWSLambdaResourceDetector.cs | 21 ++--- .../Resources/Http/Handler.cs | 4 +- .../ServerCertificateValidationProvider.cs | 18 +++-- ...TestServerCertificateValidationProvider.cs | 34 ++++++++ .../TestAWSLambdaResourceDetector.cs | 79 +++++++++++++++++++ 7 files changed, 136 insertions(+), 26 deletions(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSECSResourceDetector.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSECSResourceDetector.cs index 0379246622..5da60e468c 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSECSResourceDetector.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSECSResourceDetector.cs @@ -194,7 +194,7 @@ string type when string.Equals("fargate", type, StringComparison.OrdinalIgnoreCa { while (!streamReader.EndOfStream) { - var trimmedLine = streamReader.ReadLine().Trim(); + var trimmedLine = streamReader.ReadLine()!.Trim(); if (trimmedLine.Length > 64) { containerId = trimmedLine.Substring(trimmedLine.Length - 64); diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSEKSResourceDetector.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSEKSResourceDetector.cs index a03bc0db02..53a0dd3611 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSEKSResourceDetector.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSEKSResourceDetector.cs @@ -86,7 +86,7 @@ internal static List> ExtractResourceAttributes(str { while (!streamReader.EndOfStream) { - stringBuilder.Append(streamReader.ReadLine().Trim()); + stringBuilder.Append(streamReader.ReadLine()!.Trim()); } } @@ -110,7 +110,7 @@ internal static List> ExtractResourceAttributes(str { while (!streamReader.EndOfStream) { - var trimmedLine = streamReader.ReadLine().Trim(); + var trimmedLine = streamReader.ReadLine()!.Trim(); if (trimmedLine.Length > 64) { return trimmedLine.Substring(trimmedLine.Length - 64); diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSLambdaResourceDetector.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSLambdaResourceDetector.cs index 5db498f547..d7b9fd5285 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSLambdaResourceDetector.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/AWSLambdaResourceDetector.cs @@ -53,26 +53,17 @@ internal static List> ExtractResourceAttributes() { new KeyValuePair(AWSSemanticConventions.AttributeCloudProvider, "aws"), new KeyValuePair(AWSSemanticConventions.AttributeCloudPlatform, "aws_lambda"), - new KeyValuePair(AWSSemanticConventions.AttributeCloudRegion, GetAWSRegion()), - new KeyValuePair(AWSSemanticConventions.AttributeFaasName, GetFunctionName()), - new KeyValuePair(AWSSemanticConventions.AttributeFaasVersion, GetFunctionVersion()), + new KeyValuePair(AWSSemanticConventions.AttributeCloudRegion, GetLambdaEnvironmentVariable(AWSLambdaRegion)), + new KeyValuePair(AWSSemanticConventions.AttributeFaasName, GetLambdaEnvironmentVariable(AWSLambdaFunctionName)), + new KeyValuePair(AWSSemanticConventions.AttributeFaasVersion, GetLambdaEnvironmentVariable(AWSLambdaFunctionVersion)), }; return resourceAttributes; } - private static string GetAWSRegion() + private static string GetLambdaEnvironmentVariable(string variable) { - return Environment.GetEnvironmentVariable(AWSLambdaRegion); - } - - private static string GetFunctionName() - { - return Environment.GetEnvironmentVariable(AWSLambdaFunctionName); - } - - private static string GetFunctionVersion() - { - return Environment.GetEnvironmentVariable(AWSLambdaFunctionVersion); + return Environment.GetEnvironmentVariable(variable) + ?? throw new InvalidOperationException($"Not running in AWS Lambda (missing environment variable '{variable}')"); } } diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/Handler.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/Handler.cs index 5ee545e2dd..8c8960a9fb 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/Handler.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/Handler.cs @@ -40,9 +40,7 @@ internal class Handler } var clientHandler = new HttpClientHandler(); - clientHandler.ServerCertificateCustomValidationCallback = - (sender, x509Certificate2, x509Chain, sslPolicyErrors) => - serverCertificateValidationProvider.ValidationCallback(null, x509Certificate2, x509Chain, sslPolicyErrors); + clientHandler.ServerCertificateCustomValidationCallback = serverCertificateValidationProvider.ValidationCallback; return clientHandler; } catch (Exception ex) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/ServerCertificateValidationProvider.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/ServerCertificateValidationProvider.cs index fc48b71aac..1483810279 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/ServerCertificateValidationProvider.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Resources/Http/ServerCertificateValidationProvider.cs @@ -40,14 +40,14 @@ private ServerCertificateValidationProvider(X509Certificate2Collection? trustedC } this.trustedCertificates = trustedCertificates; - this.ValidationCallback = (sender, cert, chain, errors) => - this.ValidateCertificate(new X509Certificate2(cert), chain, errors); + this.ValidationCallback = (_, cert, chain, errors) => + this.ValidateCertificate(cert, chain, errors); this.IsCertificateLoaded = true; } public bool? IsCertificateLoaded { get; } - public RemoteCertificateValidationCallback? ValidationCallback { get; } + public Func? ValidationCallback { get; } public static ServerCertificateValidationProvider FromCertificateFile(string certificateFile) { @@ -101,7 +101,7 @@ private static bool HasCommonCertificate(X509Chain chain, X509Certificate2Collec return false; } - private bool ValidateCertificate(X509Certificate2 cert, X509Chain chain, SslPolicyErrors errors) + private bool ValidateCertificate(X509Certificate2? cert, X509Chain? chain, SslPolicyErrors errors) { var isSslPolicyPassed = errors == SslPolicyErrors.None || errors == SslPolicyErrors.RemoteCertificateChainErrors; @@ -118,7 +118,15 @@ private bool ValidateCertificate(X509Certificate2 cert, X509Chain chain, SslPoli } } - chain.ChainPolicy.ExtraStore.AddRange(this.trustedCertificates); + // when the above check passes, these parameters should not be null; if they are null, don't attempt remaining steps + if (cert == null || chain == null) + { + AWSXRayEventSource.Log.FailedToValidateCertificate(nameof(ServerCertificateValidationProvider), $"Failed to validate certificate: {nameof(cert)} or {nameof(chain)} is null"); + return false; + } + + // this callback is invoked only when trustedCertificates is non-null + chain.ChainPolicy.ExtraStore.AddRange(this.trustedCertificates!); chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; // building the chain to process basic validations e.g. signature, use, expiration, revocation diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/Http/TestServerCertificateValidationProvider.cs b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/Http/TestServerCertificateValidationProvider.cs index baa289b2db..ac7bea1d71 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/Http/TestServerCertificateValidationProvider.cs +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/Http/TestServerCertificateValidationProvider.cs @@ -64,4 +64,38 @@ public void TestInValidCertificate() // Validates if the certificate file loaded. Assert.False(serverCertificateValidationProvider.IsCertificateLoaded); } + + [Fact] + public void TestValidationCallbackWithNullCertificate() + { + using (CertificateUploader certificateUploader = new CertificateUploader()) + using (var chain = new X509Chain()) + { + certificateUploader.Create(); + + // Loads the certificate to the trusted collection from the file + ServerCertificateValidationProvider serverCertificateValidationProvider = + ServerCertificateValidationProvider.FromCertificateFile(certificateUploader.FilePath); + + Assert.False(serverCertificateValidationProvider.ValidationCallback(this, null, chain, default)); + } + } + + [Fact] + public void TestValidationCallbackWithNullChain() + { + using (CertificateUploader certificateUploader = new CertificateUploader()) + { + certificateUploader.Create(); + + // Loads the certificate to the trusted collection from the file + ServerCertificateValidationProvider serverCertificateValidationProvider = + ServerCertificateValidationProvider.FromCertificateFile(certificateUploader.FilePath); + + // borrow the certificate from the file just so we have a non-null cert to pass + using var serverCertificate = new X509Certificate2(certificateUploader.FilePath); + + Assert.False(serverCertificateValidationProvider.ValidationCallback(this, serverCertificate, null, default)); + } + } } diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/TestAWSLambdaResourceDetector.cs b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/TestAWSLambdaResourceDetector.cs index 0b6a62a1cc..addde7ef1b 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/TestAWSLambdaResourceDetector.cs +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/Resources/TestAWSLambdaResourceDetector.cs @@ -17,6 +17,7 @@ using System; using System.Linq; using OpenTelemetry.Contrib.Extensions.AWSXRay.Resources; +using OpenTelemetry.Resources; using Xunit; namespace OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.Resources; @@ -42,4 +43,82 @@ public void TestDetect() Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); } + + [Fact] + public void TestDetectWithNullEnvironmentVariables() + { + Environment.SetEnvironmentVariable("AWS_REGION", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); + + var resource = new AWSLambdaResourceDetector().Detect(); + + Assert.Same(Resource.Empty, resource); + } + + [Fact] + public void TestExtractResourceAttributesWithNullAwsRegion() + { + Environment.SetEnvironmentVariable("AWS_REGION", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", "testfunction"); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", "latest"); + + try + { + var ex = Assert.Throws( + () => AWSLambdaResourceDetector.ExtractResourceAttributes()); + + Assert.Contains("AWS_REGION", ex.Message); + } + finally + { + Environment.SetEnvironmentVariable("AWS_REGION", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); + } + } + + [Fact] + public void TestExtractResourceAttributesWithNullAwsLambdaFunctionName() + { + Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1"); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", "latest"); + + try + { + var ex = Assert.Throws( + () => AWSLambdaResourceDetector.ExtractResourceAttributes()); + + Assert.Contains("AWS_LAMBDA_FUNCTION_NAME", ex.Message); + } + finally + { + Environment.SetEnvironmentVariable("AWS_REGION", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); + } + } + + [Fact] + public void TestExtractResourceAttributesWithNullAwsLambdaFunctionVersion() + { + Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1"); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", "testfunction"); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); + + try + { + var ex = Assert.Throws( + () => AWSLambdaResourceDetector.ExtractResourceAttributes()); + + Assert.Contains("AWS_LAMBDA_FUNCTION_VERSION", ex.Message); + } + finally + { + Environment.SetEnvironmentVariable("AWS_REGION", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", null); + Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", null); + } + } } From 4a211209bcb5d4d3a42650fc94bbc61580d67967 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Sat, 18 Mar 2023 12:33:55 -0400 Subject: [PATCH 03/12] [Extensions.AWSXray] Fix CA1846 (prefer 'AsSpan' over 'Substring') --- .../Trace/AWSXRayPropagator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Trace/AWSXRayPropagator.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Trace/AWSXRayPropagator.cs index 73ae58c161..96fab40c5d 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Trace/AWSXRayPropagator.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/Trace/AWSXRayPropagator.cs @@ -306,9 +306,9 @@ internal static string ToXRayTraceIdFormat(string traceId) sb.Append(Version); sb.Append(TraceIdDelimiter); - sb.Append(traceId.Substring(0, EpochHexDigits)); + sb.Append(traceId, 0, EpochHexDigits); sb.Append(TraceIdDelimiter); - sb.Append(traceId.Substring(EpochHexDigits)); + sb.Append(traceId, EpochHexDigits, traceId.Length - EpochHexDigits); return sb.ToString(); } From 5f8e19159d3640fc1a93b46aab0c5db9c98b5529 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Sat, 18 Mar 2023 12:37:25 -0400 Subject: [PATCH 04/12] [Extensions.AWSXray] Remove reference to System.Net.Http --- .../OpenTelemetry.Contrib.Extensions.AWSXRay.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj index 3e792b508a..0249a52b0b 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/OpenTelemetry.Contrib.Extensions.AWSXRay.csproj @@ -10,7 +10,6 @@ - From 0018518f11bea033364fca3abeb548a64cf59cdd Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Mon, 20 Mar 2023 23:04:17 -0400 Subject: [PATCH 05/12] [Extensions.AWSXRay] Use Activity.TraceIdGenerator on net6.0+ Add an alternate, optimized implementation of AWSXRayIdGenerator using the Activity.TraceIdGenerator API added in .NET 6.0. This implementation also does not require a second sampling decision. --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 2 +- .../AWSXRayIdGenerator.cs | 7 ++- .../AWSXRayIdGenerator.net6.cs | 61 +++++++++++++++++++ .../TracerProviderBuilderExtensions.cs | 2 + .../TestAWSXRayIdGenerator.cs | 15 +++++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt index 14d2df82af..0be7b26747 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -23,5 +23,5 @@ OpenTelemetry.Trace.TracerProviderBuilderExtensions override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Extract(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func!>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet! override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Inject(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action! setter) -> void +static OpenTelemetry.Contrib.Extensions.AWSXRay.AWSXRayIdGenerator.ReplaceTraceId() -> void static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder! builder, OpenTelemetry.Trace.Sampler! sampler) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.cs index 7741963524..2cf7bfbf0f 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.cs @@ -14,6 +14,7 @@ // limitations under the License. // +#if !NET6_0_OR_GREATER using System; using System.Diagnostics; using System.Globalization; @@ -48,9 +49,8 @@ internal static void ReplaceTraceId(Sampler? sampler = null) { if (string.IsNullOrEmpty(activity.ParentId)) { - var awsXRayTraceId = GenerateAWSXRayCompatiableTraceId(); + var awsXRayTraceId = GenerateAWSXRayCompatibleTraceId(); - // TODO: Apply API to directly modify trace id once .NET runtime publicizes it. activity.SetParentId(awsXRayTraceId, default, activity.ActivityTraceFlags); // When not using instrumented library and creating root activity using ActivitySource.StartActivity(), @@ -69,7 +69,7 @@ internal static void ReplaceTraceId(Sampler? sampler = null) ActivitySource.AddActivityListener(awsXRayActivityListener); } - internal static ActivityTraceId GenerateAWSXRayCompatiableTraceId() + internal static ActivityTraceId GenerateAWSXRayCompatibleTraceId() { var epoch = (int)DateTime.UtcNow.ToUnixTimeSeconds(); // first 8 digit as time stamp @@ -197,3 +197,4 @@ private static ActivitySamplingResult ComputeRootActivitySamplingResult( return ActivitySamplingResult.PropagationData; } } +#endif diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs new file mode 100644 index 0000000000..03562a1ebe --- /dev/null +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs @@ -0,0 +1,61 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#if NET6_0_OR_GREATER +using System; +using System.Buffers.Binary; +using System.Diagnostics; + +namespace OpenTelemetry.Contrib.Extensions.AWSXRay; + +/// +/// Generate AWS X-Ray compatible trace IDs. +/// See https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids. +/// +public static class AWSXRayIdGenerator +{ + /// + /// Sets to . + /// + public static void ReplaceTraceId() + { + Activity.TraceIdGenerator = GenerateAWSXRayCompatibleTraceId; + } + + /// + /// Generates an AWS X-Ray compatible trace ID. + /// + /// + /// An whose first 4 bytes are the big-endian unix timestamp (in seconds) and whose + /// remaining 12 bytes are randomly generated. + /// + internal static ActivityTraceId GenerateAWSXRayCompatibleTraceId() + { + Span buffer = stackalloc byte[16]; + + // intentionally truncating to 4 bytes because AWS X-Ray requires 8 hex characters + var seconds = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + _ = BinaryPrimitives.TryWriteUInt32BigEndian(buffer, seconds); + + // fill the rest of the buffer with random bytes +#pragma warning disable CA5394 // Do not use insecure randomness + Random.Shared.NextBytes(buffer.Slice(4, 12)); +#pragma warning restore CA5394 // Do not use insecure randomness + + return ActivityTraceId.CreateFromBytes(buffer); + } +} +#endif diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs index 7257b70fb0..04bfd22046 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs @@ -36,6 +36,7 @@ public static TracerProviderBuilder AddXRayTraceId(this TracerProviderBuilder bu AWSXRayIdGenerator.ReplaceTraceId(); return builder; } +#if !NET6_0_OR_GREATER /// /// 1. Replace the trace id of root activity. @@ -51,4 +52,5 @@ public static TracerProviderBuilder AddXRayTraceIdWithSampler(this TracerProvide AWSXRayIdGenerator.ReplaceTraceId(sampler); return builder; } +#endif } diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs index ff779da30d..f73a72813c 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs @@ -36,7 +36,12 @@ public void TestGenerateTraceIdForRootNode() activity.Start(); Assert.NotEqual(originalTraceId, activity.TraceId); +#if NET6_0_OR_GREATER + // the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which does not change the parent ID + Assert.Equal(originalParentSpanId, activity.ParentSpanId); +#else Assert.NotEqual(originalParentSpanId, activity.ParentSpanId); +#endif Assert.Equal("0000000000000000", activity.ParentSpanId.ToHexString()); Assert.Equal(originalTraceFlag, activity.ActivityTraceFlags); } @@ -82,7 +87,12 @@ public void TestGenerateTraceIdForNonRootNodeNotSampled() public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOn() { using (Sdk.CreateTracerProviderBuilder() +#if NET6_0_OR_GREATER + // the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which runs before the sampler + .AddXRayTraceId() +#else .AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(1.0)) +#endif .AddSource("TestTraceIdBasedSamplerOn") .SetSampler(new TraceIdRatioBasedSampler(1.0)) .Build()) @@ -101,7 +111,12 @@ public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSam public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOff() { using (Sdk.CreateTracerProviderBuilder() +#if NET6_0_OR_GREATER + // the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which runs before the sampler + .AddXRayTraceId() +#else .AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(0.0)) +#endif .AddSource("TestTraceIdBasedSamplerOff") .SetSampler(new TraceIdRatioBasedSampler(0.0)) .Build()) From 20a7b99589d1e9769bc13b23a18681064519ad80 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Mon, 20 Mar 2023 23:45:08 -0400 Subject: [PATCH 06/12] [Extensions.AWSXRay] Update CHANGELOG.md --- src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md index 0b3cb1d475..2000067a89 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md @@ -16,6 +16,9 @@ supporting ResourceBuilderExtensions extension, and migrate all detectors to implement OpenTelemetry.Resources.IResourceDetector ([#875](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/875)) +* Add a `net6.0` build with optimized trace ID generation using the new + `Activity.TraceIdGenerator` API. + ([#1096](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1096)) ## 1.2.0 From 3e817396177ede18861a2f1a0dd53b6f446175f8 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Tue, 21 Mar 2023 07:37:35 -0400 Subject: [PATCH 07/12] [Extensions.AWSXRay] Restore methods that take a Sampler parameter This was a breaking change, so add these two methods back to the net6.0 API and mark them Obsolete instead. --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 2 ++ .../AWSXRayIdGenerator.net6.cs | 11 +++++++++++ .../TracerProviderBuilderExtensions.cs | 12 ++++++++++-- .../TestAWSXRayIdGenerator.cs | 14 ++++---------- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt index 0be7b26747..b351f6d7e1 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -24,4 +24,6 @@ override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Extrac override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet! override OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.Inject(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action! setter) -> void static OpenTelemetry.Contrib.Extensions.AWSXRay.AWSXRayIdGenerator.ReplaceTraceId() -> void +static OpenTelemetry.Contrib.Extensions.AWSXRay.AWSXRayIdGenerator.ReplaceTraceId(OpenTelemetry.Trace.Sampler! sampler) -> void static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder! builder, OpenTelemetry.Trace.Sampler! sampler) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs index 03562a1ebe..b12750ffc9 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs @@ -18,6 +18,7 @@ using System; using System.Buffers.Binary; using System.Diagnostics; +using OpenTelemetry.Trace; namespace OpenTelemetry.Contrib.Extensions.AWSXRay; @@ -35,6 +36,16 @@ public static void ReplaceTraceId() Activity.TraceIdGenerator = GenerateAWSXRayCompatibleTraceId; } + /// + /// Sets to . + /// + /// Unused. (See deprecation message.) + [Obsolete($"When targeting .NET 6.0 or later, the X-Ray ID generator does not need to update the sampling decision. Use ${nameof(ReplaceTraceId)}() instead.")] + public static void ReplaceTraceId(Sampler sampler) + { + ReplaceTraceId(); + } + /// /// Generates an AWS X-Ray compatible trace ID. /// diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs index 04bfd22046..969d0e7410 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/TracerProviderBuilderExtensions.cs @@ -36,8 +36,16 @@ public static TracerProviderBuilder AddXRayTraceId(this TracerProviderBuilder bu AWSXRayIdGenerator.ReplaceTraceId(); return builder; } -#if !NET6_0_OR_GREATER +#if NET6_0_OR_GREATER + /// + /// Replace the trace id of root activity. + /// + /// being configured. + /// Unused. (See deprecation message.) + /// The instance of . + [System.Obsolete($"When targeting .NET 6.0 or later, the X-Ray ID generator does not need to update the sampling decision. Use ${nameof(AddXRayTraceId)} instead.")] +#else /// /// 1. Replace the trace id of root activity. /// 2. Update the sampling decision for root activity when it's created through ActivitySource.StartActivity(). @@ -45,6 +53,7 @@ public static TracerProviderBuilder AddXRayTraceId(this TracerProviderBuilder bu /// being configured. /// being used. /// The instance of . +#endif public static TracerProviderBuilder AddXRayTraceIdWithSampler(this TracerProviderBuilder builder, Sampler sampler) { Guard.ThrowIfNull(builder); @@ -52,5 +61,4 @@ public static TracerProviderBuilder AddXRayTraceIdWithSampler(this TracerProvide AWSXRayIdGenerator.ReplaceTraceId(sampler); return builder; } -#endif } diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs index f73a72813c..c195bfad84 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/TestAWSXRayIdGenerator.cs @@ -86,16 +86,13 @@ public void TestGenerateTraceIdForNonRootNodeNotSampled() [Fact] public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOn() { +#pragma warning disable CS0618 // Type or member is obsolete using (Sdk.CreateTracerProviderBuilder() -#if NET6_0_OR_GREATER - // the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which runs before the sampler - .AddXRayTraceId() -#else .AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(1.0)) -#endif .AddSource("TestTraceIdBasedSamplerOn") .SetSampler(new TraceIdRatioBasedSampler(1.0)) .Build()) +#pragma warning restore CS0618 // Type or member is obsolete { using (var activitySource = new ActivitySource("TestTraceIdBasedSamplerOn")) { @@ -110,16 +107,13 @@ public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSam [Fact] public void TestGenerateTraceIdForRootNodeUsingActivitySourceWithTraceIdBasedSamplerOff() { +#pragma warning disable CS0618 // Type or member is obsolete using (Sdk.CreateTracerProviderBuilder() -#if NET6_0_OR_GREATER - // the net6.0 version of AWSXRayIdGenerator uses Activity.TraceIdGenerator, which runs before the sampler - .AddXRayTraceId() -#else .AddXRayTraceIdWithSampler(new TraceIdRatioBasedSampler(0.0)) -#endif .AddSource("TestTraceIdBasedSamplerOff") .SetSampler(new TraceIdRatioBasedSampler(0.0)) .Build()) +#pragma warning restore CS0618 // Type or member is obsolete { using (var activitySource = new ActivitySource("TestTraceIdBasedSamplerOff")) { From f3323d00145dec50834b8b5a1fb5f95daf35e731 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Thu, 11 May 2023 14:07:37 -0400 Subject: [PATCH 08/12] Revert changes to files moved to the new resource detectors project --- .../AWSECSResourceDetector.cs | 2 +- .../AWSEKSResourceDetector.cs | 4 +-- .../Http/Handler.cs | 4 ++- .../ServerCertificateValidationProvider.cs | 18 +++------- ...erverCertificateValidationProviderTests.cs | 34 ------------------- 5 files changed, 11 insertions(+), 51 deletions(-) diff --git a/src/OpenTelemetry.ResourceDetectors.AWS/AWSECSResourceDetector.cs b/src/OpenTelemetry.ResourceDetectors.AWS/AWSECSResourceDetector.cs index 8c2e1fab4f..b7de0f6e82 100644 --- a/src/OpenTelemetry.ResourceDetectors.AWS/AWSECSResourceDetector.cs +++ b/src/OpenTelemetry.ResourceDetectors.AWS/AWSECSResourceDetector.cs @@ -195,7 +195,7 @@ internal static List> ExtractMetadataV4ResourceAttr { while (!streamReader.EndOfStream) { - var trimmedLine = streamReader.ReadLine()!.Trim(); + var trimmedLine = streamReader.ReadLine().Trim(); if (trimmedLine.Length > 64) { containerId = trimmedLine.Substring(trimmedLine.Length - 64); diff --git a/src/OpenTelemetry.ResourceDetectors.AWS/AWSEKSResourceDetector.cs b/src/OpenTelemetry.ResourceDetectors.AWS/AWSEKSResourceDetector.cs index 978a61a0df..4f4faf4e77 100644 --- a/src/OpenTelemetry.ResourceDetectors.AWS/AWSEKSResourceDetector.cs +++ b/src/OpenTelemetry.ResourceDetectors.AWS/AWSEKSResourceDetector.cs @@ -85,7 +85,7 @@ internal static List> ExtractResourceAttributes(str { while (!streamReader.EndOfStream) { - stringBuilder.Append(streamReader.ReadLine()!.Trim()); + stringBuilder.Append(streamReader.ReadLine().Trim()); } } @@ -109,7 +109,7 @@ internal static List> ExtractResourceAttributes(str { while (!streamReader.EndOfStream) { - var trimmedLine = streamReader.ReadLine()!.Trim(); + var trimmedLine = streamReader.ReadLine().Trim(); if (trimmedLine.Length > 64) { return trimmedLine.Substring(trimmedLine.Length - 64); diff --git a/src/OpenTelemetry.ResourceDetectors.AWS/Http/Handler.cs b/src/OpenTelemetry.ResourceDetectors.AWS/Http/Handler.cs index 68cfa54701..27b89b2e2b 100644 --- a/src/OpenTelemetry.ResourceDetectors.AWS/Http/Handler.cs +++ b/src/OpenTelemetry.ResourceDetectors.AWS/Http/Handler.cs @@ -40,7 +40,9 @@ internal class Handler } var clientHandler = new HttpClientHandler(); - clientHandler.ServerCertificateCustomValidationCallback = serverCertificateValidationProvider.ValidationCallback; + clientHandler.ServerCertificateCustomValidationCallback = + (sender, x509Certificate2, x509Chain, sslPolicyErrors) => + serverCertificateValidationProvider.ValidationCallback(null, x509Certificate2, x509Chain, sslPolicyErrors); return clientHandler; } catch (Exception ex) diff --git a/src/OpenTelemetry.ResourceDetectors.AWS/Http/ServerCertificateValidationProvider.cs b/src/OpenTelemetry.ResourceDetectors.AWS/Http/ServerCertificateValidationProvider.cs index 0caea3fd91..5f26f4f064 100644 --- a/src/OpenTelemetry.ResourceDetectors.AWS/Http/ServerCertificateValidationProvider.cs +++ b/src/OpenTelemetry.ResourceDetectors.AWS/Http/ServerCertificateValidationProvider.cs @@ -39,14 +39,14 @@ private ServerCertificateValidationProvider(X509Certificate2Collection? trustedC } this.trustedCertificates = trustedCertificates; - this.ValidationCallback = (_, cert, chain, errors) => - this.ValidateCertificate(cert, chain, errors); + this.ValidationCallback = (sender, cert, chain, errors) => + this.ValidateCertificate(new X509Certificate2(cert), chain, errors); this.IsCertificateLoaded = true; } public bool? IsCertificateLoaded { get; } - public Func? ValidationCallback { get; } + public RemoteCertificateValidationCallback? ValidationCallback { get; } public static ServerCertificateValidationProvider FromCertificateFile(string certificateFile) { @@ -100,7 +100,7 @@ private static bool HasCommonCertificate(X509Chain chain, X509Certificate2Collec return false; } - private bool ValidateCertificate(X509Certificate2? cert, X509Chain? chain, SslPolicyErrors errors) + private bool ValidateCertificate(X509Certificate2 cert, X509Chain chain, SslPolicyErrors errors) { var isSslPolicyPassed = errors == SslPolicyErrors.None || errors == SslPolicyErrors.RemoteCertificateChainErrors; @@ -117,15 +117,7 @@ private bool ValidateCertificate(X509Certificate2? cert, X509Chain? chain, SslPo } } - // when the above check passes, these parameters should not be null; if they are null, don't attempt remaining steps - if (cert == null || chain == null) - { - AWSXRayEventSource.Log.FailedToValidateCertificate(nameof(ServerCertificateValidationProvider), $"Failed to validate certificate: {nameof(cert)} or {nameof(chain)} is null"); - return false; - } - - // this callback is invoked only when trustedCertificates is non-null - chain.ChainPolicy.ExtraStore.AddRange(this.trustedCertificates!); + chain.ChainPolicy.ExtraStore.AddRange(this.trustedCertificates); chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; // building the chain to process basic validations e.g. signature, use, expiration, revocation diff --git a/test/OpenTelemetry.ResourceDetectors.AWS.Tests/Http/ServerCertificateValidationProviderTests.cs b/test/OpenTelemetry.ResourceDetectors.AWS.Tests/Http/ServerCertificateValidationProviderTests.cs index b2d350c0b8..7f69902b6c 100644 --- a/test/OpenTelemetry.ResourceDetectors.AWS.Tests/Http/ServerCertificateValidationProviderTests.cs +++ b/test/OpenTelemetry.ResourceDetectors.AWS.Tests/Http/ServerCertificateValidationProviderTests.cs @@ -66,40 +66,6 @@ public void TestInValidCertificate() // Validates if the certificate file loaded. Assert.False(serverCertificateValidationProvider.IsCertificateLoaded); } - - [Fact] - public void TestValidationCallbackWithNullCertificate() - { - using (CertificateUploader certificateUploader = new CertificateUploader()) - using (var chain = new X509Chain()) - { - certificateUploader.Create(); - - // Loads the certificate to the trusted collection from the file - ServerCertificateValidationProvider serverCertificateValidationProvider = - ServerCertificateValidationProvider.FromCertificateFile(certificateUploader.FilePath); - - Assert.False(serverCertificateValidationProvider.ValidationCallback(this, null, chain, default)); - } - } - - [Fact] - public void TestValidationCallbackWithNullChain() - { - using (CertificateUploader certificateUploader = new CertificateUploader()) - { - certificateUploader.Create(); - - // Loads the certificate to the trusted collection from the file - ServerCertificateValidationProvider serverCertificateValidationProvider = - ServerCertificateValidationProvider.FromCertificateFile(certificateUploader.FilePath); - - // borrow the certificate from the file just so we have a non-null cert to pass - using var serverCertificate = new X509Certificate2(certificateUploader.FilePath); - - Assert.False(serverCertificateValidationProvider.ValidationCallback(this, serverCertificate, null, default)); - } - } } #endif From 867a30bcd387a40d2be085a7a383f33789fa7c80 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Thu, 11 May 2023 14:23:04 -0400 Subject: [PATCH 09/12] [Extensions.AWSXRay] remove moved PublicAPI declarations --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt index b351f6d7e1..441bf33b69 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -1,22 +1,5 @@ #nullable enable OpenTelemetry.Contrib.Extensions.AWSXRay.AWSXRayIdGenerator -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector.AWSEBSResourceDetector() -> void -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEBSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector.AWSEC2ResourceDetector() -> void -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEC2ResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector.AWSECSResourceDetector() -> void -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSECSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector.AWSEKSResourceDetector() -> void -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSEKSResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector.AWSLambdaResourceDetector() -> void -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.AWSLambdaResourceDetector.Detect() -> OpenTelemetry.Resources.Resource! -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.ResourceDetectorUtils -OpenTelemetry.Contrib.Extensions.AWSXRay.Resources.ResourceDetectorUtils.ResourceDetectorUtils() -> void OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator OpenTelemetry.Contrib.Extensions.AWSXRay.Trace.AWSXRayPropagator.AWSXRayPropagator() -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions From 75484d1b8bfdaa19dd41eac33408430cf352e517 Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Wed, 14 Jun 2023 10:03:26 -0400 Subject: [PATCH 10/12] [Extensions.AWSXRay] Use cryptographic RNG to satisfy CA5394 --- .../AWSXRayIdGenerator.net6.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs index b12750ffc9..59b65142d6 100644 --- a/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs +++ b/src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.net6.cs @@ -18,6 +18,7 @@ using System; using System.Buffers.Binary; using System.Diagnostics; +using System.Security.Cryptography; using OpenTelemetry.Trace; namespace OpenTelemetry.Contrib.Extensions.AWSXRay; @@ -62,9 +63,7 @@ internal static ActivityTraceId GenerateAWSXRayCompatibleTraceId() _ = BinaryPrimitives.TryWriteUInt32BigEndian(buffer, seconds); // fill the rest of the buffer with random bytes -#pragma warning disable CA5394 // Do not use insecure randomness - Random.Shared.NextBytes(buffer.Slice(4, 12)); -#pragma warning restore CA5394 // Do not use insecure randomness + RandomNumberGenerator.Fill(buffer.Slice(4, 12)); return ActivityTraceId.CreateFromBytes(buffer); } From d141338e089c86f8becb563f05ae6159fd713e6d Mon Sep 17 00:00:00 2001 From: Will Rogers Date: Thu, 20 Jul 2023 08:56:05 -0400 Subject: [PATCH 11/12] [Extensions.AWSXRay] Remove test target netcoreapp3.1 --- .../OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj index b15ab9648b..908d078099 100644 --- a/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj +++ b/test/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests/OpenTelemetry.Contrib.Extensions.AWSXRay.Tests.csproj @@ -1,15 +1,10 @@ - net7.0;net6.0;netcoreapp3.1 + net7.0;net6.0 $(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion) - - - true - - From 55373f069e49c7fbbb127d4a953bfe33fadb201a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 28 Jul 2023 07:32:45 +0200 Subject: [PATCH 12/12] fix public api --- .../.publicApi/net6.0/PublicAPI.Unshipped.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry.Extensions.AWS/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions.AWS/.publicApi/net6.0/PublicAPI.Unshipped.txt index a3bc0c22a5..70b757cca5 100644 --- a/src/OpenTelemetry.Extensions.AWS/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Extensions.AWS/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -2,10 +2,10 @@ OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.AWSXRayPropagator() -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Extract(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func> getter) -> OpenTelemetry.Context.Propagation.PropagationContext -override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet -override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Inject(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action setter) -> void +override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Extract(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func!>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext +override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet! +override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Inject(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action! setter) -> void static OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator.ReplaceTraceId() -> void -static OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator.ReplaceTraceId(OpenTelemetry.Trace.Sampler sampler) -> void -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder builder, OpenTelemetry.Trace.Sampler sampler) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file +static OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator.ReplaceTraceId(OpenTelemetry.Trace.Sampler! sampler) -> void +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceIdWithSampler(this OpenTelemetry.Trace.TracerProviderBuilder! builder, OpenTelemetry.Trace.Sampler! sampler) -> OpenTelemetry.Trace.TracerProviderBuilder! \ No newline at end of file