빠른 시작: Bicep 사용하여 Azure 키 자격 증명 모음 및 인증서 만들기

Azure Key Vault 는 키, 암호 및 인증서와 같은 비밀에 대한 보안 저장소를 제공하는 클라우드 서비스입니다. 이 빠른 시작에서는 키 자격 증명 모음 및 자체 서명된 인증서를 만들기 위해 Bicep 파일을 배포하는 프로세스에 중점을 둡니다.

Bicep은 선언적 구문을 사용하여 Azure 리소스를 배포하는 DSL(도메인 특정 언어)입니다. 간결한 구문, 신뢰할 수 있는 형식 안전성 및 코드 재사용 지원을 제공합니다. Bicep은 Azure에서 코드형 인프라 솔루션에 대한 최고의 제작 환경을 제공합니다.

사전 요구 사항

  • Azure 구독이 없는 경우, 시작하기 전에 무료 계정을 만드십시오.

Bicep 파일을 검토하십시오

이 빠른 시작에서 사용되는 템플릿은 Azure 빠른 시작 템플릿에서 나온 것입니다.

@description('The name of the key vault to be created.')
param vaultName string

@description('The name of the certificate to be created.')
param certificateName string

@description('The location of the resources.')
param location string = resourceGroup().location

@description('The SKU of the vault to be created.')
@allowed([
  'standard'
  'premium'
])
param skuName string = 'standard'

@description('The common name (subject) for the self-signed certificate. Defaults to the certificate name.')
param certificateCommonName string = certificateName

@description('The validity of the certificate in months.')
@minValue(1)
@maxValue(1200)
param validityInMonths int = 12

resource vault 'Microsoft.KeyVault/vaults@2023-07-01' = {
  name: vaultName
  location: location
  properties: {
    enableRbacAuthorization: true
    enableSoftDelete: true
    softDeleteRetentionInDays: 90
    enablePurgeProtection: true
    enabledForDeployment: false
    enabledForDiskEncryption: false
    enabledForTemplateDeployment: false
    tenantId: subscription().tenantId
    sku: {
      name: skuName
      family: 'A'
    }
    networkAcls: {
      defaultAction: 'Allow'
      bypass: 'AzureServices'
    }
  }
}

// Key Vault certificates are a data-plane concept and cannot be created
// directly through ARM. Use the public Bicep registry module which wraps
// `az keyvault certificate create` in a deployment script (it provisions a
// user-assigned managed identity with the Key Vault Certificate Officer
// role on the vault for the duration of the deployment).
module certificate 'br/public:deployment-scripts/create-kv-certificate:3.4.2' = {
  name: 'create-${certificateName}'
  params: {
    akvName: vault.name
    location: location
    certificateNames: [certificateName]
    certificateCommonNames: [certificateCommonName]
    validity: validityInMonths
  }
}

output location string = location
output name string = vault.name
output resourceGroupName string = resourceGroup().name
output resourceId string = vault.id
output certificateSecretId string = certificate.outputs.certificateSecretIds[0][0]
output certificateThumbprint string = certificate.outputs.certificateThumbprintHexs[0][0]

Bicep 파일에는 두 개의 Azure 리소스가 정의되어 있습니다.

  • Microsoft. KeyVault/vaults: Azure RBAC 권한 부여를 사용하도록 설정된 Azure 키 자격 증명 모음을 만듭니다(enableRbacAuthorization: true).
  • Microsoft.Resources/deployments: 볼트에 자체 서명된 인증서를 생성하기 위해 create-kv-certificate 레지스트리 모듈을 실행하는 중첩 배포입니다. 인증서는 데이터 평면 리소스이며 ARM 리소스 유형으로 직접 만들 수 없습니다.

Bicep 파일을 배포하세요.

  1. Bicep 파일을 main.bicep로 로컬 컴퓨터에 저장합니다.

  2. Azure CLI 또는 Azure PowerShell을 사용하여 Bicep 파일을 배포합니다.

    az group create --name myResourceGroup --location eastus
    az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters vaultName=<vault-name> certificateName=myCert
    

    메모

    <vault-name>vault.azure.net 네임스페이스 내에서 전역적으로 고유해야 하는 키 자격 증명 모음 이름으로 바꿉니다.

    배포가 완료되면 배포가 성공했음을 나타내는 메시지가 표시됩니다.

Key Vault RBAC 역할 할당하기

이 Bicep 파일로 생성된 Key Vault는 권한 부여에 Azure RBAC를 사용합니다. 데이터 평면을 통해 인증서에 액세스하려면(예: Azure CLI 또는 Azure PowerShell 사용) 자신에게 적절한 역할을 할당해야 합니다.

echo "Enter your key vault name:" &&
read keyVaultName &&
az role assignment create --role "Key Vault Certificates Officer" \
    --assignee-object-id $(az ad signed-in-user show --query id -o tsv) \
    --scope $(az keyvault show --name $keyVaultName --query id -o tsv)

메모

역할 할당이 전파되는 데 1~2분 정도 걸릴 수 있습니다.

배포된 리소스 검토

Azure 포털을 사용하여 키 자격 증명 모음 및 인증서를 확인하거나 다음 Azure CLI 또는 Azure PowerShell 스크립트를 사용하여 만든 인증서를 나열할 수 있습니다.

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault certificate list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."

자원을 정리하세요

더 이상 필요 없으면 Azure Portal, Azure CLI 또는 Azure PowerShell을 사용하여 리소스 그룹 및 해당 리소스를 삭제합니다.

az group delete --name myResourceGroup

메모

리소스 그룹을 삭제하면 키 자격 증명 모음도 삭제되지만, 자격 증명 모음은 일시 삭제 상태가 되어 보존 기간(기본값: 90일) 동안 복구할 수 있는 상태로 유지됩니다. 자격 증명 모음 이름은 해당 기간 동안 전역에서 예약된 상태로 유지되며, 영구 삭제 방지가 사용하도록 설정되어 있으므로 기간이 끝나기 전에 자격 증명 모음을 영구 삭제할 수 없습니다. 표준 키 자격 증명 모음의 경우 일시 삭제된 자격 증명 모음에는 요금이 청구되지 않습니다. 자세한 내용은 Key Vault 일시 삭제 개요 참조하세요.

다음 단계

이 빠른 시작에서는 Bicep 사용하여 키 자격 증명 모음 및 인증서를 만든 다음 배포의 유효성을 검사했습니다. Key Vault 및 Bicep에 대해 자세히 알아보려면 아래 문서를 계속 진행하세요.