Example #1
0
    def channel_create(self, orderer_name, channel_name, requestor,
                       config_yaml, channel_profile):
        """
        Create a channel, send request to orderer, and check the response

        :param orderer_name: Name of orderer to send request to
        :param channel_name: Name of channel to create
        :param requestor: Name of creator
        :param config_yaml: Directory path of config yaml to be set for FABRIC_
        CFG_PATH variable
        :param channel_profile: Name of the channel profile defined inside
        config yaml file
        :return: True (creation succeeds) or False (creation failed)
        """
        if self.get_channel(channel_name):
            _logger.warning("channel {} already existed when creating".format(
                channel_name))
            return False

        orderer = self.get_orderer(orderer_name)
        if not orderer:
            _logger.error("No orderer_name instance found with name {}".format(
                orderer_name))
            return False

        tx = self.generate_channel_tx(channel_name, config_yaml,
                                      channel_profile)
        if tx is None:
            _logger.error('Configtx is empty')
            return False
        _logger.info("Configtx file successfully created in current directory")

        with open(tx, 'rb') as f:
            envelope = f.read()
            config = utils.extract_channel_config(envelope)

        # convert envelope to config
        self.tx_context = TXContext(requestor, Ecies(), {})
        tx_id = self.tx_context.tx_id
        nonce = self.tx_context.nonce
        signatures = []
        org1_admin_signature = self.sign_channel_config(config)
        # append org1_admin_signature to signatures
        signatures.append(org1_admin_signature)

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }
        response = self._create_channel(request)

        if response[0].status == 200:
            self.new_channel(channel_name)
            return True
        else:
            return False
Example #2
0
    def test_string_to_signature(self):
        with open(self.channel_tx, 'rb') as f:
            channel_tx = f.read()

        channel_config = utils.extract_channel_config(channel_tx)

        client = Client()
        client.state_store = FileKeyValueStore(self.kv_store_path)

        orderer_org_admin = get_orderer_org_admin(client)
        orderer_org_admin_tx_context = \
            TXContext(orderer_org_admin, Ecies(), {})
        client.tx_context = orderer_org_admin_tx_context

        orderer_org_admin_signature = client.sign_channel_config(
            channel_config)
        orderer_org_admin_signature_bytes = \
            orderer_org_admin_signature.SerializeToString()

        proto_signature = utils.string_to_signature(
            [orderer_org_admin_signature_bytes])

        self.assertIsInstance(proto_signature, list)
        self.assertTrue(
            'OrdererMSP' in proto_signature[0].signature_header.__str__())
Example #3
0
def build_channel_request(client, channel_tx, channel_name):
    """
    Args:
        client: the client instance
        channel_tx: channel config file
        channel_name: channel name
    return channel request to create a channel
    """

    signatures = []
    prop_req = TXProposalRequest()
    with open(channel_tx, 'rb') as f:
        envelope = f.read()
        config = utils.extract_channel_config(envelope)

    orderer_config = TRUSTAS_NETWORK['orderer']

    if "LOCAL_DEPLOY" in os.environ:
        LOCAL_DEPLOY = os.environ["LOCAL_DEPLOY"] == "True"
    env = "local_" if LOCAL_DEPLOY else "gcp_"

    orderer = Orderer(
        endpoint=orderer_config[env + 'grpc_endpoint'],
        tls_ca_cert_file=orderer_config['tls_cacerts'],
        opts=(('grpc.ssl_target_name_override', 'orderer.example.com'), ),
    )

    orderer_admin = get_orderer_org_user(state_store=client.state_store)
    orderer_tx_context = TXContext(orderer_admin, ecies(), prop_req, {})
    client.tx_context = orderer_tx_context
    orderer_admin_signature = client.sign_channel_config(config)
    signatures.append(orderer_admin_signature)
    tx_id = orderer_tx_context.tx_id
    nonce = orderer_tx_context.nonce

    org1_admin = get_peer_org_user('org1.example.com', "Admin",
                                   client.state_store)
    org1_tx_context = TXContext(org1_admin, ecies(), prop_req, {})
    client.tx_context = org1_tx_context
    org1_admin_signature = client.sign_channel_config(config)
    signatures.append(org1_admin_signature)

    # org2_admin = get_peer_org_user('org2.example.com', "Admin",
    #                                client.state_store)
    # org2_tx_context = TXContext(org2_admin, ecies(), prop_req, {})
    # client.tx_context = org2_tx_context
    # org2_admin_signature = client.sign_channel_config(config)
    # signatures.append(org2_admin_signature)

    request = {
        'config': config,
        'signatures': signatures,
        'channel_name': channel_name,
        'orderer': orderer,
        'tx_id': tx_id,
        'nonce': nonce
    }

    return request
Example #4
0
def build_channel_request(client, channel_tx, channel_name):
    """
    Args:
        client: the client instance
        channel_tx: channel config file
        channel_name: channel name
    return channel request to create a channel
    """

    signatures = []
    prop_req = TXProposalRequest()
    with open(channel_tx, 'rb') as f:
        envelope = f.read()
        config = utils.extract_channel_config(envelope)

    orderer_config = E2E_CONFIG['test-network']['orderer']
    with open(orderer_config['tls_cacerts'], 'rb') as tls_cacerts:
        pem = tls_cacerts.read()

    opts = (('grpc.ssl_target_name_override', 'orderer.example.com'), )
    orderer = Orderer(endpoint=orderer_config['grpc_endpoint'],
                      pem=pem,
                      opts=opts)
    orderer_admin = get_orderer_org_admin(client)
    orderer_tx_context = TXContext(orderer_admin, Ecies(), prop_req, {})
    client.tx_context = orderer_tx_context
    orderer_admin_signature = client.sign_channel_config(config)
    orderer_admin_signature_bytes = orderer_admin_signature.SerializeToString()
    signatures.append(orderer_admin_signature_bytes)
    tx_id = orderer_tx_context.tx_id
    nonce = orderer_tx_context.nonce

    org1_admin = get_peer_org_admin(client, 'org1.example.com')
    org1_tx_context = TXContext(org1_admin, Ecies(), prop_req, {})
    client.tx_context = org1_tx_context
    org1_admin_signature = client.sign_channel_config(config)
    org1_admin_signature_bytes = org1_admin_signature.SerializeToString()

    signatures.append(org1_admin_signature_bytes)

    org2_admin = get_peer_org_admin(client, 'org2.example.com')
    org2_tx_context = TXContext(org2_admin, Ecies(), prop_req, {})
    client.tx_context = org2_tx_context
    org2_admin_signature = client.sign_channel_config(config)
    org2_admin_signature_bytes = org2_admin_signature.SerializeToString()
    signatures.append(org2_admin_signature_bytes)

    request = {
        'config': config,
        'signatures': signatures,
        'channel_name': channel_name,
        'orderer': orderer,
        'tx_id': tx_id,
        'nonce': nonce
    }

    return request
Example #5
0
    def test_extract_channel_config(self):
        with open(self.channel_tx, 'rb') as f:
            channel_tx = f.read()

        config_update = configtx_pb2.ConfigUpdate()

        channel_config = utils.extract_channel_config(channel_tx)
        self.assertTrue(hasattr(channel_config, 'decode'))

        config_update.ParseFromString(channel_config)
        self.assertEqual(config_update.channel_id, self.channel_id)
Example #6
0
    def channel_create(self, orderer_name, channel_name, requester, tx):
        """
        Create a channel, send request to orderer, and check the response

        :param orderer_name: Name of orderer to send request to
        :param channel_name: Name of channel to create
        :param requester: Name of creator
        :param tx: Path to the new channel tx file
        :return: True (creation succeeds) or False (creation failed)
        """
        if self.get_channel(channel_name):
            logging.warning("channel {} already existed when creating".format(
                channel_name))
            return True

        orderer = self.get_orderer(orderer_name)
        if not orderer:
            logging.error("No orderer_name instance found with name {}".format(
                orderer_name))
            return False

        with open(tx, 'rb') as f:
            envelope = f.read()
            config = extract_channel_config(envelope)

        # convert envelope to config
        self.tx_context = TXContext(requester, Ecies(), {})
        tx_id = self.tx_context.tx_id
        nonce = self.tx_context.nonce
        signatures = []
        org1_admin_signature = self.sign_channel_config(config)
        # append org1_admin_signature to signatures
        signatures.append(org1_admin_signature)

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }
        q = Queue(1)
        response = self._create_channel(request)
        response.subscribe(on_next=lambda x: q.put(x),
                           on_error=lambda x: q.put(x))

        status, _ = q.get(timeout=5)
        if status.status == 200:
                self.new_channel(channel_name)
                return True
        else:
            return False
Example #7
0
    def test_string_to_signature(self):
        with open(self.channel_tx, 'rb') as f:
            channel_tx = f.read()

        channel_config = utils.extract_channel_config(channel_tx)

        client = Client('test/fixtures/network.json')

        orderer_org_admin = get_orderer_org_user(
            state_store=client.state_store)
        orderer_org_admin_tx_context = \
            TXContext(orderer_org_admin, Ecies(), {})
        client.tx_context = orderer_org_admin_tx_context

        orderer_org_admin_signature = client.sign_channel_config(
            channel_config)

        proto_signature = utils.string_to_signature(
            [orderer_org_admin_signature])

        self.assertIsInstance(proto_signature, list)
        self.assertTrue(
            'OrdererMSP' in proto_signature[0].signature_header.__str__())
Example #8
0
    def create_channel(self, orderer_name, channel, creator, tx):
        """
        Create a channel
        :param orderer_name: Name of orderer to send request to
        :param channel: Name of channel to create
        :param creator: Name of creator
        :param tx: Path to the new channel tx file
        :return:
        """
        orderer = self.get_orderer(orderer_name)
        if not orderer:
            logging.error("No orderer_name instance found with name {}".format(
                orderer_name))
            return None

        with open(tx, 'rb') as f:
            envelope = f.read()
            config = extract_channel_config(envelope)

        # convert envelope to config
        self.tx_context = TXContext(creator, Ecies(), {})
        tx_id = self.tx_context.tx_id
        nonce = self.tx_context.nonce
        signatures = []
        org1_admin_signature = self.sign_channel_config(config)
        # append org1_admin_signature to signatures
        signatures.append(org1_admin_signature)

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel
        }
        return self._create_channel(request)
Example #9
0
    def test_create_channel_missing_signatures(self):
        signatures = []

        with open(self.orderer_tls_certs) as f:
            pem = f.read()

        opts = (('grpc.ssl_target_name_override', 'orderer.example.com'), )
        orderer = Orderer(pem=pem, opts=opts)

        with open(self.configtx_path, 'rb') as f:
            envelope = f.read()

        # convert envelope to config
        config = extract_channel_config(envelope)

        channel_name = 'businesschannel'

        # signatures orderer admin
        orderer_admin = get_orderer_org_admin(self.client)
        orderer_admin_tx_context = TXContext(orderer_admin, Ecies(), {})
        self.client.tx_context = orderer_admin_tx_context

        orderer_admin_signature = self.client.sign_channel_config(config)
        orderer_admin_signature.SerializeToString()

        # take the tx_id and nonce from the oderer user context
        tx_id = orderer_admin_tx_context.tx_id
        nonce = orderer_admin_tx_context.nonce

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org1 admin
        org1_admin = get_peer_org_user(self.client, 'org1.example.com')
        org1_admin_tx_context = TXContext(org1_admin, Ecies(), {})
        self.client.tx_context = org1_admin_tx_context

        org1_admin_signature = self.client.sign_channel_config(config)
        org1_admin_signature.SerializeToString()

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org2 admin
        org2_admin = get_peer_org_user(self.client, 'org2.example.com')
        org2_admin_tx_context = TXContext(org2_admin, Ecies(), {})
        self.client.tx_context = org2_admin_tx_context

        org2_admin_signature = self.client.sign_channel_config(config)
        org2_admin_signature.SerializeToString()

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }

        queue = Queue(1)

        response = self.client.create_channel(request)

        response.subscribe(on_next=lambda x: queue.put(x),
                           on_error=lambda x: queue.put(x))

        status, _ = queue.get(timeout=5)
        self.assertEqual(status.status, 400)