-
Notifications
You must be signed in to change notification settings - Fork 141
keyvault #720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
m-kovalsky
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
m-kovalsky:m-kovalsky/keyvault
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
keyvault #720
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from ._secrets import ( | ||
get_secret, | ||
set_secret, | ||
delete_secret, | ||
list_secrets, | ||
recover_deleted_secret, | ||
) | ||
|
||
|
||
__all__ = [ | ||
"get_secret", | ||
"set_secret", | ||
"delete_secret", | ||
"list_secrets", | ||
"recover_deleted_secret", | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
from typing import Any | ||
import pandas as pd | ||
from sempy_labs._helper_functions import ( | ||
_base_api, | ||
_create_dataframe, | ||
) | ||
import sempy_labs._icons as icons | ||
from sempy._utils._log import log | ||
|
||
|
||
@log | ||
def get_secret(key_vault_uri: str, secret_name: str) -> Any: | ||
""" | ||
Retrieves the latest version of a secret from the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Get Secret - Get Secret <https://learn.microsoft.com/rest/api/keyvault/secrets/get-secret/get-secret>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
secret_name : str | ||
Name of the secret in the Key Vault. | ||
|
||
Returns | ||
------- | ||
Any | ||
The value of the latest version of the secret. | ||
""" | ||
|
||
response = _base_api( | ||
request=f"{key_vault_uri}/secrets/{secret_name}/versions", client="keyvault" | ||
) | ||
url = response.json().get("value")[-1].get("id") | ||
response = _base_api(request=f"{url}", client="keyvault") | ||
return response.json().get("value") | ||
|
||
|
||
@log | ||
def set_secret(key_vault_uri: str, secret_name: str, secret_value: Any): | ||
""" | ||
Sets a secret in the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Set Secret - Set Secret <https://learn.microsoft.com/rest/api/keyvault/secrets/set-secret/set-secret>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
secret_name : str | ||
Name of the secret to be set in the Key Vault. | ||
secret_value : Any | ||
Value of the secret to be set in the Key Vault. | ||
""" | ||
|
||
payload = {"value": secret_value} | ||
_base_api( | ||
request=f"{key_vault_uri}/secrets/{secret_name}", | ||
client="keyvault", | ||
method="put", | ||
payload=payload, | ||
) | ||
print( | ||
f"{icons.green_dot} The '{secret_name}' secret has been successfully set within the '{key_vault_uri}' Key Vault." | ||
) | ||
|
||
|
||
@log | ||
def list_secrets(key_vault_uri: str) -> pd.DataFrame: | ||
""" | ||
Lists all secrets in the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Get Secrets - Get Secrets <https://learn.microsoft.com/rest/api/keyvault/secrets/get-secrets/get-secrets>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
|
||
Returns | ||
------- | ||
pandas.DataFrame | ||
A pandas DataFrame containing details of all secrets in the Key Vault. | ||
""" | ||
|
||
columns = { | ||
"Secret Id": "str", | ||
"Enabled": "bool", | ||
"Created Date": "datetime", | ||
"Updated Date": "datetime", | ||
"Recovery Level": "str", | ||
"Recoverable Days": "int", | ||
} | ||
|
||
df = _create_dataframe(columns=columns) | ||
|
||
all_items = [] | ||
url = f"{key_vault_uri}/secrets" | ||
while url: | ||
response = _base_api(request=url, client="keyvault") | ||
result = response.json() | ||
items = result.get("value", []) | ||
all_items.extend(items) | ||
url = result.get("nextLink") # Update to next page if it exists | ||
|
||
if not all_items: | ||
return df | ||
|
||
df = pd.json_normalize(all_items) | ||
df.rename( | ||
columns={ | ||
"id": "Secret Id", | ||
"attributes.enabled": "Enabled", | ||
"attributes.created": "Created Date", | ||
"attributes.updated": "Updated Date", | ||
"attributes.recoveryLevel": "Recovery Level", | ||
"attributes.recoverableDays": "Recoverable Days", | ||
}, | ||
inplace=True, | ||
) | ||
df["Created Date"] = pd.to_datetime(df["Created Date"], unit="s") | ||
df["Updated Date"] = pd.to_datetime(df["Updated Date"], unit="s") | ||
return df | ||
|
||
|
||
@log | ||
def list_deleted_secrets(key_vault_uri: str) -> pd.DataFrame: | ||
""" | ||
Lists all deleted secrets in the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Get Deleted Secrets - GetDeleted Secrets <https://learn.microsoft.com/rest/api/keyvault/secrets/get-deleted-secrets/get-deleted-secrets>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
|
||
Returns | ||
------- | ||
pandas.DataFrame | ||
A pandas DataFrame containing details of all secrets in the Key Vault. | ||
""" | ||
|
||
columns = { | ||
"Recovery Id": "str", | ||
"Deleted Date": "datetime", | ||
"Scheduled Purge Date": "datetime", | ||
"Content Type": "str", | ||
"Secret Id": "str", | ||
"Enabled": "bool", | ||
"Created Date": "datetime", | ||
"Updated Date": "datetime", | ||
"Recovery Level": "str", | ||
} | ||
|
||
df = _create_dataframe(columns=columns) | ||
|
||
all_items = [] | ||
url = f"{key_vault_uri}/deletedSecrets" | ||
while url: | ||
response = _base_api(request=url, client="keyvault") | ||
result = response.json() | ||
items = result.get("value", []) | ||
all_items.extend(items) | ||
url = result.get("nextLink") # Update to next page if it exists | ||
|
||
if not all_items: | ||
return df | ||
|
||
df = pd.json_normalize(all_items) | ||
df.rename( | ||
columns={ | ||
"recoveryId": "Recovery Id", | ||
"deletedDate": "Deleted Date", | ||
"scheduledPurgeDate": "Scheduled Purge Date", | ||
"contentType": "Content Type", | ||
"id": "Secret Id", | ||
"attributes.enabled": "Enabled", | ||
"attributes.created": "Created Date", | ||
"attributes.updated": "Updated Date", | ||
"attributes.recoveryLevel": "Recovery Level", | ||
}, | ||
inplace=True, | ||
) | ||
df["Scheduled Purge Date"] = pd.to_datetime(df["Scheduled Purge Date"], unit="s") | ||
df["Deleted Date"] = pd.to_datetime(df["Deleted Date"], unit="s") | ||
df["Created Date"] = pd.to_datetime(df["Created Date"], unit="s") | ||
df["Updated Date"] = pd.to_datetime(df["Updated Date"], unit="s") | ||
|
||
return df | ||
|
||
|
||
@log | ||
def recover_deleted_secret(key_vault_uri: str, secret_name: str): | ||
""" | ||
Recovers a deleted secret in the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Recover Deleted Secret - Recover Deleted Secret <https://learn.microsoft.com/rest/api/keyvault/secrets/recover-deleted-secret/recover-deleted-secret>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
secret_name : str | ||
Name of the deleted secret to be recovered in the Key Vault. | ||
""" | ||
|
||
_base_api( | ||
request=f"{key_vault_uri}/deletedsecrets/{secret_name}/recover", | ||
client="keyvault", | ||
method="post", | ||
) | ||
print( | ||
f"{icons.green_dot} The '{secret_name}' secret has been successfully recovered within the '{key_vault_uri}' Key Vault." | ||
|
||
) | ||
|
||
|
||
@log | ||
def delete_secret(key_vault_uri: str, secret_name: str): | ||
""" | ||
Deletes a secret in the specified Azure Key Vault. | ||
|
||
This is a wrapper function for the following API: `Delete Secret - Delete Secret <https://learn.microsoft.com/rest/api/keyvault/secrets/delete-secret/delete-secret>`_. | ||
|
||
Parameters | ||
---------- | ||
key_vault_uri : str | ||
Azure Key Vault URI. | ||
secret_name : str | ||
Name of the secret to be deleted in the Key Vault. | ||
""" | ||
|
||
_base_api( | ||
request=f"{key_vault_uri}/secrets/{secret_name}", | ||
client="keyvault", | ||
method="delete", | ||
) | ||
print( | ||
f"{icons.green_dot} The '{secret_name}' secret has been successfully deleted within the '{key_vault_uri}' Key Vault." | ||
|
||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.