|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | +import requests |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from .config import PROXY_URL |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from typing import Any, Dict |
| 13 | + |
| 14 | + |
| 15 | +def add_body_key_sanitizer(**kwargs): |
| 16 | + # type: (**Any) -> None |
| 17 | + """Registers a sanitizer that offers regex update of a specific JTokenPath within a returned body. |
| 18 | +
|
| 19 | + For example, "TableName" within a json response body having its value replaced by whatever substitution is offered. |
| 20 | +
|
| 21 | + :keyword str json_path: The SelectToken path (which could possibly match multiple entries) that will be used to |
| 22 | + select JTokens for value replacement. |
| 23 | + :keyword str value: The substitution value. |
| 24 | + :keyword str regex: A regex. Can be defined as a simple regex replace OR if groupForReplace is set, a subsitution |
| 25 | + operation. Defaults to replacing the entire string. |
| 26 | + :keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking |
| 27 | + a simple replacement operation. |
| 28 | + """ |
| 29 | + |
| 30 | + request_args = _get_request_args(**kwargs) |
| 31 | + _send_request("BodyKeySanitizer", request_args) |
| 32 | + |
| 33 | + |
| 34 | +def add_body_regex_sanitizer(**kwargs): |
| 35 | + # type: (**Any) -> None |
| 36 | + """Registers a sanitizer that offers regex replace within a returned body. |
| 37 | + |
| 38 | + Specifically, this means regex applying to the raw JSON. If you are attempting to simply replace a specific key, the |
| 39 | + BodyKeySanitizer is probably the way to go. |
| 40 | +
|
| 41 | + :keyword str value: The substitution value. |
| 42 | + :keyword str regex: A regex. Can be defined as a simple regex, or if a ``group_for_replace`` is provided, a |
| 43 | + substitution operation. |
| 44 | + :keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking |
| 45 | + a simple replacement operation. |
| 46 | + """ |
| 47 | + |
| 48 | + request_args = _get_request_args(**kwargs) |
| 49 | + _send_request("BodyRegexSanitizer", request_args) |
| 50 | + |
| 51 | + |
| 52 | +def add_continuation_sanitizer(**kwargs): |
| 53 | + # type: (**Any) -> None |
| 54 | + """Registers a sanitizer that's used to anonymize private keys in response/request pairs. |
| 55 | +
|
| 56 | + For instance, a request hands back a "sessionId" that needs to be present in the next request. Supports "all further |
| 57 | + requests get this key" as well as "single response/request pair". Defaults to maintaining same key for rest of |
| 58 | + recording. |
| 59 | +
|
| 60 | + :keyword str key: The name of the header whos value will be replaced from response -> next request. |
| 61 | + :keyword str method: The method by which the value of the targeted key will be replaced. Defaults to guid |
| 62 | + replacement. |
| 63 | + :keyword str reset_after_first: Do we need multiple pairs replaced? Or do we want to replace each value with the |
| 64 | + same value? |
| 65 | + """ |
| 66 | + |
| 67 | + request_args = _get_request_args(**kwargs) |
| 68 | + _send_request("ContinuationSanitizer", request_args) |
| 69 | + |
| 70 | + |
| 71 | +def add_general_regex_sanitizer(**kwargs): |
| 72 | + # type: (**Any) -> None |
| 73 | + """Registers a sanitizer that offers a general regex replace across request/response Body, Headers, and URI. |
| 74 | +
|
| 75 | + For the body, this means regex applying to the raw JSON. |
| 76 | +
|
| 77 | + :keyword str value: The substitution value. |
| 78 | + :keyword str regex: A regex. Can be defined as a simple regex, or if a ``group_for_replace`` is provided, a |
| 79 | + substitution operation. |
| 80 | + :keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking |
| 81 | + a simple replacement operation. |
| 82 | + """ |
| 83 | + |
| 84 | + request_args = _get_request_args(**kwargs) |
| 85 | + _send_request("GeneralRegexSanitizer", request_args) |
| 86 | + |
| 87 | + |
| 88 | +def add_header_regex_sanitizer(**kwargs): |
| 89 | + # type: (**Any) -> None |
| 90 | + """Registers a sanitizer that offers regex replace on returned headers. |
| 91 | +
|
| 92 | + Can be used for multiple purposes: 1) To replace a key with a specific value, do not set "regex" value. 2) To do a |
| 93 | + simple regex replace operation, define arguments "key", "value", and "regex". 3) To do a targeted substitution of a |
| 94 | + specific group, define all arguments "key", "value", and "regex". |
| 95 | +
|
| 96 | + :keyword str key: The name of the header we're operating against. |
| 97 | + :keyword str value: The substitution or whole new header value, depending on "regex" setting. |
| 98 | + :keyword str regex: A regex. Can be defined as a simple regex, or if a ``group_for_replace`` is provided, a |
| 99 | + substitution operation. |
| 100 | + :keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking |
| 101 | + a simple replacement operation. |
| 102 | + """ |
| 103 | + |
| 104 | + request_args = _get_request_args(**kwargs) |
| 105 | + _send_request("HeaderRegexSanitizer", request_args) |
| 106 | + |
| 107 | + |
| 108 | +def add_oauth_response_sanitizer(): |
| 109 | + # type: () -> None |
| 110 | + """Registers a sanitizer that cleans out all request/response pairs that match an oauth regex in their URI.""" |
| 111 | + |
| 112 | + _send_request("OAuthResponseSanitizer", {}) |
| 113 | + |
| 114 | + |
| 115 | +def add_remove_header_sanitizer(**kwargs): |
| 116 | + # type: (**Any) -> None |
| 117 | + """Registers a sanitizer that removes specified headers before saving a recording. |
| 118 | +
|
| 119 | + :keyword str headers: A comma separated list. Should look like "Location, Transfer-Encoding" or something along |
| 120 | + those lines. Don't worry about whitespace between the commas separating each key. They will be ignored. |
| 121 | + """ |
| 122 | + |
| 123 | + request_args = _get_request_args(**kwargs) |
| 124 | + _send_request("RemoveHeaderSanitizer", request_args) |
| 125 | + |
| 126 | + |
| 127 | +def add_request_subscription_id_sanitizer(**kwargs): |
| 128 | + # type: (**Any) -> None |
| 129 | + """Registers a sanitizer that replaces subscription IDs in requests. |
| 130 | +
|
| 131 | + Subscription IDs are replaced with "00000000-0000-0000-0000-000000000000" by default. |
| 132 | +
|
| 133 | + :keyword str value: The fake subscriptionId that will be placed where the real one is in the real request. |
| 134 | + """ |
| 135 | + |
| 136 | + request_args = _get_request_args(**kwargs) |
| 137 | + _send_request("ReplaceRequestSubscriptionId", request_args) |
| 138 | + |
| 139 | + |
| 140 | +def add_uri_regex_sanitizer(**kwargs): |
| 141 | + # type: (**Any) -> None |
| 142 | + """Registers a sanitizer for cleaning URIs via regex. |
| 143 | +
|
| 144 | + :keyword str value: The substitution value. |
| 145 | + :keyword str regex: A regex. Can be defined as a simple regex, or if a ``group_for_replace`` is provided, a |
| 146 | + substitution operation. |
| 147 | + :keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking |
| 148 | + a simple replacement operation. |
| 149 | + """ |
| 150 | + |
| 151 | + request_args = _get_request_args(**kwargs) |
| 152 | + _send_request("UriRegexSanitizer", request_args) |
| 153 | + |
| 154 | + |
| 155 | +def _get_request_args(**kwargs): |
| 156 | + # type: (**Any) -> Dict |
| 157 | + """Returns a dictionary of sanitizer constructor headers""" |
| 158 | + |
| 159 | + request_args = {} |
| 160 | + request_args["groupForReplace"] = kwargs.get("group_for_replace") |
| 161 | + request_args["headersForRemoval"] = kwargs.get("headers") |
| 162 | + request_args["jsonPath"] = kwargs.get("json_path") |
| 163 | + request_args["key"] = kwargs.get("key") |
| 164 | + request_args["method"] = kwargs.get("method") |
| 165 | + request_args["regex"] = kwargs.get("regex") |
| 166 | + request_args["resetAfterFirst"] = kwargs.get("reset_after_first") |
| 167 | + request_args["value"] = kwargs.get("value") |
| 168 | + return request_args |
| 169 | + |
| 170 | + |
| 171 | +def _send_request(sanitizer, parameters): |
| 172 | + # type: (str, Dict) -> None |
| 173 | + """Send a POST request to the test proxy endpoint to register the specified sanitizer. |
| 174 | +
|
| 175 | + :param str sanitizer: The name of the sanitizer to add. |
| 176 | + :param dict parameters: The sanitizer constructor parameters, as a dictionary. |
| 177 | + """ |
| 178 | + |
| 179 | + requests.post( |
| 180 | + "{}/Admin/AddSanitizer".format(PROXY_URL), |
| 181 | + headers={"x-abstraction-identifier": sanitizer}, |
| 182 | + json=parameters |
| 183 | + ) |
0 commit comments