Skip to content

Commit 188f0cd

Browse files
authored
Merge branch 'main' into jr/upstream-main/142-certs-kv-error
2 parents c29b184 + 71fc2c0 commit 188f0cd

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ENHANCEMENTS:
1818
BUG FIXES:
1919
* Fix upgrade when porter install has failed ([#4338](https://github.com/microsoft/AzureTRE/pull/4338))
2020
* Certs shared service: Secret nexus-ssl-password is currently in a deleted but recoverable state ([#4294](https://github.com/microsoft/AzureTRE/issues/4294)])
21+
* Fix Cosmos DB local debugging configuration ([#4340](https://github.com/microsoft/AzureTRE/pull/4340))
2122

2223
COMPONENTS:
2324

api_app/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.21.0"
1+
__version__ = "0.21.1"

api_app/api/dependencies/database.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from azure.cosmos.aio import CosmosClient, DatabaseProxy, ContainerProxy
2-
from azure.mgmt.cosmosdb.aio import CosmosDBManagementClient
32

4-
from core.config import MANAGED_IDENTITY_CLIENT_ID, STATE_STORE_ENDPOINT, STATE_STORE_KEY, STATE_STORE_SSL_VERIFY, SUBSCRIPTION_ID, RESOURCE_MANAGER_ENDPOINT, CREDENTIAL_SCOPES, RESOURCE_GROUP_NAME, COSMOSDB_ACCOUNT_NAME, STATE_STORE_DATABASE
3+
from core.config import STATE_STORE_ENDPOINT, STATE_STORE_KEY, STATE_STORE_SSL_VERIFY, STATE_STORE_DATABASE
54
from core.credentials import get_credential_async
65
from services.logging import logger
76

@@ -27,53 +26,32 @@ def __init__(cls):
2726
async def _connect_to_db(cls) -> CosmosClient:
2827
logger.debug(f"Connecting to {STATE_STORE_ENDPOINT}")
2928

30-
credential = await get_credential_async()
31-
if MANAGED_IDENTITY_CLIENT_ID:
32-
logger.debug("Connecting with managed identity")
33-
cosmos_client = CosmosClient(
34-
url=STATE_STORE_ENDPOINT,
35-
credential=credential
36-
)
37-
else:
29+
if STATE_STORE_KEY:
3830
logger.debug("Connecting with key")
39-
primary_master_key = await cls._get_store_key(credential)
40-
4131
if STATE_STORE_SSL_VERIFY:
4232
logger.debug("Connecting with SSL verification")
4333
cosmos_client = CosmosClient(
4434
url=STATE_STORE_ENDPOINT,
45-
credential=primary_master_key
35+
credential=STATE_STORE_KEY
4636
)
4737
else:
4838
logger.debug("Connecting without SSL verification")
4939
# ignore TLS (setup is a pain) when using local Cosmos emulator.
5040
cosmos_client = CosmosClient(
5141
url=STATE_STORE_ENDPOINT,
52-
credential=primary_master_key,
42+
credential=STATE_STORE_KEY,
5343
connection_verify=False
5444
)
55-
logger.debug("Connection established")
56-
return cosmos_client
57-
58-
@classmethod
59-
async def _get_store_key(cls, credential) -> str:
60-
logger.debug("Getting store key")
61-
if STATE_STORE_KEY:
62-
primary_master_key = STATE_STORE_KEY
6345
else:
64-
async with CosmosDBManagementClient(
65-
credential,
66-
subscription_id=SUBSCRIPTION_ID,
67-
base_url=RESOURCE_MANAGER_ENDPOINT,
68-
credential_scopes=CREDENTIAL_SCOPES
69-
) as cosmosdb_mng_client:
70-
database_keys = await cosmosdb_mng_client.database_accounts.list_keys(
71-
resource_group_name=RESOURCE_GROUP_NAME,
72-
account_name=COSMOSDB_ACCOUNT_NAME,
73-
)
74-
primary_master_key = database_keys.primary_master_key
46+
logger.debug("Connecting with managed identity")
47+
credential = await get_credential_async()
48+
cosmos_client = CosmosClient(
49+
url=STATE_STORE_ENDPOINT,
50+
credential=credential
51+
)
7552

76-
return primary_master_key
53+
logger.debug("Connection established")
54+
return cosmos_client
7755

7856
@classmethod
7957
async def get_container_proxy(cls, container_name) -> ContainerProxy:

api_app/tests_ma/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ def simple_pipeline_step() -> PipelineStep:
578578
@pytest_asyncio.fixture(autouse=True)
579579
async def no_database():
580580
with patch('api.dependencies.database.get_credential_async', return_value=AsyncMock()), \
581-
patch('api.dependencies.database.CosmosDBManagementClient', return_value=AsyncMock()), \
582-
patch('api.dependencies.database.CosmosClient', return_value=AsyncMock(spec=CosmosClient)) as cosmos_client_mock:
581+
patch('api.dependencies.database.CosmosClient', return_value=AsyncMock(spec=CosmosClient)) as cosmos_client_mock:
583582
cosmos_client_mock.return_value.get_database_client.return_value = AsyncMock(spec=DatabaseProxy)
584583
yield Database()

devops/scripts/setup_local_debugging.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ az role assignment create \
9191
--assignee "${LOGGED_IN_OBJECT_ID}" \
9292
--scope "${STATE_STORE_RESOURCE_ID}"
9393

94+
ROLE_DEFINITION_ID=$(az cosmosdb sql role definition list \
95+
--resource-group "${RESOURCE_GROUP_NAME}" \
96+
--account-name "${COSMOSDB_ACCOUNT_NAME}" \
97+
--query "[?roleName=='Cosmos DB Built-in Data Contributor'].id" \
98+
--output tsv)
99+
100+
az cosmosdb sql role assignment create \
101+
--resource-group "${RESOURCE_GROUP_NAME}" \
102+
--account-name "${COSMOSDB_ACCOUNT_NAME}" \
103+
--role-definition-id "${ROLE_DEFINITION_ID}" \
104+
--principal-id "${LOGGED_IN_OBJECT_ID}" \
105+
--scope "${STATE_STORE_RESOURCE_ID}"
106+
94107
az role assignment create \
95108
--role "Contributor" \
96109
--assignee "${LOGGED_IN_OBJECT_ID}" \

0 commit comments

Comments
 (0)