-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add script to generate pypi mfa qr
- Loading branch information
1 parent
7771c96
commit a5b37c7
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
import qrcode | ||
import subprocess | ||
from io import BytesIO | ||
from PIL import Image | ||
from azure.identity import DefaultAzureCredential | ||
from azure.keyvault.secrets import SecretClient | ||
|
||
vault_url = "https://mmlspark-keys.vault.azure.net" | ||
secret_name = "pypi-mfa-uri" | ||
|
||
def retrieve_secret_from_keyvault(vault_url, secret_name): | ||
try: | ||
return SecretClient(vault_url=vault_url, credential=DefaultAzureCredential()).get_secret(secret_name).value | ||
except Exception as e: | ||
print(f"Error: Failed to retrieve the secret from Azure Key Vault. {e}") | ||
return None | ||
|
||
def generate_qr_code(text): | ||
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4) | ||
qr.add_data(text) | ||
qr.make(fit=True) | ||
qr_img = qr.make_image(fill_color="black", back_color="white") | ||
return qr_img | ||
|
||
def main(): | ||
keyvault_secret_value = retrieve_secret_from_keyvault(vault_url, secret_name) | ||
|
||
if keyvault_secret_value is not None: | ||
qr_img = generate_qr_code(keyvault_secret_value) | ||
qr_img.show() | ||
else: | ||
print("Error: Failed to retrieve the secret from Azure Key Vault. Make sure you are logged in with `az login` and have access to the Azure Key Vault.") | ||
|
||
if __name__ == "__main__": | ||
main() |