Aracılığıyla paylaş


Erişim kimlik bilgilerini Azure Veri Bilimi Sanal Makinesi güvenli bir şekilde depolama

Bulut uygulama kodu genellikle bulut hizmetlerinde kimlik doğrulaması için kimlik bilgileri içerir. Bulut uygulamaları oluştururken bu kimlik bilgilerinin yönetimi ve güvenliği iyi bilinen bir zorluk. İdeal olan, kimlik bilgilerinin hiçbir zaman geliştirici iş istasyonlarında görünmemesidir. Kimlik bilgilerini asla kaynak denetimine iade etmemeliyiz.

Azure kaynakları için yönetilen kimlikler özelliği sorunu çözmeye yardımcı olur. Azure hizmetlerine Microsoft Entra Id'de otomatik olarak yönetilen bir kimlik sağlar. Microsoft Entra kimlik doğrulamasını destekleyen herhangi bir hizmette kimlik doğrulaması yapmak için bu kimliği kullanabilirsiniz. Ayrıca bu kimlik, kodunuzda eklenmiş kimlik bilgilerinin yerleştirilmesini önler.

Kimlik bilgilerinin güvenliğini sağlamak için Windows Installer'ı (MSI) Azure Key Vault ile birlikte kullanın. Azure Key Vault, gizli dizileri ve şifreleme anahtarlarını güvenli bir şekilde depolayan yönetilen bir Azure hizmetidir. Yönetilen kimliği kullanarak bir anahtar kasasına erişebilir ve ardından anahtar kasasından yetkili gizli dizileri ve şifreleme anahtarlarını alabilirsiniz.

Azure kaynakları için Key Vault ve yönetilen kimlikler hakkındaki belgeler, bu hizmetler hakkında ayrıntılı bilgi için kapsamlı bir kaynak oluşturur. Bu makalede, Azure kaynaklarına erişmek için Veri Bilimi Sanal Makinesi (DSVM) üzerinde MSI ve Key Vault'un temel kullanımı açıklanmaktadır.

DSVM'de yönetilen kimlik oluşturma

# 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 sorumlusuna Key Vault erişim izinleri atama

# 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'den anahtar kasasında gizli diziye erişme

# 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'den depolama anahtarlarına erişme

# 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'dan anahtar kasasına erişme

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'dan anahtar kasasına erişme

# 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>