Skip to content

Commit 4e38840

Browse files
authored
Instructions on how to utilize Admin/RemoveSanitizers (#8142)
* instructions on how to remove sanitizers * updating the runtime data model to provide a cleaner appearance * making the data revealed on info/active more apparent
1 parent 04333b3 commit 4e38840

5 files changed

Lines changed: 110 additions & 15 deletions

File tree

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Models/ActionDescription.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ActionDescription
2424
public string Name;
2525
public string Description;
2626
public CtorDescription ConstructorDetails;
27+
public string SanitizerId;
2728

2829
public ActionDescription() { }
2930

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Models/ActiveMetadataModel.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
using Microsoft.AspNetCore.Mvc.RazorPages;
2-
using System;
31
using System.Collections.Generic;
42
using System.Linq;
5-
using System.Reflection;
6-
using System.Xml;
7-
using System.IO;
83
using Azure.Sdk.Tools.TestProxy.Common;
94
using System.Collections.Concurrent;
10-
using Microsoft.CodeAnalysis.Operations;
115
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
126

137
namespace Azure.Sdk.Tools.TestProxy.Models
@@ -44,14 +38,14 @@ private List<ActionDescription> _populateFromHandler(RecordingHandler handler, s
4438
handler.InMemorySessions
4539
};
4640

47-
var sanitizers = handler.SanitizerRegistry.GetSanitizers();
41+
var sanitizers = handler.SanitizerRegistry.GetRegisteredSanitizers();
4842
var recordingFound = false;
4943
if (!string.IsNullOrWhiteSpace(recordingId)){
5044
foreach (var sessionDict in searchCollections)
5145
{
5246
if (sessionDict.TryGetValue(recordingId, out var session))
5347
{
54-
sanitizers = handler.SanitizerRegistry.GetSanitizers(session);
48+
sanitizers = handler.SanitizerRegistry.GetRegisteredSanitizers(session);
5549
transforms = transforms.Concat(session.AdditionalTransforms);
5650

5751
if (session.CustomMatcher != null)
@@ -76,9 +70,10 @@ private List<ActionDescription> _populateFromHandler(RecordingHandler handler, s
7670
descriptions.AddRange(sanitizers.Select(x => new ActionDescription()
7771
{
7872
ActionType = MetaDataType.Sanitizer,
79-
Name = x.GetType().Name,
80-
ConstructorDetails = GetInstanceDetails(x),
81-
Description = GetClassDocComment(x.GetType(), docXML)
73+
Name = x.Sanitizer.GetType().Name,
74+
SanitizerId = x.Id,
75+
ConstructorDetails = GetInstanceDetails(x.Sanitizer),
76+
Description = GetClassDocComment(x.Sanitizer.GetType(), docXML)
8277
}));
8378

8479
descriptions.AddRange(handler.Transforms.Select(x => new ActionDescription()

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Models/RunTimeMetadataModel.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reflection;
66
using System.Xml;
77
using System.IO;
8-
using Azure.Sdk.Tools.TestProxy.Common;
98

109
namespace Azure.Sdk.Tools.TestProxy.Models
1110
{
@@ -107,7 +106,7 @@ public CtorDescription GetInstanceDetails(object target)
107106
}
108107
}
109108

110-
arguments.Add(new Tuple<string, string>(field.Name, propValue));
109+
arguments.Add(new Tuple<string, string>(GetFriendlyFieldName(field.Name), propValue));
111110
}
112111
}
113112

@@ -118,6 +117,31 @@ public CtorDescription GetInstanceDetails(object target)
118117
};
119118
}
120119

120+
public static Dictionary<string, string> FieldNameMapping = new Dictionary<string, string>()
121+
{
122+
{ "_jsonPath", "jsonPath" },
123+
{ "_value", "value" },
124+
{ "_regex", "regex" },
125+
{ "_groupForReplace", "groupForReplace" },
126+
{ "_condition", "condition" },
127+
{ "_target", "target" },
128+
{ "_key", "key" },
129+
{ "_method", "method" },
130+
{ "_newValue", "value" },
131+
{ "_resetAfterFirst", "resetAfterFirst" },
132+
{ "_regexValue", "regex" }
133+
};
134+
135+
public string GetFriendlyFieldName(string fieldName)
136+
{
137+
if (!string.IsNullOrWhiteSpace(fieldName) && FieldNameMapping.ContainsKey(fieldName))
138+
{
139+
return FieldNameMapping[fieldName];
140+
}
141+
142+
return fieldName;
143+
}
144+
121145
public string GetClassDocComment(Type type, XmlDocument docCommentXml)
122146
{
123147
var memberSearchString = String.Format(CLASS_FORMAT_STRING, type.FullName);

tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ Of course, feel free to check any of the [examples](https://github.com/Azure/azu
463463

464464
The test-proxy is a record/playback solution. As a result, there a few concepts that devs will likely recognize from other record/playback solutions:
465465

466-
- A `Sanitizer` is used to remove sensitive information prior to storage. When a request comes in during `playback` mode, `sanitizers` are applied to the request prior to matching to a recording.
466+
- A `Sanitizer` is used to remove sensitive information prior to storage. Sanitizers are applied...
467+
- To the request matching against the recording entries during `playback` mode.
468+
- To the recording entries loaded from disk when starting a `playback` session.
469+
- To the recording entries _before_ they are saved to disk when stopping a `record` session.
467470
- `Matchers` are used to retrieve a request/response pair from a previous recording. By default, it functions by comparing `URI`, `Headers`, and `Body`. As of now, only a single matcher can be used when retrieving an entry during playback.
468471
- A `Transform` is used when a user needs to "transform" a matched recording response with some value from the incoming request. This action is specific to `playback` mode. For instance, the test-proxy has two default `transforms`:
469472
- `x-ms-client-id` is copied from request and applied to response prior to return.
@@ -551,6 +554,78 @@ In some cases, users need to register a lot (10+) of sanitizers. In this case, g
551554
]
552555
```
553556

557+
#### Knowing what was added
558+
559+
When `AddSanitizer` or `AddSanitizers` is called, check the response `body` for an array containing the ids of sanitizers that have been registered.
560+
561+
Example response body:
562+
563+
```jsonc
564+
// POSTS to Admin/AddSanitizer has individual result
565+
{
566+
"Sanitizer": "3"
567+
}
568+
```
569+
570+
```jsonc
571+
// POSTS to Admin/AddSanitizers has multiple results
572+
{
573+
"Sanitizers": ["3", "4", "9"]
574+
}
575+
```
576+
577+
#### Removing a sanitizer
578+
579+
Following #8120, sanitizers were given identifiers so that they can be removed.
580+
581+
- The default `session` sanitizers list can be found in code here.
582+
- Visiting `http://localhost:5000/Info/Active` on your browser when the proxy is running on your machine will present you with an easy summary of these available sanitizers as well.
583+
584+
When a recording or playback session is begun (`Playback/Start` or `Record/Start`), all the **current** session sanitizers are applied to the recording. Session sanitizers added **after** record or playback has begun do not apply to the prior-started session.
585+
586+
To remove a session-level sanitizer, one only must...
587+
588+
```jsonc
589+
// request method = `POST`
590+
// request URI = <proxyUrl>/Admin/RemoveSanitizers
591+
// request headers =
592+
{
593+
"Content-Type": "application/json",
594+
"Content-Length": 36
595+
}
596+
// request body =
597+
{
598+
Sanitizers: ["AZSDK002", "AZSDK003"]
599+
}
600+
```
601+
602+
On successful request, users will receive a list of the removed identifiers.
603+
604+
```jsonc
605+
// response body
606+
{
607+
Removed: ["ID1"]
608+
}
609+
```
610+
611+
To remove a sanitizer from a specific recording...
612+
613+
```jsonc
614+
// request method = `POST`
615+
// request URI = <proxyUrl>/Admin/RemoveSanitizers
616+
// request headers =
617+
{
618+
"Content-Type": "application/json",
619+
"Content-Length": 36,
620+
"x-recording-id": "your-recording-id-here"
621+
}
622+
// request body =
623+
{
624+
Sanitizers: ["AZSDK002", "AZSDK003"]
625+
}
626+
```
627+
628+
554629
### Set a Matcher
555630

556631
Setting a matcher is just like adding a `sanitizer`. Set the `x-abstraction-identifier` value to the name of the matcher you want to instantiate, and provide the proper constructor arguments in the body! Check `Info/Available/` for available matchers.

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Views/Info/ActiveExtensions.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<table style="width:100%">
5555
<tr>
5656
<td>
57-
<h2>@extension.Name</h2>
57+
<h2>"@extension.SanitizerId" - @extension.Name</h2>
5858
</td>
5959
</tr>
6060
<tr>

0 commit comments

Comments
 (0)