def exists(self, object_id, related_id):
     """Check the existence of a relationship record"""
     # pylint: disable=not-callable
     container = self._state_container()
     address = self.address(object_id=object_id, related_id=related_id)
     data = ClientSync().get_address(address=address)
     if not data:
         return False
     container.ParseFromString(data)
     stores = list(container.relationships)
     if not stores:
         LOGGER.warning(
             "%s %s relationship container for %s %s at address %s has no records",
             self.object_type.name.title(),
             object_id,
             self.related_type.name.lower(),
             related_id,
             address,
         )
         return False
     if len(stores) > 1:
         LOGGER.warning(
             "%s %s relationship container for %s %s at address %s has more than one record",
             self.object_type.name.title(),
             object_id,
             self.related_type.name.lower(),
             related_id,
             address,
         )
     store = stores[0]
     return bool(store.object_id == object_id
                 and store.related_id == related_id)
示例#2
0
def process(rec, database):
    """ Process inbound queue records
    """
    try:
        if "batch" not in rec or not rec["batch"]:
            database.run_query(
                database.get_table("inbound_queue").get(rec["id"]).delete())
            rec["sync_direction"] = "inbound"
            database.run_query(database.get_table("sync_errors").insert(rec))
            return

        batch = batch_pb2.Batch()
        batch.ParseFromString(rec["batch"])
        batch_list = batcher.batch_to_list(batch=batch)
        status = ClientSync().send_batches_get_status(batch_list=batch_list)
        if status[0]["status"] == "COMMITTED":
            if "metadata" in rec and rec["metadata"]:
                data = {
                    "address": rec["address"],
                    "object_type": rec["object_type"],
                    "object_id": rec["object_id"],
                    "provider_id": rec["provider_id"],
                    "created_at": r.now(),
                    "updated_at": r.now(),
                    **rec["metadata"],
                }
                query = (
                    database.get_table("metadata").get(
                        rec["address"]).replace(lambda doc: r.branch(
                            # pylint: disable=singleton-comparison
                            (doc == None),  # noqa
                            r.expr(data),
                            doc.merge({
                                "metadata": rec["metadata"],
                                "updated_at": r.now()
                            }),
                        )))
                result = database.run_query(query)
                if (not result["inserted"]
                        and not result["replaced"]) or result["errors"] > 0:
                    LOGGER.warning("error updating metadata record:\n%s\n%s",
                                   result, query)
            rec["sync_direction"] = "inbound"
            database.run_query(database.get_table("changelog").insert(rec))
            database.run_query(
                database.get_table("inbound_queue").get(rec["id"]).delete())
        else:
            rec["error"] = get_status_error(status)
            rec["sync_direction"] = "inbound"
            database.run_query(database.get_table("sync_errors").insert(rec))
            database.run_query(
                database.get_table("inbound_queue").get(rec["id"]).delete())
    except Exception as err:  # pylint: disable=broad-except
        LOGGER.exception("%s exception processing inbound record:\n%s",
                         type(err).__name__, rec)
        LOGGER.exception(err)
    def send(self, signer_keypair, payload):
        """Sends a payload to the validator API"""
        if not isinstance(signer_keypair, Key):
            raise TypeError("Expected signer_keypair to be a Key")
        if not isinstance(payload, protobuf.rbac_payload_pb2.RBACPayload):
            raise TypeError("Expected payload to be an RBACPayload")

        _, _, batch_list, _ = make(payload=payload, signer_keypair=signer_keypair)
        status = ClientSync().send_batches_get_status(batch_list=batch_list)
        return status
 def get(self, object_id, related_id=None):
     """Gets an address from the blockchain from the validator API"""
     address = self.address(object_id=object_id, related_id=related_id)
     # pylint: disable=not-callable
     container = self._state_container()
     container.ParseFromString(ClientSync().get_address(address=address))
     return self._find_in_state_container(
         container=container,
         address=address,
         object_id=object_id,
         related_id=related_id,
     )
    def test_state(self):
        """Grab the entire blockchain state and deserialize it"""
        subtree = addresser.family.namespace
        for item in ClientSync().list_state(subtree=subtree)["data"]:
            address_type = item["address_type"] = addresser.get_address_type(
                item["address"])
            if address_type == addresser.AddressSpace.USER:
                content = user_state_pb2.UserContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.PROPOSALS:
                content = proposal_state_pb2.ProposalsContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.SYSADMIN_ATTRIBUTES:
                content = "SYSADMIN_ATTRIBUTES"
            elif address_type == addresser.AddressSpace.SYSADMIN_MEMBERS:
                content = "SYSADMIN_MEMBERS"
            elif address_type == addresser.AddressSpace.SYSADMIN_OWNERS:
                content = "SYSADMIN_OWNERS"
            elif address_type == addresser.AddressSpace.SYSADMIN_ADMINS:
                content = "SYSADMIN_ADMINS"
            elif address_type == addresser.AddressSpace.ROLES_ATTRIBUTES:
                content = role_state_pb2.RoleAttributesContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.ROLES_MEMBERS:
                content = role_state_pb2.RoleRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.ROLES_OWNERS:
                content = role_state_pb2.RoleRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.ROLES_ADMINS:
                content = role_state_pb2.RoleRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.ROLES_TASKS:
                content = role_state_pb2.RoleRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.TASKS_ATTRIBUTES:
                content = task_state_pb2.TaskAttributesContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.TASKS_OWNERS:
                content = task_state_pb2.TaskRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            elif address_type == addresser.AddressSpace.TASKS_ADMINS:
                content = task_state_pb2.TaskRelationshipContainer()
                content.ParseFromString(b64decode(item["data"]))
            else:
                content = "ERROR: unknown type: {}".format(address_type)

            LOGGER.debug("%-80s%-30s%s", item["address"], address_type,
                         content)
 def __init__(self, *args, **kwargs):
     BatchAssertions.__init__(self, *args, **kwargs)
     self.client = ClientSync()
 def __init__(self):
     """Objects and methods shared across message libraries"""
     self.batch = Batcher()
     self.client = ClientSync()
     self.state = StateClient()
 def __init__(self):
     """Objects and methods shared across *_manager libraries"""
     self.batch = Batcher()
     self.client = ClientSync()
def process(rec, conn):
    """ Process inbound queue records
    """
    try:
        # Changes members from distinguished name to next_id for roles
        if "members" in rec["data"]:
            rec = translate_field_to_next(rec, "members")
        if "owners" in rec["data"]:
            rec = translate_field_to_next(rec, "owners")

        add_transaction(rec)
        if "batch" not in rec or not rec["batch"]:
            r.table("inbound_queue").get(rec["id"]).delete().run(conn)
            rec["sync_direction"] = "inbound"
            r.table("sync_errors").insert(rec).run(conn)
            return

        batch = batch_pb2.Batch()
        batch.ParseFromString(rec["batch"])
        batch_list = batch_to_list(batch=batch)
        client = ClientSync()
        status = client.send_batches_get_status(batch_list=batch_list)
        while status[0]["status"] == "PENDING":
            LOGGER.info("Batch status is %s", status)
            status = client.status_recheck(batch_list)
        if status[0]["status"] == "COMMITTED":
            if rec["data_type"] == "user":
                insert_to_user_mapping(rec)
            if "metadata" in rec and rec["metadata"]:
                data = {
                    "address": rec["address"],
                    "object_type": rec["object_type"],
                    "object_id": rec["object_id"],
                    "provider_id": rec["provider_id"],
                    "created_at": r.now(),
                    "updated_at": r.now(),
                    **rec["metadata"],
                }

                query = (
                    r.table("metadata").get(
                        rec["address"]).replace(lambda doc: r.branch(
                            # pylint: disable=singleton-comparison
                            (doc == None),  # noqa
                            r.expr(data),
                            doc.merge({
                                "metadata": rec["metadata"],
                                "updated_at": r.now()
                            }),
                        )))
                result = query.run(conn)
                if (not result["inserted"]
                        and not result["replaced"]) or result["errors"] > 0:
                    LOGGER.warning("error updating metadata record:\n%s\n%s",
                                   result, query)
            rec["sync_direction"] = "inbound"
            r.table("changelog").insert(rec).run(conn)
            r.table("inbound_queue").get(rec["id"]).delete().run(conn)
        else:
            rec["error"] = get_status_error(status)
            rec["sync_direction"] = "inbound"
            r.table("sync_errors").insert(rec).run(conn)
            r.table("inbound_queue").get(rec["id"]).delete().run(conn)

    except Exception as err:  # pylint: disable=broad-except
        LOGGER.exception("%s exception processing inbound record:\n%s",
                         type(err).__name__, rec)
        LOGGER.exception(err)
示例#10
0
 def __init__(self):
     self.batch = Batcher()
     self.client = ClientSync()
# Copyright 2018 Contributors to Hyperledger Sawtooth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------

from rbac.common.sawtooth.client_sync import ClientSync

# pylint: disable=invalid-name
client = ClientSync()

__all__ = ["client"]
示例#12
0
 def __init__(self):
     """Objects and methods shared across relationship libraries"""
     self.client = ClientSync()