@@ -166,44 +166,26 @@ std::unique_ptr<Azure::Core::Http::Policies::HttpPolicy> TestProxyManager::GetTe
166
166
return std::make_unique<Azure::Core::Test::TestProxyPolicy>(this );
167
167
}
168
168
169
+ namespace {
170
+ const char * g_accountRegex = " https://(?<account>[a-zA-Z0-9\\ -]+)\\ ." ;
171
+ }
172
+
169
173
bool TestProxyManager::CheckSanitizers ()
170
174
{
171
175
Azure::Core::Url checkRequest (m_proxy);
172
-
173
176
checkRequest.AppendPath (" Info" );
174
177
checkRequest.AppendPath (" Active" );
175
178
176
- {
177
- Azure::Core::Http::Request request (Azure::Core::Http::HttpMethod::Get, checkRequest);
178
- Azure::Core::Context ctx;
179
- auto response = m_privatePipeline->Send (request, ctx);
179
+ Azure::Core::Http::Request request (Azure::Core::Http::HttpMethod::Get, checkRequest);
180
+ Azure::Core::Context ctx;
181
+ auto response = m_privatePipeline->Send (request, ctx);
180
182
181
- auto rawResponse = response->GetBody ();
182
- std::string stringBody (rawResponse.begin (), rawResponse.end ());
183
- std::string regex = " \" https://(?<account>[a-zA-Z0-9\\ -]+).\" " ;
184
- std::vector<std::string> stringsInOrder
185
- = {" UriRegexSanitizer" ,
186
- regex,
187
- " BodyRegexSanitizer" ,
188
- regex,
189
- " HeaderRegexSanitizer" ,
190
- regex,
191
- " GeneralRegexSanitizer" ,
192
- regex,
193
- " CustomDefaultMatcher" };
194
-
195
- size_t position = 0 ;
196
-
197
- for (auto & part : stringsInOrder)
198
- {
199
- position = stringBody.find (part, position);
200
- if (position == std::string::npos)
201
- {
202
- return false ;
203
- }
204
- }
183
+ auto rawResponse = response->GetBody ();
184
+ std::string stringBody (rawResponse.begin (), rawResponse.end ());
185
+ if (stringBody.find (g_accountRegex) == std::string::npos)
186
+ {
187
+ return false ;
205
188
}
206
-
207
189
return true ;
208
190
}
209
191
@@ -213,25 +195,62 @@ void TestProxyManager::SetProxySanitizer()
213
195
{
214
196
return ;
215
197
}
216
- Azure::Core::Url sanitizerRequest (m_proxy);
217
- sanitizerRequest.AppendPath (" Admin" );
218
- sanitizerRequest.AppendPath (" AddSanitizer" );
219
- const std::string urlAccountRegex = R"json(
220
- {
221
- "key": "Location",
222
- "value": "REDACTED",
223
- "regex": "https://(?<account>[a-zA-Z0-9\\-]+).",
224
- "groupForReplace": "account"
225
- }
226
- )json" ;
227
- const std::string bodyClientSecretRegex = R"json(
198
+ // we have 3 types of sanitizer,
199
+ // see
200
+ // https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md#a-note-about-where-sanitizers-apply
201
+ enum class SanitizerType
202
+ {
203
+ Uri,
204
+ Header,
205
+ Body,
206
+ General,
207
+ };
208
+ auto addSanitizer = [&](SanitizerType type,
209
+ const std::string& regex,
210
+ const std::string& groupName,
211
+ const std::string& headerName = std::string ()) {
212
+ const std::map<SanitizerType, std::string> abstractionIdentifierValues = {
213
+ {SanitizerType::Uri, " UriRegexSanitizer" },
214
+ {SanitizerType::Header, " HeaderRegexSanitizer" },
215
+ {SanitizerType::Body, " BodyRegexSanitizer" },
216
+ {SanitizerType::General, " GeneralRegexSanitizer" },
217
+ };
218
+
219
+ Azure::Core::Url sanitizerRequest (m_proxy);
220
+ sanitizerRequest.AppendPath (" Admin" );
221
+ sanitizerRequest.AppendPath (" AddSanitizer" );
222
+
223
+ auto jsonRoot = Json::_internal::json::object ();
224
+ jsonRoot[" value" ] = " REDACTED" ;
225
+ jsonRoot[" regex" ] = regex;
226
+ jsonRoot[" groupForReplace" ] = groupName;
227
+ if (!headerName.empty ())
228
228
{
229
- "key": "Location",
230
- "value": "REDACTED",
231
- "regex": "client_secret=(?<clientsecret>[a-zA-Z0-9\\%]+)",
232
- "groupForReplace": "clientsecret"
229
+ jsonRoot[" key" ] = headerName;
233
230
}
234
- )json" ;
231
+ auto jsonString = jsonRoot.dump ();
232
+
233
+ Azure::Core::IO::MemoryBodyStream payloadStream (
234
+ reinterpret_cast <const uint8_t *>(jsonString.data ()), jsonString.size ());
235
+ Azure::Core::Http::Request request (
236
+ Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
237
+ request.SetHeader (" x-abstraction-identifier" , abstractionIdentifierValues.at (type));
238
+ Azure::Core::Context ctx;
239
+ auto response = m_privatePipeline->Send (request, ctx);
240
+ (void )response;
241
+ };
242
+
243
+ addSanitizer (SanitizerType::General, g_accountRegex, " account" );
244
+ addSanitizer (
245
+ SanitizerType::Body, " client_secret=(?<clientsecret>[a-zA-Z0-9\\ %]+)" , " clientsecret" );
246
+ const std::string storageSasSignatureRegex = " \\ ?.*sig=(?<sassig>[a-zA-Z0-9\\ %\\ /+=]+)" ;
247
+ addSanitizer (SanitizerType::Uri, storageSasSignatureRegex, " sassig" );
248
+ addSanitizer (SanitizerType::Header, storageSasSignatureRegex, " sassig" , " x-ms-copy-source" );
249
+ addSanitizer (SanitizerType::Header, storageSasSignatureRegex, " sassig" , " x-ms-rename-source" );
250
+ const std::string storageUserDelegationKeyRegex
251
+ = " \\ u003CValue\\ u003E(?<userdelegationkey>[a-zA-Z0-9\\ /=+]+).*\\ u003C\\ /"
252
+ " UserDelegationKey\\ u003E" ;
253
+ addSanitizer (SanitizerType::Body, storageUserDelegationKeyRegex, " userdelegationkey" );
235
254
236
255
Azure::Core::Url matcherRequest (m_proxy);
237
256
matcherRequest.AppendPath (" Admin" );
@@ -280,53 +299,6 @@ void TestProxyManager::SetProxySanitizer()
280
299
});
281
300
matcherBody = jsonRoot.dump ();
282
301
}
283
-
284
- {
285
- Azure::Core::IO::MemoryBodyStream payloadStream (
286
- reinterpret_cast <const uint8_t *>(urlAccountRegex.data ()), urlAccountRegex.size ());
287
- Azure::Core::Http::Request request (
288
- Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
289
- request.SetHeader (" x-abstraction-identifier" , " UriRegexSanitizer" );
290
- Azure::Core::Context ctx;
291
- auto response = m_privatePipeline->Send (request, ctx);
292
- }
293
- {
294
- Azure::Core::IO::MemoryBodyStream payloadStream (
295
- reinterpret_cast <const uint8_t *>(urlAccountRegex.data ()), urlAccountRegex.size ());
296
- Azure::Core::Http::Request request (
297
- Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
298
- request.SetHeader (" x-abstraction-identifier" , " BodyRegexSanitizer" );
299
- Azure::Core::Context ctx;
300
- auto response = m_privatePipeline->Send (request, ctx);
301
- }
302
- {
303
- Azure::Core::IO::MemoryBodyStream payloadStream (
304
- reinterpret_cast <const uint8_t *>(bodyClientSecretRegex.data ()),
305
- bodyClientSecretRegex.size ());
306
- Azure::Core::Http::Request request (
307
- Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
308
- request.SetHeader (" x-abstraction-identifier" , " BodyRegexSanitizer" );
309
- Azure::Core::Context ctx;
310
- auto response = m_privatePipeline->Send (request, ctx);
311
- }
312
- {
313
- Azure::Core::IO::MemoryBodyStream payloadStream (
314
- reinterpret_cast <const uint8_t *>(urlAccountRegex.data ()), urlAccountRegex.size ());
315
- Azure::Core::Http::Request request (
316
- Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
317
- request.SetHeader (" x-abstraction-identifier" , " HeaderRegexSanitizer" );
318
- Azure::Core::Context ctx;
319
- auto response = m_privatePipeline->Send (request, ctx);
320
- }
321
- {
322
- Azure::Core::IO::MemoryBodyStream payloadStream (
323
- reinterpret_cast <const uint8_t *>(urlAccountRegex.data ()), urlAccountRegex.size ());
324
- Azure::Core::Http::Request request (
325
- Azure::Core::Http::HttpMethod::Post, sanitizerRequest, &payloadStream);
326
- request.SetHeader (" x-abstraction-identifier" , " GeneralRegexSanitizer" );
327
- Azure::Core::Context ctx;
328
- auto response = m_privatePipeline->Send (request, ctx);
329
- }
330
302
{
331
303
Azure::Core::IO::MemoryBodyStream payloadStream (
332
304
reinterpret_cast <const uint8_t *>(matcherBody.data ()), matcherBody.size ());
0 commit comments