You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Docs] Add info about test proxy sanitizer opt-out (#35579)
Added some documentation updates regarding test proxy sanitization.
Signed-off-by: Paul Van Eck <[email protected]>
Co-authored-by: McCoy Patiño <[email protected]>
In some cases, a value in a response body is used in the following request as part of the URL, body, or headers. If this value is sanitized, the recorded request might differ than what is expected during playback. Common culprits include sanitization of "name", "id", and "Location" fields. To resolve this, you can either opt out of specific sanitization or add another sanitizer to align with the sanitized value.
150
+
151
+
#### Opt out
152
+
153
+
You can opt out of sanitization for the fields that are used for your requests by calling the `remove_batch_sanitizer` method from `devtools_testutils` with the [sanitizer IDs][test_proxy_sanitizers] to exclude. Generally, this is done in the `conftest.py` file, in the one of the session-scoped fixtures. Example:
154
+
155
+
```python
156
+
from devtools_testutils import remove_batch_sanitizers, test_proxy
157
+
158
+
159
+
@pytest.fixture(scope="session", autouse=True)
160
+
defadd_sanitizers(test_proxy):
161
+
...
162
+
# Remove the following body key sanitizer: AZSDK3493: $..name
163
+
remove_batch_sanitizers(["AZSDK3493"])
164
+
```
165
+
166
+
Some sanitizer IDs that are often opted out of are:
167
+
-`AZSDK2003`: `Location` - Header regex sanitizer
168
+
-`AZSDK3430`: `$..id` - Body key sanitizer
169
+
-`AZSDK3493`: `$..name` - Body key sanitizer
170
+
171
+
However, **please be mindful when opting out of a sanitizer, and ensure that no sensitive data is being exposed**.
172
+
173
+
#### Add another sanitizer
174
+
175
+
Alternatively, you can add another sanitizer to align the recorded request with the expected request, modifying the URL, body, or headers as needed. Example:
176
+
177
+
```python
178
+
from devtools_testutils import add_uri_regex_sanitizer
Copy file name to clipboardExpand all lines: doc/dev/tests.md
+30-12Lines changed: 30 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ testing infrastructure, and demonstrates how to write and run tests for a servic
24
24
-[Update test recordings](#update-test-recordings)
25
25
-[Sanitize secrets](#sanitize-secrets)
26
26
-[Special case: SAS tokens](#special-case-sas-tokens)
27
+
-[Opt out of specific sanitizers](#opt-out-of-specific-sanitizers)
27
28
-[Functional vs. unit tests](#functional-vs-unit-tests)
28
29
-[Further reading](#further-reading)
29
30
@@ -163,7 +164,7 @@ the
163
164
164
165
## Write or run tests
165
166
166
-
Newer SDK tests utilize the [Azure SDK Tools Test Proxy][proxy_general_docs] to record and playback HTTP interactions.
167
+
Newer SDK tests utilize the [Azure SDK Tools Test Proxy][proxy_general_docs] to record and playback HTTP interactions, while also automatically sanitizing sensitive information from recordings.
167
168
To migrate an existing test suite to use the test proxy, or to learn more about using the test proxy, refer to the
@@ -437,11 +438,14 @@ The `.json` files created from running tests in live mode can include authorizat
437
438
access signatures, and other secrets. The recordings are included in our public GitHub repository, making it important
438
439
for us to remove any secrets from these recordings before committing them to the repository.
439
440
441
+
By default, the test proxy server sanitizes several [common patterns][test_proxy_sanitizers] of secrets, but there are additional
442
+
steps you can take to ensure that any other sensitive information is removed from recordings.
443
+
440
444
There are two primary ways to keep secrets from being written into recordings:
441
445
442
446
1. The `EnvironmentVariableLoader` will automatically sanitize the values of captured environment variables with the
443
447
provided fake values.
444
-
2.Sanitizers can be registered via `add_*_sanitizer` methods in `devtools_testutils`. For example, the general-use
448
+
2.Additional sanitizers can be registered via `add_*_sanitizer` methods in `devtools_testutils`. For example, the general-use
445
449
method for sanitizing recording bodies, headers, and URIs is `add_general_string_sanitizer`. Other sanitizers are
446
450
available for more specific scenarios and can be found at [devtools_testutils/sanitizers.py][py_sanitizers].
447
451
@@ -480,15 +484,29 @@ For more details about sanitizers and their options, please refer to [devtools_t
480
484
481
485
#### Special case: SAS tokens
482
486
483
-
Tests that use a Shared Access Signature (SAS) token to authenticate a client should use the
484
-
[`AzureRecordedTestCase.generate_sas`][generate_sas] method to generate the token. This will automatically register a
485
-
sanitizer to keep this token out of test recordings. An example of using this method can be found
486
-
[here][generate_sas_example].
487
+
In the past, it was recommended that the tests using Shared Access Signature (SAS) tokens should use the `AzureRecordedTestCase.generate_sas` method to generate the token and automatically register a sanitizer to keep this token out of test recordings. This method is now deprecated since the test proxy automatically sanitizes SAS tokens. If you have tests that use SAS tokens, you can remove the usage of the `generate_sas` method.
488
+
489
+
#### Opt out of specific sanitizers
490
+
491
+
Since, in some cases, the default sanitizers might be considered too aggressive and breaks tests during playback, you can opt out of certain sanitizers using the `remove_batch_sanitizers` function in your respective `conftest.py` files. For example:
492
+
493
+
```python
494
+
from devtools_testutils import remove_batch_sanitizers, test_proxy
495
+
496
+
497
+
@pytest.fixture(scope="session", autouse=True)
498
+
defadd_sanitizers(test_proxy):
499
+
...
500
+
# Remove the following body key sanitizer: AZSDK3493: $..name
501
+
remove_batch_sanitizers(["AZSDK3493"])
502
+
```
503
+
504
+
A list of sanitizers and their IDs can be found [here][test_proxy_sanitizers]. However, **please be mindful when opting out of a sanitizer, and ensure that no sensitive data is being exposed**.
487
505
488
-
`generate_sas` accepts any number of positional arguments: the first being the method that creates the SAS, and the
489
-
remaining positional arguments being positional arguments for the SAS-generating method. Any keyword arguments given to
490
-
`generate_sas` will be passed to the SAS-generating method as well. The generated token will be returned and its value
491
-
will be sanitized.
506
+
Some sanitizers IDs that are often opted out of are:
507
+
-`AZSDK2003`: `Location` - Header regex sanitizer
508
+
-`AZSDK3430`: `$..id` - Body key sanitizer
509
+
-`AZSDK3493`: `$..name` - Body key sanitizer
492
510
493
511
## Functional vs. unit tests
494
512
@@ -561,7 +579,6 @@ For information about more advanced testing scenarios, refer to the [advanced te
0 commit comments