使用 Terraform 更新服务主体密码
Updating Service Principle Password with Terraform
根据密码到期时间使用 Terraform 更新服务原则密码
第一次使用密码设置服务原则非常有效,但是,我想让密码过期,如果密码即将过期,则会生成一个新密码并用它更新服务原则,我'我不完全确定如何在 Terraform 中执行条件,因为我对 Terraform 还很陌生,文档并没有真正谈论更新服务原则只是创建它并且当它即将过期时没有数据对象要获取
到目前为止我有这个(完全公开这是我正在帮助的一个更大的地形基础的一部分):
resource "azuread_application" "current" {
name = "test"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
由于密码仅在 90 天内有效,我想 运行 terraform 在它过期之前应用并更新密码
更新 1:
看来,如果确实更改了 azuread_service_principal_password
资源,它会被视为依赖树中的更改并重新创建您附加服务原则的资源,这意味着无法保留如果需要更新,请在 Terraform 中说明服务原则凭据
更新 2:
我已尝试执行以下操作,但缺点是每次您 运行 terraform 应用时都会 运行s:
terraform 脚本:
resource "azuread_application" "current" {
name = "${var.metadata_name}"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
resource "null_resource" "password_updater" {
# Updates everytime you run terraform apply so it will run this script everytime
triggers {
timestamp = "${timestamp()}"
}
provisioner "local-exec" {
command = "sh ${path.module}/update_service_password.sh ${azuread_service_principal.current.id} ${var.resource_group} ${azurerm_kubernetes_cluster.current.name}"
}
}
脚本:
#!/bin/sh
service_principle_id=
resource_group=
cluster_name=
# get service password expiration
expiration=$(az ad sp list --filter="objectId eq '$service_principle_id'" | jq '.[].passwordCredentials' | jq '.[].endDate' | cut -d'T' -f 1 | cut -d'"' -f 2)
# Format date for condition
now=$(date +%Y%m%d%H%M%S)
expiration_date=$(date -d "$expiration - 30 days" +%Y%m%d%H%M%S)
# Compare today with expiration date
if [ ${now} -ge ${expiration_date} ];
then
# IF expiration date in the next 30 days rest password
sp_id=$(az aks show -g ${resource_group} -n ${cluster_name} --query servicePrincipalProfile.clientId -o tsv)
service_principle_secret=$(az ad sp credential reset --name ${sp_id} --end-date $(date -d "+ 90 days" +%Y-%m-%d) --query password -o tsv)
# Update cluster with new password
az aks update-credentials \
--resource-group ${resource_group} \
--name ${cluster_name} \
--reset-service-principal \
--service-principal ${sp_id} \
--client-secret ${service_principle_secret}
fi
对于服务主体,可以通过 Azure CLI 重置其密码az ad sp reset
,但您需要有权限才能这样做。
在与服务原则 terraform 模块的开发人员交谈后,他们告诉我不可能有任何其他方法,如果找到更好的方法,请发表评论:
答案:
使用 null_resource 提供程序 运行 一个 运行 更新的脚本 -
resource "azuread_application" "current" {
name = "${var.metadata_name}"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
resource "null_resource" "password_updater" {
# Updates everytime you run terraform apply so it will run this script everytime
triggers {
timestamp = "${timestamp()}"
}
provisioner "local-exec" {
command = "sh ${path.module}/update_service_password.sh ${azuread_service_principal.current.id} ${var.resource_group} ${azurerm_kubernetes_cluster.current.name}"
}
}
脚本:
#!/bin/sh
service_principle_id=
resource_group=
cluster_name=
# get service password expiration
expiration=$(az ad sp list --filter="objectId eq '$service_principle_id'" | jq '.[].passwordCredentials' | jq '.[].endDate' | cut -d'T' -f 1 | cut -d'"' -f 2)
# Format date for condition
now=$(date +%Y%m%d%H%M%S)
expiration_date=$(date -d "$expiration - 30 days" +%Y%m%d%H%M%S)
# Compare today with expiration date
if [ ${now} -ge ${expiration_date} ];
then
# IF expiration date in the next 30 days rest password
sp_id=$(az aks show -g ${resource_group} -n ${cluster_name} --query servicePrincipalProfile.clientId -o tsv)
service_principle_secret=$(az ad sp credential reset --name ${sp_id} --end-date $(date -d "+ 90 days" +%Y-%m-%d) --query password -o tsv)
# Update cluster with new password
az aks update-credentials \
--resource-group ${resource_group} \
--name ${cluster_name} \
--reset-service-principal \
--service-principal ${sp_id} \
--client-secret ${service_principle_secret}
fi
我认为更好的方法是:
- 您的 Terraform 代码很可能包含在更大的进程中。您很可能使用 bash 来启动该过程,然后进行地形改造。如果没有 - 我建议你这样做,因为这是 terraform 的最佳实践。
- 例如,在 运行 terraform 之前的 bash 代码中,使用 az cli 检查相关服务主体的到期时间。 (无所谓)
- 如果已过期,请使用 terraform taint 命令将服务主体密码资源标记为已污染。我没有详细信息,也许您也需要污染服务主体。也许不是。
- 如果被污染,terraform 将重新创建资源并重新生成密码。
根据密码到期时间使用 Terraform 更新服务原则密码
第一次使用密码设置服务原则非常有效,但是,我想让密码过期,如果密码即将过期,则会生成一个新密码并用它更新服务原则,我'我不完全确定如何在 Terraform 中执行条件,因为我对 Terraform 还很陌生,文档并没有真正谈论更新服务原则只是创建它并且当它即将过期时没有数据对象要获取
到目前为止我有这个(完全公开这是我正在帮助的一个更大的地形基础的一部分):
resource "azuread_application" "current" {
name = "test"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
由于密码仅在 90 天内有效,我想 运行 terraform 在它过期之前应用并更新密码
更新 1:
看来,如果确实更改了 azuread_service_principal_password
资源,它会被视为依赖树中的更改并重新创建您附加服务原则的资源,这意味着无法保留如果需要更新,请在 Terraform 中说明服务原则凭据
更新 2:
我已尝试执行以下操作,但缺点是每次您 运行 terraform 应用时都会 运行s:
terraform 脚本:
resource "azuread_application" "current" {
name = "${var.metadata_name}"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
resource "null_resource" "password_updater" {
# Updates everytime you run terraform apply so it will run this script everytime
triggers {
timestamp = "${timestamp()}"
}
provisioner "local-exec" {
command = "sh ${path.module}/update_service_password.sh ${azuread_service_principal.current.id} ${var.resource_group} ${azurerm_kubernetes_cluster.current.name}"
}
}
脚本:
#!/bin/sh
service_principle_id=
resource_group=
cluster_name=
# get service password expiration
expiration=$(az ad sp list --filter="objectId eq '$service_principle_id'" | jq '.[].passwordCredentials' | jq '.[].endDate' | cut -d'T' -f 1 | cut -d'"' -f 2)
# Format date for condition
now=$(date +%Y%m%d%H%M%S)
expiration_date=$(date -d "$expiration - 30 days" +%Y%m%d%H%M%S)
# Compare today with expiration date
if [ ${now} -ge ${expiration_date} ];
then
# IF expiration date in the next 30 days rest password
sp_id=$(az aks show -g ${resource_group} -n ${cluster_name} --query servicePrincipalProfile.clientId -o tsv)
service_principle_secret=$(az ad sp credential reset --name ${sp_id} --end-date $(date -d "+ 90 days" +%Y-%m-%d) --query password -o tsv)
# Update cluster with new password
az aks update-credentials \
--resource-group ${resource_group} \
--name ${cluster_name} \
--reset-service-principal \
--service-principal ${sp_id} \
--client-secret ${service_principle_secret}
fi
对于服务主体,可以通过 Azure CLI 重置其密码az ad sp reset
,但您需要有权限才能这样做。
在与服务原则 terraform 模块的开发人员交谈后,他们告诉我不可能有任何其他方法,如果找到更好的方法,请发表评论:
答案:
使用 null_resource 提供程序 运行 一个 运行 更新的脚本 -
resource "azuread_application" "current" {
name = "${var.metadata_name}"
}
resource "azuread_service_principal" "current" {
application_id = "${azuread_application.current.application_id}"
}
resource "random_string" "password" {
length = 64
special = true
}
resource "azuread_service_principal_password" "current" {
service_principal_id = "${azuread_service_principal.current.id}"
value = "${random_string.password.result}"
end_date_relative = "2160h" # valid for 90 days
}
resource "null_resource" "password_updater" {
# Updates everytime you run terraform apply so it will run this script everytime
triggers {
timestamp = "${timestamp()}"
}
provisioner "local-exec" {
command = "sh ${path.module}/update_service_password.sh ${azuread_service_principal.current.id} ${var.resource_group} ${azurerm_kubernetes_cluster.current.name}"
}
}
脚本:
#!/bin/sh
service_principle_id=
resource_group=
cluster_name=
# get service password expiration
expiration=$(az ad sp list --filter="objectId eq '$service_principle_id'" | jq '.[].passwordCredentials' | jq '.[].endDate' | cut -d'T' -f 1 | cut -d'"' -f 2)
# Format date for condition
now=$(date +%Y%m%d%H%M%S)
expiration_date=$(date -d "$expiration - 30 days" +%Y%m%d%H%M%S)
# Compare today with expiration date
if [ ${now} -ge ${expiration_date} ];
then
# IF expiration date in the next 30 days rest password
sp_id=$(az aks show -g ${resource_group} -n ${cluster_name} --query servicePrincipalProfile.clientId -o tsv)
service_principle_secret=$(az ad sp credential reset --name ${sp_id} --end-date $(date -d "+ 90 days" +%Y-%m-%d) --query password -o tsv)
# Update cluster with new password
az aks update-credentials \
--resource-group ${resource_group} \
--name ${cluster_name} \
--reset-service-principal \
--service-principal ${sp_id} \
--client-secret ${service_principle_secret}
fi
我认为更好的方法是:
- 您的 Terraform 代码很可能包含在更大的进程中。您很可能使用 bash 来启动该过程,然后进行地形改造。如果没有 - 我建议你这样做,因为这是 terraform 的最佳实践。
- 例如,在 运行 terraform 之前的 bash 代码中,使用 az cli 检查相关服务主体的到期时间。 (无所谓)
- 如果已过期,请使用 terraform taint 命令将服务主体密码资源标记为已污染。我没有详细信息,也许您也需要污染服务主体。也许不是。
- 如果被污染,terraform 将重新创建资源并重新生成密码。