繁体   English   中英

如何使用 Terraform 为 Azure 服务主体创建客户端密码

[英]How to create client secret for Azure Service Principal using Terraform

我是 Azure 和 Terraform 的新手,正在尝试使用 Terraform 为 Azure 服务主体创建一个秘密客户端。我无法弄清楚。

这就是我现在所拥有的:

provider "azuread" {
  version = "=0.7.0"
  client_id = var.aws_client_id
  subscription_id = var.aws_subscription_id
  tenant_id = var.aws_tenant_id
  client_secret = var.aws_client_secret
}

# Create an application
resource "azuread_application" "app" {
  name = var.azurerd_app_name
}

# Create a 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
}

这显然是行不通的。 这没有给出任何错误,但是,在 Azure 控制台上看不到任何秘密。 看起来这是为了将一些密码附加到服务主体,但我不太确定它在做什么。

请让我知道对此可以做些什么。 任何帮助,将不胜感激。 谢谢

实际上, azuread_service_principal_password运行良好,但密码没有显示在门户中。

您可以使用azuread_application_password管理与 Azure AD 中的应用程序关联的密码。 请参阅注意,确保应用程序具有提到的权限。

在您的示例中创建的服务原则的客户端密码将起作用。 客户端密码将具有random_string.password.result的值,因为您将其分配给客户端密码azuread_service_principal_password.app.value

如果您想将 output 客户端密码发送到控制台以查看它,您可以创建 terraform output:

output "client_secret" {
  value = random_string.password.result
  sensitive = false # Note that you might not want to print this in out in the console all the time
}

您也可以随时询问 terraform 打印出 state 中的值:

$ terraform state show random_string.password.result

您可以让 Terraform 和 Azure 为您创建密码,然后使用terraform output找回密码。 不过,您可能希望将其标记为敏感

# Create Azure AD App Registration
resource "azuread_application" "app" {
  display_name = "my-app"
}

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

# Create Service Principal password
resource "azuread_service_principal_password" "app" {
  service_principal_id = azuread_service_principal.app.id
}

# Output the Service Principal and password
output "sp" {
  value     = azuread_service_principal.app.id
  sensitive = true
}

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

然后terraform output sp_password将为您获取它,并且您不会在每个plan上将其打印到控制台并apply

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM