-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fail fast when insecure channel is not configured for .NET Core…
… 3.1 with gRPC (#2691) * feat: Fail fast when unsecure channel is not configured for .NET Core 3.1 Signed-off-by: Tom Kerkhove <[email protected]> * Update changelog Signed-off-by: Tom Kerkhove <[email protected]> * NotSupportedException -> InvalidOperationException Signed-off-by: Tom Kerkhove <[email protected]> * Code quality Signed-off-by: Tom Kerkhove <[email protected]> * Use static class, instead of base class Signed-off-by: Tom Kerkhove <[email protected]> * Revert change Signed-off-by: Tom Kerkhove <[email protected]> * Add missing file header Signed-off-by: Tom Kerkhove <[email protected]> * Change build directive Signed-off-by: Tom Kerkhove <[email protected]> * Code linting Signed-off-by: Tom Kerkhove <[email protected]> * Remove redundant check Signed-off-by: Tom Kerkhove <[email protected]> * Update changelog Signed-off-by: Tom Kerkhove <[email protected]> * Fix build errors after rebase Signed-off-by: Tom Kerkhove <[email protected]> * Use previous build directives Signed-off-by: Tom Kerkhove <[email protected]> * Update changelog with latest RC Signed-off-by: Tom Kerkhove <[email protected]> * Check for version, instead of build directive Signed-off-by: Tom Kerkhove <[email protected]> * Align tests Signed-off-by: Tom Kerkhove <[email protected]> * Change error message Co-authored-by: Alan West <[email protected]> * Change comment Co-authored-by: Alan West <[email protected]> * Change test name Co-authored-by: Alan West <[email protected]> * Change test name Co-authored-by: Alan West <[email protected]> * fix: Build integration test image with SDK of specified .NET version Signed-off-by: Tom Kerkhove <[email protected]> * revert: Build in 6.0 due to language version Signed-off-by: Tom Kerkhove <[email protected]> * Use version check instead of build directive Signed-off-by: Tom Kerkhove <[email protected]> * Fix formatting Signed-off-by: Tom Kerkhove <[email protected]> * Fix check on version Signed-off-by: Tom Kerkhove <[email protected]> * Remove flaky test * Incorporate @pellared's feedback Signed-off-by: Tom Kerkhove <[email protected]> * Change code style Signed-off-by: Tom Kerkhove <[email protected]> * Code review remarks Signed-off-by: Tom Kerkhove <[email protected]> * Update changelog Co-authored-by: Cijo Thomas <[email protected]> * Update changelog Co-authored-by: Cijo Thomas <[email protected]> Co-authored-by: Alan West <[email protected]> Co-authored-by: Cijo Thomas <[email protected]>
- Loading branch information
1 parent
fd2aa0b
commit 1191a2a
Showing
9 changed files
with
246 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/ExporterClientValidation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// <copyright file="ExporterClientValidation.cs" company="OpenTelemetry Authors"> | ||
// 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. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient | ||
{ | ||
internal static class ExporterClientValidation | ||
{ | ||
internal static void EnsureUnencryptedSupportIsEnabled(OtlpExporterOptions options) | ||
{ | ||
var version = System.Environment.Version; | ||
|
||
// This verification is only required for .NET Core 3.x | ||
if (version.Major != 3) | ||
{ | ||
return; | ||
} | ||
|
||
if (options.Endpoint.Scheme.Equals("http", StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
if (AppContext.TryGetSwitch( | ||
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", out var unencryptedIsSupported) == false | ||
|| unencryptedIsSupported == false) | ||
{ | ||
throw new InvalidOperationException( | ||
"Calling insecure gRPC services on .NET Core 3.x requires enabling the 'System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport' switch. See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client"); | ||
} | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/ExporterClientValidationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// <copyright file="ExporterClientValidationTests.cs" company="OpenTelemetry Authors"> | ||
// 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. | ||
// </copyright> | ||
|
||
using System; | ||
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests | ||
{ | ||
public class ExporterClientValidationTests : Http2UnencryptedSupportTests | ||
{ | ||
private const string HttpEndpoint = "http://localhost:4173"; | ||
private const string HttpsEndpoint = "https://localhost:4173"; | ||
|
||
[Fact] | ||
public void ExporterClientValidation_FlagIsEnabledForHttpEndpoint() | ||
{ | ||
var options = new OtlpExporterOptions | ||
{ | ||
Endpoint = new Uri(HttpEndpoint), | ||
}; | ||
|
||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); | ||
|
||
var exception = Record.Exception(() => ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options)); | ||
Assert.Null(exception); | ||
} | ||
|
||
[Fact] | ||
public void ExporterClientValidation_FlagIsNotEnabledForHttpEndpoint() | ||
{ | ||
var options = new OtlpExporterOptions | ||
{ | ||
Endpoint = new Uri(HttpEndpoint), | ||
}; | ||
|
||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false); | ||
|
||
var exception = Record.Exception(() => ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options)); | ||
|
||
if (Environment.Version.Major == 3) | ||
{ | ||
Assert.NotNull(exception); | ||
Assert.IsType<InvalidOperationException>(exception); | ||
} | ||
else | ||
{ | ||
Assert.Null(exception); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ExporterClientValidation_FlagIsNotEnabledForHttpsEndpoint() | ||
{ | ||
var options = new OtlpExporterOptions | ||
{ | ||
Endpoint = new Uri(HttpsEndpoint), | ||
}; | ||
|
||
var exception = Record.Exception(() => ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options)); | ||
Assert.Null(exception); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Http2UnencryptedSupportTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// <copyright file="Http2UnencryptedSupportTests.cs" company="OpenTelemetry Authors"> | ||
// 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. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests | ||
{ | ||
public class Http2UnencryptedSupportTests : IDisposable | ||
{ | ||
private readonly bool initialFlagStatus; | ||
|
||
public Http2UnencryptedSupportTests() | ||
{ | ||
this.initialFlagStatus = this.DetermineInitialFlagStatus(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", this.initialFlagStatus); | ||
} | ||
|
||
private bool DetermineInitialFlagStatus() | ||
{ | ||
if (AppContext.TryGetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", out var flag)) | ||
{ | ||
return flag; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters