Azure Cosmos DB can be used with DefaultAzureCrentials in C#. However, for enabling usage of DefaultAzureCrentials with Azure Cosmos DB requires special data roles to be added to the Cosmos DB account. There are two built in roles data reader and data contributor. Unlike other RBAC roles in Azure these roles cannot be assigned via Azure portal and they must be added programatically. They have to added via Azure CLI, Bicep, Powershell, REST API or Terraform.
The purpose is to have the data contributor (if you only need read access to data add data reader) as shown below. The added roles can pnly viewd programatically and not visiblle in Azure portal as of the time of writing of this blob post. Below command in Azure CLI can be used to get the built in role assignements of the Cosmos DB account. Below you can see three identites are assigned with data contributor role (the identities used below are of the dev user group, ops user group and the AKS workload identity user assignd identity)
az cosmosdb sql role assignment list -a <cosmosdbaccountname> -g <resourcegroupname>
Let's look at how to setup the role via terraform.
We can setup Cosmos account as shown below.
Then we can read the groups and create a user assigned identity for AKS as shown below.
Next we need tor read the "Cosmos DB Built-in Data Contributor" with its id. This cannot be reffered with the name.
Then we can assigne the role to the required users, identities or groups as shown below.
# AKS user assigned identity as a data contributor resource "azurerm_cosmosdb_sql_role_assignment" "cosmos_data_contributor_aks_uai" { resource_group_name = azurerm_cosmosdb_account.cosmos_acc.resource_group_name account_name = azurerm_cosmosdb_account.cosmos_acc.name scope = azurerm_cosmosdb_account.cosmos_acc.id role_definition_id = data.azurerm_cosmosdb_sql_role_definition.cosmos_db_data_contributor.id principal_id = azurerm_user_assigned_identity.aks_uai.principal_id } # Ops as a data contributor resource "azurerm_cosmosdb_sql_role_assignment" "cosmos_data_contributor_ops" { resource_group_name = azurerm_cosmosdb_account.cosmos_acc.resource_group_name account_name = azurerm_cosmosdb_account.cosmos_acc.name scope = azurerm_cosmosdb_account.cosmos_acc.id role_definition_id = data.azurerm_cosmosdb_sql_role_definition.cosmos_db_data_contributor.id principal_id = data.azuread_group.demo_ops.object_id } # Devs as a data contributor resource "azurerm_cosmosdb_sql_role_assignment" "cosmos_data_contributor_devs" { count = var.environment == local.dev_environment || var.environment == local.qa_environment ? 1 : 0 resource_group_name = azurerm_cosmosdb_account.cosmos_acc.resource_group_name account_name = azurerm_cosmosdb_account.cosmos_acc.name scope = azurerm_cosmosdb_account.cosmos_acc.id role_definition_id = data.azurerm_cosmosdb_sql_role_definition.cosmos_db_data_contributor.id principal_id = data.azuread_group.demo_devs.object_id }
Let's look at how to use the "Cosmos DB Built-in Data Contributor" to access Cosmos DB data with C# and use DefaultAzureCredentials in the next post.
No comments:
Post a Comment