Skip to content

Commit a6fc301

Browse files
Fix: add env var override and key validation in setup_eventhub_connection
Agent-Logs-Url: https://github.com/microsoft/real-time-intelligence-operations-solution-accelerator/sessions/8e4a1c2b-b566-4dd9-914d-8fa732f98757 Co-authored-by: Yatish-Microsoft <234036280+Yatish-Microsoft@users.noreply.github.com>
1 parent f70035a commit a6fc301

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

infra/scripts/fabric/fabric_eventhub.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828

2929
import argparse
30+
import os
3031
from azure.identity import AzureCliCredential
3132
from azure.mgmt.eventhub import EventHubManagementClient
3233
from fabric_api import FabricApiClient, FabricApiError
@@ -140,7 +141,7 @@ def get_event_hub_namespace_primary_key(namespace_name: str, subscription_id: st
140141
return result
141142

142143
except Exception as e:
143-
print(f"❌ Error: {e}")
144+
print(f"❌ Failed to retrieve namespace access keys for '{namespace_name}': {e}")
144145
raise
145146

146147

@@ -178,16 +179,39 @@ def setup_eventhub_connection(
178179
Exception: If connection creation/update fails
179180
"""
180181
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.")
191215

192216
# Use the passed fabric_client instead of creating a new one
193217
client = fabric_client

0 commit comments

Comments
 (0)