【问题标题】:How to use User-Managed Identity from Azure Cloud Function (python) in making a request to Azure KeyVault?如何使用 Azure Cloud Function (python) 中的用户管理身份向 Azure KeyVault 发出请求?
【发布时间】:2019-11-08 11:31:13
【问题描述】:

我当前的设置:

我的 DevOps 人员创建了一个用户托管标识并将其添加到 Azure 密钥保管库的访问策略中。
我创建了一个函数应用 testing-01 并在平台设置下为其分配了 User-ManagedIdentity。
我使用 Python 3.6 作为运行时语言。

这是我用来检查我是否能够从密钥保管库访问机密的辅助方法。 我正在回复它的回应。

def cred_checker():
    credential = ManagedIdentityCredential()
    # credential = ManagedIdentityCredential(client_id='client_id 
    vault_name= "myvault"
    client = SecretClient(vault_url=f"https://{vault_name}.vault.azure.net/", credential=credential)
    username = client.get_secret(name="username")
    password= client.get_secret(name="password")

    return f"AKV client created successfully {client} .<br> name: {username},<br> pass: {password} " 

我能够毫无错误地创建客户端。但是当我尝试从中获取秘密时,我得到了这个 ClientAuthenticationError :

Exception while executing function: Functions.HttpTriggerFunc <--- Result: Failure Exception: ClientAuthenticationError: Unexpected response '{'statusCode': 400, 'message': 'Unable to load requested managed identity.', 'correlationId': '92daf146-fed2-4a75-8359-9r955939815e'}'

【问题讨论】:

    标签: python azure azure-functions azure-managed-identity azure-function-app


    【解决方案1】:

    当您使用用户分配的身份时,您需要在ManagedIdentityCredential()中指定MSI的client_id

    credential = ManagedIdentityCredential(client_id="xxxxxxxx")
    

    要获取 client_id,请导航到您的函数应用 -> Identity -> User assigned -> 单击您的用户分配的身份 -> 复制 client_id

    【讨论】:

    • 我也尝试过这种方法,如果您在我的函数中看到注释(python 注释)。它没有用。
    【解决方案2】:

    以下是在 python 中使用托管身份的方法:

    from msrestazure.azure_active_directory import MSIAuthentication
    creds = MSIAuthentication()
    client = SecretClient(vault_url=MyVaultUrl, credentials=creds)
    

    但是,要使其正常工作,您必须将身份分配给您的 azure 功能作为部署的一部分。如果您使用的是 terraform,则可以使用一个提供程序,它接受一个身份 {} 块,您可以在其中指定 type="UserAssigned" 和 identity_ids = ["id1", "id2",...]

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 2021-01-26
      • 1970-01-01
      • 2021-08-09
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      相关资源
      最近更新 更多