From bfd933ab2a3b23b37c9a7a9a6f3d0a3f6541b61a Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 27 Sep 2024 20:02:03 +0300 Subject: [PATCH 1/3] Update tags. --- moonstreamapi/moonstreamapi/actions.py | 6 ++--- .../moonstreamapi/routes/subscriptions.py | 24 +++++++++++++++---- moonstreamapi/moonstreamapi/settings.py | 2 ++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/moonstreamapi/moonstreamapi/actions.py b/moonstreamapi/moonstreamapi/actions.py index f4d35cb6..f123a90c 100644 --- a/moonstreamapi/moonstreamapi/actions.py +++ b/moonstreamapi/moonstreamapi/actions.py @@ -1184,7 +1184,7 @@ def create_resource_for_user( def chekc_user_resource_access( customer_id: uuid.UUID, user_token: uuid.UUID, -) -> bool: +) -> Optional[BugoutResource]: """ Check if user has access to customer_id """ @@ -1198,10 +1198,10 @@ def chekc_user_resource_access( except BugoutResponseException as e: if e.status_code == 404: - return False + return None raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail) except Exception as e: logger.error(f"Error get customer: {str(e)}") raise MoonstreamHTTPException(status_code=500, internal_error=e) - return str(response.id) == customer_id + return response diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index d2f76aa0..a0305994 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -109,12 +109,14 @@ async def add_subscription_handler( user_token=token, ) - if not results: + if results is None: raise MoonstreamHTTPException( status_code=403, detail="User has no access to this customer", ) + customer_instance_name = results.resource_data["name"] + active_subscription_types_response = subscription_types.list_subscription_types( active_only=True ) @@ -172,6 +174,18 @@ async def add_subscription_handler( {"user_id": f"{user.id}"}, ] + if customer_id is not None and customer_instance_name is not None: + required_fields.extend( + [ + { + "customer_id": f"{customer_id}", + }, + { + "instance_name": f"{customer_instance_name}", + }, + ] + ) + if allowed_required_fields: required_fields.extend(allowed_required_fields) @@ -218,7 +232,7 @@ async def add_subscription_handler( f"{key}:{value}" for tag in entity_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" ] if entity_secondary_fields.get("abi") and customer_id is not None: @@ -402,7 +416,7 @@ async def get_subscriptions_handler( f"{key}:{value}" for tag in tags for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" ] subscriptions.append( @@ -465,7 +479,7 @@ async def update_subscriptions_handler( user_token=token, ) - if not results: + if results is None: raise MoonstreamHTTPException( status_code=403, detail="User has no access to this customer", @@ -624,7 +638,7 @@ async def update_subscriptions_handler( f"{key}:{value}" for tag in subscription_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS and key != "instance_name" ] return data.SubscriptionResourceData( diff --git a/moonstreamapi/moonstreamapi/settings.py b/moonstreamapi/moonstreamapi/settings.py index 4e387199..e8418ff4 100644 --- a/moonstreamapi/moonstreamapi/settings.py +++ b/moonstreamapi/moonstreamapi/settings.py @@ -309,6 +309,8 @@ "user_id", "address", "blockchain", + "customer_id", + "instance_name", ] ## Moonstream resources types From f6e7f194e1dd7449d72155a4d4f117e906c8d30f Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 2 Oct 2024 17:09:57 +0300 Subject: [PATCH 2/3] Add changes. --- moonstreamapi/moonstreamapi/actions.py | 2 +- .../moonstreamapi/routes/subscriptions.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/moonstreamapi/moonstreamapi/actions.py b/moonstreamapi/moonstreamapi/actions.py index f123a90c..f75715e0 100644 --- a/moonstreamapi/moonstreamapi/actions.py +++ b/moonstreamapi/moonstreamapi/actions.py @@ -1181,7 +1181,7 @@ def create_resource_for_user( return resource -def chekc_user_resource_access( +def check_user_resource_access( customer_id: uuid.UUID, user_token: uuid.UUID, ) -> Optional[BugoutResource]: diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index a0305994..3fedfcc5 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -20,7 +20,7 @@ EntityJournalNotFoundException, apply_moonworm_tasks, check_if_smart_contract, - chekc_user_resource_access, + check_user_resource_access, get_entity_subscription_journal_id, get_list_of_support_interfaces, get_moonworm_tasks, @@ -104,15 +104,15 @@ async def add_subscription_handler( if customer_id is not None: - results = chekc_user_resource_access( + results = check_user_resource_access( customer_id=customer_id, user_token=token, ) if results is None: raise MoonstreamHTTPException( - status_code=403, - detail="User has no access to this customer", + status_code=404, + detail="Not found customer", ) customer_instance_name = results.resource_data["name"] @@ -474,15 +474,15 @@ async def update_subscriptions_handler( if customer_id is not None: - results = chekc_user_resource_access( + results = check_user_resource_access( customer_id=customer_id, user_token=token, ) if results is None: raise MoonstreamHTTPException( - status_code=403, - detail="User has no access to this customer", + status_code=404, + detail="Not found customer", ) try: @@ -638,7 +638,7 @@ async def update_subscriptions_handler( f"{key}:{value}" for tag in subscription_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS and key != "instance_name" + if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" ] return data.SubscriptionResourceData( From 1fcd69dc9828c477ada88918bb18b16123c301e4 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 2 Oct 2024 17:41:46 +0300 Subject: [PATCH 3/3] Refactor filter. --- .../moonstreamapi/routes/subscriptions.py | 31 ++++++++++++------- moonstreamapi/moonstreamapi/settings.py | 24 +++++++------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index 3fedfcc5..10c7925a 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -158,12 +158,12 @@ async def add_subscription_handler( if description: content["description"] = description + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS + allowed_required_fields: List[Any] = [] if tags: allowed_required_fields = [ - item - for item in tags - if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + item for item in tags if not any(key in item for key in excluded_keys) ] required_fields: List[Dict[str, Union[str, bool, int, List[Any]]]] = [ @@ -228,11 +228,15 @@ async def add_subscription_handler( entity_secondary_fields = ( entity.secondary_fields if entity.secondary_fields is not None else {} ) + + # We remove the instance_name for return that tag to the frontend + excluded_keys = excluded_keys - {"instance_name"} + normalized_entity_tags = [ f"{key}:{value}" for tag in entity_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" + if key not in excluded_keys ] if entity_secondary_fields.get("abi") and customer_id is not None: @@ -324,7 +328,7 @@ async def delete_subscription_handler( f"{key}:{value}" for tag in tags_raw for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS + if key not in (MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"}) ] if deleted_entity.secondary_fields is not None: @@ -397,6 +401,9 @@ async def get_subscriptions_handler( List[BugoutSearchResultAsEntity], subscriptions_list.results ) + # We remove the instance_name for return that tag to the frontend + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"} + for subscription in user_subscriptions_results: tags = subscription.required_fields @@ -416,7 +423,7 @@ async def get_subscriptions_handler( f"{key}:{value}" for tag in tags for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" + if key not in excluded_keys ] subscriptions.append( @@ -485,6 +492,8 @@ async def update_subscriptions_handler( detail="Not found customer", ) + excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS + try: journal_id = get_entity_subscription_journal_id( resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION, @@ -504,7 +513,7 @@ async def update_subscriptions_handler( update_required_fields = [ field for field in subscription_entity.required_fields - if any(key in field for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + if any(key in field for key in excluded_keys) ] update_secondary_fields = ( @@ -570,9 +579,7 @@ async def update_subscriptions_handler( if tags: allowed_required_fields = [ - item - for item in tags - if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) + item for item in tags if not any(key in item for key in excluded_keys) ] if allowed_required_fields: @@ -633,12 +640,14 @@ async def update_subscriptions_handler( if subscription.secondary_fields is not None else {} ) + # We remove the instance_name for return that tag to the frontend + excluded_keys = excluded_keys - {"instance_name"} normalized_entity_tags = [ f"{key}:{value}" for tag in subscription_required_fields for key, value in tag.items() - if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS or key == "instance_name" + if key not in excluded_keys ] return data.SubscriptionResourceData( diff --git a/moonstreamapi/moonstreamapi/settings.py b/moonstreamapi/moonstreamapi/settings.py index e8418ff4..f7848dce 100644 --- a/moonstreamapi/moonstreamapi/settings.py +++ b/moonstreamapi/moonstreamapi/settings.py @@ -301,17 +301,19 @@ ) # Entities reserved tags -MOONSTREAM_ENTITIES_RESERVED_TAGS = [ - "type", - "subscription_type_id", - "color", - "label", - "user_id", - "address", - "blockchain", - "customer_id", - "instance_name", -] +MOONSTREAM_ENTITIES_RESERVED_TAGS = set( + [ + "type", + "subscription_type_id", + "color", + "label", + "user_id", + "address", + "blockchain", + "customer_id", + "instance_name", + ] +) ## Moonstream resources types