1717from ._models import JsonWebKey , KeyRotationLifetimeAction
1818from ._shared import KeyVaultClientBase
1919from ._shared ._polling import DeleteRecoverPollingMethod , KeyVaultOperationPoller
20- from ._models import DeletedKey , KeyVaultKey , KeyProperties , KeyReleasePolicy , KeyRotationPolicy , ReleaseKeyResult
20+ from ._models import (
21+ DeletedKey ,
22+ ExternalKey ,
23+ KeyVaultKey ,
24+ KeyProperties ,
25+ KeyReleasePolicy ,
26+ KeyRotationPolicy ,
27+ ReleaseKeyResult ,
28+ )
2129
2230
2331def _get_key_id (vault_url , key_name , version = None ):
@@ -57,6 +65,7 @@ def _get_attributes(
5765 not_before : Optional [datetime ],
5866 expires_on : Optional [datetime ],
5967 exportable : Optional [bool ] = None ,
68+ external_key : Optional [ExternalKey ] = None ,
6069 ) -> Optional [KeyAttributes ]:
6170 """Return a KeyAttributes object if non-None attributes are provided, or None otherwise.
6271
@@ -68,13 +77,25 @@ def _get_attributes(
6877 :type expires_on: ~datetime.datetime or None
6978 :param exportable: Whether the private key can be exported.
7079 :type exportable: bool or None
80+ :param external_key: A reference to an external key, when registering an external key.
81+ :type external_key: ~azure.keyvault.keys.ExternalKey or None
7182
7283 :returns: An autorest-generated model of the key's attributes.
7384 :rtype: KeyAttributes
7485 """
75- if enabled is not None or not_before is not None or expires_on is not None or exportable is not None :
86+ if (
87+ enabled is not None
88+ or not_before is not None
89+ or expires_on is not None
90+ or exportable is not None
91+ or external_key is not None
92+ ):
7693 return self ._models .KeyAttributes (
77- enabled = enabled , not_before = not_before , expires = expires_on , exportable = exportable
94+ enabled = enabled ,
95+ not_before = not_before ,
96+ expires = expires_on ,
97+ exportable = exportable ,
98+ external_key = external_key ._to_generated () if external_key is not None else None ,
7899 )
79100 return None
80101
@@ -398,6 +419,64 @@ def create_oct_key(
398419 ** kwargs ,
399420 )
400421
422+ @distributed_trace
423+ def create_external_key (
424+ self ,
425+ name : str ,
426+ external_key : ExternalKey ,
427+ * ,
428+ enabled : Optional [bool ] = None ,
429+ tags : Optional [Dict [str , str ]] = None ,
430+ not_before : Optional [datetime ] = None ,
431+ expires_on : Optional [datetime ] = None ,
432+ release_policy : Optional [KeyReleasePolicy ] = None ,
433+ ** kwargs : Any ,
434+ ) -> KeyVaultKey :
435+ """Register a Managed HSM key that points at material managed by an external HSM.
436+
437+ Requires the keys/create permission. Only available with API version
438+ ``2026-01-01-preview`` and newer, and only supported on Managed HSM.
439+
440+ :param str name: The name for the new key.
441+ :param external_key: A reference identifying the external key material.
442+ :type external_key: ~azure.keyvault.keys.ExternalKey
443+
444+ :keyword enabled: Whether the key is enabled for use.
445+ :paramtype enabled: bool or None
446+ :keyword tags: Application specific metadata in the form of key-value pairs.
447+ :paramtype tags: dict[str, str] or None
448+ :keyword not_before: Not before date of the key in UTC.
449+ :paramtype not_before: ~datetime.datetime or None
450+ :keyword expires_on: Expiry date of the key in UTC.
451+ :paramtype expires_on: ~datetime.datetime or None
452+ :keyword release_policy: The policy rules under which the key can be exported.
453+ :paramtype release_policy: ~azure.keyvault.keys.KeyReleasePolicy or None
454+
455+ :returns: The created key.
456+ :rtype: ~azure.keyvault.keys.KeyVaultKey
457+
458+ :raises ~azure.core.exceptions.HttpResponseError:
459+ """
460+ attributes = self ._get_attributes (
461+ enabled = enabled , not_before = not_before , expires_on = expires_on , external_key = external_key
462+ )
463+
464+ policy = release_policy
465+ if policy is not None :
466+ policy = self ._models .KeyReleasePolicy (
467+ encoded_policy = policy .encoded_policy , content_type = policy .content_type , immutable = policy .immutable
468+ )
469+ # External keys are mutually exclusive with `kty`. The generated overload requires `kty`,
470+ # but the runtime constructor accepts arbitrary kwargs.
471+ parameters = self ._models .KeyCreateParameters ( # type: ignore[call-overload]
472+ key_attributes = attributes ,
473+ tags = tags ,
474+ release_policy = policy ,
475+ )
476+
477+ bundle = self ._client .create_key (key_name = name , parameters = parameters , ** kwargs )
478+ return KeyVaultKey ._from_key_bundle (bundle )
479+
401480 @distributed_trace
402481 def begin_delete_key ( # pylint:disable=bad-option-value,delete-operation-wrong-return-type
403482 self , name : str , ** kwargs : Any
0 commit comments