|
| 1 | +# coding=utf-8 |
| 2 | +# -------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | + |
| 7 | +from typing import List, Any, Optional, TYPE_CHECKING |
| 8 | + |
| 9 | +from azure.core import PipelineClient |
| 10 | +from msrest import Deserializer, Serializer |
| 11 | +from six import python_2_unicode_compatible |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + # pylint: disable=unused-import,ungrouped-imports |
| 15 | + from typing import Any |
| 16 | + |
| 17 | + from azure.core.credentials import TokenCredential |
| 18 | + from azure.core.pipeline.transport import HttpRequest, HttpResponse |
| 19 | + |
| 20 | +from ._generated import AzureAttestationRestClient |
| 21 | +from ._generated.models import AttestationType, PolicyResult, PolicyCertificatesResult, JSONWebKey, AttestationCertificateManagementBody, PolicyCertificatesModificationResult as GeneratedPolicyCertificatesModificationResult |
| 22 | +from ._configuration import AttestationClientConfiguration |
| 23 | +from ._models import AttestationSigner, AttestationToken, AttestationResponse, StoredAttestationPolicy, AttestationSigningKey, PolicyCertificatesModificationResult |
| 24 | +from ._common import Base64Url |
| 25 | +import cryptography |
| 26 | +import cryptography.x509 |
| 27 | +import base64 |
| 28 | +from azure.core.tracing.decorator import distributed_trace |
| 29 | +from threading import Lock, Thread |
| 30 | + |
| 31 | + |
| 32 | +class AttestationAdministrationClient(object): |
| 33 | + """Provides administrative APIs for managing an instance of the Attestation Service. |
| 34 | +
|
| 35 | + :param str instance_url: base url of the service |
| 36 | + :param credential: Credentials for the caller used to interact with the service. |
| 37 | + :type credential: azure.core.credentials.TokenCredential |
| 38 | + :keyword Pipeline pipeline: If omitted, the standard pipeline is used. |
| 39 | + :keyword HttpTransport transport: If omitted, the standard pipeline is used. |
| 40 | + :keyword list[HTTPPolicy] policies: If omitted, the standard pipeline is used. |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + credential, # type: "TokenCredential" |
| 46 | + instance_url, # type: str |
| 47 | + **kwargs # type: Any |
| 48 | + ): |
| 49 | + # type: (...) -> None |
| 50 | + if not credential: |
| 51 | + raise ValueError("Missing credential.") |
| 52 | + self._config = AttestationClientConfiguration(credential, instance_url, **kwargs) |
| 53 | + self._client = AzureAttestationRestClient(credential, instance_url, **kwargs) |
| 54 | + self._statelock = Lock() |
| 55 | + self._signing_certificates = None |
| 56 | + |
| 57 | + @distributed_trace |
| 58 | + def get_policy(self, attestation_type, **kwargs): |
| 59 | + #type(AttestationType, **Any) -> AttestationResult[str]: |
| 60 | + """ Retrieves the attestation policy for a specified attestation type. |
| 61 | +
|
| 62 | + :param azure.security.attestation.AttestationType attestation_type: :class:`azure.security.attestation.AttestationType` for |
| 63 | + which to retrieve the policy. |
| 64 | + :return AttestationResponse[str]: Attestation service response encapsulating a string attestation policy. |
| 65 | +
|
| 66 | + """ |
| 67 | + |
| 68 | + policyResult = self._client.policy.get(attestation_type, **kwargs) |
| 69 | + token = AttestationToken[PolicyResult](token=policyResult.token, body_type=PolicyResult) |
| 70 | + token_body = token.get_body() |
| 71 | + stored_policy = AttestationToken[StoredAttestationPolicy](token=token_body.policy, body_type=StoredAttestationPolicy) |
| 72 | + |
| 73 | + actual_policy = stored_policy.get_body().attestation_policy #type: bytes |
| 74 | + |
| 75 | + if self._config.token_validation_options.validate_token: |
| 76 | + token.validate_token(self._config.token_validation_options, self._get_signers(**kwargs)) |
| 77 | + |
| 78 | + return AttestationResponse[str](token, actual_policy.decode('utf-8')) |
| 79 | + |
| 80 | + @distributed_trace |
| 81 | + def set_policy(self, attestation_type, attestation_policy, signing_key=None, **kwargs): |
| 82 | + #type:(AttestationType, str, Optional[AttestationSigningKey], **Any) -> AttestationResponse[PolicyResult] |
| 83 | + """ Sets the attestation policy for the specified attestation type. |
| 84 | +
|
| 85 | + :param azure.security.attestation.AttestationType attestation_type: :class:`azure.security.attestation.AttestationType` for |
| 86 | + which to set the policy. |
| 87 | + :param str attestation_policy: Attestation policy to be set. |
| 88 | + :param Optional[AttestationSigningKey] signing_key: Optional signing key to be |
| 89 | + used to sign the policy before sending it to the service. |
| 90 | + :return AttestationResponse[PolicyResult]: Attestation service response encapsulating a :class:`PolicyResult`. |
| 91 | +
|
| 92 | + .. note:: |
| 93 | + If the attestation instance is in *Isolated* mode, then the |
| 94 | + `signing_key` parameter MUST be a signing key containing one of the |
| 95 | + certificates returned by :meth:`get_policy_management_certificates`. |
| 96 | +
|
| 97 | + If the attestation instance is in *AAD* mode, then the `signing_key` |
| 98 | + parameter does not need to be provided. |
| 99 | + """ |
| 100 | + policy_token = AttestationToken[StoredAttestationPolicy]( |
| 101 | + body=StoredAttestationPolicy(attestation_policy = attestation_policy.encode('ascii')), |
| 102 | + signer=signing_key, |
| 103 | + body_type=StoredAttestationPolicy) |
| 104 | + policyResult = self._client.policy.set(attestation_type=attestation_type, new_attestation_policy=policy_token.serialize(), **kwargs) |
| 105 | + token = AttestationToken[PolicyResult](token=policyResult.token, |
| 106 | + body_type=PolicyResult) |
| 107 | + if self._config.token_validation_options.validate_token: |
| 108 | + if not token.validate_token(self._config.token_validation_options, self._get_signers(**kwargs)): |
| 109 | + raise Exception("Token Validation of PolicySet API failed.") |
| 110 | + |
| 111 | + |
| 112 | + return AttestationResponse[PolicyResult](token, token.get_body()) |
| 113 | + |
| 114 | + @distributed_trace |
| 115 | + def get_policy_management_certificates(self, **kwargs): |
| 116 | + #type:(**Any) -> AttestationResponse[list[list[bytes]]] |
| 117 | + """ Retrieves the set of policy management certificates for the instance. |
| 118 | +
|
| 119 | + The list of policy management certificates will only be non-empty if the |
| 120 | + attestation service instance is in Isolated mode. |
| 121 | +
|
| 122 | + :return AttestationResponse[list[list[bytes]]: Attestation service response |
| 123 | + encapsulating a list of DER encoded X.509 certificate chains. |
| 124 | + """ |
| 125 | + |
| 126 | + cert_response = self._client.policy_certificates.get(**kwargs) |
| 127 | + token = AttestationToken[PolicyCertificatesResult]( |
| 128 | + token=cert_response.token, |
| 129 | + body_type=PolicyCertificatesResult) |
| 130 | + if self._config.token_validation_options.validate_token: |
| 131 | + if not token.validate_token(self._config.token_validation_options, self._get_signers(**kwargs)): |
| 132 | + raise Exception("Token Validation of PolicyCertificates API failed.") |
| 133 | + certificates = [] |
| 134 | + |
| 135 | + cert_list = token.get_body() |
| 136 | + |
| 137 | + for key in cert_list.policy_certificates.keys: |
| 138 | + key_certs = [base64.b64decode(cert) for cert in key.x5_c] |
| 139 | + certificates.append(key_certs) |
| 140 | + return AttestationResponse(token, certificates) |
| 141 | + |
| 142 | + @distributed_trace |
| 143 | + def add_policy_management_certificate(self, certificate_to_add, signing_key, **kwargs): |
| 144 | + #type:(bytes, AttestationSigningKey, **Any)-> AttestationResponse[PolicyCertificatesModificationResult] |
| 145 | + """ Adds a new policy management certificate to the set of policy management certificates for the instance. |
| 146 | +
|
| 147 | + :param bytes certificate_to_add: DER encoded X.509 certificate to add to |
| 148 | + the list of attestation policy management certificates. |
| 149 | + :param AttestationSigningKey signing_key: Signing Key representing one of |
| 150 | + the *existing* attestation signing certificates. |
| 151 | + :return AttestationResponse[PolicyCertificatesModificationResult]: Attestation service response |
| 152 | + encapsulating the status of the add request. |
| 153 | +
|
| 154 | + The :class:`PolicyCertificatesModificationResult` response to the |
| 155 | + :meth:`add_policy_management_certificate` API contains two attributes |
| 156 | + of interest. |
| 157 | + |
| 158 | + The first is `certificate_resolution`, which indicates |
| 159 | + whether the certificate in question is present in the set of policy |
| 160 | + management certificates after the operation has completed, or if it is |
| 161 | + absent. |
| 162 | +
|
| 163 | + The second is the `thumbprint` of the certificate added. The `thumbprint` |
| 164 | + for the certificate is the SHA1 hash of the DER encoding of the |
| 165 | + certificate. |
| 166 | +
|
| 167 | + """ |
| 168 | + key=JSONWebKey(kty='RSA', x5_c = [ base64.b64encode(certificate_to_add).decode('ascii')]) |
| 169 | + add_body = AttestationCertificateManagementBody(policy_certificate=key) |
| 170 | + cert_add_token = AttestationToken[AttestationCertificateManagementBody]( |
| 171 | + body=add_body, |
| 172 | + signer=signing_key, |
| 173 | + body_type=AttestationCertificateManagementBody) |
| 174 | + |
| 175 | + cert_response = self._client.policy_certificates.add(cert_add_token.serialize(), **kwargs) |
| 176 | + token = AttestationToken[GeneratedPolicyCertificatesModificationResult](token=cert_response.token, |
| 177 | + body_type=GeneratedPolicyCertificatesModificationResult) |
| 178 | + if self._config.token_validation_options.validate_token: |
| 179 | + if not token.validate_token(self._config.token_validation_options, self._get_signers(**kwargs)): |
| 180 | + raise Exception("Token Validation of PolicyCertificate Add API failed.") |
| 181 | + return AttestationResponse[PolicyCertificatesModificationResult](token, PolicyCertificatesModificationResult._from_generated(token.get_body())) |
| 182 | + |
| 183 | + @distributed_trace |
| 184 | + def remove_policy_management_certificate(self, certificate_to_add, signing_key, **kwargs): |
| 185 | + #type:(bytes, AttestationSigningKey, **Any)-> AttestationResponse[PolicyCertificatesModificationResult] |
| 186 | + """ Removes a new policy management certificate to the set of policy management certificates for the instance. |
| 187 | +
|
| 188 | + :param bytes certificate_to_add: DER encoded X.509 certificate to add to |
| 189 | + the list of attestation policy management certificates. |
| 190 | + :param AttestationSigningKey signing_key: Signing Key representing one of |
| 191 | + the *existing* attestation signing certificates. |
| 192 | + :return AttestationResponse[PolicyCertificatesModificationResult]: Attestation service response |
| 193 | + encapsulating a list of DER encoded X.509 certificate chains. |
| 194 | +
|
| 195 | + The :class:`PolicyCertificatesModificationResult` response to the |
| 196 | + :meth:`remove_policy_management_certificate` API contains two attributes |
| 197 | + of interest. |
| 198 | + |
| 199 | + The first is `certificate_resolution`, which indicates |
| 200 | + whether the certificate in question is present in the set of policy |
| 201 | + management certificates after the operation has completed, or if it is |
| 202 | + absent. |
| 203 | +
|
| 204 | + The second is the `thumbprint` of the certificate added. The `thumbprint` |
| 205 | + for the certificate is the SHA1 hash of the DER encoding of the |
| 206 | + certificate. |
| 207 | + |
| 208 | + """ |
| 209 | + key=JSONWebKey(kty='RSA', x5_c = [ base64.b64encode(certificate_to_add).decode('ascii')]) |
| 210 | + add_body = AttestationCertificateManagementBody(policy_certificate=key) |
| 211 | + cert_add_token = AttestationToken[AttestationCertificateManagementBody]( |
| 212 | + body=add_body, |
| 213 | + signer=signing_key, |
| 214 | + body_type=AttestationCertificateManagementBody) |
| 215 | + |
| 216 | + cert_response = self._client.policy_certificates.remove(cert_add_token.serialize(), **kwargs) |
| 217 | + token = AttestationToken[GeneratedPolicyCertificatesModificationResult](token=cert_response.token, |
| 218 | + body_type=GeneratedPolicyCertificatesModificationResult) |
| 219 | + if self._config.token_validation_options.validate_token: |
| 220 | + if not token.validate_token(self._config.token_validation_options, self._get_signers(**kwargs)): |
| 221 | + raise Exception("Token Validation of PolicyCertificate Remove API failed.") |
| 222 | + return AttestationResponse[PolicyCertificatesModificationResult](token, PolicyCertificatesModificationResult._from_generated(token.get_body())) |
| 223 | + |
| 224 | + def _get_signers(self, **kwargs): |
| 225 | + #type(**Any) -> List[AttestationSigner] |
| 226 | + """ Returns the set of signing certificates used to sign attestation tokens. |
| 227 | + """ |
| 228 | + |
| 229 | + with self._statelock: |
| 230 | + if (self._signing_certificates == None): |
| 231 | + signing_certificates = self._client.signing_certificates.get(**kwargs) |
| 232 | + self._signing_certificates = [] |
| 233 | + for key in signing_certificates.keys: |
| 234 | + # Convert the returned certificate chain into an array of X.509 Certificates. |
| 235 | + certificates = [] |
| 236 | + for x5c in key.x5_c: |
| 237 | + der_cert = base64.b64decode(x5c) |
| 238 | + certificates.append(der_cert) |
| 239 | + self._signing_certificates.append(AttestationSigner(certificates, key.kid)) |
| 240 | + signers = self._signing_certificates |
| 241 | + return signers |
| 242 | + |
| 243 | + def close(self): |
| 244 | + # type: () -> None |
| 245 | + self._client.close() |
| 246 | + |
| 247 | + def __enter__(self): |
| 248 | + # type: () -> AttestationAdministrationClient |
| 249 | + self._client.__enter__() |
| 250 | + return self |
| 251 | + |
| 252 | + def __exit__(self, *exc_details): |
| 253 | + # type: (Any) -> None |
| 254 | + self._client.__exit__(*exc_details) |
0 commit comments