Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to using managed identity for auth to CosmosDB. #3806

Merged
merged 35 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5071e45
Move to using managed identity for auth to CosmosDB.
marrobi Dec 6, 2023
81ceb1e
Add permissions to TRE DB to the API MSI
marrobi Dec 6, 2023
88b1f9b
Merge upstream
marrobi Dec 7, 2023
4ff3dfe
Update CHANGELOG
marrobi Dec 7, 2023
45a9fb7
Add missing await and fix tests.
marrobi Dec 20, 2023
3914d31
update core version
marrobi Dec 20, 2023
fc2dd9a
Update core version
marrobi Dec 20, 2023
5e7a417
remove DB create as this is done in terraform
marrobi Dec 20, 2023
97018a2
split management plane and data plane operations.
marrobi Dec 21, 2023
11f0f16
fix test
marrobi Dec 21, 2023
d0752e4
fix test
marrobi Dec 21, 2023
c345aa2
Refactor container creation.
marrobi Dec 21, 2023
0f6f592
remove import
marrobi Dec 21, 2023
f87f2bc
Added workaround for transport being closed.
marrobi Dec 21, 2023
69c8568
Move database connection to singleton
marrobi Dec 22, 2023
93729d3
fix linting
marrobi Dec 22, 2023
1fd7fa7
Remove async with cosmos_client as this is closing the client on exit
marrobi Dec 22, 2023
0a96818
Create method to get creds async without context manager
marrobi Dec 22, 2023
5715b1c
Merge branch 'main' into marrobi/issue345
marrobi Dec 22, 2023
6ab2fba
Factor out cosmos client from everywhere but base repo and health checks
marrobi Jan 2, 2024
8874a6a
Merge branch 'marrobi/issue345' of github.com:marrobi/AzureTRE into m…
marrobi Jan 2, 2024
7d85d04
Remove uneeded cosmos references
marrobi Jan 2, 2024
7ab298f
Remove uneeded database imports
marrobi Jan 2, 2024
b815846
fix async issue with FastAPI depends
marrobi Jan 2, 2024
96c296c
reduce changes
marrobi Jan 2, 2024
bfca0cd
reduce churn
marrobi Jan 2, 2024
4ff113e
Fix bad imports
marrobi Jan 2, 2024
8e98ebf
fix reverted change
marrobi Jan 2, 2024
df711f6
Move get container into database class
marrobi Jan 3, 2024
f82fd06
Remove await
marrobi Jan 3, 2024
7dfc153
tidy up credentials modifications
marrobi Jan 3, 2024
90f7557
fix linting
marrobi Jan 3, 2024
18b651d
fix msi handling
marrobi Jan 3, 2024
2e95926
missed file.
marrobi Jan 3, 2024
c33d9db
fix linting
marrobi Jan 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FEATURES:

ENHANCEMENTS:
* Switch from OpenCensus to OpenTelemetry for logging ([#3762](https://github.com/microsoft/AzureTRE/pull/3762))
* Use mangaged identity for API connection to CosmosDB ([#345](https://github.com/microsoft/AzureTRE/issues/345))

BUG FIXES:

Expand Down
2 changes: 1 addition & 1 deletion api_app/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.17.1"
__version__ = "0.17.7"
38 changes: 23 additions & 15 deletions api_app/api/dependencies/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from azure.mgmt.cosmosdb.aio import CosmosDBManagementClient
from fastapi import Depends, FastAPI, HTTPException
from fastapi import Request, status
from core.config import MANAGED_IDENTITY_CLIENT_ID
from core import config, credentials
from db.errors import UnableToAccessDatabase
from db.repositories.base import BaseRepository
Expand All @@ -13,27 +14,34 @@

async def connect_to_db() -> CosmosClient:
logger.debug(f"Connecting to {config.STATE_STORE_ENDPOINT}")

try:
async with credentials.get_credential_async() as credential:
primary_master_key = await get_store_key(credential)

if config.STATE_STORE_SSL_VERIFY:
async with credentials.get_credential_async() as credential:
if MANAGED_IDENTITY_CLIENT_ID:
logger.debug("Connecting with managed identity")
cosmos_client = CosmosClient(
url=config.STATE_STORE_ENDPOINT, credential=primary_master_key
url=config.STATE_STORE_ENDPOINT,
credential=credential
)
else:
# ignore TLS (setup is a pain) when using local Cosmos emulator.
cosmos_client = CosmosClient(
config.STATE_STORE_ENDPOINT, primary_master_key, connection_verify=False
)
logger.debug("Connection established")
return cosmos_client
except Exception:
logger.exception("Connection to state store could not be established.")
logger.debug("Connecting with key")
primary_master_key = await get_store_key(credential)

if config.STATE_STORE_SSL_VERIFY:
logger.debug("Connecting with SSL verification")
cosmos_client = CosmosClient(
url=config.STATE_STORE_ENDPOINT, credential=primary_master_key
)
else:
logger.debug("Connecting without SSL verification")
# ignore TLS (setup is a pain) when using local Cosmos emulator.
cosmos_client = CosmosClient(
config.STATE_STORE_ENDPOINT, primary_master_key, connection_verify=False
)
logger.debug("Connection established")
return cosmos_client


async def get_store_key(credential) -> str:
logger.debug("Getting store key")
if config.STATE_STORE_KEY:
primary_master_key = config.STATE_STORE_KEY
else:
Expand Down
1 change: 1 addition & 0 deletions api_app/db/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ async def bootstrap_database(app) -> bool:
await ResourceRepository.create(client)
return True
except Exception as e:
logger.exception("Could not bootstrap database")
logger.debug(e)
return False
7 changes: 2 additions & 5 deletions api_app/services/health_checker.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Tuple
from azure.core import exceptions
from azure.cosmos.aio import CosmosClient
from azure.servicebus.aio import ServiceBusClient
from azure.mgmt.compute.aio import ComputeManagementClient
from azure.cosmos.exceptions import CosmosHttpResponseError
from azure.servicebus.exceptions import ServiceBusConnectionError, ServiceBusAuthenticationError
from api.dependencies.database import get_store_key
from api.dependencies.database import connect_to_db

from core import config
from models.schemas.status import StatusEnum
Expand All @@ -16,10 +15,8 @@
async def create_state_store_status(credential) -> Tuple[StatusEnum, str]:
status = StatusEnum.ok
message = ""
debug = True if config.LOGGING_LEVEL == "DEBUG" else False
try:
primary_master_key = await get_store_key(credential)
cosmos_client = CosmosClient(config.STATE_STORE_ENDPOINT, primary_master_key, connection_verify=debug)
cosmos_client = connect_to_db()
async with cosmos_client:
list_databases_response = cosmos_client.list_databases()
[database async for database in list_databases_response]
Expand Down
13 changes: 13 additions & 0 deletions core/terraform/api-identity.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ resource "azurerm_role_assignment" "cosmos_contributor" {
principal_id = azurerm_user_assigned_identity.id.principal_id
}

data "azurerm_cosmosdb_sql_role_definition" "cosmosdb_db_contributor" {
resource_group_name = azurerm_resource_group.core.name
account_name = azurerm_cosmosdb_account.tre_db_account.name
role_definition_id = "00000000-0000-0000-0000-000000000002" # Cosmos DB Built-in Data Contributor
}

resource "azurerm_cosmosdb_sql_role_assignment" "tre_db_contributor" {
resource_group_name = azurerm_resource_group.core.name
account_name = azurerm_cosmosdb_account.tre_db_account.name
role_definition_id = data.azurerm_cosmosdb_sql_role_definition.cosmosdb_db_contributor.id
principal_id = azurerm_user_assigned_identity.id.principal_id
scope = azurerm_cosmosdb_account.tre_db_account.id
}
Loading