Terraform Init 使用企业应用程序 ID 而不是服务主体客户端 ID

问题描述 投票:0回答:1

我正在尝试运行

terraform init
命令,但它的行为很奇怪。

这是我正在运行的命令:

terraform init `
    -backend-config="resource_group_name=testrg" `
    -backend-config="storage_account_name=testsa" `
    -backend-config="container_name=tests" `
    -backend-config="key=test.tfstate" `
    -backend-config="subscription_id=<subscription_id>" `
    -backend-config="client_id=<client_id>" `
    -backend-config="client_secret=<client_secret>" `
    -backend-config="tenant_id=<tenant_id>" `
    -backend-config="use_oidc=true"

问题是,当我在 DevOps 代理上运行此命令时,出现以下错误:

Initializing the backend...
╷
│ Error: Failed to get existing workspaces: Error retrieving keys for Storage Account "testsa": storage.AccountsClient#ListKeys: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error. Status=403 Code="AuthorizationFailed" Message="The client '<enterprise_application_id>' with object id '<enterprise_application_id>' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over scope '/subscriptions/<subscription_id>/resourceGroups/testrg/providers/Microsoft.Storage/storageAccounts/testsa' or the scope is invalid. If access was recently granted, please refresh your credentials."
╵

客户端 ID 不同,因为出于某种原因,Terraform 使用

enterprise application
中的 ID,而不是配置中提供的
service principal
的客户端 ID。

为什么会出现这种情况?我的服务主体配置是否不正确,或者我应该在

terraform init
命令中添加一些内容以使用服务主体中的客户端 ID?当我在本地运行此命令时,一切正常。

我也可以将角色分配给

enterprise application
,但当我转到 IAM 时,我在 UI 中只看到应用程序注册。

感谢您的帮助!

编辑:

当我在企业应用程序级别禁用 SSO 时: enter image description here

terraform init
挂起,当我尝试在本地计算机上运行
az login --service-principal
时,出现错误:

AADSTS7000112:应用程序“”(appreg_name) 已禁用。跟踪 ID:guid1 相关 ID:guid2 时间戳:2024-05-14 09:22:44Z

azure azure-devops terraform azure-pipelines
1个回答
0
投票

我认为您的服务主体无法对远程状态存储帐户进行身份验证。

根据 azurerm 后端文档,azurerm 后端支持 3 种对存储帐户进行身份验证的方法:

  • 访问密钥(默认)
  • Azure 活动目录
  • SAS 令牌

访问密钥

可以通过

access_key
属性或
ARM_ACCESS_KEY
环境变量(推荐)设置访问密钥:

terraform init -backend-config="access_key=xxxx"

Azure Active Directory 身份验证

使用 Azure Active Directory 身份验证时,必须确保将

Storage Blob Data Owner
Container Blob Data Owner
角色分配给您的存储帐户。

SAS 令牌

SAS Token方式只能直接使用。您必须为状态文件 blob 生成 SAS 令牌并将其传递到后端配置。

© www.soinside.com 2019 - 2024. All rights reserved.