Skip to content

Commit

Permalink
[KV] Update logging tests (Azure#21454)
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp authored Oct 28, 2021
1 parent ab34056 commit 4786890
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,20 @@ def test_logging_enabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
if body["provider"] == "Test":
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -622,15 +626,21 @@ def test_logging_disabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
assert body["provider"] != "Test", "Client request body was logged"
# the body of the request should be JSON
body = json.loads(section)
if body["provider"] == "Test":
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()

@only_2016_10_01()
@client_setup
def test_models(self, client, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,20 @@ async def test_logging_enabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
if body["provider"] == "Test":
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -628,15 +632,21 @@ async def test_logging_disabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
assert body["provider"] != "Test", "Client request body was logged"
# the body of the request should be JSON
body = json.loads(section)
if body["provider"] == "Test":
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()

@all_api_versions()
@client_setup
async def test_get_certificate_version(self, client, **kwargs):
Expand Down
28 changes: 19 additions & 9 deletions sdk/keyvault/azure-keyvault-keys/tests/test_key_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,21 @@ def test_logging_enabled(self, client, is_hsm, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
expected_kty = "RSA-HSM" if is_hsm else "RSA"
if body["kty"] == expected_kty:
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON or has no kty property
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -453,16 +457,22 @@ def test_logging_disabled(self, client, is_hsm, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
expected_kty = "RSA-HSM" if is_hsm else "RSA"
assert body["kty"] != expected_kty, "Client request body was logged"
if body["kty"] == expected_kty:
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON or has no kty property
pass

mock_handler.close()

@only_hsm_7_3_preview()
@client_setup
def test_get_random_bytes(self, client, **kwargs):
Expand Down
28 changes: 19 additions & 9 deletions sdk/keyvault/azure-keyvault-keys/tests/test_keys_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,21 @@ async def test_logging_enabled(self, client, is_hsm, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
expected_kty = "RSA-HSM" if is_hsm else "RSA"
if body["kty"] == expected_kty:
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON or has no kty property
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -450,16 +454,22 @@ async def test_logging_disabled(self, client, is_hsm, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
expected_kty = "RSA-HSM" if is_hsm else "RSA"
assert body["kty"] != expected_kty, "Client request body was logged"
if body["kty"] == expected_kty:
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON or has no kty property
pass

mock_handler.close()

@only_hsm_7_3_preview()
@client_setup
async def test_get_random_bytes(self, client, **kwargs):
Expand Down
26 changes: 18 additions & 8 deletions sdk/keyvault/azure-keyvault-secrets/tests/test_secrets_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,20 @@ async def test_logging_enabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
if body["value"] == "secret-value":
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -317,15 +321,21 @@ async def test_logging_disabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
assert body["value"] != "secret-value", "Client request body was logged"
# the body of the request should be JSON
body = json.loads(section)
if body["value"] == "secret-value":
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
pass

mock_handler.close()


def test_service_headers_allowed_in_logs():
service_headers = {"x-ms-keyvault-network-info", "x-ms-keyvault-region", "x-ms-keyvault-service-version"}
Expand Down
26 changes: 18 additions & 8 deletions sdk/keyvault/azure-keyvault-secrets/tests/test_secrets_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,20 @@ def test_logging_enabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
# the body of the request should be JSON
body = json.loads(section)
if body["value"] == "secret-value":
mock_handler.close()
return
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
# this means the request section is not JSON
pass

mock_handler.close()
assert False, "Expected request body wasn't logged"

@logging_disabled()
Expand All @@ -323,15 +327,21 @@ def test_logging_disabled(self, client, **kwargs):

for message in mock_handler.messages:
if message.levelname == "DEBUG" and message.funcName == "on_request":
messages_request = message.message.split("/n")
for m in messages_request:
# parts of the request are logged on new lines in a single message
request_sections = message.message.split("/n")
for section in request_sections:
try:
body = json.loads(m)
assert body["value"] != "secret-value", "Client request body was logged"
# the body of the request should be JSON
body = json.loads(section)
if body["value"] == "secret-value":
mock_handler.close()
assert False, "Client request body was logged"
except (ValueError, KeyError):
# this means the message is not JSON or has no kty property
pass

mock_handler.close()


def test_service_headers_allowed_in_logs():
service_headers = {"x-ms-keyvault-network-info", "x-ms-keyvault-region", "x-ms-keyvault-service-version"}
Expand Down

0 comments on commit 4786890

Please sign in to comment.