-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[Key Vault] Test keys library against managed HSM #17458
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 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1192115
Parameterize test_key_client.py
mccoyp 50615b1
Parameterize test_keys_async.py
mccoyp 906ce07
Parameterize test_crypto_client.py
mccoyp a6827ba
Parameterize test_crypto_client_async.py
mccoyp 26a14cf
Add HSM config to live keys tests
mccoyp b473e2b
Shared keys test classes
mccoyp 9e1e0ab
Refactor tests
mccoyp c1e9392
Clean up test skipping helper
mccoyp a961e53
Base class for KeysTestCase
mccoyp edb30f3
Simplify base class logic
mccoyp 06a603b
_test_case_base -> _test_case
mccoyp 295f2b8
Drop PSH prep. in vault/hsm tests
mccoyp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "include": [ | ||
| { | ||
| "Agent": { | ||
| "ubuntu-18.04": { | ||
| "OSVmImage": "MMSUbuntu18.04", | ||
| "Pool": "azsdk-pool-mms-ubuntu-1804-general" | ||
| } | ||
| }, | ||
| "HSM": { | ||
| "ArmTemplateParameters": "@{ enableHsm = $true }" | ||
| }, | ||
| "PythonVersion": "3.9", | ||
| "CoverageArg": "" | ||
| } | ||
| ] | ||
| } |
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,52 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import os | ||
|
|
||
| from azure.keyvault.keys import KeyClient | ||
| from azure.keyvault.keys.crypto import CryptographyClient | ||
| from azure.keyvault.keys._shared import HttpChallengeCache | ||
| from parameterized import parameterized | ||
| import pytest | ||
| from six.moves.urllib_parse import urlparse | ||
|
|
||
| from _shared.test_case import KeyVaultTestCase | ||
|
|
||
|
|
||
| def suffixed_test_name(testcase_func, param_num, param): | ||
| suffix = "mhsm" if param.kwargs.get("is_hsm") else "vault" | ||
| return "{}_{}".format(testcase_func.__name__, parameterized.to_safe_name(suffix)) | ||
|
|
||
|
|
||
| class KeysTestCase(KeyVaultTestCase): | ||
| def setUp(self, *args, **kwargs): | ||
| playback_url = "https://managedhsmname.managedhsm.azure.net" | ||
| if self.is_live: | ||
| self.managed_hsm_url = os.environ.get("AZURE_MANAGEDHSM_URL") | ||
| if self.managed_hsm_url: | ||
| real = urlparse(self.managed_hsm_url) | ||
| playback = urlparse(playback_url) | ||
| self.scrubber.register_name_pair(real.netloc, playback.netloc) | ||
| else: | ||
| self.managed_hsm_url = playback_url | ||
| super(KeysTestCase, self).setUp(*args, **kwargs) | ||
|
|
||
| def tearDown(self): | ||
| HttpChallengeCache.clear() | ||
| assert len(HttpChallengeCache._cache) == 0 | ||
| super(KeysTestCase, self).tearDown() | ||
|
|
||
| def create_key_client(self, vault_uri, **kwargs): | ||
| credential = self.get_credential(KeyClient) | ||
| return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs) | ||
|
|
||
| def create_crypto_client(self, key, **kwargs): | ||
| credential = self.get_credential(CryptographyClient) | ||
| return self.create_client_from_credential(CryptographyClient, credential=credential, key=key, **kwargs) | ||
|
|
||
| def _skip_if_not_configured(self, is_hsm): | ||
| if self.is_live and is_hsm: | ||
| if self.managed_hsm_url is None: | ||
| pytest.skip("No HSM endpoint for live testing") | ||
| return False |
52 changes: 52 additions & 0 deletions
52
sdk/keyvault/azure-keyvault-keys/tests/_test_case_async.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,52 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import os | ||
| from urllib.parse import urlparse | ||
|
|
||
| from azure.keyvault.keys.aio import KeyClient | ||
| from azure.keyvault.keys.crypto.aio import CryptographyClient | ||
| from azure.keyvault.keys._shared import HttpChallengeCache | ||
| from parameterized import parameterized | ||
| import pytest | ||
|
|
||
| from _shared.test_case_async import KeyVaultTestCase | ||
|
|
||
|
|
||
| def suffixed_test_name(testcase_func, param_num, param): | ||
| suffix = "mhsm" if param.kwargs.get("is_hsm") else "vault" | ||
| return "{}_{}".format(testcase_func.__name__, parameterized.to_safe_name(suffix)) | ||
|
|
||
|
|
||
| class KeysTestCase(KeyVaultTestCase): | ||
| def setUp(self, *args, **kwargs): | ||
| playback_url = "https://managedhsmname.managedhsm.azure.net" | ||
| if self.is_live: | ||
| self.managed_hsm_url = os.environ.get("AZURE_MANAGEDHSM_URL") | ||
| if self.managed_hsm_url: | ||
| real = urlparse(self.managed_hsm_url) | ||
| playback = urlparse(playback_url) | ||
| self.scrubber.register_name_pair(real.netloc, playback.netloc) | ||
| else: | ||
| self.managed_hsm_url = playback_url | ||
| super().setUp(*args, **kwargs) | ||
|
|
||
| def tearDown(self): | ||
| HttpChallengeCache.clear() | ||
| assert len(HttpChallengeCache._cache) == 0 | ||
| super().tearDown() | ||
|
|
||
| def create_key_client(self, vault_uri, **kwargs): | ||
| 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): | ||
| credential = self.get_credential(CryptographyClient, is_async=True) | ||
| return self.create_client_from_credential(CryptographyClient, credential=credential, key=key, **kwargs) | ||
|
|
||
| def _skip_if_not_configured(self, is_hsm): | ||
| if self.is_live and is_hsm: | ||
| if self.managed_hsm_url is None: | ||
| pytest.skip("No HSM endpoint for live testing") | ||
| return False | ||
131 changes: 131 additions & 0 deletions
131
...keyvault/azure-keyvault-keys/tests/recordings/test_crypto_client.test_ec_key_id_mhsm.yaml
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,131 @@ | ||
| interactions: | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '0' | ||
| Content-Type: | ||
| - application/json | ||
| User-Agent: | ||
| - azsdk-python-keyvault-keys/4.4.0b4 Python/3.5.3 (Windows-10-10.0.19041-SP0) | ||
| method: POST | ||
| uri: https://managedhsmname.managedhsm.azure.net/keys/livekvtesteckey33180f9c/create?api-version=7.2-preview | ||
| response: | ||
| body: | ||
| string: '' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '0' | ||
| content-security-policy: | ||
| - default-src 'self' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| www-authenticate: | ||
| - Bearer authorization="https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47", | ||
| resource="https://managedhsm.azure.net" | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-frame-options: | ||
| - SAMEORIGIN | ||
| x-ms-server-latency: | ||
| - '1' | ||
| status: | ||
| code: 401 | ||
| message: Unauthorized | ||
| - request: | ||
| body: '{"kty": "EC-HSM"}' | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '17' | ||
| Content-Type: | ||
| - application/json | ||
| User-Agent: | ||
| - azsdk-python-keyvault-keys/4.4.0b4 Python/3.5.3 (Windows-10-10.0.19041-SP0) | ||
| method: POST | ||
| uri: https://managedhsmname.managedhsm.azure.net/keys/livekvtesteckey33180f9c/create?api-version=7.2-preview | ||
| response: | ||
| body: | ||
| string: '{"attributes":{"created":1616194950,"enabled":true,"exportable":false,"recoverableDays":90,"recoveryLevel":"Recoverable+Purgeable","updated":1616194950},"key":{"crv":"P-256","key_ops":["verify","sign"],"kid":"https://managedhsmname.managedhsm.azure.net/keys/livekvtesteckey33180f9c/38f9028c28e24b9b80fe3b2800c5950d","kty":"EC-HSM","x":"aD-Od-CpwDHTx3T9XEPYR3-KxdmZg_wtFekJBlrAaSM","y":"exOWHTfjEM5Qwg6GAF09KXJpwN7Ov8LN_ZxxIlqpK9I"}}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '433' | ||
| content-security-policy: | ||
| - default-src 'self' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-frame-options: | ||
| - SAMEORIGIN | ||
| x-ms-keyvault-network-info: | ||
| - addr=172.92.159.124 | ||
| x-ms-keyvault-region: | ||
| - eastus2 | ||
| x-ms-server-latency: | ||
| - '261' | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - application/json | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Connection: | ||
| - keep-alive | ||
| User-Agent: | ||
| - azsdk-python-keyvault-keys/4.4.0b4 Python/3.5.3 (Windows-10-10.0.19041-SP0) | ||
| method: GET | ||
| uri: https://managedhsmname.managedhsm.azure.net/keys/livekvtesteckey33180f9c/38f9028c28e24b9b80fe3b2800c5950d?api-version=7.2-preview | ||
| response: | ||
| body: | ||
| string: '{"attributes":{"created":1616194950,"enabled":true,"exportable":false,"recoverableDays":90,"recoveryLevel":"Recoverable+Purgeable","updated":1616194950},"key":{"crv":"P-256","key_ops":["verify","sign"],"kid":"https://managedhsmname.managedhsm.azure.net/keys/livekvtesteckey33180f9c/38f9028c28e24b9b80fe3b2800c5950d","kty":"EC-HSM","x":"aD-Od-CpwDHTx3T9XEPYR3-KxdmZg_wtFekJBlrAaSM","y":"exOWHTfjEM5Qwg6GAF09KXJpwN7Ov8LN_ZxxIlqpK9I"}}' | ||
| headers: | ||
| cache-control: | ||
| - no-cache | ||
| content-length: | ||
| - '433' | ||
| content-security-policy: | ||
| - default-src 'self' | ||
| content-type: | ||
| - application/json; charset=utf-8 | ||
| strict-transport-security: | ||
| - max-age=31536000; includeSubDomains | ||
| x-content-type-options: | ||
| - nosniff | ||
| x-frame-options: | ||
| - SAMEORIGIN | ||
| x-ms-build-version: | ||
| - 1.0.20210306-1-6fb7c19a-develop | ||
| x-ms-keyvault-network-info: | ||
| - addr=172.92.159.124 | ||
| x-ms-keyvault-region: | ||
| - eastus2 | ||
| x-ms-server-latency: | ||
| - '126' | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
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.