def test_with_certfile(valid_keys, certfile_path):
    priv_key, pub_key = generate_keys()
    cert = generate_cert(['pytest'], priv_key, pub_key)
    with open(certfile_path, 'wb') as cf:
        cf.write(cert.public_bytes(encoding=serialization.Encoding.PEM))

    test_system = ArrowheadSystem.with_certfile(
        'test_system',
        '127.0.0.1',
        1337,
        certfile_path,
    )
Example #2
0
    def create(
        cls,
        system_name: str,
        address: str,
        port: int,
        config: Dict = None,
        keyfile: str = '',
        certfile: str = '',
        cafile: str = '',
        log_mode: str = 'debug',
        **kwargs,
    ) -> ArrowheadClient:
        """
        Factory method for client instances.
        This is the preferred way of creating class instances because it takes care of instantiating producers,
        consumers, and logging properly.

        If you wish to use different producers or consumers, create a new class inheriting from either
        :code:`client.ArrowheadClientAsync` or :code:`client.ArrowheadClientSync` and specify the
        :code:`__arrowhead_provider__` and :code:`__arrowhead_consumers__` fields.

        Args:
            system_name: Name for the system the client will register as in the service and system registries.
            address: System address.
            port: System port.
            config: Config object, format not yet specified. Optional.
            keyfile: Path to a PEM keyfile. If you use pkcs#12 keystores, you need to convert them to PEM format first.
            certfile: Path to a PEM certfile. If you use pkcs#12 keystores, you need to convert them to PEM format first.
            cafile: Path to a PEM certificate authority file. If you use pkcs#12 keystores, you need to convert them to PEM format first.
        Returns:
            A new ArrowheadClient instance.

        Example::

            from arrowhead_client.implementations import AsyncClient

            example_client = AsyncClient.create(
                    system_name='example_client',
                    address='127.0.0.1',
                    port=5678,
                    config=example_config,
                    keyfile='certificates/example.key',
                    certfile='certificates/example.pem',
                    cafile='certificates/example_cloud.ca',
            )
        """
        logger = get_logger(system_name, log_mode)
        system = ArrowheadSystem.with_certfile(
            system_name,
            address,
            port,
            certfile,
        )
        new_instance = cls(
            system,
            tuple(
                consumer(keyfile, certfile, cafile)
                for consumer in cls.__arrowhead_consumers__),
            cls.__arrowhead_provider__(cafile),
            logger,
            config=config,
            keyfile=keyfile,
            certfile=certfile,
            **kwargs,
        )

        return new_instance