From 4851bf2daed7b2eb6febf6a9568108f6599dabea Mon Sep 17 00:00:00 2001 From: "Marcin.Werzenski" Date: Wed, 21 Feb 2024 15:45:22 +0100 Subject: [PATCH 1/4] fix: Session added as optional parameter to enable tls communication with clients --- filip/utils/cleanup.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/filip/utils/cleanup.py b/filip/utils/cleanup.py index 9974e8c2..92d520ce 100644 --- a/filip/utils/cleanup.py +++ b/filip/utils/cleanup.py @@ -11,9 +11,10 @@ ContextBrokerClient, \ IoTAClient, \ QuantumLeapClient +from requests import Session -def clear_context_broker(url: str, fiware_header: FiwareHeader): +def clear_context_broker(url: str, fiware_header: FiwareHeader, session: Session = None ): """ Function deletes all entities, registrations and subscriptions for a given fiware header @@ -25,12 +26,13 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader): Args: url: Url of the context broker service fiware_header: header of the tenant + session: session object with set ca.crt and ca.key for TLS support Returns: None """ # create client - client = ContextBrokerClient(url=url, fiware_header=fiware_header) + client = ContextBrokerClient(url=url, fiware_header=fiware_header, session=session) # clean entities client.delete_entities(entities=client.get_entity_list()) @@ -45,7 +47,7 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader): assert len(client.get_registration_list()) == 0 -def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader): +def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, session: Session = None): """ Function deletes all device groups and devices for a given fiware header @@ -53,12 +55,13 @@ def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader): Args: url: Url of the context broker service fiware_header: header of the tenant + session: session object with set ca.crt and ca.key for TLS support Returns: None """ # create client - client = IoTAClient(url=url, fiware_header=fiware_header) + client = IoTAClient(url=url, fiware_header=fiware_header, session=session) # clear registrations for device in client.get_device_list(): @@ -72,12 +75,13 @@ def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader): assert len(client.get_group_list()) == 0 -def clear_quantumleap(url: str, fiware_header: FiwareHeader): +def clear_quantumleap(url: str, fiware_header: FiwareHeader, session: Session = None): """ Function deletes all data for a given fiware header Args: url: Url of the quantumleap service fiware_header: header of the tenant + session: session object with set ca.crt and ca.key for TLS support Returns: None @@ -97,7 +101,7 @@ def handle_emtpy_db_exception(err: RequestException) -> None: else: raise # create client - client = QuantumLeapClient(url=url, fiware_header=fiware_header) + client = QuantumLeapClient(url=url, fiware_header=fiware_header, session=session) # clear data entities = [] @@ -116,7 +120,8 @@ def clear_all(*, fiware_header: FiwareHeader, cb_url: str = None, iota_url: Union[str, List[str]] = None, - ql_url: str = None): + ql_url: str = None, + session: Session = None): """ Clears all services that a url is provided for @@ -125,6 +130,7 @@ def clear_all(*, cb_url: url of the context broker service iota_url: url of the IoT-Agent service ql_url: url of the QuantumLeap service + session: session object with set ca.crt and ca.key for TLS support Returns: None @@ -133,11 +139,11 @@ def clear_all(*, if isinstance(iota_url, (str, AnyUrl)): iota_url = [iota_url] for url in iota_url: - clear_iot_agent(url=url, fiware_header=fiware_header) + clear_iot_agent(url=url, fiware_header=fiware_header, session=session) if cb_url is not None: - clear_context_broker(url=cb_url, fiware_header=fiware_header) + clear_context_broker(url=cb_url, fiware_header=fiware_header, session=session) if ql_url is not None: - clear_quantumleap(url=ql_url, fiware_header=fiware_header) + clear_quantumleap(url=ql_url, fiware_header=fiware_header, session=session) def clean_test(*, @@ -145,7 +151,8 @@ def clean_test(*, fiware_servicepath: str, cb_url: str = None, iota_url: Union[str, List[str]] = None, - ql_url: str = None) -> Callable: + ql_url: str = None, + session: Session = None) -> Callable: """ Decorator to clean up the server before and after the test @@ -161,6 +168,7 @@ def clean_test(*, cb_url: url of context broker service iota_url: url of IoT-Agent service ql_url: url of quantumleap service + session: session object with set ca.crt and ca.key for TLS support Returns: Decorator for clean tests @@ -170,7 +178,8 @@ def clean_test(*, clear_all(fiware_header=fiware_header, cb_url=cb_url, iota_url=iota_url, - ql_url=ql_url) + ql_url=ql_url, + session=session) # Inner decorator function def decorator(func): # Wrapper function for the decorated function @@ -182,6 +191,7 @@ def wrapper(*args, **kwargs): clear_all(fiware_header=fiware_header, cb_url=cb_url, iota_url=iota_url, - ql_url=ql_url) + ql_url=ql_url, + session=session) return decorator From 0d4f3a60dbb51197f8f7622d82abb0484e608d6d Mon Sep 17 00:00:00 2001 From: mwr-ebc <142895501+mwr-ebc@users.noreply.github.com> Date: Wed, 21 Feb 2024 16:04:59 +0100 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95e7e54f..b3fcbe1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - update pandas version to `~=2.1.4` for `python>=3.9` ([#231](https://github.com/RWTH-EBC/FiLiP/pull/231)) - fix: wrong msg in iotac post device ([#214](https://github.com/RWTH-EBC/FiLiP/pull/214)) - add function to override the existing entity ([#232 ](https://github.com/RWTH-EBC/FiLiP/pull/232 )) +- fix: Session added as optional parameter to enable tls communication with clients ([#249](https://github.com/RWTH-EBC/FiLiP/pull/249)) #### v0.3.0 - fix: bug in typePattern validation @richardmarston ([#180](https://github.com/RWTH-EBC/FiLiP/pull/180)) From f21b8c03a790b8a098b5638ffd72fcef7e4fc391 Mon Sep 17 00:00:00 2001 From: "Marcin.Werzenski" Date: Wed, 3 Apr 2024 16:25:53 +0200 Subject: [PATCH 3/4] fix: Session deleted as optional parameter. clients added instead to enable TLS connections for self-signed certificates. --- filip/utils/cleanup.py | 81 +++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/filip/utils/cleanup.py b/filip/utils/cleanup.py index 92d520ce..d4f798a2 100644 --- a/filip/utils/cleanup.py +++ b/filip/utils/cleanup.py @@ -11,13 +11,13 @@ ContextBrokerClient, \ IoTAClient, \ QuantumLeapClient -from requests import Session -def clear_context_broker(url: str, fiware_header: FiwareHeader, session: Session = None ): +def clear_context_broker(url: str, fiware_header: FiwareHeader, cb_client: ContextBrokerClient = None): """ Function deletes all entities, registrations and subscriptions for a - given fiware header + given fiware header. To use TLS connection you need to provide the cb_client parameter + as an argument with the Session object including the certificate and private key. Note: Always clear the devices first because the IoT-Agent will otherwise @@ -26,13 +26,18 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader, session: Session Args: url: Url of the context broker service fiware_header: header of the tenant - session: session object with set ca.crt and ca.key for TLS support + cb_client: enables TLS communication if created with Session object, only needed for self-signed certificates Returns: None """ + # create client - client = ContextBrokerClient(url=url, fiware_header=fiware_header, session=session) + if cb_client is None: + client = ContextBrokerClient(url=url, fiware_header=fiware_header) + else: + client = cb_client + # clean entities client.delete_entities(entities=client.get_entity_list()) @@ -47,21 +52,26 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader, session: Session assert len(client.get_registration_list()) == 0 -def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, session: Session = None): +def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, iota_client: IoTAClient = None): """ Function deletes all device groups and devices for a - given fiware header + given fiware header. To use TLS connection you need to provide the iota_client parameter + as an argument with the Session object including the certificate and private key. Args: url: Url of the context broker service fiware_header: header of the tenant - session: session object with set ca.crt and ca.key for TLS support + iota_client: enables TLS communication if created with Session object, only needed for self-signed certificates Returns: None """ + # create client - client = IoTAClient(url=url, fiware_header=fiware_header, session=session) + if iota_client is None: + client = IoTAClient(url=url, fiware_header=fiware_header) + else: + client = iota_client # clear registrations for device in client.get_device_list(): @@ -75,13 +85,14 @@ def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, se assert len(client.get_group_list()) == 0 -def clear_quantumleap(url: str, fiware_header: FiwareHeader, session: Session = None): +def clear_quantumleap(url: str, fiware_header: FiwareHeader, ql_client: QuantumLeapClient = None): """ - Function deletes all data for a given fiware header + Function deletes all data for a given fiware header. To use TLS connection you need to provide the ql_client parameter + as an argument with the Session object including the certificate and private key. Args: url: Url of the quantumleap service fiware_header: header of the tenant - session: session object with set ca.crt and ca.key for TLS support + ql_client: enables TLS communication if created with Session object, only needed for self-signed certificates Returns: None @@ -101,7 +112,10 @@ def handle_emtpy_db_exception(err: RequestException) -> None: else: raise # create client - client = QuantumLeapClient(url=url, fiware_header=fiware_header, session=session) + if ql_client is None: + client = QuantumLeapClient(url=url, fiware_header=fiware_header) + else: + client = ql_client # clear data entities = [] @@ -121,7 +135,9 @@ def clear_all(*, cb_url: str = None, iota_url: Union[str, List[str]] = None, ql_url: str = None, - session: Session = None): + cb_client: ContextBrokerClient = None, + iota_client: IoTAClient = None, + ql_client: QuantumLeapClient = None): """ Clears all services that a url is provided for @@ -130,7 +146,9 @@ def clear_all(*, cb_url: url of the context broker service iota_url: url of the IoT-Agent service ql_url: url of the QuantumLeap service - session: session object with set ca.crt and ca.key for TLS support + cb_client: enables TLS communication if created with Session object, only needed for self-signed certificates + iota_client enables TLS communication if created with Session object, only needed for self-signed certificates + ql_client: enables TLS communication if created with Session object, only needed for self-signed certificates Returns: None @@ -139,12 +157,19 @@ def clear_all(*, if isinstance(iota_url, (str, AnyUrl)): iota_url = [iota_url] for url in iota_url: - clear_iot_agent(url=url, fiware_header=fiware_header, session=session) + clear_iot_agent(url=url, fiware_header=fiware_header, iota_client=iota_client) + elif iota_client is not None: + clear_iot_agent(url=iota_url, fiware_header=fiware_header, iota_client=iota_client) + if cb_url is not None: - clear_context_broker(url=cb_url, fiware_header=fiware_header, session=session) - if ql_url is not None: - clear_quantumleap(url=ql_url, fiware_header=fiware_header, session=session) + clear_context_broker(url=cb_url, fiware_header=fiware_header, cb_client=cb_client) + elif cb_client is not None: + clear_context_broker(url=cb_url, fiware_header=fiware_header, cb_client=cb_client) + if ql_url is not None: + clear_quantumleap(url=ql_url, fiware_header=fiware_header, ql_client=ql_client) + elif ql_client is not None: + clear_quantumleap(url=ql_url, fiware_header=fiware_header, ql_client=ql_client) def clean_test(*, fiware_service: str, @@ -152,7 +177,9 @@ def clean_test(*, cb_url: str = None, iota_url: Union[str, List[str]] = None, ql_url: str = None, - session: Session = None) -> Callable: + cb_client: ContextBrokerClient = None, + iota_client: IoTAClient = None, + ql_client: QuantumLeapClient = None) -> Callable: """ Decorator to clean up the server before and after the test @@ -168,7 +195,9 @@ def clean_test(*, cb_url: url of context broker service iota_url: url of IoT-Agent service ql_url: url of quantumleap service - session: session object with set ca.crt and ca.key for TLS support + cb_client: enables TLS communication if created with Session object, only needed for self-signed certificates + iota_client: enables TLS communication if created with Session object, only needed for self-signed certificates + ql_client: enables TLS communication if created with Session object, only needed for self-signed certificates Returns: Decorator for clean tests @@ -179,7 +208,9 @@ def clean_test(*, cb_url=cb_url, iota_url=iota_url, ql_url=ql_url, - session=session) + cb_client=cb_client, + iota_client=iota_client, + ql_client=ql_client) # Inner decorator function def decorator(func): # Wrapper function for the decorated function @@ -192,6 +223,8 @@ def wrapper(*args, **kwargs): cb_url=cb_url, iota_url=iota_url, ql_url=ql_url, - session=session) + cb_client=cb_client, + iota_client=iota_client, + ql_client=ql_client) - return decorator + return decorator \ No newline at end of file From 777b717dadff1caae8c18f78ec4c94f772c476b5 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Tue, 16 Apr 2024 18:08:44 +0200 Subject: [PATCH 4/4] chore: make url as not required --- filip/utils/cleanup.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/filip/utils/cleanup.py b/filip/utils/cleanup.py index d4f798a2..bda995cc 100644 --- a/filip/utils/cleanup.py +++ b/filip/utils/cleanup.py @@ -13,7 +13,9 @@ QuantumLeapClient -def clear_context_broker(url: str, fiware_header: FiwareHeader, cb_client: ContextBrokerClient = None): +def clear_context_broker(url: str = None, + fiware_header: FiwareHeader = None, + cb_client: ContextBrokerClient = None): """ Function deletes all entities, registrations and subscriptions for a given fiware header. To use TLS connection you need to provide the cb_client parameter @@ -31,7 +33,7 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader, cb_client: Conte Returns: None """ - + assert url or cb_client, "Either url or client object must be given" # create client if cb_client is None: client = ContextBrokerClient(url=url, fiware_header=fiware_header) @@ -52,7 +54,9 @@ def clear_context_broker(url: str, fiware_header: FiwareHeader, cb_client: Conte assert len(client.get_registration_list()) == 0 -def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, iota_client: IoTAClient = None): +def clear_iot_agent(url: Union[str, AnyHttpUrl] = None, + fiware_header: FiwareHeader = None, + iota_client: IoTAClient = None): """ Function deletes all device groups and devices for a given fiware header. To use TLS connection you need to provide the iota_client parameter @@ -66,7 +70,7 @@ def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, io Returns: None """ - + assert url or iota_client, "Either url or client object must be given" # create client if iota_client is None: client = IoTAClient(url=url, fiware_header=fiware_header) @@ -85,7 +89,9 @@ def clear_iot_agent(url: Union[str, AnyHttpUrl], fiware_header: FiwareHeader, io assert len(client.get_group_list()) == 0 -def clear_quantumleap(url: str, fiware_header: FiwareHeader, ql_client: QuantumLeapClient = None): +def clear_quantumleap(url: str = None, + fiware_header: FiwareHeader = None, + ql_client: QuantumLeapClient = None): """ Function deletes all data for a given fiware header. To use TLS connection you need to provide the ql_client parameter as an argument with the Session object including the certificate and private key. @@ -111,6 +117,7 @@ def handle_emtpy_db_exception(err: RequestException) -> None: pass else: raise + assert url or ql_client, "Either url or client object must be given" # create client if ql_client is None: client = QuantumLeapClient(url=url, fiware_header=fiware_header) @@ -131,7 +138,7 @@ def handle_emtpy_db_exception(err: RequestException) -> None: def clear_all(*, - fiware_header: FiwareHeader, + fiware_header: FiwareHeader = None, cb_url: str = None, iota_url: Union[str, List[str]] = None, ql_url: str = None, @@ -146,31 +153,29 @@ def clear_all(*, cb_url: url of the context broker service iota_url: url of the IoT-Agent service ql_url: url of the QuantumLeap service - cb_client: enables TLS communication if created with Session object, only needed for self-signed certificates - iota_client enables TLS communication if created with Session object, only needed for self-signed certificates - ql_client: enables TLS communication if created with Session object, only needed for self-signed certificates + cb_client: enables TLS communication if created with Session object, only needed + for self-signed certificates + iota_client: enables TLS communication if created with Session object, only needed + for self-signed certificates + ql_client: enables TLS communication if created with Session object, only needed + for self-signed certificates Returns: None """ - if iota_url is not None: + if iota_url is not None or iota_client is not None: if isinstance(iota_url, (str, AnyUrl)): iota_url = [iota_url] for url in iota_url: clear_iot_agent(url=url, fiware_header=fiware_header, iota_client=iota_client) - elif iota_client is not None: - clear_iot_agent(url=iota_url, fiware_header=fiware_header, iota_client=iota_client) - if cb_url is not None: - clear_context_broker(url=cb_url, fiware_header=fiware_header, cb_client=cb_client) - elif cb_client is not None: + if cb_url is not None or cb_client is not None: clear_context_broker(url=cb_url, fiware_header=fiware_header, cb_client=cb_client) - if ql_url is not None: - clear_quantumleap(url=ql_url, fiware_header=fiware_header, ql_client=ql_client) - elif ql_client is not None: + if ql_url is not None or ql_client is not None: clear_quantumleap(url=ql_url, fiware_header=fiware_header, ql_client=ql_client) + def clean_test(*, fiware_service: str, fiware_servicepath: str,