Skip to content

Commit 05aec3d

Browse files
authored
Redact account keys in recordings (Azure#20537)
1 parent 6a9cb86 commit 05aec3d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

sdk/batch/azure-mgmt-batch/tests/recordings/test_mgmt_batch.test_mgmt_batch_account.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ interactions:
200200
uri: https://centraluseuap.management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test_mgmt_batch_test_mgmt_batch_account3e1b0fe5/providers/Microsoft.Batch/batchAccounts/batch3e1b0fe5/listKeys?api-version=2021-06-01
201201
response:
202202
body:
203-
string: '{"accountName":"batch3e1b0fe5","primary":"3UQ9ry1mRmgftC37/IOylMEnaC713zLTXoMqp/zBQZ1ANY8eLsv1j5lkvN3PnaSevqoKjtfjKFyJ5Vsc6SGA0w==","secondary":"6dM/Myi6VRmOwbqgMjcIv4lSS7SQvlSTCmQX3RiwLvbivKU9oFi5zgdx7oNtOATEbB9rYO8oDkYVwn8PJLaTcg=="}'
203+
string: '{"accountName":"batch3e1b0fe5","primary":"redacted6f7d7a","secondary":"redacted59d978"}'
204204
headers:
205205
cache-control:
206206
- no-cache
@@ -248,7 +248,7 @@ interactions:
248248
uri: https://centraluseuap.management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test_mgmt_batch_test_mgmt_batch_account3e1b0fe5/providers/Microsoft.Batch/batchAccounts/batch3e1b0fe5/regenerateKeys?api-version=2021-06-01
249249
response:
250250
body:
251-
string: '{"accountName":"batch3e1b0fe5","primary":"3UQ9ry1mRmgftC37/IOylMEnaC713zLTXoMqp/zBQZ1ANY8eLsv1j5lkvN3PnaSevqoKjtfjKFyJ5Vsc6SGA0w==","secondary":"Q3pqv2ncSAxxnhTR14lumWnq9GRUVvy8exfqF2q5x6SZYGpregob+HI5eehGuFusbCaHLdzdzr3ZqBKXY3Qtyw=="}'
251+
string: '{"accountName":"batch3e1b0fe5","primary":"redacted6f7d7a","secondary":"redactedebdb55"}'
252252
headers:
253253
cache-control:
254254
- no-cache

sdk/batch/azure-mgmt-batch/tests/test_mgmt_batch.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
# Licensed under the MIT License. See License.txt in the project root for
66
# license information.
77
#--------------------------------------------------------------------------
8+
import binascii
9+
import hashlib
810
import io
11+
import json
912
import logging
1013
import time
1114
import unittest
1215

1316
import requests
17+
import six
1418

1519
import azure.mgmt.batch
1620
from azure.mgmt.batch import models
1721
import azure.mgmt.network.models
1822
from mgmt_batch_preparers import KeyVaultPreparer, SimpleBatchPreparer
1923

24+
from azure_devtools.scenario_tests.recording_processors import GeneralNameReplacer, RecordingProcessor
2025
from devtools_testutils import (
2126
AzureMgmtTestCase,
2227
ResourceGroupPreparer,
@@ -32,10 +37,40 @@
3237
EXPECTED_DEDICATED_CORE_QUOTA = 500
3338
EXPECTED_LOW_PRIO_CORE_QUOTA = 500
3439
EXPECTED_POOL_QUOTA = 100
40+
SECRET_FIELDS = ["primary", "secondary"]
41+
42+
43+
def get_redacted_key(key):
44+
redacted_value = "redacted"
45+
digest = hashlib.sha256(six.ensure_binary(key)).digest()
46+
redacted_value += six.ensure_str(binascii.hexlify(digest))[:6]
47+
return redacted_value
48+
49+
50+
class RecordingRedactor(RecordingProcessor):
51+
"""Removes keys from test recordings"""
52+
53+
def process_response(self, response):
54+
try:
55+
body = json.loads(response["body"]["string"])
56+
except (KeyError, ValueError):
57+
return response
58+
59+
for field in body:
60+
if field in SECRET_FIELDS:
61+
body[field] = get_redacted_key(body[field])
62+
63+
response["body"]["string"] = json.dumps(body)
64+
return response
3565

3666

3767
class MgmtBatchTest(AzureMgmtTestCase):
3868

69+
def __init__(self, *args, **kwargs):
70+
scrubber = GeneralNameReplacer()
71+
redactor = RecordingRedactor()
72+
super(MgmtBatchTest, self).__init__(*args, recording_processors=[redactor, scrubber], **kwargs)
73+
3974
def setUp(self):
4075
super(MgmtBatchTest, self).setUp()
4176
self.mgmt_batch_client = self.create_mgmt_client(

0 commit comments

Comments
 (0)