-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[KeyVault] Keyvault Keys to Test Proxy #24165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2229ad4
move conftest into the tests folder
kashifkhan 2ce9520
test proxy changes
kashifkhan e76b3e2
new recordings
kashifkhan f702783
more recordings for crud
kashifkhan 252208c
sync test recordings
kashifkhan 1145d13
move over to test proxy
kashifkhan 4392c72
kv async recordings
kashifkhan 42bfeb7
simple clean ups
kashifkhan 6c2c23b
recordings
kashifkhan 70c07f0
clean up imports
kashifkhan 7d246c7
pick right vault name
kashifkhan c50e9a1
clean up
kashifkhan 56d8c83
fix test parse id offline test
kashifkhan f90b9da
override pytest default event loop
kashifkhan 7014c0e
fix for async tests, change to aiohttp request
kashifkhan df172e0
remove commented code
kashifkhan b48e3c7
formatting fixes
kashifkhan 01fe987
Delete vcrpy recordings
kashifkhan 625d34c
with block for async client
kashifkhan a196c30
clean up
kashifkhan da9df1b
code clean ups
kashifkhan 8bcb7d2
move keys specific methods in to a separate class
kashifkhan ddcf29c
PR comments
kashifkhan 9f8611c
refactor test to use preparer
kashifkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
sdk/keyvault/azure-keyvault-keys/tests/_async_test_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import json | ||
| import os | ||
|
|
||
| import pytest | ||
| from azure.core.pipeline import AsyncPipeline | ||
| from azure.core.pipeline.transport import AioHttpTransport, HttpRequest | ||
| from azure.keyvault.keys import KeyReleasePolicy | ||
| from azure.keyvault.keys._shared.client_base import DEFAULT_VERSION, ApiVersion | ||
| from devtools_testutils import AzureRecordedTestCase | ||
|
|
||
|
|
||
| async def get_attestation_token(attestation_uri): | ||
| request = HttpRequest("GET", "{}/generate-test-token".format(attestation_uri)) | ||
| async with AsyncPipeline(transport=AioHttpTransport()) as pipeline: | ||
| response = await pipeline.run(request) | ||
| return json.loads(response.http_response.text())["token"] | ||
|
|
||
|
|
||
| def get_decorator(only_hsm=False, only_vault=False, api_versions=None, **kwargs): | ||
| """returns a test decorator for test parameterization""" | ||
| params = [ | ||
| pytest.param(p[0],p[1], id=p[0] + ("_mhsm" if p[1] else "_vault" )) | ||
| for p in get_test_parameters(only_hsm, only_vault, api_versions=api_versions) | ||
| ] | ||
| return params | ||
|
|
||
|
|
||
| def get_release_policy(attestation_uri, **kwargs): | ||
| release_policy_json = { | ||
| "anyOf": [ | ||
| { | ||
| "anyOf": [ | ||
| { | ||
| "claim": "sdk-test", | ||
| "equals": True | ||
| } | ||
| ], | ||
| "authority": attestation_uri.rstrip("/") + "/" | ||
| } | ||
| ], | ||
| "version": "1.0.0" | ||
| } | ||
| policy_string = json.dumps(release_policy_json).encode() | ||
| return KeyReleasePolicy(policy_string, **kwargs) | ||
|
|
||
|
|
||
| def get_test_parameters(only_hsm=False, only_vault=False, api_versions=None): | ||
| """generates a list of parameter pairs for test case parameterization, where [x, y] = [api_version, is_hsm]""" | ||
| combinations = [] | ||
| versions = api_versions or ApiVersion | ||
| hsm_supported_versions = {ApiVersion.V7_2, ApiVersion.V7_3} | ||
|
|
||
| for api_version in versions: | ||
| if not only_vault and api_version in hsm_supported_versions: | ||
| combinations.append([api_version, True]) | ||
| if not only_hsm: | ||
| combinations.append([api_version, False]) | ||
| return combinations | ||
|
|
||
|
|
||
| def is_public_cloud(): | ||
| return (".microsoftonline.com" in os.getenv('AZURE_AUTHORITY_HOST', '')) | ||
|
|
||
|
|
||
| class AsyncKeysClientPreparer(AzureRecordedTestCase): | ||
| def __init__(self, *args, **kwargs): | ||
| vault_playback_url = "https://vaultname.vault.azure.net" | ||
| hsm_playback_url = "https://managedhsmvaultname.vault.azure.net" | ||
| self.is_logging_enabled = kwargs.pop("logging_enable", True) | ||
|
|
||
| if self.is_live: | ||
| self.vault_url = os.environ["AZURE_KEYVAULT_URL"] | ||
| self.managed_hsm_url = os.environ.get("AZURE_MANAGEDHSM_URL") | ||
| else: | ||
| self.vault_url = vault_playback_url | ||
| self.managed_hsm_url = hsm_playback_url | ||
|
|
||
| self._set_mgmt_settings_real_values() | ||
|
|
||
| def __call__(self, fn): | ||
| async def _preparer(test_class, api_version, is_hsm, **kwargs): | ||
|
|
||
| self._skip_if_not_configured(api_version, is_hsm) | ||
| if not self.is_logging_enabled: | ||
| kwargs.update({"logging_enable": False}) | ||
| endpoint_url = self.managed_hsm_url if is_hsm else self.vault_url | ||
| client = self.create_key_client(endpoint_url, api_version=api_version, **kwargs) | ||
| async with client: | ||
| await fn(test_class, client, is_hsm=is_hsm, managed_hsm_url = self.managed_hsm_url, vault_url = self.vault_url) | ||
|
|
||
| return _preparer | ||
|
|
||
|
|
||
|
|
||
| def create_key_client(self, vault_uri, **kwargs): | ||
|
|
||
| from azure.keyvault.keys.aio import KeyClient | ||
|
|
||
| credential = self.get_credential(KeyClient, is_async=True) | ||
|
|
||
| return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs) | ||
|
|
||
| def create_crypto_client(self, key, **kwargs): | ||
|
|
||
| from azure.keyvault.keys.crypto.aio import CryptographyClient | ||
|
|
||
| credential = self.get_credential(CryptographyClient, is_async=True) | ||
| return self.create_client_from_credential(CryptographyClient, credential=credential, key=key, **kwargs) | ||
|
|
||
| def _get_attestation_uri(self): | ||
| playback_uri = "https://fakeattestation.azurewebsites.net" | ||
| if self.is_live: | ||
| real_uri = os.environ.get("AZURE_KEYVAULT_ATTESTATION_URL") | ||
| if real_uri is None: | ||
| pytest.skip("No AZURE_KEYVAULT_ATTESTATION_URL environment variable") | ||
| self._scrub_url(real_uri, playback_uri) | ||
| return real_uri | ||
| return playback_uri | ||
|
kashifkhan marked this conversation as resolved.
Outdated
|
||
|
|
||
| def _set_mgmt_settings_real_values(self): | ||
| if self.is_live: | ||
| os.environ["AZURE_TENANT_ID"] = os.environ["KEYVAULT_TENANT_ID"] | ||
| os.environ["AZURE_CLIENT_ID"] = os.environ["KEYVAULT_CLIENT_ID"] | ||
| os.environ["AZURE_CLIENT_SECRET"] = os.environ["KEYVAULT_CLIENT_SECRET"] | ||
|
|
||
| def _skip_if_not_configured(self, api_version, is_hsm): | ||
| if self.is_live and api_version != DEFAULT_VERSION: | ||
| pytest.skip("This test only uses the default API version for live tests") | ||
| if self.is_live and is_hsm and self.managed_hsm_url is None: | ||
| pytest.skip("No HSM endpoint for live testing") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.