def test_gen_sysadmin_member_addr(self):
        """Tests the sysadmin member address creation function as well as the
        address_is function.
        """

        member_id = uuid4().hex
        address = addresser.make_sysadmin_members_address(member_id)

        self.assertEqual(len(address), addresser.ADDRESS_LENGTH,
                         "The address is 70 characters")

        self.assertTrue(addresser.is_address(address),
                        "The address is 70 character hexidecimal")

        self.assertTrue(addresser.namespace_ok(address),
                        "The address has correct namespace prefix")

        self.assertTrue(
            addresser.is_family_address(address),
            "The address is 70 character hexidecimal with family prefix",
        )

        self.assertEqual(
            addresser.address_is(address),
            AddressSpace.SYSADMIN_MEMBERS,
            "The address created must be a SysAdmin Attributes address.",
        )
    def test_det_sysadmin_member_addr(self):
        """Tests that a specific member_id generates the expected
        sysadmin member address, and thus is probably deterministic.
        """

        member_id = "966ab67317234df489adb4bc1f517b88"
        expected_address = "9f4448000000000000000000000000\
0000000000000000000000000000000000000083"

        address = addresser.make_sysadmin_members_address(member_id)

        self.assertEqual(len(address), addresser.ADDRESS_LENGTH,
                         "The address is 70 characters")

        self.assertTrue(addresser.is_address(address),
                        "The address is 70 character hexidecimal")

        self.assertTrue(addresser.namespace_ok(address),
                        "The address has correct namespace prefix")

        self.assertTrue(
            addresser.is_family_address(address),
            "The address is 70 character hexidecimal with family prefix",
        )

        self.assertEqual(address, expected_address,
                         "The address is the one we expected it to be")

        self.assertEqual(
            addresser.address_is(address),
            AddressSpace.SYSADMIN_MEMBERS,
            "The address created must be a SysAdmin Attributes address.",
        )
def create_task(txn_key, batch_key, task_id, task_name, admins, owners, metadata):

    create_payload = task_transaction_pb2.CreateTask(task_id=task_id, name=task_name)

    create_payload.admins.extend(admins)

    inputs = [
        addresser.make_task_attributes_address(task_id=task_id),
        addresser.make_sysadmin_members_address(txn_key.public_key),
    ]

    inputs.extend([addresser.make_user_address(user_id=u) for u in admins])
    inputs.extend([addresser.make_task_admins_address(task_id, u) for u in admins])

    outputs = [addresser.make_task_attributes_address(task_id=task_id)]

    outputs.extend([addresser.make_task_admins_address(task_id, u) for u in admins])

    if owners:
        create_payload.owners.extend(owners)
        inputs.extend([addresser.make_user_address(user_id=u) for u in owners])
        inputs.extend([addresser.make_task_owners_address(task_id, u) for u in owners])
        outputs.extend([addresser.make_task_owners_address(task_id, u) for u in owners])

    rbac_payload = rbac_payload_pb2.RBACPayload(
        content=create_payload.SerializeToString(),
        message_type=rbac_payload_pb2.RBACPayload.CREATE_TASK,
    )

    return make_header_and_batch(rbac_payload, inputs, outputs, txn_key, batch_key)
Beispiel #4
0
def create_role(txn_key, batch_key, role_name, role_id, metadata, admins,
                owners):
    """Create a BatchList with a CreateRole transaction in it.

    Args:
        txn_key (Key): The transaction signer's key pair.
        batch_key (Key): The batch signer's key pair.
        role_name (str): The name of the Role.
        role_id (str): A uuid that identifies this Role.
        metadata (str): Client supplied information that is not parsed.
        admins (list): A list of User ids of the Users who are admins of this
            Role.
        owners (list): A list of User ids of the Users who are owners of this
            Role.

    Returns:
        tuple
            BatchList, batch header_signature tuple
    """

    create_role_payload = role_transaction_pb2.CreateRole(role_id=role_id,
                                                          name=role_name,
                                                          metadata=metadata,
                                                          admins=admins,
                                                          owners=owners)

    inputs = [
        addresser.make_sysadmin_members_address(txn_key.public_key),
        addresser.make_role_attributes_address(role_id),
    ]
    inputs.extend([addresser.make_user_address(u) for u in admins])
    inputs.extend([addresser.make_user_address(u) for u in owners])
    inputs.extend([
        addresser.make_role_admins_address(role_id=role_id, user_id=a)
        for a in admins
    ])
    inputs.extend([
        addresser.make_role_owners_address(role_id=role_id, user_id=o)
        for o in owners
    ])

    outputs = [addresser.make_role_attributes_address(role_id)]
    outputs.extend([
        addresser.make_role_admins_address(role_id=role_id, user_id=a)
        for a in admins
    ])
    outputs.extend([
        addresser.make_role_owners_address(role_id=role_id, user_id=o)
        for o in owners
    ])

    rbac_payload = rbac_payload_pb2.RBACPayload(
        content=create_role_payload.SerializeToString(),
        message_type=rbac_payload_pb2.RBACPayload.CREATE_ROLE,
    )

    return make_header_and_batch(rbac_payload, inputs, outputs, txn_key,
                                 batch_key)
    def create(self, signer_keypair, role, do_batch=True, do_send=True, do_get=False):
        if not isinstance(signer_keypair, Key):
            raise TypeError("Expected signer_keypair to be a Key")
        if not isinstance(role, role_transaction_pb2.CreateRole):
            raise TypeError(
                "Expected role to be a role_transaction_pb2.CreateRole, use make first"
            )

        inputs = [
            addresser.make_sysadmin_members_address(signer_keypair.public_key),
            addresser.make_role_attributes_address(role.role_id),
        ]
        inputs.extend([addresser.make_user_address(u) for u in role.admins])
        inputs.extend([addresser.make_user_address(u) for u in role.owners])
        inputs.extend(
            [
                addresser.make_role_admins_address(role_id=role.role_id, user_id=a)
                for a in role.admins
            ]
        )
        inputs.extend(
            [
                addresser.make_role_owners_address(role_id=role.role_id, user_id=o)
                for o in role.owners
            ]
        )

        transaction = self.batch.make_transaction(
            message=role,
            message_type=RBACPayload.CREATE_ROLE,
            inputs=inputs,
            outputs=inputs,
            signer_keypair=signer_keypair,
        )

        if not do_batch:
            return transaction

        batch = self.batch.make_batch(transaction=transaction)
        if not do_send:
            return batch

        batch_list = self.batch.batch_to_list(batch)
        status = self.client.send_batches_get_status(batch_list=batch_list)
        if not do_get:
            return status

        return self.get(role_id=role.role_id)
    def test_sysadmin_addresses(self):
        """Tests the SysAdmin address creation functions as well as the
        address_is function.

        Notes:
            1. Create a SysAdmin address.
                - SysAdmin Attributes
                - SysAdmin Members
                - SysAdmin Owners
                - SysAdmin Admins
            2. Assert that the address_is function returns the correct
               address type.
        """

        sysadmin_address = addresser.make_sysadmin_attr_address()

        self.assertEqual(
            len(sysadmin_address), 70, "The address is a well-formed address."
        )

        self.assertEqual(
            addresser.address_is(sysadmin_address),
            AddressSpace.SYSADMIN_ATTRIBUTES,
            "The SysAdmin Attributes address created must be found "
            "to be a SysAdmin Attributes address.",
        )

        sysadmin_members_address = addresser.make_sysadmin_members_address(
            user_id=uuid4().hex
        )

        self.assertEqual(
            len(sysadmin_members_address), 70, "The address is a well-formed address."
        )

        self.assertEqual(
            addresser.address_is(sysadmin_members_address),
            AddressSpace.SYSADMIN_MEMBERS,
            "The SysAdmin Members address created must be found to "
            "be a SysAdmin Members address.",
        )

        sysadmin_owners_address = addresser.make_sysadmin_owners_address(
            user_id=uuid4().hex
        )

        self.assertEqual(
            len(sysadmin_owners_address), 70, "The address is a well-formed address."
        )

        self.assertEqual(
            addresser.address_is(sysadmin_owners_address),
            AddressSpace.SYSADMIN_OWNERS,
            "The SysAdmin Owners address created must be found to "
            "be a SysAdmin Owners address.",
        )

        sysadmin_admins_address = addresser.make_sysadmin_admins_address(
            user_id=uuid4().hex
        )

        self.assertEqual(
            len(sysadmin_admins_address), 70, "The address is a well-formed address."
        )

        self.assertEqual(
            addresser.address_is(sysadmin_admins_address),
            AddressSpace.SYSADMIN_ADMINS,
            "The SysAdmin Admins address created must be found to "
            "be a SysAdmin Admins address.",
        )