Cvičení – aktualizace a verze specifikace šablony

Dokončeno

Specifikace šablony Azure Cosmos DB se teď používá v celé organizaci ke zřizování velkého množství nových účtů Azure Cosmos DB. Proto jsou všechny nakonfigurované tak, aby používaly průběžné zálohování.

Váš bezpečnostní tým nedávno zkontroloval možnosti zabezpečení služby Azure Cosmos DB. Rozhodlo se, že nové účty by měly používat ověřování Microsoft Entra a řízení přístupu na základě role služby Azure Cosmos DB.

V tomto cvičení aktualizujete specifikaci šablony novou verzí, která zahrnuje aktualizovanou konfiguraci ověřování.

Během tohoto procesu:

  • Aktualizujte šablonu a změňte konfiguraci zásad zálohování.
  • Publikujte novou verzi specifikace šablony.
  • Ověřte, že byla aktualizována specifikace šablony.
  • Otestujte novou verzi specifikace šablony nasazením jiného účtu služby Azure Cosmos DB.

Aktualizace šablony

  1. V editoru Visual Studio Code otevřete soubor azuredeploy.json .

  2. Aktualizujte soubor azuredeploy.json tak, aby zahrnoval následující změny:

    {
      "$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'))]"
          ]
        }
      ]
    }
    
  3. Uložte soubor.

  1. V editoru Visual Studio Code otevřete soubor main.bicep .

  2. Aktualizujte soubor main.bicep tak, aby zahrnoval následující změny:

    @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
      }
    }
    
  3. Uložte soubor.

Publikování nové verze specifikace šablony

Publikujte specifikaci šablony pomocí této rutiny Azure PowerShellu v terminálu editoru Visual Studio Code:

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

Publikujte specifikaci šablony pomocí tohoto příkazu Azure CLI v terminálu editoru Visual Studio Code:

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

Ověření specifikace šablony

  1. V prohlížeči se vraťte na web Azure Portal. Přejděte do skupiny prostředků.

  2. Vyberte specifikaci šablony. Všimněte si, že nejnovější verze je teď uvedená jako 2.0.

    Screenshot of the Azure portal interface for the template spec, showing the latest version as 2.0.

  3. Vyberte položku nabídky Verze. Všimněte si, že obě verze jsou teď uvedené.

    Screenshot of the Azure portal interface for the template spec, showing the list of versions as 1.0 and 2.0.

    Pokud potřebujete, můžete se vrátit k předchozím verzím specifikace šablony.

Nasazení nové verze specifikace šablony

  1. Spuštěním následujícího příkazu Azure PowerShellu získejte ID prostředku nové verze specifikace šablony:

    $templateSpecVersionResourceId = ( `
       Get-AzTemplateSpec `
          -ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
          -Name ToyCosmosDBAccount `
          -Version 2.0 `
       ).Versions[0].Id
    

    Všimněte si, že pomocí Versions vlastnosti získáte ID prostředku verze specifikace šablony.

  2. Vaše nová verze specifikace šablony má parametr pro ID objektu zabezpečení uživatele. K získání ID objektu zabezpečení vlastního uživatelského účtu použijte následující příkazy:

    $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
    

    Příkazy používají rozhraní Microsoft Graph API k dotazování na vlastní profil uživatele.

  3. Nasaďte specifikaci šablony pomocí tohoto příkazu Azure PowerShellu v terminálu editoru Visual Studio Code:

    New-AzResourceGroupDeployment `
      -TemplateSpecId $templateSpecVersionResourceId `
      -roleAssignmentPrincipalId $userObjectId
    
  1. Spuštěním následujícího příkazu Azure CLI získejte ID prostředku specifikace šablony:

    id=$(az ts show \
     --name ToyCosmosDBAccount \
     --resource-group "<rgn>[sandbox resource group name]</rgn>" \
     --version "2.0" \
     --query "id")
    
  2. Nasaďte specifikaci šablony pomocí tohoto příkazu Azure CLI v terminálu editoru Visual Studio Code:

    az deployment group create \
     --template-spec $id \
     --parameters roleAssignmentPrincipalId="d68d19b3-d7ef-4ae9-9ee4-90695a4e417d"
    

Dokončení nasazení může trvat minutu nebo dvě.

Ověření nasazení

  1. V prohlížeči se vraťte na web Azure Portal. Přejděte do skupiny prostředků.

  2. Vedle možnosti Nasazení vyberte 2 Úspěšné.

    Screenshot of the Azure portal interface for the resource group overview, with the deployments section showing that two succeeded.

  3. Vyberte nejnovější nasazení.

    Screenshot of the Azure portal interface for the deployments, with two deployments listed.

  4. Výběrem možnosti Podrobnosti o nasazení ho rozbalíte. Ověřte, že jsou nasazené prostředky pro řízení přístupu na základě role služby Azure Cosmos DB.

    Screenshot of the Azure portal interface for the specific deployment, with the Azure Cosmos DB resources listed.