-
Notifications
You must be signed in to change notification settings - Fork 235
Sanitize requests when logging #6636
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b479395
Sanitize requests when logging
JoshLove-msft a6582e5
remove unneeded changes
JoshLove-msft 5429d16
erroneous usings
JoshLove-msft 00b46b7
more cleanup
JoshLove-msft 9bf790c
Fix props
JoshLove-msft ef9c3f2
move targets to test directory
JoshLove-msft 0283714
remove prop
JoshLove-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/Directory.Build.targets
This file contains hidden or 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,9 @@ | ||
| <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.targets))\Directory.Build.targets" /> | ||
| <PropertyGroup> | ||
| <!-- Sign the test assembly so that we can use Internals Visible To for it--> | ||
| <SignAssembly>true</SignAssembly> | ||
| <DelaySign>false</DelaySign> | ||
| <AssemblyOriginatorKeyFile>$(RepoEngPath)\AzureSDKToolsKey.snk</AssemblyOriginatorKeyFile> | ||
| </PropertyGroup> | ||
| </Project> |
115 changes: 115 additions & 0 deletions
115
tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/LoggingTests.cs
This file contains hidden or 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,115 @@ | ||
| using System.IO; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Azure.Sdk.Tools.TestProxy.Common; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Xunit; | ||
|
|
||
| namespace Azure.Sdk.Tools.TestProxy.Tests | ||
| { | ||
| /// <summary> | ||
| /// Logging tests cannot be run in parallel with other tests because they share a static logger. | ||
| /// </summary> | ||
| [Collection(nameof(LoggingCollection))] | ||
| public class LoggingTests | ||
| { | ||
| [Fact] | ||
| public async Task PlaybackLogsSanitizedRequest() | ||
| { | ||
| var logger = new TestLogger(); | ||
| DebugLogger.Logger = logger; | ||
|
|
||
| try | ||
| { | ||
| RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory()); | ||
| var httpContext = new DefaultHttpContext(); | ||
| var body = "{\"x-recording-file\":\"Test.RecordEntries/request_with_binary_content.json\"}"; | ||
| httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody(body); | ||
| httpContext.Request.ContentLength = body.Length; | ||
|
|
||
| var controller = new Playback(testRecordingHandler, new NullLoggerFactory()) | ||
| { | ||
| ControllerContext = new ControllerContext() | ||
| { | ||
| HttpContext = httpContext | ||
| } | ||
| }; | ||
| await controller.Start(); | ||
|
|
||
| var recordingId = httpContext.Response.Headers["x-recording-id"].ToString(); | ||
| Assert.NotNull(recordingId); | ||
| Assert.True(testRecordingHandler.PlaybackSessions.ContainsKey(recordingId)); | ||
| var entry = testRecordingHandler.PlaybackSessions[recordingId].Session.Entries[0]; | ||
| HttpRequest request = TestHelpers.CreateRequestFromEntry(entry); | ||
| request.Headers["Authorization"] = "fake-auth-header"; | ||
|
|
||
| HttpResponse response = new DefaultHttpContext().Response; | ||
| await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response); | ||
|
|
||
| Assert.Single(logger.Logs); | ||
| var logEntry = logger.Logs[0].ToString(); | ||
| Assert.DoesNotContain(@"""Authorization"":[""fake-auth-header""]", logEntry); | ||
| Assert.Contains(@"""Authorization"":[""Sanitized""]", logEntry); | ||
| } | ||
| finally | ||
| { | ||
| DebugLogger.Logger = null; | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RecordingHandlerLogsSanitizedRequests() | ||
| { | ||
| var logger = new TestLogger(); | ||
| DebugLogger.Logger = logger; | ||
| var httpContext = new DefaultHttpContext(); | ||
| var bodyBytes = Encoding.UTF8.GetBytes("{\"hello\":\"world\"}"); | ||
| var mockClient = new HttpClient(new MockHttpHandler(bodyBytes, "application/json")); | ||
| var path = Directory.GetCurrentDirectory(); | ||
| var recordingHandler = new RecordingHandler(path) | ||
| { | ||
| RedirectableClient = mockClient, | ||
| RedirectlessClient = mockClient | ||
| }; | ||
|
|
||
| var relativePath = "recordings/logs"; | ||
| var fullPathToRecording = Path.Combine(path, relativePath) + ".json"; | ||
|
|
||
| await recordingHandler.StartRecordingAsync(relativePath, httpContext.Response); | ||
|
|
||
| var recordingId = httpContext.Response.Headers["x-recording-id"].ToString(); | ||
|
|
||
| httpContext.Request.ContentType = "application/json"; | ||
| httpContext.Request.Headers["Authorization"] = "fake-auth-header"; | ||
| httpContext.Request.ContentLength = 0; | ||
| httpContext.Request.Headers["x-recording-id"] = recordingId; | ||
| httpContext.Request.Headers["x-recording-upstream-base-uri"] = "http://example.org"; | ||
| httpContext.Request.Method = "GET"; | ||
| httpContext.Request.Body = new MemoryStream(CompressionUtilities.CompressBody(bodyBytes, httpContext.Request.Headers)); | ||
|
|
||
| await recordingHandler.HandleRecordRequestAsync(recordingId, httpContext.Request, httpContext.Response); | ||
| recordingHandler.StopRecording(recordingId); | ||
|
|
||
| try | ||
| { | ||
| Assert.Single(logger.Logs); | ||
| var logEntry = logger.Logs[0].ToString(); | ||
| Assert.DoesNotContain(@"""Authorization"":[""fake-auth-header""]", logEntry); | ||
| Assert.Contains(@"""Authorization"":[""Sanitized""]", logEntry); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(fullPathToRecording); | ||
| DebugLogger.Logger = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [CollectionDefinition(nameof(LoggingCollection), DisableParallelization = true)] | ||
| public class LoggingCollection | ||
| { | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/TestLogger.cs
This file contains hidden or 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,27 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Azure.Sdk.Tools.TestProxy.Tests | ||
| { | ||
| public class TestLogger : ILogger | ||
| { | ||
| internal List<object> Logs { get; }= new List<object>(); | ||
|
|
||
| public IDisposable BeginScope<TState>(TState state) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, | ||
| Func<TState, Exception, string> formatter) | ||
| { | ||
| Logs.Add(state); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Properties/AssemblyInfo.cs
This file contains hidden or 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,7 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [assembly: InternalsVisibleTo("Azure.Sdk.Tools.TestProxy.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100259ae92701e6c1d912e6126950be871a0aa0bc76c69b573a8f549708e4f5b9658246d97f239964447af47052f09df117f955af39c1bfc43c369ada5460750e7dd0b0f178a70bb970a8fb74f9d892636a4ac38234157de5482482d3debd80f082d6b55a5761cc97c261e5ad3ba3025c06990011f1f86cc021de48381c8174049a")] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.