Skip to content

Commit 26109e2

Browse files
authored
Merge pull request #13 from seesharprun/remove-keys
Remove all references to Key Vault and ROPC
2 parents 32fe04f + 516f8e3 commit 26109e2

File tree

4 files changed

+36
-86
lines changed

4 files changed

+36
-86
lines changed

azure.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ hooks:
2121
continueOnError: false
2222
run: |
2323
echo "
24-
CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME=\"$CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME\"
2524
CONFIGURATION__AZURECOSMOSDB__ENDPOINT=\"$CONFIGURATION__AZURECOSMOSDB__ENDPOINT\"
26-
CONFIGURATION__AZURECOSMOSDB__KEY=\"$CONFIGURATION__AZURECOSMOSDB__KEY\"
2725
CONFIGURATION__AZURECOSMOSDB__TABLENAME=\"$CONFIGURATION__AZURECOSMOSDB__TABLENAME\"
2826
" > ./src/.env
2927
windows:
@@ -32,8 +30,6 @@ hooks:
3230
continueOnError: false
3331
run: |
3432
echo "
35-
CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME=""$env:CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME""
3633
CONFIGURATION__AZURECOSMOSDB__ENDPOINT=""$env:CONFIGURATION__AZURECOSMOSDB__ENDPOINT""
37-
CONFIGURATION__AZURECOSMOSDB__KEY=""$env:CONFIGURATION__AZURECOSMOSDB__KEY""
3834
CONFIGURATION__AZURECOSMOSDB__TABLENAME=""$env:CONFIGURATION__AZURECOSMOSDB__TABLENAME""
3935
" > ./src/.env

infra/main.bicep

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,17 @@ var tags = {
2323
repo: 'https://github.com/azure-samples/cosmos-db-table-python-quickstart'
2424
}
2525

26-
var tableName = 'cosmicworks-products'
27-
28-
module keyVault 'br/public:avm/res/key-vault/vault:0.10.2' = {
29-
name: 'key-vault'
26+
module managedIdentity 'br/public:avm/res/managed-identity/user-assigned-identity:0.4.0' = {
27+
name: 'user-assigned-identity'
3028
params: {
31-
name: 'key-vault-${resourceToken}'
29+
name: 'managed-identity-${resourceToken}'
3230
location: location
33-
sku: 'standard'
34-
enablePurgeProtection: false
35-
enableSoftDelete: false
36-
publicNetworkAccess: 'Enabled'
37-
enableRbacAuthorization: true
38-
secrets: [
39-
{
40-
name: 'key-vault-secret-azure-cosmos-db-table-key'
41-
value: ''
42-
}
43-
]
31+
tags: tags
4432
}
4533
}
4634

35+
var tableName = 'cosmicworks-products'
36+
4737
module cosmosDbAccount 'br/public:avm/res/document-db/database-account:0.8.1' = {
4838
name: 'cosmos-db-account'
4939
params: {
@@ -57,8 +47,8 @@ module cosmosDbAccount 'br/public:avm/res/document-db/database-account:0.8.1' =
5747
}
5848
]
5949
tags: tags
60-
disableKeyBasedMetadataWriteAccess: false
61-
disableLocalAuth: false
50+
disableKeyBasedMetadataWriteAccess: true
51+
disableLocalAuth: true
6252
networkRestrictions: {
6353
publicNetworkAccess: 'Enabled'
6454
ipRules: []
@@ -68,13 +58,25 @@ module cosmosDbAccount 'br/public:avm/res/document-db/database-account:0.8.1' =
6858
'EnableServerless'
6959
'EnableTable'
7060
]
71-
secretsExportConfiguration: {
72-
keyVaultResourceId: keyVault.outputs.resourceId
73-
primaryWriteKeySecretName: 'key-vault-secret-azure-cosmos-db-table-key'
74-
}
61+
sqlRoleDefinitions: [
62+
{
63+
name: 'table-data-plane-contributor'
64+
dataAction: [
65+
'Microsoft.DocumentDB/databaseAccounts/readMetadata' // Read account metadata
66+
'Microsoft.DocumentDB/databaseAccounts/tables/*' // Manage tables
67+
'Microsoft.DocumentDB/databaseAccounts/tables/containers/entities/*' // Create entities
68+
]
69+
}
70+
]
71+
sqlRoleAssignmentsPrincipalIds: union(
72+
[
73+
managedIdentity.outputs.principalId
74+
],
75+
!empty(deploymentUserPrincipalId) ? [deploymentUserPrincipalId] : []
76+
)
7577
tables: [
7678
{
77-
name: tableName
79+
name: tableName
7880
}
7981
]
8082
}
@@ -127,34 +129,8 @@ module containerAppsEnvironment 'br/public:avm/res/app/managed-environment:0.8.0
127129
}
128130
}
129131

130-
module managedIdentity 'br/public:avm/res/managed-identity/user-assigned-identity:0.4.0' = {
131-
name: 'user-assigned-identity'
132-
params: {
133-
name: 'managed-identity-${resourceToken}'
134-
location: location
135-
tags: tags
136-
}
137-
}
138-
139-
var keyVaultRole = subscriptionResourceId(
140-
'Microsoft.Authorization/roleDefinitions',
141-
'4633458b-17de-408a-b874-0445c86b69e6'
142-
) // Key Vault Secrets User built-in role
143-
144-
module keyVaultAppAssignment 'br/public:avm/ptn/authorization/resource-role-assignment:0.1.1' = {
145-
name: 'key-vault-role-assignment-secrets-user'
146-
params: {
147-
principalId: managedIdentity.outputs.principalId
148-
resourceId: keyVault.outputs.resourceId
149-
roleDefinitionId: keyVaultRole
150-
}
151-
}
152-
153132
module containerAppsApp 'br/public:avm/res/app/container-app:0.9.0' = {
154133
name: 'container-apps-app'
155-
dependsOn: [
156-
keyVaultAppAssignment // Need to wait for the role assignment to complete before creating the container app
157-
]
158134
params: {
159135
name: 'container-app-${resourceToken}'
160136
environmentResourceId: containerAppsEnvironment.outputs.resourceId
@@ -185,13 +161,8 @@ module containerAppsApp 'br/public:avm/res/app/container-app:0.9.0' = {
185161
value: 'https://${cosmosDbAccount.outputs.name}.table.cosmos.azure.com:443/'
186162
}
187163
{
188-
name: 'azure-cosmos-db-table-account-name'
189-
value: cosmosDbAccount.outputs.name
190-
}
191-
{
192-
identity: managedIdentity.outputs.resourceId
193-
name: 'azure-cosmos-db-table-write-key'
194-
keyVaultUrl: cosmosDbAccount.outputs.exportedSecrets['key-vault-secret-azure-cosmos-db-table-key'].secretUri
164+
name: 'user-assigned-managed-identity-client-id'
165+
value: managedIdentity.outputs.clientId
195166
}
196167
]
197168
}
@@ -204,37 +175,27 @@ module containerAppsApp 'br/public:avm/res/app/container-app:0.9.0' = {
204175
memory: '.5Gi'
205176
}
206177
env: [
207-
{
208-
name: 'CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME'
209-
secretRef: 'azure-cosmos-db-table-account-name'
210-
}
211178
{
212179
name: 'CONFIGURATION__AZURECOSMOSDB__ENDPOINT'
213180
secretRef: 'azure-cosmos-db-table-endpoint'
214181
}
215-
{
216-
name: 'CONFIGURATION__AZURECOSMOSDB__KEY'
217-
secretRef: 'azure-cosmos-db-table-write-key'
218-
}
219182
{
220183
name: 'CONFIGURATION__AZURECOSMOSDB__TABLENAME'
221184
value: tableName
222185
}
186+
{
187+
name: 'AZURE_CLIENT_ID'
188+
secretRef: 'user-assigned-managed-identity-client-id'
189+
}
223190
]
224191
}
225192
]
226193
}
227194
}
228195

229196
// Azure Cosmos DB for Table outputs
230-
output CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME string = cosmosDbAccount.outputs.name
231197
output CONFIGURATION__AZURECOSMOSDB__ENDPOINT string = 'https://${cosmosDbAccount.outputs.name}.table.cosmos.azure.com:443/'
232-
#disable-next-line outputs-should-not-contain-secrets // This secret is required
233-
output CONFIGURATION__AZURECOSMOSDB__KEY string = listKeys(
234-
resourceId('Microsoft.DocumentDB/databaseAccounts', 'cosmos-db-table-${resourceToken}'),
235-
'2021-04-15'
236-
).primaryMasterKey
237198
output CONFIGURATION__AZURECOSMOSDB__TABLENAME string = tableName
238199

239200
// Azure Container Registry outputs
240-
output AZURE_CONTAINER_REGISTRY_ENDPOINT string = containerRegistry.outputs.loginServer
201+
output AZURE_CONTAINER_REGISTRY_ENDPOINT string = containerRegistry.outputs.loginServer

src/cosmos.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dotenv import load_dotenv
22

3-
from azure.core.credentials import AzureNamedKeyCredential
43
from azure.data.tables import TableServiceClient
4+
from azure.identity import DefaultAzureCredential
55

66
import json
77
import os
@@ -14,19 +14,11 @@ def runDemo(writeOutput):
1414
load_dotenv()
1515

1616
# <create_client>
17-
accountName = os.getenv("CONFIGURATION__AZURECOSMOSDB__ACCOUNTNAME")
18-
if not accountName:
19-
raise EnvironmentError("Azure Cosmos DB for Table account name not set.")
20-
2117
endpoint = os.getenv("CONFIGURATION__AZURECOSMOSDB__ENDPOINT")
2218
if not endpoint:
2319
raise EnvironmentError("Azure Cosmos DB for Table account endpoint not set.")
2420

25-
key = os.getenv("CONFIGURATION__AZURECOSMOSDB__KEY")
26-
if not key:
27-
raise EnvironmentError("Azure Cosmos DB for Table write key not set.")
28-
29-
credential = AzureNamedKeyCredential(accountName, key)
21+
credential = DefaultAzureCredential()
3022

3123
client = TableServiceClient(endpoint=endpoint, credential=credential)
3224
# </create_client>

src/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Flask==3.0.3
22
Flask-SocketIO==5.4.1
3-
azure-data-tables==12.5.0
3+
azure-data-tables==12.6.0
4+
azure-identity==1.19.0
45
gunicorn==23.0.0
56
eventlet==0.37.0
67
python-dotenv==1.0.1

0 commit comments

Comments
 (0)