1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
from azure.iot.deviceprovisioning.aio import DeviceProvisioningClient
from conftest import (
API_VERSION,
CUSTOM_ALLOCATION,
DEVICE_INFO,
INITIAL_TWIN_PROPERTIES,
REPROVISION_MIGRATE,
TEST_DICT,
TEST_ENDORSEMENT_KEY,
WEBHOOK_URL,
ProvisioningServicePreparer,
)
from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async
from utility.common import create_test_cert, generate_enrollment, generate_key
class TestIndividualEnrollments(AzureRecordedTestCase):
def create_provisioning_service_client(self, endpoint):
credential = self.get_credential(DeviceProvisioningClient, is_async=True)
client = DeviceProvisioningClient(endpoint=endpoint, credential=credential)
return client
@ProvisioningServicePreparer()
@recorded_by_proxy_async
async def test_enrollment_tpm_lifecycle(self, iothub_dps_endpoint):
client = self.create_provisioning_service_client(
iothub_dps_endpoint
)
attestation_type = "tpm"
enrollment_id = self.create_random_name("ind_enroll_tpm_")
device_id = self.create_random_name("device_")
enrollment = generate_enrollment(
id=enrollment_id,
attestation_type=attestation_type,
endorsement_key=TEST_ENDORSEMENT_KEY,
provisioning_status="enabled",
device_id=device_id,
allocation_policy="static",
)
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id, enrollment=enrollment
)
assert enrollment_response["registrationId"] == enrollment_id
assert enrollment_response["attestation"]["tpm"]
assert (
enrollment_response["attestation"]["tpm"]["endorsementKey"]
== TEST_ENDORSEMENT_KEY
)
assert enrollment_response["allocationPolicy"] == "static"
assert enrollment_response["deviceId"] == device_id
assert enrollment_response["provisioningStatus"] == "enabled"
# check for enrollment in query response
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
enrollments = [e async for e in enrollment_list]
assert len(enrollments) == 1
assert enrollments[0]["registrationId"] == enrollment_id
# check enrollment get
enrollment_response = await client.enrollment.get(id=enrollment_id)
assert enrollment_response["registrationId"] == enrollment_id
# check attestation
attestation_response = (
await client.enrollment.get_attestation_mechanism(
id=enrollment_id
)
)
assert attestation_response["tpm"]["endorsementKey"] == TEST_ENDORSEMENT_KEY
# update enrollment
enrollment["provisioningStatus"] = "disabled"
enrollment["optionalDeviceInformation"] = DEVICE_INFO
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id,
enrollment=enrollment,
if_match=enrollment_response["etag"],
)
assert enrollment_response["provisioningStatus"] == "disabled"
assert (
enrollment_response["optionalDeviceInformation"]["additionalProperties"]
== TEST_DICT
)
# delete enrollment
await client.enrollment.delete(id=enrollment_id)
# ensure deletion
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
assert len([e async for e in enrollment_list]) == 0
@ProvisioningServicePreparer()
@recorded_by_proxy_async
async def test_enrollment_x509_lifecycle(self, iothub_dps_endpoint):
client = self.create_provisioning_service_client(
iothub_dps_endpoint
)
enrollment_id = self.create_random_name("x509_enrollment_")
device_id = self.create_random_name("x509_device_")
attestation_type = "x509"
cert_name = self.create_random_name("enroll_cert_")
cert = create_test_cert(subject=cert_name)
cert_contents = cert["certificate"]
thumb = cert["thumbprint"]
enrollment = generate_enrollment(
id=enrollment_id,
device_id=device_id,
primary_cert=cert_contents,
secondary_cert=cert_contents,
provisioning_status="enabled",
allocation_policy="hashed",
attestation_type=attestation_type,
reprovision_policy=REPROVISION_MIGRATE,
)
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id,
enrollment=enrollment,
)
assert enrollment_response
assert enrollment_response["registrationId"] == enrollment_id
assert enrollment_response["deviceId"] == device_id
# check auth
assert enrollment_response["attestation"]["x509"]
assert enrollment_response["attestation"]["type"] == "x509"
assert (
enrollment_response["attestation"]["x509"]["clientCertificates"]["primary"][
"info"
]["subjectName"]
== f"CN={cert_name}"
)
if self.is_live:
assert (
enrollment_response["attestation"]["x509"]["clientCertificates"][
"primary"
]["info"]["sha256Thumbprint"]
== thumb
)
assert enrollment_response["provisioningStatus"] == "enabled"
assert enrollment_response["allocationPolicy"] == "hashed"
assert enrollment_response["reprovisionPolicy"]["migrateDeviceData"]
assert enrollment_response["reprovisionPolicy"]["updateHubAssignment"]
# check for enrollment in query response
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
enrollments = [e async for e in enrollment_list]
assert len(enrollments) == 1
assert enrollments[0]["registrationId"] == enrollment_id
# check enrollment get
enrollment_response = await client.enrollment.get(id=enrollment_id)
assert enrollment_response["registrationId"] == enrollment_id
# check enrollment update
enrollment["provisioningStatus"] = "disabled"
enrollment["allocationPolicy"] = "custom"
enrollment["customAllocationDefinition"] = CUSTOM_ALLOCATION
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id,
enrollment=enrollment,
if_match=enrollment_response["etag"],
)
assert enrollment_response["registrationId"] == enrollment_id
assert enrollment_response["provisioningStatus"] == "disabled"
assert (
enrollment_response["customAllocationDefinition"]["webhookUrl"]
== WEBHOOK_URL
)
assert (
enrollment_response["customAllocationDefinition"]["apiVersion"]
== API_VERSION
)
# delete enrollment
await client.enrollment.delete(id=enrollment_id)
# ensure deletion
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
assert len([e async for e in enrollment_list]) == 0
@ProvisioningServicePreparer()
@recorded_by_proxy_async
async def test_enrollment_symmetrickey_lifecycle(
self, iothub_dps_endpoint
):
client = self.create_provisioning_service_client(
iothub_dps_endpoint
)
attestation_type = "symmetricKey"
enrollment_id = self.create_random_name("sym_enrollment_")
primary_key = generate_key()
secondary_key = generate_key()
device_id = self.create_random_name("sym_key_device_")
allocation_policy = "geoLatency"
reprovision_policy = REPROVISION_MIGRATE
enrollment = generate_enrollment(
id=enrollment_id,
device_id=device_id,
allocation_policy=allocation_policy,
reprovision_policy=reprovision_policy,
attestation_type=attestation_type,
primary_key=primary_key,
secondary_key=secondary_key,
initial_twin_properties=INITIAL_TWIN_PROPERTIES,
)
enrollment_id2 = self.create_random_name("sym_enrollment2_")
# Use provided keys
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id, enrollment=enrollment
)
assert enrollment_response["registrationId"] == enrollment_id
assert enrollment_response["deviceId"] == device_id
assert enrollment_response["attestation"]["symmetricKey"]
assert enrollment_response["initialTwin"] == INITIAL_TWIN_PROPERTIES
if self.is_live:
assert (
enrollment_response["attestation"]["symmetricKey"]["primaryKey"]
== primary_key
)
assert (
enrollment_response["attestation"]["symmetricKey"]["secondaryKey"]
== secondary_key
)
# reprovision migrate true
assert enrollment_response["reprovisionPolicy"]["migrateDeviceData"]
# reprovision update true
assert enrollment_response["reprovisionPolicy"]["updateHubAssignment"]
# check for enrollment in query response
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
enrollments = [e async for e in enrollment_list]
assert len(enrollments) == 1
assert enrollments[0]["registrationId"] == enrollment_id
# check enrollment get
enrollment_response = await client.enrollment.get(id=enrollment_id)
assert enrollment_response["registrationId"] == enrollment_id
# check attestation
attestation_response = (
await client.enrollment.get_attestation_mechanism(
id=enrollment_id
)
)
if self.is_live:
assert attestation_response["symmetricKey"]["primaryKey"] == primary_key
assert attestation_response["symmetricKey"]["secondaryKey"] == secondary_key
# check enrollment update
enrollment["provisioningStatus"] = "disabled"
enrollment["allocationPolicy"] = "custom"
enrollment["customAllocationDefinition"] = CUSTOM_ALLOCATION
enrollment_response = await client.enrollment.create_or_update(
id=enrollment_id,
enrollment=enrollment,
if_match=enrollment_response["etag"],
)
assert enrollment_response["registrationId"] == enrollment_id
assert enrollment_response["provisioningStatus"] == "disabled"
assert (
enrollment_response["customAllocationDefinition"]["webhookUrl"]
== WEBHOOK_URL
)
assert (
enrollment_response["customAllocationDefinition"]["apiVersion"]
== API_VERSION
)
# reprovision migrate true
assert enrollment_response["reprovisionPolicy"]["migrateDeviceData"]
# reprovision update true
assert enrollment_response["reprovisionPolicy"]["updateHubAssignment"]
# second enrollment
enrollment2 = generate_enrollment(
id=enrollment_id2,
attestation_type=attestation_type,
allocation_policy="custom",
webhook_url=WEBHOOK_URL,
api_version=API_VERSION,
)
enrollment_2 = await client.enrollment.create_or_update(
id=enrollment_id2, enrollment=enrollment2
)
assert enrollment_2
# check both enrollments
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
enrollments = [e["registrationId"] async for e in enrollment_list]
assert len(enrollments) == 2
assert enrollment_id in enrollments
assert enrollment_id2 in enrollments
# delete both enrollments
await client.enrollment.delete(id=enrollment_id)
await client.enrollment.delete(id=enrollment_id2)
# ensure deletion
enrollment_list = await client.enrollment.query(
query_specification={"query": "SELECT *"}
)
assert len([e async for e in enrollment_list]) == 0
@ProvisioningServicePreparer()
@recorded_by_proxy_async
async def test_individual_enrollment_bulk_operations(
self, iothub_dps_endpoint
):
client = self.create_provisioning_service_client(
iothub_dps_endpoint
)
attestation_type = "tpm"
enrollment_id = self.create_random_name("ind_enroll_tpm_")
device_id = self.create_random_name("device_")
enrollment = generate_enrollment(
id=enrollment_id,
attestation_type=attestation_type,
endorsement_key=TEST_ENDORSEMENT_KEY,
provisioning_status="enabled",
device_id=device_id,
allocation_policy="static",
)
attestation_type = "symmetricKey"
enrollment_id2 = self.create_random_name("sym_key_enrollment_")
primary_key = generate_key()
secondary_key = generate_key()
device_id = self.create_random_name("sym_key_device_")
allocation_policy = "geoLatency"
reprovision_policy = REPROVISION_MIGRATE
enrollment2 = generate_enrollment(
id=enrollment_id2,
device_id=device_id,
allocation_policy=allocation_policy,
reprovision_policy=reprovision_policy,
attestation_type=attestation_type,
primary_key=primary_key,
secondary_key=secondary_key,
)
bulk_enrollment_operation = {
"enrollments": [enrollment, enrollment2],
"mode": "create",
}
bulk_enrollment_response = (
await client.enrollment.run_bulk_operation(
bulk_operation=bulk_enrollment_operation
)
)
assert bulk_enrollment_response
bulk_enrollment_operation["enrollments"][0]["provisioningStatus"] == "disabled"
bulk_enrollment_operation["enrollments"][1]["provisioningStatus"] == "disabled"
bulk_enrollment_operation["mode"] = "update"
bulk_enrollment_response = (
await client.enrollment.run_bulk_operation(
bulk_operation=bulk_enrollment_operation
)
)
assert bulk_enrollment_response
bulk_enrollment_operation["mode"] = "delete"
bulk_enrollment_response = (
await client.enrollment.run_bulk_operation(
bulk_operation=bulk_enrollment_operation
)
)
assert bulk_enrollment_response
|