From 9bdaf41172f39036589f2cecde5207c621e011c1 Mon Sep 17 00:00:00 2001 From: kkeirstead <85592574+kkeirstead@users.noreply.github.com> Date: Mon, 2 Aug 2021 09:41:43 -0700 Subject: [PATCH] Disable http response egress - testing (#637) --- .../EgressTests.cs | 44 ++++++++++++++++++- .../Runners/MonitorRunner.cs | 16 +++++-- .../Runners/ScenarioRunner.cs | 4 +- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/EgressTests.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/EgressTests.cs index 57e49505c93..de71d1153e5 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/EgressTests.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/EgressTests.cs @@ -2,13 +2,16 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.AspNetCore.Http; using Microsoft.Diagnostics.Monitoring.TestCommon; using Microsoft.Diagnostics.Monitoring.UnitTests.Fixtures; using Microsoft.Diagnostics.Monitoring.UnitTests.HttpApi; using Microsoft.Diagnostics.Monitoring.UnitTests.Models; using Microsoft.Diagnostics.Monitoring.UnitTests.Options; using Microsoft.Diagnostics.Monitoring.UnitTests.Runners; +using Microsoft.Diagnostics.Monitoring.WebApi; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.FileFormats; using Microsoft.FileFormats.ELF; using Microsoft.FileFormats.MachO; @@ -24,6 +27,7 @@ using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; +using DiagnosticPortConnectionMode = Microsoft.Diagnostics.Monitoring.UnitTests.Options.DiagnosticPortConnectionMode; namespace Microsoft.Diagnostics.Monitoring.UnitTests { @@ -35,7 +39,9 @@ public class EgressTests : IDisposable private readonly DirectoryInfo _tempEgressPath; private const string FileProviderName = "files"; - + + // This should be identical to the error message found in Strings.resx + private const string DisabledHTTPEgressErrorMessage = "HTTP egress is not enabled."; public EgressTests(ITestOutputHelper outputHelper, ServiceProviderFixture serviceProviderFixture) { @@ -153,7 +159,6 @@ await ScenarioRunner.SingleTarget( Assert.Equal(HttpStatusCode.TooManyRequests, ex.StatusCode); Assert.Equal((int)HttpStatusCode.TooManyRequests, ex.Details.Status.GetValueOrDefault()); - await CancelEgressOperation(apiClient, response1); await CancelEgressOperation(apiClient, response2); @@ -211,6 +216,41 @@ await ScenarioRunner.SingleTarget( }); } + /// + /// Tests that turning off HTTP egress results in an error for dumps and logs (gcdumps and traces are currently not tested) + /// + [Fact] + public async Task DisableHttpEgressTest() + { + await ScenarioRunner.SingleTarget( + _outputHelper, + _httpClientFactory, + DiagnosticPortConnectionMode.Connect, + TestAppScenarios.AsyncWait.Name, + appValidate: async (appRunner, appClient) => + { + ProcessInfo processInfo = await appClient.GetProcessAsync(appRunner.ProcessId); + Assert.NotNull(processInfo); + + // Dump Error Check + ValidationProblemDetailsException validationProblemDetailsExceptionDumps = await Assert.ThrowsAsync( + () => appClient.CaptureDumpAsync(appRunner.ProcessId, DumpType.Mini)); + Assert.Equal(HttpStatusCode.BadRequest, validationProblemDetailsExceptionDumps.StatusCode); + Assert.Equal(StatusCodes.Status400BadRequest, validationProblemDetailsExceptionDumps.Details.Status); + Assert.Equal(DisabledHTTPEgressErrorMessage, validationProblemDetailsExceptionDumps.Message); + + // Logs Error Check + ValidationProblemDetailsException validationProblemDetailsExceptionLogs = await Assert.ThrowsAsync( + () => appClient.CaptureLogsAsync(appRunner.ProcessId, TestTimeouts.LogsDuration, LogLevel.None, LogFormat.NDJson)); + Assert.Equal(HttpStatusCode.BadRequest, validationProblemDetailsExceptionLogs.StatusCode); + Assert.Equal(StatusCodes.Status400BadRequest, validationProblemDetailsExceptionLogs.Details.Status); + Assert.Equal(DisabledHTTPEgressErrorMessage, validationProblemDetailsExceptionLogs.Message); + + await appRunner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue); + }, + disableHttpEgress: true); + } + private async Task TraceWithDelay(ApiClient client, int processId, bool delay = true) { HttpResponseMessage message = await client.ApiCall(FormattableString.Invariant($"/trace?pid={processId}&durationSeconds=-1")); diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/MonitorRunner.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/MonitorRunner.cs index 16ed759d170..fa26e44d31d 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/MonitorRunner.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/MonitorRunner.cs @@ -46,7 +46,7 @@ internal sealed class MonitorRunner : IAsyncDisposable private readonly string _runnerTmpPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D")); - private bool _isDiposed; + private bool _isDisposed; /// /// The path of the currently executing assembly. @@ -93,6 +93,11 @@ internal sealed class MonitorRunner : IAsyncDisposable /// public bool DisableAuthentication { get; set; } + /// + /// Determines whether HTTP egress is disabled when starting dotnet-monitor. + /// + public bool DisableHttpEgress { get; set; } + /// /// Determines whether a temporary api key should be generated while starting dotnet-monitor. /// @@ -132,11 +137,11 @@ public async ValueTask DisposeAsync() { lock (_adapter) { - if (_isDiposed) + if (_isDisposed) { return; } - _isDiposed = true; + _isDisposed = true; } _adapter.ReceivedStandardOutputLine -= StandardOutputCallback; @@ -191,6 +196,11 @@ public async Task StartAsync(CancellationToken token) argsList.Add("--no-auth"); } + if (DisableHttpEgress) + { + argsList.Add("--no-http-egress"); + } + if (UseTempApiKey) { argsList.Add("--temp-apikey"); diff --git a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/ScenarioRunner.cs b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/ScenarioRunner.cs index 5e344f6e853..1a8abcd1102 100644 --- a/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/ScenarioRunner.cs +++ b/src/Tests/Microsoft.Diagnostics.Monitoring.Tool.UnitTests/Runners/ScenarioRunner.cs @@ -23,7 +23,8 @@ public static async Task SingleTarget( Func appValidate, Func postAppValidate = null, Action configureApp = null, - Action configureTool = null) + Action configureTool = null, + bool disableHttpEgress = false) { DiagnosticPortHelper.Generate( mode, @@ -34,6 +35,7 @@ public static async Task SingleTarget( toolRunner.ConnectionMode = mode; toolRunner.DiagnosticPortPath = diagnosticPortPath; toolRunner.DisableAuthentication = true; + toolRunner.DisableHttpEgress = disableHttpEgress; configureTool?.Invoke(toolRunner);