Skip to content

[Key Vault] Refine Security Domain tests to not require CLI use mid-execution #42089

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mccoyp
Copy link
Member

@mccoyp mccoyp commented Jul 18, 2025

Description

Security Domain tests until now have required the Azure CLI to run, since we relied on their utility to wrap a security domain with the appropriate keys. To streamline our process and avoid the (somewhat circular) dependency, this adds utilities to support this wrapping locally. These utilities are lifted directly from the CLI to avoid reinventing the wheel: https://github.com/Azure/azure-cli/tree/dev/src/azure-cli/azure/cli/command_modules/keyvault/security_domain

Additionally, consistent test certs/keys are added in this PR to simplify the testing process and make things more reliable.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@mccoyp mccoyp added KeyVault Client This issue points to a problem in the data-plane of the library. labels Jul 18, 2025
@mccoyp mccoyp force-pushed the sd-test-refinement branch from 77ba331 to 9fe0d95 Compare July 18, 2025 19:05
@mccoyp mccoyp marked this pull request as ready for review July 18, 2025 21:27
@Copilot Copilot AI review requested due to automatic review settings July 18, 2025 21:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request modernizes and improves the Security Domain tests by eliminating the dependency on Azure CLI for security domain wrapping during test execution. The changes introduce local utilities to handle security domain encryption/wrapping operations directly in Python, making the tests more self-contained and reliable.

Key changes:

  • Adds comprehensive security domain wrapping utilities (cryptographic operations, key derivation, JWE handling)
  • Introduces consistent test certificates and keys stored in the resources directory
  • Refactors test code to use local wrapping instead of requiring mid-test CLI execution

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
wrapping.py New comprehensive utility implementing security domain cryptographic operations and wrapping logic
jwe.py New JWE (JSON Web Encryption) implementation for handling encrypted key operations
utils.py Refactored to use consistent resource paths and simplified certificate handling
test files Updated to use new wrapping utilities instead of CLI dependency
resources/.cer/.pem Added consistent test certificates and private keys
.gitignore/CredScanSuppression.json Updated to allow test certificates while maintaining security
Comments suppressed due to low confidence (4)

sdk/keyvault/azure-keyvault-securitydomain/tests/wrapping.py:288

  • This condition uses chained comparison incorrectly. It evaluates as 'i < len(self.jwe_decode.auth_tag)' and then compares that boolean result with 32, which will never be True. Should be 'while i < len(self.jwe_decode.auth_tag) and len(self.jwe_decode.auth_tag) == 32:'
    jwe_wrapped = JWE()

sdk/keyvault/azure-keyvault-securitydomain/tests/wrapping.py:25

  • [nitpick] Class name 'ModMath' should follow PascalCase convention but is missing descriptive context. Consider 'ModularMath' or 'ModularArithmetic' for clarity.
class ModMath:

sdk/keyvault/azure-keyvault-securitydomain/tests/jwe.py:281

  • Method name should follow snake_case convention. Should be 'aes256_hmac_sha512_decrypt'.
    def Aes256HmacSha512Decrypt(self, cek):

sdk/keyvault/azure-keyvault-securitydomain/tests/jwe.py:306

  • Method name should follow snake_case convention. Should be 'aes256_hmac_sha512_encrypt'.
    def Aes256HmacSha512Encrypt(self, cek, plaintext):

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from utils import Utils

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth adding a header comment to describe and link to where this module is copied from and how it should be maintained? Or is that not really necessary, as the source we copied from isn't really a source of truth to be kept in sync with?

jku=None, jwk=None, kid=None, x5u=None, x5c=None, x5t=None,
x5t_S256=None, typ=None, cty=None, crit=None):
"""
JWE header
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
JWE header
JWE header

Strange indent.


# At this point, use the Azure CLI to encrypt the security domain to prepare for upload
# `az keyvault security-domain restore-blob --sd-exchange-key <>-transfer-key.pem --sd-file <>-security-domain.json --sd-wrapping-keys <>-certificate0.key <>-certificate1.key <>-certificate2.key --sd-file-restore-blob <>-restore-blob.json`
poller = upload_client.begin_upload(security_domain=result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did this used to work by re-uploading the unwrapped result?

This test requires two Managed HSMs to be set up, one for downloading and one for uploading. The URL of the
first Managed HSM is specified in the environment variable AZURE_MANAGEDHSM_URL, and the second is specified
in SECONDARY_MANAGEDHSM_URL.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I could see, the util code that read from these env vars was removed? Are they read again somewhere else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. KeyVault
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

2 participants