Terraform 的服务主体创建未在输出中提供 password/secret

Service Principal Creation by Terraform doesn't provide password/secret in the output

在 Azure manually 中生成服务主体时,作为操作的结果,我获得了密码。

情况并非如此,但是如果我使用 Terraform 创建服务主体,则密码不在该模块的输出中:

  + azuread_service_principal.k8s_principal
      id:                <computed>
      application_id:    "${azuread_application.app.application_id}"
      display_name:      <computed>

有什么我遗漏的吗?与 CLI 相比,为什么 Terraform 的输出行为不同?

在 terraform 文档中,azuread_service_principal block only defines the Argument application_id and Attributes id, display_name, So you only could see these resources. Also, the azuread_service_principal_password 块允许您导出服务主体密码的密钥 ID。还是看不到真实密码

在 Azure CLI 中 az ad sp create-for-rbac 有一个可选参数 --Password。所以你可以看到密码输出。

password 需要输入到 azuread_service_principal_password 块。因此,您可以生成一个随机密码并自行导出。完整的 Terraform 代码是这样的:

resource "azuread_application" "app" {
  name = "${local.application_name}"
}

# Create Service Principal
resource "azuread_service_principal" "app" {
  application_id = "${azuread_application.app.application_id}"
}

resource "random_string" "password" {
  length  = 32
  special = true
}

# Create Service Principal password
resource "azuread_service_principal_password" "app" {
  end_date             = "2299-12-30T23:00:00Z"                        # Forever
  service_principal_id = "${azuread_service_principal.app.id}"
  value                = "${random_string.password.result}"
}

output "sp_password" {
  value = "${azuread_service_principal_password.app.value}"
  sensitive = true
}