如何使用 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
,这是客户端密码。
如果您想将客户端密码输出到控制台进行查看,您可以创建一个 terraform 输出:
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 从其状态中打印出值:
$ terraform state show random_string.password.result
您可以让 Terraform 和 Azure 为您创建密码,然后使用 terraform output
检索它。不过,您可能希望将其标记为 sensitive。
# 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
.
上将它打印到控制台
我是 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
,这是客户端密码。
如果您想将客户端密码输出到控制台进行查看,您可以创建一个 terraform 输出:
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 从其状态中打印出值:
$ terraform state show random_string.password.result
您可以让 Terraform 和 Azure 为您创建密码,然后使用 terraform output
检索它。不过,您可能希望将其标记为 sensitive。
# 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
.