Adding an example for instantiating a ServicePrincipal credential. Adding an example of listing and reading keys+secrets.
@@ -11,7 +11,7 @@ The following code creates an instance of the client.
See :doc:`Resource Management Authentication <quickstart_authentication>`
for details on handling Azure Active Directory authentication with the Python SDK, and creating a ``Credentials`` instance.
-.. important:: You must specific `resource="https://vault.azure.net"` while authenticating to get a valid token
+.. important:: You must specify `resource="https://vault.azure.net"` while authenticating to get a valid token
.. code:: python
@@ -22,7 +22,24 @@ for details on handling Azure Active Directory authentication with the Python SD
credentials = UserPassCredentials(
'user@domain.com', # Your user
'my_password', # Your password
- resource="https://vault.azure.net"
+ resource='https://vault.azure.net'
+ )
+
+ client = KeyVaultClient(
+ credentials
+ )
+
+.. code:: python
+
+ from azure.keyvault import KeyVaultClient
+ from azure.common.credentials import ServicePrincipalCredentials
+
+ # See above for details on creating different types of AAD credentials
+ credentials = ServicePrincipalCredentials(
+ 'client_id', # Client id of the service
+ 'client_key', # Client key/password set while creating the service principal
+ tenant='00000000-0000-0000-0000-000000000000', # Your tenant id
+ resource='https://vault.azure.net'
)
client = KeyVaultClient(
@@ -40,8 +57,11 @@ to this credentials using the Azure Portal, the Azure CLI or the :doc:`Key Vault
Example
-------
+``KEY_VAULT_URI`` is the base url of your keyvault. Eg. `https://myvault.vault.azure.net`
+
.. code:: python
+
# Create a key
key_bundle = client.create_key(KEY_VAULT_URI, 'FirstKey', 'RSA')
key_id = key_vault_id.parse_key_id(key_bundle.key.kid)
@@ -52,9 +72,21 @@ Example
# Update a key with version
client.update_key(key_id.id, key_attributes={'enabled': False})
- # Delete my key
+ # Print a list of versions for a key
+ versions = client.get_key_versions(KEY_VAULT_URI, 'FirstKey')
+ for version in versions:
+ print version.kid # https://myvault.vault.azure.net/keys/FirstKey/000102030405060708090a0b0c0d0e0f
+
+ # Read a key without version
+ client.get_key(key_id.base_id)
+
+ # Read a key with version
+ client.get_key(key_id.id)
+
+ # Delete a key
client.delete_key(KEY_VAULT_URI, 'FirstKey')
+
# Create a secret
secret_bundle = client.set_secret(KEY_VAULT_URI, 'FirstSecret', 'Hush, that is secret!!')
secret_id = key_vault_id.parse_secret_id(secret_bundle.id)
@@ -65,5 +97,16 @@ Example
# Update a secret with version
client.update_key(secret_id.id, secret_attributes={'enabled': False})
- # Delete my secret
+ # Print a list of versions for a secret
+ versions = client.get_secret_versions(KEY_VAULT_URI, 'FirstSecret')
+ for version in versions:
+ print version.id # https://myvault.vault.azure.net/secrets/FirstSecret/000102030405060708090a0b0c0d0e0f
+
+ # Read a secret without version
+ client.get_secret(secret_id.base_id)
+
+ # Read a secret with version
+ client.get_secret(secret_id.id)
+
+ # Delete a secret
client.delete_secret(KEY_VAULT_URI, 'FirstSecret')
