|
27 | 27 | """ |
28 | 28 |
|
29 | 29 | import argparse |
| 30 | +import os |
30 | 31 | from azure.identity import AzureCliCredential |
31 | 32 | from azure.mgmt.eventhub import EventHubManagementClient |
32 | 33 | from fabric_api import FabricApiClient, FabricApiError |
@@ -140,7 +141,7 @@ def get_event_hub_namespace_primary_key(namespace_name: str, subscription_id: st |
140 | 141 | return result |
141 | 142 |
|
142 | 143 | except Exception as e: |
143 | | - print(f"❌ Error: {e}") |
| 144 | + print(f"❌ Failed to retrieve namespace access keys for '{namespace_name}': {e}") |
144 | 145 | raise |
145 | 146 |
|
146 | 147 |
|
@@ -178,16 +179,39 @@ def setup_eventhub_connection( |
178 | 179 | Exception: If connection creation/update fails |
179 | 180 | """ |
180 | 181 | try: |
181 | | - # Retrieve access key automatically |
182 | | - print(f"🔑 Retrieving access key for Event Hub: {event_hub_name}") |
183 | | - key_info = get_event_hub_namespace_primary_key( |
184 | | - namespace_name=namespace_name, |
185 | | - subscription_id=subscription_id, |
186 | | - resource_group_name=resource_group_name, |
187 | | - authorization_rule_name=authorization_rule_name |
188 | | - ) |
189 | | - access_key = key_info['primary_key'] |
190 | | - print(f"✅ Successfully retrieved access key") |
| 182 | + # Allow an explicit key override via environment variable (useful in CI when listKeys RBAC is unavailable) |
| 183 | + access_key = os.getenv("AZURE_EVENT_HUB_SHARED_ACCESS_KEY") |
| 184 | + if access_key and access_key.strip(): |
| 185 | + print("🔑 Using AZURE_EVENT_HUB_SHARED_ACCESS_KEY from environment (override).") |
| 186 | + access_key = access_key.strip() |
| 187 | + else: |
| 188 | + # Retrieve access key from Azure management API |
| 189 | + print(f"🔑 Retrieving access key for Event Hub namespace: {namespace_name}") |
| 190 | + try: |
| 191 | + key_info = get_event_hub_namespace_primary_key( |
| 192 | + namespace_name=namespace_name, |
| 193 | + subscription_id=subscription_id, |
| 194 | + resource_group_name=resource_group_name, |
| 195 | + authorization_rule_name=authorization_rule_name |
| 196 | + ) |
| 197 | + access_key = (key_info or {}).get("primary_key") |
| 198 | + except Exception as key_error: |
| 199 | + raise RuntimeError( |
| 200 | + f"Failed to retrieve Event Hub shared access key for namespace '{namespace_name}': {key_error}. " |
| 201 | + "Either set the AZURE_EVENT_HUB_SHARED_ACCESS_KEY environment variable, or grant the deployment " |
| 202 | + "identity permission to list Event Hub namespace keys " |
| 203 | + "(Microsoft.EventHub/namespaces/authorizationRules/listKeys/action)." |
| 204 | + ) from key_error |
| 205 | + |
| 206 | + if not access_key or not access_key.strip(): |
| 207 | + raise RuntimeError( |
| 208 | + "Event Hub shared access key is missing or empty. " |
| 209 | + "Either set the AZURE_EVENT_HUB_SHARED_ACCESS_KEY environment variable, or grant the deployment " |
| 210 | + "identity permission to list Event Hub namespace keys " |
| 211 | + "(Microsoft.EventHub/namespaces/authorizationRules/listKeys/action)." |
| 212 | + ) |
| 213 | + |
| 214 | + print("✅ Successfully obtained Event Hub shared access key.") |
191 | 215 |
|
192 | 216 | # Use the passed fabric_client instead of creating a new one |
193 | 217 | client = fabric_client |
|
0 commit comments