如何使用Terraform自动创建服务主体或MSI以在Azure管道中使用以管理AKS资源? [英] How do I automatically create service principals or MSIs with Terraform for use in Azure Pipelines to manage AKS resources?

查看:14
本文介绍了如何使用Terraform自动创建服务主体或MSI以在Azure管道中使用以管理AKS资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

following the official docs to create Azure Kubernetes clusters。文档指出,我需要首先手动创建一个服务主体,并提供客户端id和客户端机密。

不能手动执行。

以下是我的服务主体的代码。它装饰有指向最新Terraform文档的链接以供参考。

data "azurerm_subscription" "current" {}
data "azuread_client_config" "current" {}

resource "random_id" "current" {
  byte_length = 8
  prefix      = "ExternalDnsTf"
}

# Create Azure AD App.
# https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application
resource "azuread_application" "current" {
  display_name = random_id.current.hex
  owners       = [data.azuread_client_config.current.object_id]

}

# Create Service Principal associated with the Azure AD App
# https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal
resource "azuread_service_principal" "current" {
  application_id               = azuread_application.current.application_id
  app_role_assignment_required = false
  owners                       = [data.azuread_client_config.current.object_id]
}

# Create Service Principal password
# https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application_password
resource "azuread_application_password" "current" {
  application_object_id = azuread_application.current.object_id
}

# Create role assignment for service principal
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment
resource "azurerm_role_assignment" "current" {
  scope                = data.azurerm_subscription.current.id
  role_definition_name = "Contributor"

 # When assigning to a SP, use the object_id, not the appId
 # see: https://docs.microsoft.com/en-us/azure/role-based-access-control/role-assignments-cli
  principal_id = azuread_service_principal.current.object_id
}

我一直收到以下错误:(请注意,我是我的订阅的所有者)

ApplicationsClient.BaseClient.Post(): unexpected status 403 with OData
│ error: Authorization_RequestDenied: Insufficient privileges to complete the
│ operation.

我想要做的是消除设置支持服务的手动步骤。以ExternalDNS为例。The Azure docs声明我需要使用az ad sp create-for-rbac -n ExternalDnsServicePrincipal; az role assignment create --role "Reader" --assignee <appId GUID> --scope <resource group resource id>; az role assignment create --role "Contributor" --assignee <appId GUID> --scope <dns zone resource id>

最终,我将尝试创建azure cli命令的平台版本。

支持create-for-rbacwas a feature request on github。这在过去很有效,但变化太大了,不适用于当前的API版本。此外,由于AAD Graph受到抨击,取而代之的是Microsoft Graph API,我想知道我是否在这方面遇到了障碍。

ExternalDNS文档还建议使用托管服务标识(MSI)。服务主体、MSI、MSGraph API集成,老实说,我不在乎使用哪一个。只要我不必登录门户手动创建或授予权限,或手动运行az cli命令,任何当前最佳做法都可以

编辑:权限说明

当然,我正在使用Terraform来提供资源。如果我在没有terraform(手动或使用bash脚本)的情况下执行所有这些操作,我将使用azure cli我通过执行以下操作开始设置权限:

  • az login
  • az account set -s <my-subscription-id>

我是我的订阅的所有者。我可以毫无问题地运行所有命令、创建SP、MSI、分配角色等。

在管道中,I am using the charleszipp az pipelines terraform插件。在日志中,我看到:

  • az login --service-principal -t <my-tenant-id> -u *** -p ***
  • az account set -s <my-subscription-id>

我不确定这是否有区别。我将其解释为最终在登录并设置帐户订阅后执行命令,就像我手动执行的那样。

从技术上讲,我在其中几个任务中没有使用服务连接。但是,在需要服务连接的地方,我已经创建了一个服务连接,并将其范围定义为订阅级别。其类型为Azure资源管理器。

但是,如果我单击"管理服务主体",它会将我带到未定义权限的门户。

虽然我是我的订阅的所有者,但我不是根管理组。我被别人拥有/供应。最终,他们拥有对Active Directory的控制权。我无法添加或编辑权限。如果我尝试在权限下添加任何API并选择Microsoft Graph,它会显示需要授权。Grant Admin Consent for <parent organization呈灰色显示。

但如果我是我订阅的所有者,这有什么重要的?如果我可以通过az cli命令行做任何我想做的事情,是什么阻止我在管道中做同样的事情?

推荐答案

我使用的是用户管理的身份,它看起来最简单,对我来说很好用。

resource "azurerm_user_managed_identity", "mi" {
    resource_group_name = "rg"
    name = "mi"
    location = "eastus"
}

resource "azurerm_role_assignment" "ra" {
    scope = azurerm_subnet.sn.id  // subnet I created earlier
    role_definition_name = "Network Contributor"  // required with kubenet
    principal_id = azurerm_user_managed_identity.mi.principal_id
}

resource "azurerm_kubernetes_cluster" "aks" {
    name = "aks"
    identity {
        type = "UserAssigned"
        user_assigned_identity_id = azurerm_user_managed_identity.mi.id
    }
    <...remaining attributes...>
    depends_on = [azurerm_role_assignment.ra] // just to be safe
}

这篇关于如何使用Terraform自动创建服务主体或MSI以在Azure管道中使用以管理AKS资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆