Exemplo n.º 1
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)
Exemplo n.º 4
0
    def setUp(self):
        tpm_am = AttestationMechanism.create_with_tpm("my-ek")
        self.ie = IndividualEnrollment.create("reg-id", tpm_am)

        self.ret_ie = copy.deepcopy(self.ie._internal)
        self.ret_ie.created_updated_time_utc = 1000
        self.ret_ie.last_updated_time_utc = 1000
Exemplo n.º 5
0
 def test_ie_constructor_full_model(self):
     tpm = genmodels.TpmAttestation(TEST_EK)
     att = genmodels.AttestationMechanism(TPM_LABEL, tpm=tpm)
     drs = genmodels.DeviceRegistrationState(TEST_REG_ID,
                                             REG_STATUS_ASSIGNED)
     twin = genmodels.InitialTwin()
     capabilities = genmodels.DeviceCapabilities(False)
     ie = genmodels.IndividualEnrollment(
         TEST_REG_ID,
         att,
         capabilities=capabilities,
         device_id=TEST_DEV_ID,
         registration_state=drs,
         iot_hub_host_name=TEST_HOST_NAME,
         initial_twin=twin,
         etag=TEST_ETAG,
         provisioning_status=PROV_STATUS_ENABLED,
         created_date_time_utc=TEST_TIME,
         last_updated_date_time_utc=TEST_TIME2)
     ret = IndividualEnrollment(ie)
     self.assertIsInstance(ret, IndividualEnrollment)
     self.assertIs(ret._internal, ie)
     self.assertIsInstance(ret._att_wrapper, AttestationMechanism)
     self.assertIs(ret._att_wrapper._internal, att)
     self.assertIsInstance(ret._drs_wrapper, DeviceRegistrationState)
     self.assertIs(ret._drs_wrapper._internal, drs)
     self.assertIsInstance(ret._twin_wrapper, InitialTwin)
     self.assertIs(ret._twin_wrapper._internal, twin)
     self.assertIsInstance(ret._capabilities_wrapper, DeviceCapabilities)
     self.assertIs(ret._capabilities_wrapper._internal, capabilities)
    def setUp(self):
        tpm_am = AttestationMechanism.create_with_tpm("my-ek")
        self.ie = IndividualEnrollment.create("reg-id", tpm_am)

        self.ret_ie = copy.deepcopy(self.ie._internal)
        self.ret_ie.created_updated_time_utc = 1000
        self.ret_ie.last_updated_time_utc = 1000
Exemplo n.º 7
0
    def get_individual_enrollment(self, registration_id):
        """
        Retrieve an Individual Enrollment from the Provisioning Service

        :param str registration_id: The registration id of the target Individual Enrollment
        :returns: Individual Enrollment from the Provisioning Service corresponding to the given
         registration id
        :rtype: :class:`IndividualEnrollment<provisioningserviceclient.models.IndividualEnrollment>`
        :raises: :class:ProvisioningServiceError
         <provisioningserviceclient.ProvisioningServiceError>` if an error occurs on the
         Provisioning Service
        """
        custom_headers = {}
        custom_headers[ProvisioningServiceClient.
                       authorization_header] = self._gen_sastoken_str()

        try:
            raw_resp = self._runtime_client.device_enrollment.get(
                registration_id, custom_headers, True)
        except genmodels.ProvisioningServiceErrorDetailsException as e:
            raise ProvisioningServiceError(
                self.err_msg_unexpected.format(e.response.status_code), e)

        if not _is_successful(raw_resp.response.status_code):
            raise ProvisioningServiceError(raw_resp.response.reason)

        result = raw_resp.output
        return IndividualEnrollment(result)
Exemplo n.º 8
0
 def setUp(self):
     tpm = genmodels.TpmAttestation(TEST_EK)
     self.gen_att = genmodels.AttestationMechanism(TPM_LABEL, tpm=tpm)
     self.gen_drs = genmodels.DeviceRegistrationState(TEST_REG_ID, REG_STATUS_ASSIGNED)
     self.gen_twin = genmodels.InitialTwin()
     gen_ie = genmodels.IndividualEnrollment(TEST_REG_ID, self.gen_att, TEST_DEV_ID, self.gen_drs, \
         TEST_HOST_NAME, self.gen_twin, TEST_ETAG, PROV_STATUS_ENABLED, TEST_TIME, TEST_TIME2)
     self.ie = IndividualEnrollment(gen_ie)
 def test_ie_constructor_min_model(self):
     tpm = genmodels.TpmAttestation(TEST_EK)
     att = genmodels.AttestationMechanism(TPM_LABEL, tpm=tpm)
     ie = genmodels.IndividualEnrollment(TEST_REG_ID, att)
     ret = IndividualEnrollment(ie)
     self.assertIsInstance(ret, IndividualEnrollment)
     self.assertIs(ret._internal, ie)
     self.assertIsInstance(ret._att_wrapper, AttestationMechanism)
     self.assertIs(ret._att_wrapper._internal, att)
     self.assertIsNone(ret._drs_wrapper)
     self.assertIsNone(ret._twin_wrapper)
Exemplo n.º 10
0
    def test_copy_and_unwrap_bulkop(self):
        enrollments = []
        for i in range(5):
            att = AttestationMechanism.create_with_tpm("test-ek")
            enrollments.append(IndividualEnrollment.create("reg-id" + str(i), att))
        bulkop = BulkEnrollmentOperation("create", enrollments)

        res = _copy_and_unwrap_bulkop(bulkop)

        for i in range(len(res.enrollments)):
            self.assertIs(res.enrollments[i], bulkop.enrollments[i]._internal)
    def test_copy_and_unwrap_bulkop(self):
        enrollments = []
        for i in range(5):
            att = AttestationMechanism.create_with_tpm("test-ek")
            enrollments.append(
                IndividualEnrollment.create("reg-id" + str(i), att))
        bulkop = BulkEnrollmentOperation("create", enrollments)

        res = _copy_and_unwrap_bulkop(bulkop)

        for i in range(len(res.enrollments)):
            self.assertIs(res.enrollments[i], bulkop.enrollments[i]._internal)
Exemplo n.º 12
0
    def setUp(self):
        enrollments = []
        for i in range(5):
            att = AttestationMechanism.create_with_tpm("test-ek")
            enrollments.append(IndividualEnrollment.create("reg-id" + str(i), att))
        self.bulkop = BulkEnrollmentOperation("create", enrollments)

        internal = []
        for enrollment in self.bulkop.enrollments:
            internal.append(enrollment._internal)
        self.internal_bulkop = BulkEnrollmentOperation("create", internal)

        self.bulkop_resp = BulkEnrollmentOperationResult(True)
    def setUp(self):
        enrollments = []
        for i in range(5):
            att = AttestationMechanism.create_with_tpm("test-ek")
            enrollments.append(
                IndividualEnrollment.create("reg-id" + str(i), att))
        self.bulkop = BulkEnrollmentOperation("create", enrollments)

        internal = []
        for enrollment in self.bulkop.enrollments:
            internal.append(enrollment._internal)
        self.internal_bulkop = BulkEnrollmentOperation("create", internal)

        self.bulkop_resp = BulkEnrollmentOperationResult(True)
Exemplo n.º 14
0
 def test_ie_constructor_full_model(self):
     tpm = genmodels.TpmAttestation(TEST_EK)
     att = genmodels.AttestationMechanism(TPM_LABEL, tpm=tpm)
     drs = genmodels.DeviceRegistrationState(TEST_REG_ID, REG_STATUS_ASSIGNED)
     twin = genmodels.InitialTwin()
     ie = genmodels.IndividualEnrollment(TEST_REG_ID, att, TEST_DEV_ID, drs, TEST_HOST_NAME,
                                         twin, TEST_ETAG, REG_STATUS_ASSIGNED, TEST_TIME, TEST_TIME2)
     ret = IndividualEnrollment(ie)
     self.assertIsInstance(ret, IndividualEnrollment)
     self.assertIs(ret._internal, ie)
     self.assertIsInstance(ret._att_wrapper, AttestationMechanism)
     self.assertIs(ret._att_wrapper._internal, att)
     self.assertIsInstance(ret._drs_wrapper, DeviceRegistrationState)
     self.assertIs(ret._drs_wrapper._internal, drs)
     self.assertIsInstance(ret._twin_wrapper, InitialTwin)
     self.assertIs(ret._twin_wrapper._internal, twin)
    def setUp(self):
        enrollments = []
        for i in range(5):
            att = AttestationMechanism.create_with_tpm("test-ek")
            tags_tc = TwinCollection(additional_properties=TAGS)
            desired_properties_tc = TwinCollection(
                additional_properties=DESIRED_PROPERTIES)
            properties = InitialTwinProperties(desired=desired_properties_tc)
            twin = genmodels.InitialTwin(tags=tags_tc, properties=properties)
            twin_wrapper = InitialTwin._create_from_internal(twin)
            enrollments.append(
                IndividualEnrollment.create("reg-id" + str(i),
                                            att,
                                            initial_twin=twin_wrapper))
        self.bulkop = BulkEnrollmentOperation("create", enrollments)

        self.bulkop_resp = BulkEnrollmentOperationResult(is_successful=True)
    def setUp(self):
        self.am = AttestationMechanism.create_with_tpm("my-ek")
        tags_tc = TwinCollection(additional_properties=TAGS)
        desired_properties_tc = TwinCollection(
            additional_properties=DESIRED_PROPERTIES)
        properties = InitialTwinProperties(desired=desired_properties_tc)
        twin = genmodels.InitialTwin(tags=tags_tc, properties=properties)
        self.ie = IndividualEnrollment.create("reg-id",
                                              self.am,
                                              initial_twin=twin)

        self.ret_ie = copy.deepcopy(self.ie)
        self.ret_ie.created_updated_time_utc = 1000
        self.ret_ie.last_updated_time_utc = 1000

        twin_wrapper = InitialTwin._create_from_internal(self.ie.initial_twin)
        self.ie.initial_twin = twin_wrapper
 def test_ie_create_min_model(self):
     att = AttestationMechanism.create_with_tpm(TEST_EK)
     ret = IndividualEnrollment.create(TEST_REG_ID, att)
     internal = ret._internal
     self.assertIsInstance(ret, IndividualEnrollment)
     self.assertEqual(internal.registration_id, TEST_REG_ID)
     self.assertIsNone(internal.device_id)
     self.assertIsNone(internal.iot_hub_host_name)
     self.assertIsNone(internal.provisioning_status)
     self.assertEqual(internal.attestation, att._internal)
     self.assertIs(ret._att_wrapper, att)
     self.assertIsNone(internal.initial_twin)
     self.assertIsNone(ret._twin_wrapper)
     self.assertIsNone(internal.registration_state)
     self.assertIsNone(ret._drs_wrapper)
     self.assertIsNone(internal.etag)
     self.assertIsNone(internal.created_date_time_utc)
     self.assertIsNone(internal.last_updated_date_time_utc)
 def test_ie_create_full_model(self):
     att = AttestationMechanism.create_with_tpm(TEST_EK)
     ts = InitialTwin.create()
     ret = IndividualEnrollment.create(TEST_REG_ID, att, TEST_DEV_ID, TEST_HOST_NAME, ts, \
         PROV_STATUS_ENABLED)
     internal = ret._internal
     self.assertIsInstance(ret, IndividualEnrollment)
     self.assertEqual(internal.registration_id, TEST_REG_ID)
     self.assertEqual(internal.device_id, TEST_DEV_ID)
     self.assertEqual(internal.iot_hub_host_name, TEST_HOST_NAME)
     self.assertEqual(internal.provisioning_status, PROV_STATUS_ENABLED)
     self.assertEqual(internal.attestation, att._internal)
     self.assertIs(ret._att_wrapper, att)
     self.assertEqual(internal.initial_twin, ts._internal)
     self.assertIs(ret._twin_wrapper, ts)
     self.assertIsNone(internal.registration_state)
     self.assertIsNone(ret._drs_wrapper)
     self.assertIsNone(internal.etag)
     self.assertIsNone(internal.created_date_time_utc)
     self.assertIsNone(internal.last_updated_date_time_utc)
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
    psc.delete(ie)
    #could also use psc.delete_individual_enrollment_by_param(ie.registration_id, ie.etag)

    #bulk create IndividualEnrollments
    enrollments = []