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

Move Existing ServerCertificateValidation Classes to Shared #1550

Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{1FCC8E
src\Shared\PropertyFetcher.cs = src\Shared\PropertyFetcher.cs
src\Shared\ResourceSemanticConventions.cs = src\Shared\ResourceSemanticConventions.cs
src\Shared\SemanticConventions.cs = src\Shared\SemanticConventions.cs
src\Shared\ServerCertificateValidationEventSource.cs = src\Shared\ServerCertificateValidationEventSource.cs
src\Shared\ServerCertificateValidationHandler.cs = src\Shared\ServerCertificateValidationHandler.cs
src\Shared\ServerCertificateValidationProvider.cs = src\Shared\ServerCertificateValidationProvider.cs
src\Shared\ServiceProviderExtensions.cs = src\Shared\ServiceProviderExtensions.cs
src\Shared\SpanAttributeConstants.cs = src\Shared\SpanAttributeConstants.cs
src\Shared\SpanHelper.cs = src\Shared\SpanHelper.cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using OpenTelemetry.ResourceDetectors.AWS.Http;
using OpenTelemetry.ResourceDetectors.AWS.Models;
using OpenTelemetry.Resources;

Expand All @@ -31,7 +30,7 @@ public sealed class AWSEKSResourceDetector : IResourceDetector
public Resource Detect()
{
var credentials = GetEKSCredentials(AWSEKSCredentialPath);
using var httpClientHandler = Handler.Create(AWSEKSCertificatePath);
using var httpClientHandler = ServerCertificateValidationHandler.Create(AWSEKSCertificatePath);

if (credentials == null || !IsEKSProcess(credentials, httpClientHandler))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@ public void FailedToExtractResourceAttributes(string format, string exception)
{
this.WriteEvent(3, format, exception);
}

[Event(2, Message = "Failed to validate certificate in format: '{0}', error: '{1}'.", Level = EventLevel.Warning)]
public void FailedToValidateCertificate(string format, string error)
{
this.WriteEvent(4, format, error);
}
}
40 changes: 0 additions & 40 deletions src/OpenTelemetry.ResourceDetectors.AWS/Http/Handler.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\Shared\ServerCertificateValidationHandler.cs" Link="Includes\ServerCertificateValidationHandler.cs" />
<Compile Include="$(RepoRoot)\src\Shared\ServerCertificateValidationProvider.cs" Link="Includes\ServerCertificateValidationProvider.cs" />
<Compile Include="$(RepoRoot)\src\Shared\ServerCertificateValidationEventSource.cs" Link="Includes\ServerCertificateValidationEventSource.cs" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/Shared/ServerCertificateValidationEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if !NETFRAMEWORK

using System.Diagnostics.Tracing;

namespace OpenTelemetry.ResourceDetectors;

[EventSource(Name = "OpenTelemetry-ResourceDetectors-Http")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenTelemetry-ResourceDetectors-Http -- is there a component with this name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I have created that as a separate name as a placeholder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use shared name for the eventsources. It'll cause issues like open-telemetry/opentelemetry-dotnet#4516 (comment)

internal class ServerCertificateValidationEventSource : EventSource
{
public static ServerCertificateValidationEventSource Log = new();

[Event(1, Message = "Failed to extract resource attributes in '{0}'.", Level = EventLevel.Warning)]
public void FailedToExtractResourceAttributes(string format, string exception)
{
this.WriteEvent(3, format, exception);
}

[Event(2, Message = "Failed to validate certificate in format: '{0}', error: '{1}'.", Level = EventLevel.Warning)]
public void FailedToValidateCertificate(string format, string error)
{
this.WriteEvent(4, format, error);
}
}

#endif
42 changes: 42 additions & 0 deletions src/Shared/ServerCertificateValidationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if !NETFRAMEWORK

using System;
using System.Net.Http;
using OpenTelemetry.Internal;

namespace OpenTelemetry.ResourceDetectors;

internal class ServerCertificateValidationHandler
{
public static HttpClientHandler? Create(string certificateFile)
{
try
{
ServerCertificateValidationProvider? serverCertificateValidationProvider = ServerCertificateValidationProvider.FromCertificateFile(certificateFile);

if (serverCertificateValidationProvider == null)
{
ServerCertificateValidationEventSource.Log.FailedToValidateCertificate(nameof(ServerCertificateValidationHandler), "Failed to Load the certificate file into trusted collection");
return null;
}

var clientHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
(sender, x509Certificate2, x509Chain, sslPolicyErrors) =>
serverCertificateValidationProvider.ValidationCallback(sender, x509Certificate2, x509Chain, sslPolicyErrors),
};
return clientHandler;
}
catch (Exception ex)
{
ServerCertificateValidationEventSource.Log.FailedToExtractResourceAttributes($"{nameof(ServerCertificateValidationHandler)} : Failed to create HttpClientHandler", ex.ToInvariantString());
}

return null;
}
}
#endif
Loading
Loading