|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from pydantic.v1 import BaseModel |
| 4 | + |
| 5 | +from prowler.lib.logger import logger |
| 6 | +from prowler.providers.huaweicloud.lib.service.service import HuaweiCloudService |
| 7 | + |
| 8 | +SMN_PAGE_SIZE = 100 |
| 9 | + |
| 10 | + |
| 11 | +class SMN(HuaweiCloudService): |
| 12 | + """ |
| 13 | + SMN (Simple Message Notification) service class for Huawei Cloud. |
| 14 | +
|
| 15 | + This class provides methods to interact with Huawei Cloud SMN service |
| 16 | + to retrieve notification topics and their subscription counts. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, provider): |
| 20 | + super().__init__(__class__.__name__, provider) |
| 21 | + |
| 22 | + self.topics: List[SMNTopic] = [] |
| 23 | + |
| 24 | + if getattr(self.session, "is_mock", False): |
| 25 | + self._load_mock_data() |
| 26 | + return |
| 27 | + |
| 28 | + self._list_topics() |
| 29 | + |
| 30 | + def _load_mock_data(self): |
| 31 | + """Load mock data for testing.""" |
| 32 | + region = "la-south-2" |
| 33 | + self.topics = [ |
| 34 | + SMNTopic( |
| 35 | + topic_urn="urn:smn:la-south-2:123456789012:alert-topic", |
| 36 | + topic_id="topic-001", |
| 37 | + name="alert-topic", |
| 38 | + display_name="Alert Topic", |
| 39 | + push_policy=0, |
| 40 | + confirmed_subscription_count=2, |
| 41 | + region=region, |
| 42 | + ), |
| 43 | + SMNTopic( |
| 44 | + topic_urn="urn:smn:la-south-2:123456789012:empty-topic", |
| 45 | + topic_id="topic-002", |
| 46 | + name="empty-topic", |
| 47 | + display_name="Empty Topic", |
| 48 | + push_policy=0, |
| 49 | + confirmed_subscription_count=0, |
| 50 | + region=region, |
| 51 | + ), |
| 52 | + ] |
| 53 | + |
| 54 | + def _list_topics(self): |
| 55 | + """List all SMN topics across regions and get their subscription counts.""" |
| 56 | + if not self.regional_clients: |
| 57 | + return |
| 58 | + |
| 59 | + for region, client in self.regional_clients.items(): |
| 60 | + logger.info(f"SMN - Listing Topics in {region}...") |
| 61 | + |
| 62 | + try: |
| 63 | + from huaweicloudsdksmn.v2 import ( |
| 64 | + ListSubscriptionsByTopicRequest, |
| 65 | + ListTopicsRequest, |
| 66 | + ) |
| 67 | + |
| 68 | + offset = 0 |
| 69 | + while True: |
| 70 | + request = ListTopicsRequest(offset=offset, limit=SMN_PAGE_SIZE) |
| 71 | + response = self._call_with_retries(client.list_topics, request) |
| 72 | + topics = getattr(response, "topics", None) or [] |
| 73 | + |
| 74 | + for topic in topics: |
| 75 | + topic_urn = getattr(topic, "topic_urn", "") or "" |
| 76 | + topic_id = getattr(topic, "topic_id", "") or "" |
| 77 | + name = getattr(topic, "name", "") or "" |
| 78 | + display_name = getattr(topic, "display_name", "") or "" |
| 79 | + push_policy = getattr(topic, "push_policy", None) |
| 80 | + |
| 81 | + try: |
| 82 | + confirmed_subscription_count = 0 |
| 83 | + subscription_offset = 0 |
| 84 | + while True: |
| 85 | + sub_request = ListSubscriptionsByTopicRequest( |
| 86 | + topic_urn=topic_urn, |
| 87 | + offset=subscription_offset, |
| 88 | + limit=SMN_PAGE_SIZE, |
| 89 | + ) |
| 90 | + sub_response = self._call_with_retries( |
| 91 | + client.list_subscriptions_by_topic, sub_request |
| 92 | + ) |
| 93 | + subscriptions = ( |
| 94 | + getattr(sub_response, "subscriptions", None) or [] |
| 95 | + ) |
| 96 | + confirmed_subscription_count += sum( |
| 97 | + getattr(subscription, "status", None) == 1 |
| 98 | + for subscription in subscriptions |
| 99 | + ) |
| 100 | + subscription_count = ( |
| 101 | + getattr(sub_response, "subscription_count", 0) or 0 |
| 102 | + ) |
| 103 | + subscription_offset += SMN_PAGE_SIZE |
| 104 | + if subscription_offset >= subscription_count: |
| 105 | + break |
| 106 | + except Exception as sub_error: |
| 107 | + logger.error( |
| 108 | + f"{region} -- {sub_error.__class__.__name__}" |
| 109 | + f"[{sub_error.__traceback__.tb_lineno}]: {sub_error}" |
| 110 | + ) |
| 111 | + continue |
| 112 | + |
| 113 | + self.topics.append( |
| 114 | + SMNTopic( |
| 115 | + topic_urn=topic_urn, |
| 116 | + topic_id=topic_id, |
| 117 | + name=name, |
| 118 | + display_name=display_name, |
| 119 | + push_policy=push_policy, |
| 120 | + confirmed_subscription_count=confirmed_subscription_count, |
| 121 | + region=region, |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + offset += SMN_PAGE_SIZE |
| 126 | + topic_count = getattr(response, "topic_count", 0) or 0 |
| 127 | + if offset >= topic_count: |
| 128 | + break |
| 129 | + |
| 130 | + except Exception as error: |
| 131 | + logger.error( |
| 132 | + f"{region} -- {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +class SMNTopic(BaseModel): |
| 137 | + """SMN topic model.""" |
| 138 | + |
| 139 | + topic_urn: str |
| 140 | + topic_id: str = "" |
| 141 | + name: str = "" |
| 142 | + display_name: str = "" |
| 143 | + push_policy: int = None |
| 144 | + confirmed_subscription_count: int = 0 |
| 145 | + region: str = "" |
0 commit comments