Skip to content
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

chore: Add script to generate pypi mfa qr #2150

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tools/pypi/generate-pypi-mfa-qr.py
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()