如何通过Python SDK列出Azure订阅下的所有SSH密钥?以及如何基于client_id、client_secret和tenant_id创建NewEnvironmentCredential?
Hey there! Let's walk through how to replicate your Go functionality in Python, step by step. First, we'll cover setting up the credentials correctly, then we'll write code to list all SSH keys across your Azure subscription.
You mentioned wanting to create a NewEnvironmentCredential equivalent in Python, so let's break down two practical approaches based on how you prefer to pass your credentials:
Option 1: Use EnvironmentCredential (matches your Go approach)
This credential reads values directly from environment variables, just like your Go implementation. First, set these variables in your shell:
export AZURE_CLIENT_ID="your-client-id-here" export AZURE_CLIENT_SECRET="your-client-secret-here" export AZURE_TENANT_ID="your-tenant-id-here" export AZURE_SUBSCRIPTION_ID="your-subscription-id-here"
Then initialize the credential and compute client in Python:
from azure.identity import EnvironmentCredential from azure.mgmt.compute import ComputeManagementClient credential = EnvironmentCredential() compute_client = ComputeManagementClient(credential, subscription_id="your-subscription-id-here")
Option 2: Directly Pass Credentials with ClientSecretCredential
If you don't want to rely on environment variables and prefer to explicitly pass client_id, client_secret, and tenant_id, use ClientSecretCredential instead:
from azure.identity import ClientSecretCredential from azure.mgmt.compute import ComputeManagementClient # Replace with your actual values tenant_id = "your-tenant-id-here" client_id = "your-client-id-here" client_secret = "your-client-secret-here" subscription_id = "your-subscription-id-here" credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret) compute_client = ComputeManagementClient(credential, subscription_id)
Your Go code targets a single resource group, but if you want to list all SSH keys across the entire subscription, we'll need to:
- Fetch all resource groups in the subscription
- For each resource group, retrieve its SSH public keys
Here's a complete working example that mirrors your Go logic but scales to the full subscription:
from azure.identity import ClientSecretCredential # Swap for EnvironmentCredential if using env vars from azure.mgmt.compute import ComputeManagementClient from azure.mgmt.resource import ResourceManagementClient def fetch_all_ssh_keys(): # Configure your credentials and subscription tenant_id = "your-tenant-id-here" client_id = "your-client-id-here" client_secret = "your-client-secret-here" subscription_id = "your-subscription-id-here" # Initialize credentials and clients credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret) resource_client = ResourceManagementClient(credential, subscription_id) compute_client = ComputeManagementClient(credential, subscription_id) all_ssh_keys = [] # Iterate over all resource groups for rg in resource_client.resource_groups.list(): rg_name = rg.name print(f"Scanning resource group: {rg_name}") # Get all SSH keys in the current resource group ssh_key_iterator = compute_client.ssh_public_keys.list_by_resource_group(rg_name) for key in ssh_key_iterator: # Collect key details (matches your KeyPairDetail struct) all_ssh_keys.append({ "key_name": key.name, "key_value": key.public_key, "resource_group": rg_name, "location": key.location }) return all_ssh_keys # Run the function and print results if __name__ == "__main__": ssh_keys = fetch_all_ssh_keys() for i, key in enumerate(ssh_keys, 1): print(f"\n--- SSH Key {i} ---") print(f"Name: {key['key_name']}") print(f"Resource Group: {key['resource_group']}") print(f"Public Key:\n{key['key_value']}")
Don't forget to install the necessary Python packages first:
pip install azure-identity azure-mgmt-compute azure-mgmt-resource
A quick note on differences from your Go code: The Python Azure SDK handles pagination automatically for you—you don't need to manually loop through pages like with pager.NextPage(ctx) in Go. The list_by_resource_group() method returns an iterator that fetches all pages behind the scenes.
内容的提问来源于stack exchange,提问作者Shah Zain

