def test_from_dto(valid_keys): dto = { "systemName": "test_system", "address": "127.0.0.1", "port": 0, "authenticationInfo": "bla" } test_system = ArrowheadSystem(**dto) assert test_system.dto().keys() == valid_keys assert test_system.authority == '127.0.0.1:0' assert test_system.system_name == 'test_system' assert test_system.authentication_info == 'bla'
def _core_system_setup(self) -> None: service_registry = ArrowheadSystem( 'service_registry', str(self.config['service_registry']['address']), int(self.config['service_registry']['port']), '') orchestrator = ArrowheadSystem( 'orchestrator', str(self.config['orchestrator']['address']), int(self.config['orchestrator']['port']), '') self._store_consumed_service(core_service('register'), service_registry, 'POST') self._store_consumed_service(core_service('unregister'), service_registry, 'DELETE') self._store_consumed_service(core_service('orchestration-service'), orchestrator, 'POST')
def __init__(self, system_name: str, address: str, port: int, authentication_info: str = '', keyfile: str = '', certfile: str = ''): logger = get_logger(system_name, 'debug') wsgi_server = pywsgi.WSGIServer( (address, port), None, keyfile=keyfile, certfile=certfile, log=logger, ) super().__init__( ArrowheadSystem(system_name, address, port, authentication_info), HttpConsumer(), HttpProvider(wsgi_server), logger, config, keyfile=keyfile, certfile=certfile ) self._logger.info(f'{self.__class__.__name__} initialized at {self.system.address}:{self.system.port}')
def handle_orchestration_response(service_orchestration_response: Mapping) \ -> List[Tuple[Service, ArrowheadSystem]]: """ Turns orchestration response into list of services """ orchestration_response_list = service_orchestration_response['response'] extracted_data = [] for orchestration_response in orchestration_response_list: service_dto = orchestration_response provider_dto = service_dto['provider'] service_definition = service_dto['service']['serviceDefinition'] service_uri = service_dto['serviceUri'] interface = service_dto['interfaces'][0]['interfaceName'] system_name = provider_dto['systemName'] address = provider_dto['address'] port = provider_dto['port'] service = Service( service_definition, service_uri, interface, ) system = ArrowheadSystem(system_name, address, port, '') extracted_data.append((service, system)) return extracted_data
def test_arrowhead_system(valid_keys): test_system = ArrowheadSystem.make('test_system', '127.0.0.1', 0, 'blablabla') assert test_system.dto().keys() == valid_keys assert test_system.authority == '127.0.0.1:0' assert test_system.system_name == 'test_system' assert test_system.port == 0
def test_arrowhead_system(): test_system = ArrowheadSystem( 'test_system', '127.0.0.1', 0, '', ) assert test_system.dto.keys() == valid_keys assert test_system.authority == '127.0.0.1:0'
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, )
def query_result(self, query_response): query_data = query_response.read_json()['serviceQueryData'][0] service = Service( query_data['serviceDefinition']['serviceDefinition'], query_data['serviceUri'], query_data['interfaces'][0]['interfaceName'], query_data['secure'], query_data['metadata'], query_data['version'], ) system = ArrowheadSystem.from_dto(query_data['provider']) return service, system
def _extract_rule(core_service_tuple: CoreConfig, config: Dict, secure: bool) -> OrchestrationRule: secure_string = constants.Security.SECURE if secure else constants.Security.INSECURE access_policy = constants.AccessPolicy.CERTIFICATE if secure else constants.AccessPolicy.UNRESTRICTED interface = ServiceInterface(core_service_tuple.protocol, secure_string, core_service_tuple.payload) core_system = ArrowheadSystem(**config[core_service_tuple.system]) return OrchestrationRule( Service( core_service_tuple.service_definition, core_service_tuple.uri, interface, access_policy, ), core_system, core_service_tuple.method, )
def test_arrowhead_system(): test_consumer = ArrowheadSystem( 'test_consumer', '127.0.0.1', 0, '', ) valid_keys = { 'systemName', 'address', 'port', 'authenticationInfo' } assert test_consumer.dto.keys() == valid_keys assert test_consumer.authority == '127.0.0.1:0'
def __init__(self, system_name: str, address: str, port: int, authentication_info: str = '', keyfile: str = '', certfile: str = ''): super().__init__(ArrowheadSystem(system_name, address, port, authentication_info), Consumer(keyfile, certfile), Provider(), get_logger(system_name, 'debug'), config, keyfile=keyfile, certfile=certfile) self._logger.info( f'{self.__class__.__name__} initialized at {self.system.address}:{self.system.port}' )
def process_service_query( query_response: Response) -> List[Tuple[Service, ArrowheadSystem]]: """ Handles provided_service query responses and returns a lists of services and systems """ # TODO: Status 400 is general for all core systems and should be put in the handler. if query_response.status_code == 400: raise errors.CoreServiceInputError( query_response.read_json()[constants.Misc.ERROR_MESSAGE]) query_response_ = client_forms.ServiceQueryResponse( **query_response.read_json()) service_and_system = [(Service( service_definition=query_result.service_definition.service_definition, service_uri=query_result.service_uri, interface=ServiceInterface.from_str( query_result.interfaces[0].interface_name), access_policy='', metadata=query_result.metadata, version=query_result.version, ), ArrowheadSystem(**query_result.provider.dict())) for query_result in query_response_.service_query_data] return service_and_system
def _extract_orchestration_rules( orchestration_result: client_forms.OrchestrationResponse, method, ) -> OrchestrationRule: """ Helper function to extract orchestration rules from an orchestration result. Args: orchestration_result: An orchestration result. Returns: Orchestration rule extracted from the orchestration result. """ service_dto = orchestration_result provider_dto = service_dto.provider service = _extract_service(service_dto) system = ArrowheadSystem(**provider_dto.dict()) interface = service_dto.interfaces[0].interface_name auth_tokens = service_dto.authorization_tokens auth_token = auth_tokens.get(interface, '') if auth_tokens else '' return OrchestrationRule(service, system, method, auth_token)
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
keyfile='certificates/crypto/sysop.key', certfile='certificates/crypto/sysop.crt', cafile='certificates/crypto/sysop.ca' ) print('Setting up local cloud') setup_client.orchestration_rules.store( OrchestrationRule( Service( 'mgmt_register_service', 'serviceregistry/mgmt', ServiceInterface.from_str('HTTP-SECURE-JSON'), ), ArrowheadSystem( **default_config['service_registry'] ), 'POST', ) ) setup_client.orchestration_rules.store( OrchestrationRule( Service( 'mgmt_get_systems', 'serviceregistry/mgmt/systems', ServiceInterface('HTTP', 'SECURE', 'JSON'), ), ArrowheadSystem( **default_config['service_registry'] ),
from arrowhead_client.system import ArrowheadSystem _default_address = '127.0.0.1' default_config = { 'service_registry': ArrowheadSystem( 'service_registry', _default_address, 8443, '', ), 'orchestrator': ArrowheadSystem( 'orchestrator', _default_address, 8441, '', ), 'eventhandler': ArrowheadSystem( 'eventhandler', _default_address, 8455, '', ), 'gatekeeper': ArrowheadSystem( 'gatekeeper', _default_address, 8449, '',
from arrowhead_client.system import ArrowheadSystem from arrowhead_client.service import Service import arrowhead_client.client.core_service_forms as forms requester_system = ArrowheadSystem('test_system', 'localhost', 0, '') provider_system = ArrowheadSystem('test_system', 'localhost', 0, '') def test_registration_form(): service = Service( 'test_service', '/test/test/test', 'HTTP-SECURE-JSON', ) registration_form = forms.ServiceRegistrationForm( provided_service=service, provider_system=provider_system, secure='CERTIFICATE', metadata={'dummy': 'data'}, end_of_validity='dummy-date', version=0, ) valid_keys = { 'serviceDefinition', 'serviceUri', 'providerSystem', 'secure', 'interfaces', 'metadata', 'version',
from arrowhead_client.system import ArrowheadSystem from arrowhead_client.service import Service, ServiceInterface from arrowhead_client.constants import OrchestrationFlags import arrowhead_client.client.core_service_forms.client as forms requester_system = ArrowheadSystem.make('test_system', 'localhost', 0) provider_system = ArrowheadSystem.make('test_system', 'localhost', 0) provided_service = Service( service_definition='test_service', service_uri='/test/test/test', interface=ServiceInterface('HTTP', 'SECURE', 'JSON'), access_policy='CERTIFICATE', metadata={'dummy': 'data'}, version=0, ) def test_registration_form(): registration_form = forms.ServiceRegistrationForm.make( provided_service=provided_service, provider_system=provider_system, end_of_validity='dummy-date', ) valid_keys = { 'serviceDefinition', 'serviceUri', 'providerSystem', 'secure', 'interfaces', 'metadata',
from arrowhead_client.system import ArrowheadSystem from arrowhead_client.service import Service, ServiceInterface from arrowhead_client.rules import OrchestrationRule, OrchestrationRuleContainer provider_system = ArrowheadSystem.make('test', '127.0.0.1', 1337, '') consumed_service = Service( 'test', 'test', ServiceInterface('HTTP', 'SECURE', 'JSON'), metadata={'dummy': 'data'}, ) method = 'GET' authorization_token = 'token.token.token' def test_orchestration_rule(): rule = OrchestrationRule(consumed_service, provider_system, method, authorization_token) assert rule.service_definition == consumed_service.service_definition assert rule.protocol == consumed_service.interface.protocol assert rule.secure == consumed_service.interface.secure assert rule.payload_type == consumed_service.interface.payload assert rule.access_policy == consumed_service.access_policy assert rule.metadata == consumed_service.metadata assert rule.version == consumed_service.version assert rule.system_name == provider_system.system_name assert rule.endpoint == f'{provider_system.authority}/{consumed_service.service_uri}' assert rule.authentication_info == provider_system.authentication_info assert rule.method == method assert rule.authorization_token == authorization_token