Librerie di Azure Key Vault per PythonAzure Key Vault libraries for Python
Azure Key Vault è il sistema di archiviazione e gestione dei segreti, dei certificati e delle chiavi di crittografia di Azure.Azure Key Vault is Azure's storage and management system for cryptographic keys, secrets, and certificate management. L'API di Python SDK per Key Vault è suddivisa tra le librerie client e le librerie di gestione.The Python SDK API for Key Vault is split between client libraries and management libraries.
Usare la libreria client per:Use the client library to:
- Accedere, aggiornare o eliminare elementi archiviati in un Azure Key VaultAccess, update, or delete items stored in an Azure Key Vault
- Ottenere i metadati per i certificati archiviatiGet metadata for stored certificates
- Verificare le firme in base alle chiavi simmetriche in Key VaultVerify signatures against symmetric keys in Key Vault
Usare la libreria di gestione per:Use the management library to:
- Creare, aggiornare o eliminare nuovi archivi Key VaultCreate, update, or delete new Key Vault stores
- Controllare i criteri di accesso all'insieme di credenzialiControl vault access policies
- Elencare gli insiemi di credenziali per sottoscrizione o gruppo di risorseList vaults by subscription or resource group
- Controllare la disponibilità dei nomi di insiemi di credenzialiCheck for vault name availability
Installare le librerieInstall the libraries
Libreria clientClient library
pip install azure-keyvault
EsempiExamples
Negli esempi seguenti si usa l'autenticazione basata su entità servizio, che costituisce il metodo di accesso consigliato per le applicazioni che si connettono ad Azure.The following examples use service principal authentication, which is the recommended sign in method for applications that connect to Azure. Per informazioni sull'autenticazione basata su entità servizio, vedere Eseguire l'autenticazione con le librerie di gestione di Azure per PythonTo learn about service principal authentication, see Authenticate with the Azure SDK for Python
Recuperare la parte pubblica di una chiave asimmetrica da un insieme di credenziali:Retrieve the public portion of an asymmetric key from a vault:
from azure.keyvault import KeyVaultClient
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id = '...',
secret = '...',
tenant = '...'
)
client = KeyVaultClient(credentials)
# VAULT_URL must be in the format 'https://<vaultname>.vault.azure.net'
# KEY_VERSION is required, and can be obtained with the KeyVaultClient.get_key_versions(self, vault_url, key_name) API
key_bundle = client.get_key(VAULT_URL, KEY_NAME, KEY_VERSION)
key = key_bundle.key
Recuperare un segreto da un insieme di credenziali:Retrieve a secret from a vault:
from azure.keyvault import KeyVaultClient
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id = '...',
secret = '...',
tenant = '...'
)
client = KeyVaultClient(credentials)
# VAULT_URL must be in the format 'https://<vaultname>.vault.azure.net'
# SECRET_VERSION is required, and can be obtained with the KeyVaultClient.get_secret_versions(self, vault_url, secret_id) API
secret_bundle = client.get_secret(VAULT_URL, SECRET_ID, SECRET_VERSION)
secret = secret_bundle.value
Libreria di gestioneManagement library
pip install azure-mgmt-keyvault
EsempioExample
Il seguente esempio mostra come creare un'istanza di Azure Key Vault:The following example shows how to create an Azure Key Vault.
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id = '...',
secret = '...',
tenant = '...'
)
# Even when using service principal credentials, a subscription ID is required. For service principals,
# this should be the subscription used to create the service principal. Storing a token like a valid
# subscription ID in code is not recommended and only shown here for example purposes.
SUBSCRIPTION_ID = '...'
client = KeyVaultManagementClient(credentials, SUBSCRIPTION_ID)
# The object ID and organization ID (tenant) of the user, application, or service principal for access policies.
# These values can be found through the Azure CLI or the Portal.
ALLOW_OBJECT_ID = '...'
ALLOW_TENANT_ID = '...'
RESOURCE_GROUP = '...'
VAULT_NAME = '...'
# Vault properties may also be created by using the azure.mgmt.keyvault.models.VaultCreateOrUpdateParameters
# class, rather than a map.
operation = client.vaults.create_or_update(
RESOURCE_GROUP,
VAULT_NAME,
{
'location': 'eastus',
'properties': {
'sku': {
'name': 'standard'
},
'tenant_id': TENANT_ID,
'access_policies': [{
'object_id': OBJECT_ID,
'tenant_id': ALLOW_TENANT_ID,
'permissions': {
'keys': ['all'],
'secrets': ['all']
}
}]
}
}
)
vault = operation.result()
print(f'New vault URI: {vault.properties.vault_uri}')
EsempiSamples
- Gestire più Azure Key VaultManage Azure Key Vaults
- Ripristino di Azure Key VaultAzure Key Vault recovery
Visualizzare l'elenco completo degli esempi di codice per Azure Key Vault.View the complete list of Azure Key Vault samples.