From adea0f5af7d943f30609093086f8d2ea146a56d0 Mon Sep 17 00:00:00 2001 From: Ro'e Katz Date: Wed, 14 Feb 2024 16:41:16 +0200 Subject: [PATCH] Fix pre-commit formatting checks --- packages/opal-client/opal_client/policy/updater.py | 8 ++++---- .../opal-client/opal_client/policy_store/opa_client.py | 2 -- .../opal-client/opal_client/tests/opa_client_test.py | 1 - .../tests/server_to_client_intergation_test.py | 1 - packages/opal-common/opal_common/cli/docs.py | 1 - packages/opal-common/opal_common/confi/confi.py | 1 - packages/opal-common/opal_common/emport.py | 5 ++--- .../opal_common/fetcher/tests/failure_handler_test.py | 1 + .../opal_common/fetcher/tests/rpc_fetch_test.py | 1 + packages/opal-common/opal_common/logging/formatter.py | 1 - packages/opal-common/opal_common/monitoring/apm.py | 4 ++-- packages/opal-server/opal_server/git_fetcher.py | 10 ++++------ packages/opal-server/opal_server/policy/webhook/api.py | 1 - packages/opal-server/opal_server/scopes/api.py | 2 +- packages/opal-server/opal_server/server.py | 8 ++++---- 15 files changed, 19 insertions(+), 28 deletions(-) diff --git a/packages/opal-client/opal_client/policy/updater.py b/packages/opal-client/opal_client/policy/updater.py index 10e0dbd9..57d93099 100644 --- a/packages/opal-client/opal_client/policy/updater.py +++ b/packages/opal-client/opal_client/policy/updater.py @@ -288,10 +288,10 @@ async def update_policy( bundle = None bundle_succeeded = True try: - bundle: Optional[PolicyBundle] = ( - await self._policy_fetcher.fetch_policy_bundle( - directories, base_hash=base_hash - ) + bundle: Optional[ + PolicyBundle + ] = await self._policy_fetcher.fetch_policy_bundle( + directories, base_hash=base_hash ) if bundle: if bundle.old_hash is None: diff --git a/packages/opal-client/opal_client/policy_store/opa_client.py b/packages/opal-client/opal_client/policy_store/opa_client.py index 8fd20f7d..1d8ec021 100644 --- a/packages/opal-client/opal_client/policy_store/opa_client.py +++ b/packages/opal-client/opal_client/policy_store/opa_client.py @@ -382,7 +382,6 @@ def __init__( self._policy_data_cache = OpaStaticDataCache() def _get_custom_ssl_context(self) -> Optional[ssl.SSLContext]: - if not self._tls_ca: return None @@ -629,7 +628,6 @@ async def _attempt_operations_with_postponed_failure_retry( return if len(failed_ops) == len(ops): - # all ops failed on this iteration, no point at retrying for failure_msg in failure_msgs: logger.error(failure_msg) diff --git a/packages/opal-client/opal_client/tests/opa_client_test.py b/packages/opal-client/opal_client/tests/opa_client_test.py index c6e1d1f1..819f4f1d 100644 --- a/packages/opal-client/opal_client/tests/opa_client_test.py +++ b/packages/opal-client/opal_client/tests/opa_client_test.py @@ -43,7 +43,6 @@ def test_constuctor_should_panic_tls_configured_without_all_parts(): def test_constructor_should_set_up_ca_certificate_even_without_tls_auth_type(tmpdir): - ca_path = os.path.join(tmpdir, "ca.pem") with open(ca_path, "w") as ca: ca.write(TEST_CA_CERT) diff --git a/packages/opal-client/opal_client/tests/server_to_client_intergation_test.py b/packages/opal-client/opal_client/tests/server_to_client_intergation_test.py index 38f25859..76249af0 100644 --- a/packages/opal-client/opal_client/tests/server_to_client_intergation_test.py +++ b/packages/opal-client/opal_client/tests/server_to_client_intergation_test.py @@ -73,7 +73,6 @@ async def startup_event(): def setup_client(event): - # config to use mock OPA policy_store = PolicyStoreClientFactory.create(store_type=PolicyStoreTypes.MOCK) data_updater = DataUpdater( diff --git a/packages/opal-common/opal_common/cli/docs.py b/packages/opal-common/opal_common/cli/docs.py index f568e92b..28eb4d10 100644 --- a/packages/opal-common/opal_common/cli/docs.py +++ b/packages/opal-common/opal_common/cli/docs.py @@ -1,6 +1,5 @@ class MainTexts: def __init__(self, first_line, name): - self.header = f"""\b {first_line} Open-Policy Administration Layer - {name}\b\f""" diff --git a/packages/opal-common/opal_common/confi/confi.py b/packages/opal-common/opal_common/confi/confi.py index 4e2587a6..f391c26a 100644 --- a/packages/opal-common/opal_common/confi/confi.py +++ b/packages/opal-common/opal_common/confi/confi.py @@ -252,7 +252,6 @@ def get_cli_object( help: str = None, on_start: Callable = None, ): - if config_objects is None: config_objects = [] config_objects.append(self) diff --git a/packages/opal-common/opal_common/emport.py b/packages/opal-common/opal_common/emport.py index b9ec0640..6d2888da 100644 --- a/packages/opal-common/opal_common/emport.py +++ b/packages/opal-common/opal_common/emport.py @@ -76,7 +76,6 @@ def get_flat_list(self): """ res = [] for member in self._members: - # if a member is an Emport itself flatten it as well if isinstance(member, Emport): res += member.get_flat_list() @@ -138,7 +137,7 @@ def emport_by_class(from_path, cls, import_items=None): import_items = import_items or ["*"] module_obj = __import__(from_path, globals(), locals(), import_items, 0) clean_items = ObjectUtils.get_class_members_who_derive_of(module_obj, cls) - for (sub_name, sub_module) in ObjectUtils.get_members_who_are_instance_of( + for sub_name, sub_module in ObjectUtils.get_members_who_are_instance_of( module_obj, module_obj.__class__ ): results = ObjectUtils.get_class_members_who_derive_of(sub_module, cls) @@ -166,7 +165,7 @@ def emport_objects_by_class(from_path, cls, import_items=None): clean_items = ObjectUtils.get_class_members_who_derive_of(module_obj, cls) results.extend(clean_items) # nested - for (sub_name, sub_module) in ObjectUtils.get_members_who_are_instance_of( + for sub_name, sub_module in ObjectUtils.get_members_who_are_instance_of( module_obj, module_obj.__class__ ): objects = ObjectUtils.get_class_members_who_derive_of(sub_module, cls) diff --git a/packages/opal-common/opal_common/fetcher/tests/failure_handler_test.py b/packages/opal-common/opal_common/fetcher/tests/failure_handler_test.py index 9d582b4a..18024370 100644 --- a/packages/opal-common/opal_common/fetcher/tests/failure_handler_test.py +++ b/packages/opal-common/opal_common/fetcher/tests/failure_handler_test.py @@ -46,6 +46,7 @@ async def error_callback(error: Exception, event: FetchEvent): # register the callback engine.register_failure_handler(error_callback) + # callback for success - shouldn't eb called in this test async def callback(result): got_data_event.set() diff --git a/packages/opal-common/opal_common/fetcher/tests/rpc_fetch_test.py b/packages/opal-common/opal_common/fetcher/tests/rpc_fetch_test.py index 12a7a4e5..e974f3a3 100644 --- a/packages/opal-common/opal_common/fetcher/tests/rpc_fetch_test.py +++ b/packages/opal-common/opal_common/fetcher/tests/rpc_fetch_test.py @@ -67,6 +67,7 @@ async def test_simple_rpc_fetch(server): rpc_method_name="get_data", rpc_arguments={"suffix": SUFFIX} ), ) + # Callback for event async def callback(result): data = result.result diff --git a/packages/opal-common/opal_common/logging/formatter.py b/packages/opal-common/opal_common/logging/formatter.py index 7ad7b75d..1a4c3f81 100644 --- a/packages/opal-common/opal_common/logging/formatter.py +++ b/packages/opal-common/opal_common/logging/formatter.py @@ -1,5 +1,4 @@ class Formatter: - MAX_FIELD_LEN = 25 def __init__(self, format_string: str): diff --git a/packages/opal-common/opal_common/monitoring/apm.py b/packages/opal-common/opal_common/monitoring/apm.py index c8deb6dd..5860f90c 100644 --- a/packages/opal-common/opal_common/monitoring/apm.py +++ b/packages/opal-common/opal_common/monitoring/apm.py @@ -1,8 +1,8 @@ +import os from typing import Optional from urllib.parse import urlparse -import os -from ddtrace import Span, patch, tracer, config +from ddtrace import Span, config, patch, tracer from ddtrace.filters import TraceFilter from loguru import logger diff --git a/packages/opal-server/opal_server/git_fetcher.py b/packages/opal-server/opal_server/git_fetcher.py index 034cdaf2..0887c916 100644 --- a/packages/opal-server/opal_server/git_fetcher.py +++ b/packages/opal-server/opal_server/git_fetcher.py @@ -109,9 +109,7 @@ def verify_found_repo_matches_remote( f"found target repo url is referred by remote: {remote.name}, url={remote.url}" ) return - error: str = ( - f"Repo mismatch! No remote matches target url: {expected_remote_url}, found urls: {[remote.url for remote in repo.remotes]}" - ) + error: str = f"Repo mismatch! No remote matches target url: {expected_remote_url}, found urls: {[remote.url for remote in repo.remotes]}" logger.error(error) raise ValueError(error) @@ -196,9 +194,9 @@ async def fetch_and_notify_on_changes( logger.debug( f"Fetching remote (force_fetch={force_fetch}): {self._remote} ({self._source.url})" ) - GitPolicyFetcher.repos_last_fetched[self.source_id] = ( - datetime.datetime.now() - ) + GitPolicyFetcher.repos_last_fetched[ + self.source_id + ] = datetime.datetime.now() await run_sync( repo.remotes[self._remote].fetch, callbacks=self._auth_callbacks, diff --git a/packages/opal-server/opal_server/policy/webhook/api.py b/packages/opal-server/opal_server/policy/webhook/api.py index 2ef04064..c19595ad 100644 --- a/packages/opal-server/opal_server/policy/webhook/api.py +++ b/packages/opal-server/opal_server/policy/webhook/api.py @@ -71,7 +71,6 @@ def get_webhook_router( dependencies=route_dependencies, ) async def trigger_webhook(request: Request, git_changes: GitChanges = git_changes): - # TODO: breaking change: change "repo_url" to "remote_url" in next major if source_type == PolicySourceTypes.Git: # look at values extracted from request diff --git a/packages/opal-server/opal_server/scopes/api.py b/packages/opal-server/opal_server/scopes/api.py index 72d5f7c5..95181866 100644 --- a/packages/opal-server/opal_server/scopes/api.py +++ b/packages/opal-server/opal_server/scopes/api.py @@ -15,7 +15,6 @@ from fastapi.responses import RedirectResponse from fastapi_websocket_pubsub import PubSubEndpoint from git import InvalidGitRepositoryError -from opal_common.monitoring import metrics from opal_common.async_utils import run_sync from opal_common.authentication.authz import ( require_peer_type, @@ -26,6 +25,7 @@ from opal_common.authentication.types import EncryptionKeyFormat, JWTClaims from opal_common.authentication.verifier import Unauthorized from opal_common.logger import logger +from opal_common.monitoring import metrics from opal_common.schemas.data import ( DataSourceConfig, DataUpdate, diff --git a/packages/opal-server/opal_server/server.py b/packages/opal-server/opal_server/server.py index 86b74c9c..13b55860 100644 --- a/packages/opal-server/opal_server/server.py +++ b/packages/opal-server/opal_server/server.py @@ -8,13 +8,13 @@ from fastapi import Depends, FastAPI from fastapi_websocket_pubsub.event_broadcaster import EventBroadcasterContextManager -from opal_common.monitoring import apm, metrics from opal_common.authentication.deps import JWTAuthenticator, StaticBearerAuthenticator from opal_common.authentication.signer import JWTSigner from opal_common.confi.confi import load_conf_if_none from opal_common.config import opal_common_config from opal_common.logger import configure_logs, logger from opal_common.middleware import configure_middleware +from opal_common.monitoring import apm, metrics from opal_common.schemas.data import ServerDataSourceConfig from opal_common.synchronization.named_lock import NamedLock from opal_common.topics.publisher import ( @@ -168,9 +168,9 @@ def __init__( # if stats are enabled, the server workers must be listening on the broadcast # channel for their own synchronization, not just for their clients. therefore # we need a "global" listening context - self.broadcast_listening_context: Optional[EventBroadcasterContextManager] = ( - None - ) + self.broadcast_listening_context: Optional[ + EventBroadcasterContextManager + ] = None if self.broadcaster_uri is not None and opal_common_config.STATISTICS_ENABLED: self.broadcast_listening_context = ( self.pubsub.endpoint.broadcaster.get_listening_context()