Skip to content

Commit 568dd58

Browse files
committed
use managed identity ACA Cosmos
1 parent e3c3c80 commit 568dd58

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,6 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
209209
* https://azureossd.github.io/2024/02/12/Container-Apps-General-troubleshooting-with-Dapr-on-Container-Apps/
210210
* https://github.com/Azure/aca-dotnet-workshop
211211
* https://docs.dapr.io/developing-applications/building-blocks/state-management/howto-state-query-api/
212+
* https://github.com/Azure-Samples/Tutorial-Deploy-Dapr-Microservices-ACA/blob/main/azuredeploy.bicep
213+
212214

infra/main.bicep

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,6 @@ module containerMovieGallerySvcApp 'modules/apps/movie-gallery-svc.bicep' = {
753753
acrPullRoleName: uaiAzureRambiAcrPull.name
754754
shared_secrets: shared_secrets
755755
containerAppsEnvironment: containerAppsEnv.name
756-
kvName: kv.name
757756
}
758757
}
759758

infra/modules/apps/movie-gallery-svc.bicep

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ resource containerAppsEnv 'Microsoft.App/managedEnvironments@2024-10-02-preview'
5151
name: 'collection'
5252
value: 'state'
5353
}
54+
{
55+
name: 'azureClientId'
56+
value: managedIdentity.properties.clientId
57+
}
5458
]
5559
scopes: [
5660
containerName
@@ -63,9 +67,10 @@ resource containerMovieGallerySvcApp 'Microsoft.App/containerApps@2024-10-02-pre
6367
name: containerName
6468
location: location
6569
identity: {
66-
type: 'SystemAssigned,UserAssigned'
70+
type: 'UserAssigned'
6771
userAssignedIdentities: {
6872
'${uaiAzureRambiAcrPull.id}': {}
73+
'${managedIdentity.id}': {}
6974
}
7075
}
7176
tags: { 'azd-service-name': replace(containerName, '-', '_') }
@@ -205,10 +210,15 @@ resource cosmosDbDatabaseCollection 'Microsoft.DocumentDB/databaseAccounts/sqlDa
205210
// Assign cosmosdb account read/write access to aca system assigned identity
206211
// To know more: https://learn.microsoft.com/azure/cosmos-db/how-to-setup-rbac
207212
resource backendApiService_cosmosdb_role_assignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2022-08-15' = {
208-
name: guid(subscription().id, containerMovieGallerySvcApp.name, '00000000-0000-0000-0000-000000000002')
213+
name: guid(
214+
subscription().id,
215+
'docdbcontributor',
216+
containerMovieGallerySvcApp.name,
217+
'00000000-0000-0000-0000-000000000002'
218+
)
209219
parent: cosmosDbAccount
210220
properties: {
211-
principalId: containerMovieGallerySvcApp.identity.principalId
221+
principalId: managedIdentity.properties.principalId
212222
roleDefinitionId: resourceId(
213223
'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions',
214224
cosmosDbAccount.name,
@@ -218,5 +228,10 @@ resource backendApiService_cosmosdb_role_assignment 'Microsoft.DocumentDB/databa
218228
}
219229
}
220230

231+
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2022-01-31-preview' = {
232+
name: 'dapr-state-store-identity'
233+
location: location
234+
}
235+
221236
output name string = containerMovieGallerySvcApp.name
222237
output fqdn string = containerMovieGallerySvcApp.properties.configuration.ingress.fqdn

src/movie_gallery_svc/entities.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
22
import uuid
33
import logging
4-
from pydantic import BaseModel
54
from typing import List
5+
from pydantic import BaseModel
66

77
class MovieRequest(BaseModel):
88
"""Request model for adding a new movie."""
@@ -15,7 +15,7 @@ def __init__(self, movie_id : str, title : str, description : str):
1515
dict.__init__(self, movie_id=movie_id, title=title, description=description)
1616

1717
def __repr__(self):
18-
return f"MovieGallery(id={self['movie_id']}, title={self['title']}, description={self['description']})"
18+
return f"MovieGallery(movie_id={self['movie_id']}, title={self['title']}, description={self['description']})"
1919

2020
def getattr(self, key):
2121
"""Get attribute value by key."""
@@ -39,7 +39,7 @@ def from_bytes(json_bytes : bytes) -> 'Movie':
3939
"""Convert bytes to Movie object."""
4040
item = json.loads(json_bytes.decode('utf-8'))
4141
return Movie(
42-
item["id"],
42+
item["movie_id"],
4343
item["title"],
4444
item["description"]
4545
)

src/movie_gallery_svc/store.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def upsert(self, movie: Movie) -> Movie:
1818
"""Add a movie to the store."""
1919
logging.info("Adding movie: %s", movie)
2020
logging.info("JSON %s", json.dumps(movie))
21-
logging.info("saving movie to store %s using this key %s", self.state_store_name, movie['id'])
21+
logging.info("saving movie to store %s using this key %s", self.state_store_name, movie['movie_id'])
2222
self.dapr_client.save_state(
2323
store_name=self.state_store_name,
24-
key=movie['id'],
24+
key=movie['movie_id'],
2525
value=json.dumps(movie)
2626
)
27-
logging.info("Movie %s added to store", movie['id'])
28-
return self.try_find_by_id(movie['id'])
27+
logging.info("Movie %s added to store", movie['movie_id'])
28+
return self.try_find_by_id(movie['movie_id'])
2929

3030
def try_find_by_id(self, movie_id : str) -> Movie:
3131
"""Find a movie by its ID."""

0 commit comments

Comments
 (0)