def main():
    connection_string = "[Connection String]"
    signing_cert = "[Signing Cert]"
    group_id = "[Group ID]"

    #set up the provisioning service client
    psc = ProvisioningServiceClient.create_from_connection_string(
        connection_string)

    #build EnrollmentGroup model
    att = AttestationMechanism.create_with_x509_signing_certs(signing_cert)
    eg = EnrollmentGroup.create(group_id, att)

    #create EnrollmentGroup on the Provisioning Service
    eg = psc.create_or_update(eg)
    six.print_(eg)

    #get EnrollmentGroup from the Provisioning Service (note: this step is useless here, as eg is already up to date)
    eg = psc.get_enrollment_group(group_id)
    six.print_(eg)

    #make a Provisioning Service query
    qs = QuerySpecification("*")
    page_size = 2  #two results per page -> don't pass this parameter if you just want all of them at once
    query = psc.create_enrollment_group_query(qs, page_size)

    results = []
    for page in query:
        results += page
    six.print_(results)
    #alternatively, call query.next() to get a new page

    #delete EnrollmentGroup from the Provisioning Service
    psc.delete(eg)
예제 #2
0
def run_scenario_individual_enrollment():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)
    att = AttestationMechanism.create_with_tpm(PROVISIONING_E2E_ENDORSEMENT_KEY)
    ie = IndividualEnrollment(registration_id=REGISTRATION_ID, attestation=att)

    #create
    ret_ie = psc.create_or_update(ie)
    assert ret_ie.registration_id == REGISTRATION_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_ie.initial_twin = twin
    capabilities = DeviceCapabilities.create(True)
    ret_ie.capabilities = capabilities

    ret_ie = psc.create_or_update(ret_ie)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES
    assert ret_ie.capabilities.iot_edge == True

    #get
    ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get attestation mechanism
    ret_am = psc.get_individual_enrollment_attestation_mechanism(REGISTRATION_ID)
    assert ret_am.tpm.endorsement_key == PROVISIONING_E2E_ENDORSEMENT_KEY

    #delete
    psc.delete(ret_ie)
    try:
        ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError

    #bulk enrollment
    enrollments = []
    for i in range(BULK_SIZE):
        new = IndividualEnrollment(registration_id=REGISTRATION_ID + str(i), attestation=att)
        enrollments.append(new)
    bulk_op = BulkEnrollmentOperation(mode=BULKOP_CREATE, enrollments=enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful

    #query
    qs = QuerySpecification(query="*")
    q = psc.create_individual_enrollment_query(qs)
    q_results = q.next()
    assert len(q_results) == BULK_SIZE

    #cleanup
    bulk_op = BulkEnrollmentOperation(mode=BULKOP_DELETE, enrollments=enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful
def run_scenario_individual_enrollment():
    psc = ProvisioningServiceClient.create_from_connection_string(CONNECTION_STRING)
    att = AttestationMechanism.create_with_tpm(ENDORSEMENT_KEY)
    ie = IndividualEnrollment.create(REGISTRATION_ID, att)

    #create
    ret_ie = psc.create_or_update(ie)
    assert ret_ie.registration_id == REGISTRATION_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_ie.initial_twin = twin
    ret_ie = psc.create_or_update(ret_ie)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get
    ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    assert ret_ie.registration_id == REGISTRATION_ID
    assert ret_ie.initial_twin.tags == TAGS
    assert ret_ie.initial_twin.desired_properties == DESIRED_PROPERTIES

    #delete
    psc.delete(ret_ie)
    try:
        ret_ie = psc.get_individual_enrollment(REGISTRATION_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError

    #bulk enrollment
    enrollments = []
    for i in range(BULK_SIZE):
        new = IndividualEnrollment.create(REGISTRATION_ID + str(i), att)
        enrollments.append(new)
    bulk_op = BulkEnrollmentOperation(CREATE, enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful

    #query
    qs = QuerySpecification("*")
    q = psc.create_individual_enrollment_query(qs)
    q_results = q.next()
    assert len(q_results) == BULK_SIZE

    #cleanup
    bulk_op = BulkEnrollmentOperation(DELETE, enrollments)
    res = psc.run_bulk_operation(bulk_op)
    assert res.is_successful
def main():
    connection_string = "[Connection String]"
    endorsement_key = "[Endorsement Key]"
    registration_id = "[Registration ID]"

    #set up the provisioning service client
    psc = ProvisioningServiceClient.create_from_connection_string(
        connection_string)

    #build IndividualEnrollment model
    att = AttestationMechanism.create_with_tpm(endorsement_key)
    ie = IndividualEnrollment.create(registration_id, att)

    #create IndividualEnrollment on the Provisioning Service
    ie = psc.create_or_update(ie)
    six.print_(ie)

    #get IndividualEnrollment from the Provisioning Service (note: this step is useless here, as ie is already up to date)
    ie = psc.get_individual_enrollment(registration_id)
    six.print_(ie)

    #delete IndividualEnrollment from the Provisioning Service
    psc.delete(ie)
    #could also use psc.delete_individual_enrollment_by_param(ie.registration_id, ie.etag)

    #bulk create IndividualEnrollments
    enrollments = []
    for i in range(5):
        enrollments.append(
            IndividualEnrollment.create(registration_id + str(i + 1), att))
    bulk_op = BulkEnrollmentOperation("create", enrollments)

    results = psc.run_bulk_operation(bulk_op)
    six.print_(ie)

    #make a Provisioning Service query
    qs = QuerySpecification("*")
    page_size = 2  #two results per page -> don't pass this parameter if you just want all of them at once
    query = psc.create_individual_enrollment_query(qs, page_size)

    results = []
    for page in query:
        results += page
    #alternatively, call query.next() to get a new page
    six.print_(results)

    #delete the bulk created enrollments
    bulk_op.mode = "delete"
    psc.run_bulk_operation(bulk_op)
예제 #5
0
def clear_dps_hub():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)

    #Individual Enrollments
    qs = QuerySpecification(query="*")
    query = psc.create_individual_enrollment_query(qs)
    items = []
    for page in query:
        items += page
    bulkop = BulkEnrollmentOperation(mode=BULKOP_DELETE, enrollments=items)
    psc.run_bulk_operation(bulkop)

    #Enrollment Groups
    query = psc.create_enrollment_group_query(qs)
    for page in query:
        for enrollment in page:
            psc.delete(enrollment)
예제 #6
0
def run_scenario_enrollment_group():
    psc = ProvisioningServiceClient.create_from_connection_string(PROVISIONING_CONNECTION_STRING)
    att = AttestationMechanism.create_with_x509_signing_certs(PROVISIONING_E2E_X509_CERT)
    eg = EnrollmentGroup(enrollment_group_id=GROUP_ID, attestation=att)

    #create
    ret_eg = psc.create_or_update(eg)
    assert ret_eg.enrollment_group_id == GROUP_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_eg.initial_twin = twin
    ret_eg = psc.create_or_update(ret_eg)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get
    ret_eg = psc.get_enrollment_group(GROUP_ID)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get attestation mechansim
    ret_am = psc.get_enrollment_group_attestation_mechanism(GROUP_ID)

    #query
    qs = QuerySpecification(query="*")
    q = psc.create_enrollment_group_query(qs)
    q_results = q.next()
    assert len(q_results) == 1

    #delete
    psc.delete(ret_eg)
    try:
        ret_eg = psc.get_enrollment_group(GROUP_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError
def run_scenario_enrollment_group():
    psc = ProvisioningServiceClient.create_from_connection_string(CONNECTION_STRING)
    att = AttestationMechanism.create_with_x509_signing_certs(SIGNING_CERTIFICATE)
    eg = EnrollmentGroup.create(GROUP_ID, att)

    #create
    ret_eg = psc.create_or_update(eg)
    assert ret_eg.enrollment_group_id == GROUP_ID

    #update
    twin = InitialTwin.create(TAGS, DESIRED_PROPERTIES)
    ret_eg.initial_twin = twin
    ret_eg = psc.create_or_update(ret_eg)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #get
    ret_eg = psc.get_enrollment_group(GROUP_ID)
    assert ret_eg.enrollment_group_id == GROUP_ID
    assert ret_eg.initial_twin.tags == TAGS
    assert ret_eg.initial_twin.desired_properties == DESIRED_PROPERTIES

    #query
    qs = QuerySpecification("*")
    q = psc.create_enrollment_group_query(qs)
    q_results = q.next()
    assert len(q_results) == 1

    #delete
    psc.delete(ret_eg)
    try:
        ret_eg = psc.get_enrollment_group(GROUP_ID)
    except ProvisioningServiceError:
        pass
    else:
        raise AssertionError
예제 #8
0
from azure_provisioning_e2e.service_helper import Helper, connection_string_to_hostname
from azure.iot.device.aio import ProvisioningDeviceClient
from provisioningserviceclient import ProvisioningServiceClient, IndividualEnrollment
from provisioningserviceclient.protocol.models import AttestationMechanism, ReprovisionPolicy
import pytest
import logging
import os
import uuid

pytestmark = pytest.mark.asyncio
logging.basicConfig(level=logging.DEBUG)

PROVISIONING_HOST = os.getenv("PROVISIONING_DEVICE_ENDPOINT")
ID_SCOPE = os.getenv("PROVISIONING_DEVICE_IDSCOPE")
conn_str = os.getenv("PROVISIONING_SERVICE_CONNECTION_STRING")
service_client = ProvisioningServiceClient.create_from_connection_string(
    os.getenv("PROVISIONING_SERVICE_CONNECTION_STRING"))
service_client = ProvisioningServiceClient.create_from_connection_string(
    conn_str)
device_registry_helper = Helper(os.getenv("IOTHUB_CONNECTION_STRING"))
linked_iot_hub = connection_string_to_hostname(
    os.getenv("IOTHUB_CONNECTION_STRING"))


@pytest.mark.it(
    "A device gets provisioned to the linked IoTHub with the device_id equal to the registration_id"
    "of the individual enrollment that has been created with a symmetric key authentication"
)
@pytest.mark.parametrize("protocol", ["mqtt", "mqttws"])
async def test_device_register_with_no_device_id_for_a_symmetric_key_individual_enrollment(
        protocol):
    try:
# full license information.

import six

import context #only needed in this directory
from provisioningserviceclient import ProvisioningServiceClient, QuerySpecification, \
    BulkEnrollmentOperation
from provisioningserviceclient.models import IndividualEnrollment, AttestationMechanism

if __name__ == '__main__':
    connection_string = "[Connection String]"
    endorsement_key = "[Endorsement Key]"
    registration_id = "[Registration ID]"
    
    #set up the provisioning service client
    psc = ProvisioningServiceClient.create_from_connection_string(connection_string)

    #build IndividualEnrollment model

    att = AttestationMechanism.create_with_tpm(endorsement_key)
    ie = IndividualEnrollment.create(registration_id, att)

    #create IndividualEnrollment on the Provisioning Service
    ie = psc.create_or_update(ie)
    six._print(ie)

    #get IndividualEnrollment from the Provisioning Service (note: this step is useless here, as ie is already up to date)
    ie = psc.get_individual_enrollment(registration_id)
    six._print(ie)

    #delete IndividualEnrollment from the Provisioning Service