示例#1
0
    async def update_policy(
        self,
        name: str,
        policy: CertificatePolicy,
        **kwargs: "**Any"
    ) -> CertificatePolicy:
        """Updates the policy for a certificate.

        Set specified members in the certificate policy. Leaves others as null.
        This operation requries the certificates/update permission.

        :param str name: The name of the certificate in the given vault.
        :param policy: The policy for the certificate.
        :type policy: ~azure.keyvault.certificates.models.CertificatePolicy
        :return: The certificate policy
        :rtype: ~azure.keyvault.certificates.models.CertificatePolicy
        :raises: :class:`~azure.core.exceptions.HttpResponseError`
        """
        bundle = await self._client.update_certificate_policy(
            vault_base_url=self.vault_url,
            certificate_name=name,
            certificate_policy=policy._to_certificate_policy_bundle(),
            **kwargs
        )
        return CertificatePolicy._from_certificate_policy_bundle(certificate_policy_bundle=bundle)
示例#2
0
    async def create_certificate(
            self, name: str, policy: CertificatePolicy, **kwargs: "**Any"
    ) -> Union[KeyVaultCertificate, CertificateOperation]:
        """Creates a new certificate.

        If this is the first version, the certificate resource is created. This
        operation requires the certificates/create permission.

        :param str name: The name of the certificate.
        :param policy: The management policy for the certificate.
        :type policy:
         ~azure.keyvault.certificates.models.CertificatePolicy
        :returns: A coroutine for the creation of the certificate. Awaiting the coroutine
         returns the created KeyVaultCertificate if creation is successful, the CertificateOperation if not.
        :rtype: ~azure.keyvault.certificates.models.KeyVaultCertificate or
         ~azure.keyvault.certificates.models.CertificateOperation
        :raises: :class:`~azure.core.exceptions.HttpResponseError`

        Keyword arguments
            - *enabled (bool)* - Determines whether the object is enabled.
            - *tags (dict[str, str])* - Application specific metadata in the form of key-value pairs.

        Example:
            .. literalinclude:: ../tests/test_examples_certificates_async.py
                :start-after: [START create_certificate]
                :end-before: [END create_certificate]
                :language: python
                :caption: Create a certificate
                :dedent: 8
        """

        enabled = kwargs.pop("enabled", None)
        tags = kwargs.pop("tags", None)

        if enabled is not None:
            attributes = self._client.models.CertificateAttributes(
                enabled=enabled)
        else:
            attributes = None
        cert_bundle = await self._client.create_certificate(
            vault_base_url=self.vault_endpoint,
            certificate_name=name,
            certificate_policy=policy._to_certificate_policy_bundle(),
            certificate_attributes=attributes,
            tags=tags,
            **kwargs)

        create_certificate_operation = CertificateOperation._from_certificate_operation_bundle(
            cert_bundle)

        command = partial(self.get_certificate_operation, name=name, **kwargs)

        get_certificate_command = partial(self.get_certificate,
                                          name=name,
                                          **kwargs)

        create_certificate_polling = CreateCertificatePollerAsync(
            get_certificate_command=get_certificate_command)
        return await async_poller(command, create_certificate_operation, None,
                                  create_certificate_polling)
示例#3
0
    async def import_certificate(
        self,
        name: str,
        certificate_bytes: bytes,
        password: Optional[str] = None,
        policy: Optional[CertificatePolicy] = None,
        enabled: Optional[bool] = None,
        tags: Optional[Dict[str, str]] = None,
        **kwargs: "**Any"
        ) -> Certificate:
        """Imports a certificate into a specified key vault.

        Imports an existing valid certificate, containing a private key, into
        Azure Key Vault. The certificate to be imported can be in either PFX or
        PEM format. If the certificate is in PEM format the PEM file must
        contain the key as well as x509 certificates. This operation requires
        the certificates/import permission.

        :param str name: The name of the certificate.
        :param bytes certificate_bytes: Bytes of the certificate object to import.
            This certificate needs to contain the private key.
        :param str password: If the private key in the passed in certificate is encrypted,
            it is the password used for encryption.
        :param policy: The management policy for the certificate.
        :type policy:
         ~azure.keyvault.certificates.models.CertificatePolicy
        :param bool enabled: Determines whether the object is enabled.
        :param tags: Application specific metadata in the form of key-value pairs.
        :type tags: dict[str, str]
        :returns: The imported Certificate
        :rtype: ~azure.keyvault.certificates.models.Certificate
        :raises: :class:`~azure.core.exceptions.HttpResponseError`
        """
        if enabled is not None:
            attributes = self._client.models.CertificateAttributes(
                enabled=enabled
            )
        else:
            attributes = None
        base64_encoded_certificate = base64.b64encode(certificate_bytes).decode("utf-8")
        bundle = await self._client.import_certificate(
            vault_base_url=self.vault_url,
            certificate_name=name,
            base64_encoded_certificate=base64_encoded_certificate,
            password=password,
            certificate_policy=CertificatePolicy._to_certificate_policy_bundle(policy),
            certificate_attributes=attributes,
            tags=tags,
            **kwargs
        )
        return Certificate._from_certificate_bundle(certificate_bundle=bundle)
示例#4
0
    async def get_policy(self, certificate_name: str, **kwargs: "**Any") -> CertificatePolicy:
        """Gets the policy for a certificate.

        Returns the specified certificate policy resources in the key
        vault. This operation requires the certificates/get permission.

        :param str certificate_name: The name of the certificate in a given key vault.
        :return: The certificate policy
        :rtype: ~azure.keyvault.certificates.models.CertificatePolicy
        :raises: :class:`~azure.core.exceptions.HttpResponseError`
        """
        bundle = await self._client.get_certificate_policy(
            vault_base_url=self.vault_url, certificate_name=certificate_name, **kwargs
        )
        return CertificatePolicy._from_certificate_policy_bundle(certificate_policy_bundle=bundle)
示例#5
0
    async def import_certificate(self, name: str, certificate_bytes: bytes,
                                 **kwargs: "**Any") -> KeyVaultCertificate:
        """Imports a certificate into a specified key vault.

        Imports an existing valid certificate, containing a private key, into
        Azure Key Vault. The certificate to be imported can be in either PFX or
        PEM format. If the certificate is in PEM format the PEM file must
        contain the key as well as x509 certificates. This operation requires
        the certificates/import permission.

        :param str name: The name of the certificate.
        :param bytes certificate_bytes: Bytes of the certificate object to import.
            This certificate needs to contain the private key.
        :returns: The imported KeyVaultCertificate
        :rtype: ~azure.keyvault.certificates.models.KeyVaultCertificate
        :raises: :class:`~azure.core.exceptions.HttpResponseError`

        Keyword arguments
            - *enabled (bool)* - Determines whether the object is enabled.
            - *tags (dict[str, str])* - Application specific metadata in the form of key-value pairs.
            - *password (str)* - If the private key in the passed in certificate is encrypted,
              it is the password used for encryption.
            - *policy (~azure.keyvault.certificates.models.CertificatePolicy)* - The management policy
              for the certificate.
        """

        enabled = kwargs.pop("enabled", None)
        password = kwargs.pop("password", None)
        policy = kwargs.pop("policy", None)

        if enabled is not None:
            attributes = self._client.models.CertificateAttributes(
                enabled=enabled)
        else:
            attributes = None
        base64_encoded_certificate = base64.b64encode(
            certificate_bytes).decode("utf-8")
        bundle = await self._client.import_certificate(
            vault_base_url=self.vault_endpoint,
            certificate_name=name,
            base64_encoded_certificate=base64_encoded_certificate,
            password=password,
            certificate_policy=CertificatePolicy._to_certificate_policy_bundle(
                policy),
            certificate_attributes=attributes,
            **kwargs)
        return KeyVaultCertificate._from_certificate_bundle(
            certificate_bundle=bundle)
示例#6
0
    async def create_certificate(self,
                                 name: str,
                                 policy: Optional[CertificatePolicy] = None,
                                 enabled: Optional[bool] = None,
                                 tags: Optional[Dict[str, str]] = None,
                                 **kwargs: "**Any") -> CertificateOperation:
        """Creates a new certificate.

        If this is the first version, the certificate resource is created. This
        operation requires the certificates/create permission.

        :param str name: The name of the certificate.
        :param policy: The management policy for the certificate.
        :type policy:
         ~azure.keyvault.certificates.models.CertificatePolicy
        :param bool enabled: Determines whether the object is enabled.
        :param tags: Application specific metadata in the form of key-value pairs.
        :type tags: dict(str, str)
        :returns: The created CertificateOperation
        :rtype: coroutine
        :raises: :class:`~azure.core.exceptions.HttpResponseError`

        Example:
            .. literalinclude:: ../tests/test_examples_certificates_async.py
                :start-after: [START create_certificate]
                :end-before: [END create_certificate]
                :language: python
                :caption: Create a certificate
                :dedent: 8
        """
        if enabled is not None:
            attributes = self._client.models.CertificateAttributes(
                enabled=enabled)
        else:
            attributes = None

        if not policy:
            lifetime_actions = [
                LifetimeAction(days_before_expiry=90, action_type="AutoRenew")
            ]
            policy = CertificatePolicy(key_properties=KeyProperties(
                exportable=True,
                key_type='RSA',
                key_size=2048,
                reuse_key=True,
                key_usage=[
                    KeyUsageType.c_rl_sign, KeyUsageType.data_encipherment,
                    KeyUsageType.digital_signature, KeyUsageType.key_agreement,
                    KeyUsageType.key_cert_sign, KeyUsageType.key_encipherment
                ]),
                                       issuer_name="Self",
                                       lifetime_actions=lifetime_actions,
                                       content_type=SecretContentType.PKCS12,
                                       subject_name="CN=DefaultPolicy",
                                       validity_in_months=12)

        create_certificate_operation = await self._client.create_certificate(
            vault_base_url=self.vault_url,
            certificate_name=name,
            certificate_policy=policy._to_certificate_policy_bundle(),
            certificate_attributes=attributes,
            tags=tags,
            **kwargs)

        command = partial(self._client.get_certificate_operation,
                          vault_base_url=self.vault_url,
                          certificate_name=name,
                          **kwargs)

        create_certificate_polling = CreateCertificatePollerAsync(
            unknown_issuer=(policy.issuer_name.lower() == 'unknown'))
        return async_poller(command,
                            create_certificate_operation.status.lower(), None,
                            create_certificate_polling)