Övning – Uppdatera och versionshantera en mallspecifikation
Din Azure Cosmos DB-mallspecifikation används nu i hela organisationen för att etablera många nya Azure Cosmos DB-konton. Därför är alla konfigurerade för att använda kontinuerlig säkerhetskopiering.
Säkerhetsteamet har nyligen granskat säkerhetsfunktionerna i Azure Cosmos DB. Den beslutade att nya konton ska använda Microsoft Entra-autentisering och rollbaserad åtkomstkontroll i Azure Cosmos DB.
I den här övningen uppdaterar du mallspecifikationen med en ny version som innehåller den uppdaterade autentiseringskonfigurationen.
Under processen gör du följande:
- Uppdatera mallen för att konfigurera om säkerhetskopieringsprincipen.
- Publicera en ny version av mallspecifikationen.
- Kontrollera att mallspecifikationen har uppdaterats.
- Testa den nya versionen av mallspecifikationen genom att distribuera ett annat Azure Cosmos DB-konto.
Uppdatera mallen
Öppna filen azuredeploy.json i Visual Studio Code.
Uppdatera azuredeploy.json-filen så att den innehåller följande ändringar:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location": { "type": "string", "defaultValue": "[resourceGroup().location]", "metadata": { "description": "The Azure region into which the Cosmos DB resources should be deployed." } }, "cosmosDBAccountName": { "type": "string", "defaultValue": "[concat('toy-', uniqueString(resourceGroup().id))]", "maxLength": 44, "minLength": 3, "metadata": { "description": "The name of the Cosmos DB account. This name must be globally unique, and it must only include lowercase letters, numbers, and hyphens." } }, "roleDefinitionFriendlyName": { "type": "string", "defaultValue": "Read and Write", "metadata": { "description": "A descriptive name for the role definition." } }, "roleDefinitionDataActions": { "type": "array", "defaultValue": [ "Microsoft.DocumentDB/databaseAccounts/readMetadata", "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*" ], "metadata": { "description": "The list of actions that the role definition permits." } }, "roleAssignmentPrincipalId": { "type": "string", "metadata": { "description": "The object ID of the Azure AD principal that should be granted access using the role definition." } } }, "variables": { "roleDefinitionName": "[guid('sql-role-definition', resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName')))]", "roleAssignmentName": "[guid('sql-role-assignment', resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName')))]" }, "resources": [ { "type": "Microsoft.DocumentDB/databaseAccounts", "apiVersion": "2021-04-15", "name": "[parameters('cosmosDBAccountName')]", "kind": "GlobalDocumentDB", "location": "[parameters('location')]", "properties": { "consistencyPolicy": { "defaultConsistencyLevel": "Session" }, "locations": [ { "locationName": "[parameters('location')]", "failoverPriority": 0, "isZoneRedundant": false } ], "databaseAccountOfferType": "Standard", "enableAutomaticFailover": false, "enableMultipleWriteLocations": false, "backupPolicy": { "type": "Continuous" } } }, { "type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions", "apiVersion": "2021-04-15", "name": "[format('{0}/{1}', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]", "properties": { "roleName": "[parameters('roleDefinitionFriendlyName')]", "type": "CustomRole", "assignableScopes": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]" ], "permissions": [ { "dataActions": "[parameters('roleDefinitionDataActions')]" } ] }, "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]" ] }, { "type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments", "apiVersion": "2021-04-15", "name": "[format('{0}/{1}', parameters('cosmosDBAccountName'), variables('roleAssignmentName'))]", "properties": { "roleDefinitionId": "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]", "principalId": "[parameters('roleAssignmentPrincipalId')]", "scope": "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]" }, "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]", "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]" ] } ] }
Spara filen.
Öppna filen main.bicep i Visual Studio Code.
Uppdatera filen main.bicep så att den innehåller följande ändringar:
@description('The Azure region into which the Cosmos DB resources should be deployed.') param location string = resourceGroup().location @description('The name of the Cosmos DB account. This name must be globally unique, and it must only include lowercase letters, numbers, and hyphens.') @minLength(3) @maxLength(44) param cosmosDBAccountName string = 'toy-${uniqueString(resourceGroup().id)}' @description('A descriptive name for the role definition.') param roleDefinitionFriendlyName string = 'Read and Write' @description('The list of actions that the role definition permits.') param roleDefinitionDataActions array = [ 'Microsoft.DocumentDB/databaseAccounts/readMetadata' 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*' ] @description('The object ID of the Azure AD principal that should be granted access using the role definition.') param roleAssignmentPrincipalId string var roleDefinitionName = guid('sql-role-definition', cosmosDBAccount.id) var roleAssignmentName = guid('sql-role-assignment', cosmosDBAccount.id) resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = { name: cosmosDBAccountName kind: 'GlobalDocumentDB' location: location properties: { consistencyPolicy: { defaultConsistencyLevel: 'Session' } locations: [ { locationName: location failoverPriority: 0 isZoneRedundant: false } ] databaseAccountOfferType: 'Standard' enableAutomaticFailover: false enableMultipleWriteLocations: false } } resource roleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions@2021-04-15' = { parent: cosmosDBAccount name: roleDefinitionName properties: { roleName: roleDefinitionFriendlyName type: 'CustomRole' assignableScopes: [ cosmosDBAccount.id ] permissions: [ { dataActions: roleDefinitionDataActions } ] } } resource roleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2021-04-15' = { parent: cosmosDBAccount name: roleAssignmentName properties: { roleDefinitionId: roleDefinition.id principalId: roleAssignmentPrincipalId scope: cosmosDBAccount.id } }
Spara filen.
Publicera en ny version av mallspecifikationen
Publicera mallspecifikationen med den här Azure PowerShell-cmdleten i Visual Studio Code-terminalen:
New-AzTemplateSpec `
-ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
-Name ToyCosmosDBAccount `
-Version '2.0' `
-VersionDescription 'Adds Cosmos DB role-based access control.' `
-TemplateFile main.bicep
New-AzTemplateSpec `
-ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
-Name ToyCosmosDBAccount `
-Version '2.0' `
-VersionDescription 'Adds Cosmos DB role-based access control.' `
-TemplateFile azuredeploy.json
Publicera mallspecifikationen med hjälp av det här Azure CLI-kommandot i Visual Studio Code-terminalen:
az ts create \
--name ToyCosmosDBAccount \
--version 2.0 \
--version-description "Adds Cosmos DB role-based access control." \
--template-file main.bicep
az ts create \
--name ToyCosmosDBAccount \
--version 2.0 \
--version-description "Adds Cosmos DB role-based access control." \
--template-file azuredeploy.json
Verifiera mallspecifikationen
Gå tillbaka till Azure-portalen i webbläsaren. Gå till resursgruppen.
Välj mallspecifikationen. Observera att den senaste versionen nu visas som 2.0.
Välj menyalternativet Versioner. Observera att båda versionerna nu visas.
Med mallspecifikationsversioner kan du gå tillbaka till tidigare versioner av mallspecifikationen om du behöver det.
Distribuera den nya mallspecifikationsversionen
Hämta den nya mallspecifikationsversionens resurs-ID genom att köra följande Azure PowerShell-kommando:
$templateSpecVersionResourceId = ( ` Get-AzTemplateSpec ` -ResourceGroupName <rgn>[sandbox resource group name]</rgn> ` -Name ToyCosmosDBAccount ` -Version 2.0 ` ).Versions[0].Id
Observera att du använder egenskapen
Versions
för att hämta mallspecifikationsversionens resurs-ID.Den nya mallspecifikationsversionen har en parameter för användarens huvud-ID. Använd följande kommandon för att hämta ditt eget användarkontos huvud-ID:
$token = (Get-AzAccessToken -ResourceUrl "https://graph.windows.net/").Token $userObjectId = (Invoke-RestMethod -Uri 'https://graph.windows.net/me?api-version=1.6' -Headers @{ 'Authorization' = "Bearer $token"}).objectID
Kommandona använder Microsoft Graph API för att köra frågor mot din egen användarprofil.
Distribuera mallspecifikationen med hjälp av det här Azure PowerShell-kommandot i Visual Studio Code-terminalen:
New-AzResourceGroupDeployment ` -TemplateSpecId $templateSpecVersionResourceId ` -roleAssignmentPrincipalId $userObjectId
Hämta mallspecifikationsversionens resurs-ID genom att köra följande Azure CLI-kommando:
id=$(az ts show \ --name ToyCosmosDBAccount \ --resource-group "<rgn>[sandbox resource group name]</rgn>" \ --version "2.0" \ --query "id")
Distribuera mallspecifikationen med hjälp av det här Azure CLI-kommandot i Visual Studio Code-terminalen:
az deployment group create \ --template-spec $id \ --parameters roleAssignmentPrincipalId="d68d19b3-d7ef-4ae9-9ee4-90695a4e417d"
Utplaceringen kan ta en eller två minuter att slutföra.
Verifiera utrullningen
Gå tillbaka till Azure-portalen i webbläsaren. Gå till resursgruppen.
Bredvid Utrullningarväljer du 2 lyckades.
Välj den senaste distributionen.
Välj Distributionsinformation för att expandera den. Bekräfta att resurserna för rollbaserad åtkomstkontroll i Azure Cosmos DB har distribuerats.