gpt4 book ai didi

azure-pipelines - "Could not retrieve credential from local cache for service principal"在 Azure Devops 托管代理上的 Python SDK 中使用 Azure CLI 凭据时

转载 作者:行者123 更新时间:2023-12-04 13:50:31 30 4
gpt4 key购买 nike

我在 上的 Azure 管道自托管代理 我用这个任务

      - task: AzureCLI@2
displayName: Azure CLI task with Python SDK
inputs:
azureSubscription: 'SUBSCRIPTION-SERVICE-CONNECTION'
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
python ./magic-script.py

这样我就可以使用凭据来验证 Azure Python SDK :
client = get_client_from_cli_profile(GraphRbacManagementClient)
当我将此过程转移到 MS 托管代理 我收到此错误:
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/common/client_factory.py", line 85, in get_client_from_cli_profile
with_tenant=True,
File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/common/credentials.py", line 98, in get_azure_cli_credentials
cred, subscription_id, tenant_id = profile.get_login_credentials(resource=resource)
File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/cli/core/_profile.py", line 335, in get_login_credentials
credential = self._create_credential(account, client_id=client_id)
File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/cli/core/_profile.py", line 592, in _create_credential
return identity.get_service_principal_credential(username_or_sp_id)
File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/cli/core/auth/identity.py", line 185, in get_service_principal_credential
entry = self._msal_secret_store.load_entry(client_id, self.tenant_id)
File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/azure/cli/core/auth/identity.py", line 270, in load_entry
.format(sp_id))
knack.util.CLIError: Could not retrieve credential from local cache for service principal ***. Run `az login` for this service principal.

我也试过 AzureCliCredential喜欢
credential = AzureCliCredential()
client = GraphRbacManagementClient(credential, os.environ["subscriptionId"])
这是我登录的,但在使用 GraphClient 时我在我的开发箱和代理上本地收到此错误:
    root_group = [g for g in graph_client.groups.list(
File "C:\Python38\lib\site-packages\msrest\paging.py", line 143, in __next__
self.advance_page()
File "C:\Python38\lib\site-packages\msrest\paging.py", line 129, in advance_page
self._response = self._get_next(self.next_link)
File "C:\Python38\lib\site-packages\azure\graphrbac\operations\groups_operations.py", line 336, in internal_paging
response = self._client.send(request, stream=False, **operation_config)
File "C:\Python38\lib\site-packages\msrest\service_client.py", line 336, in send
pipeline_response = self.config.pipeline.run(request, **kwargs)
File "C:\Python38\lib\site-packages\msrest\pipeline\__init__.py", line 197, in run
return first_node.send(pipeline_request, **kwargs) # type: ignore
File "C:\Python38\lib\site-packages\msrest\pipeline\__init__.py", line 150, in send
response = self.next.send(request, **kwargs)
File "C:\Python38\lib\site-packages\msrest\pipeline\requests.py", line 65, in send
self._creds.signed_session(session)
AttributeError: 'AzureCliCredential' object has no attribute 'signed_session'

最佳答案

解决 issue这是由版本 引起的2.30.0 MS 托管代理 我必须设置 addSpnToEnvironment: true :

      - task: AzureCLI@2
displayName: Azure CLI task with Python SDK
inputs:
azureSubscription: 'SUBSCRIPTION-SERVICE-CONNECTION'
scriptType: bash
scriptLocation: inlineScript
addSpnToEnvironment: true
inlineScript: |
export subscriptionId=$(az account show --query id -o tsv)
python ./magic-script.py

能够访问 Azure CLI 身份验证 secret 并选择另一种生成凭据的方法:
    if "tenantId" in os.environ:
print('using environment variables')
config_dict = {
"clientId": os.environ["servicePrincipalId"],
"clientSecret": os.environ["servicePrincipalKey"],
"subscriptionId": os.environ["subscriptionId"],
"tenantId": os.environ["tenantId"],
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"managementEndpointUrl": "https://management.core.windows.net/"
}
client = get_client_from_json_dict(
GraphRbacManagementClient, config_dict)
else:
print('using CLI credentials')
client = get_client_from_cli_profile(GraphRbacManagementClient)

关于azure-pipelines - "Could not retrieve credential from local cache for service principal"在 Azure Devops 托管代理上的 Python SDK 中使用 Azure CLI 凭据时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69895247/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com