Share via


Azure Data Science Virtual Machine에서 액세스 자격 증명을 안전하게 저장

클라우드 애플리케이션 코드에는 클라우드 서비스에 인증하기 위한 자격 증명이 포함되는 경우가 많습니다. 이러한 자격 증명의 관리 및 보안은 클라우드 애플리케이션을 빌드할 때 잘 알려진 과제입니다. 이상적으로는 자격 증명이 개발자 워크스테이션에 표시되어서는 안 됩니다. 소스 제어에 자격 증명을 체크 인해서는 안 됩니다.

Azure 리소스에 대한 관리 ID 기능은 문제를 해결하는 데 도움이 됩니다. Microsoft Entra ID에서 Azure 서비스에 자동으로 관리 ID를 제공합니다. 이 ID를 사용하여 Microsoft Entra 인증을 지원하는 모든 서비스에 인증할 수 있습니다. 또한 이 ID는 코드에 포함된 자격 증명이 배치되는 것을 방지합니다.

자격 증명을 보호하려면 Windows Installer(MSI)를 Azure Key Vault와 함께 사용합니다. Azure Key Vault는 비밀과 암호화 키를 안전하게 저장하는 관리 Azure 서비스입니다. 관리 ID를 사용하여 키 자격 증명 모음에 액세스하고 키 자격 증명 모음에서 권한이 부여된 비밀 및 암호화 키를 검색할 수 있습니다.

Key Vault 및 Azure 리소스용 관리 ID에 대한 설명서는 이러한 서비스에 대한 심층적인 정보를 제공하는 포괄적인 리소스로 구성되어 있습니다. 이 아티클은 Azure 리소스에 액세스하기 위해 DSVM(Data Science Virtual Machine)에서 MSI 및 Key Vault의 기본적인 사용을 설명합니다.

DSVM에서 관리 ID 만들기

# Prerequisite: You already created a Data Science VM in the usual way.

# Create an identity principal for the VM.
az vm assign-identity -g <Resource Group Name> -n <Name of the VM>
# Get the principal ID of the DSVM.
az resource list -n <Name of the VM> --query [*].identity.principalId --out tsv

VM 보안 주체에 Key Vault 액세스 권한 할당

# Prerequisite: You already created an empty Key Vault resource on Azure through use of the Azure portal or Azure CLI.

# Assign only get and set permissions but not the capability to list the keys.
az keyvault set-policy --object-id <Principal ID of the DSVM from previous step> --name <Key Vault Name> -g <Resource Group of Key Vault>  --secret-permissions get set

DSVM에서 키 자격 증명 모음의 암호에 액세스

# Get the access token for the VM.
x=`curl http://localhost:50342/oauth2/token --data "resource=https://vault.azure.net" -H Metadata:true`
token=`echo $x | python -c "import sys, json; print(json.load(sys.stdin)['access_token'])"`

# Access the key vault by using the access token.
curl https://<Vault Name>.vault.azure.net/secrets/SQLPasswd?api-version=2016-10-01 -H "Authorization: Bearer $token"

DSVM에서 스토리지 키에 액세스

# Prerequisite: You granted your VMs MSI access to use storage account access keys, based on instructions at https://learn.microsoft.com/azure/active-directory/managed-service-identity/tutorial-linux-vm-access-storage. This article describes the process in more detail.

y=`curl http://localhost:50342/oauth2/token --data "resource=https://management.azure.com/" -H Metadata:true`
ytoken=`echo $y | python -c "import sys, json; print(json.load(sys.stdin)['access_token'])"`
curl https://management.azure.com/subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroup of Storage account>/providers/Microsoft.Storage/storageAccounts/<Storage Account Name>/listKeys?api-version=2016-12-01 --request POST -d "" -H "Authorization: Bearer $ytoken"

# Now you can access the data in the storage account from the retrieved storage account keys.

Python에서 키 자격 증명 모음에 액세스

from azure.keyvault import KeyVaultClient
from msrestazure.azure_active_directory import MSIAuthentication

"""MSI Authentication example."""

# Get credentials.
credentials = MSIAuthentication(
    resource='https://vault.azure.net'
)

# Create a Key Vault client.
key_vault_client = KeyVaultClient(
    credentials
)

key_vault_uri = "https://<key Vault Name>.vault.azure.net/"

secret = key_vault_client.get_secret(
    key_vault_uri,  # Your key vault URL.
    # The name of your secret that already exists in the key vault.
    "SQLPasswd",
    ""              # The version of the secret; empty string for latest.
)
print("My secret value is {}".format(secret.value))

Azure CLI에서 키 자격 증명 모음에 액세스

# With managed identities for Azure resources set up on the DSVM, users on the DSVM can use Azure CLI to perform the authorized functions. The following commands enable access to the key vault from Azure CLI, without a required Azure account login.
# Prerequisites: MSI is already set up on the DSVM, as indicated earlier. Specific permissions, like accessing storage account keys, reading specific secrets, and writing new secrets, are provided to the MSI.

# Authenticate to Azure CLI without a required Azure account. 
az login --msi

# Retrieve a secret from the key vault. 
az keyvault secret show --vault-name <Vault Name> --name SQLPasswd

# Create a new secret in the key vault.
az keyvault secret set --name MySecret --vault-name <Vault Name> --value "Helloworld"

# List access keys for the storage account.
az storage account keys list -g <Storage Account Resource Group> -n <Storage Account Name>