使用 Terraform 为虚拟机分配 Azure RBAC 角色

3
我在我的Azure订阅中有一个虚拟机,应该能够读取和写入同一订阅中的存储容器。
因此,我创建了一个RBAC角色,允许从存储容器中读取和写入。为了将角色分配给虚拟机,我启用了VM中的系统分配标识,并将rbac角色与容器的资源管理器ID作为范围分配给系统分配标识。
虚拟机启动后,我假设我可以在VM中执行一些az storage blob...命令,例如列出给定容器中的blob,但是该命令失败并显示身份验证问题。
az storage blob list --account-name examplestorage --container-name example-container

你的命令和环境中未提供凭据,我们将查询你的存储帐户的帐户密钥。
建议在命令中提供--connection-string、--account-key或--sas-token作为凭据。
如果你的登录账户被分配了必需的 RBAC 角色,请在命令中添加--auth-mode login以使用 Azure Active Directory (Azure AD) 进行授权。
了解有关存储中 RBAC 角色的更多信息,请访问https://learn.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli
此外,设置相应的环境变量可以避免在命令中输入凭据。请使用 --help 获取有关环境变量使用情况的更多信息。
由于失败而跳过查询帐户密钥:请运行 'az login' 来设置帐户。
此存储帐户不允许公共访问。
请求标识:a3f2a5ce-301e-00a6-315b-931f48000000
时间:2021-08-17T11:30:31.2242111Z
错误代码:PublicAccessNotPermitted
错误:无
以下是我用来创建资源并分配角色的 Terraform 代码的部分内容:
resource azurerm_storage_account storage_account {
  name                     = "examplestorage"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource azurerm_storage_container storage_container {
  name                  = "example-container"
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = "private"
}

resource azurerm_role_definition storage_access_role {
  name        = "example-storage-access"
  description = "Role granting permissions to access the blob container storage."
  scope       = data.azurerm_subscription.example.id

  permissions {
    actions = [
      "Microsoft.Storage/storageAccounts/blobServices/containers/delete",
      "Microsoft.Storage/storageAccounts/blobServices/containers/read",
      "Microsoft.Storage/storageAccounts/blobServices/containers/write",
    ]
    data_actions = [
      "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/delete",
      "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read",
      "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
    ]
  }

  assignable_scopes = [
    data.azurerm_subscription.example.id
  ]
}

resource azurerm_linux_virtual_machine example_instance {
  name                = "example-instance"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  size                = "Standard_F2"
  admin_username      = "adminuser"

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  identity {
    type = "SystemAssigned"
  }
}

resource azurerm_role_assignment example_role_assignment {
  scope              = azurerm_storage_container.storage_container.resource_manager_id
  role_definition_id = azurerm_role_definition.storage_access_role.id
  principal_id       = azurerm_linux_virtual_machine.example_instance.identity[0].principal_id
}


执行 terraform plan 和 apply 无误,我在我的 Azure 门户中看到了系统分配给我的 VM 的标识。此标识已分配具有存储容器作用域的定义角色。
我进行了以下测试以检查权限:
- 创建具有定义的角色的服务主体 --> 使用此服务主体凭据从我的 VM 登录到 Azure,我可以列出容器中的 blob。 - 将 Contributor 角色分配给具有我的订阅作用域的系统分配的标识 --> 但错误仍然存在。
我的 Terraform 版本为:0.13.04 我的 Terraform 提供程序版本为:
初始化提供程序插件...
- 使用先前安装的 hashicorp/cloudinit v2.2.0 - terraform.io/builtin/terraform 内置于 Terraform - 使用先前安装的 hashicorp/azurerm v2.46.0 - 使用先前安装的 hashicorp/template v2.2
请问我错过了什么或做错了什么?非常感谢任何帮助。
谢谢,Christian

请尝试在CLI请求末尾添加“--auth-mode login”。例如:az storage blob list --account-name examplestorage --container-name example-container --auth-mode login - Gaurav Mantri
1
你为什么不使用内置角色“存储 Blob 数据参与者”?https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#storage-blob-data-owner - silent
当我加上 --auth-mode login 时,我会得到类似的错误信息。错误提示如下: 请运行 'az login' 来设置账户。 此外,没有理由不使用角色,但使用角色可能不能解决我的问题。 - kleegrewec
1个回答

1
我决定采用一些变通方法,实现从我的虚拟机访问 Azure 存储。受 terraform Azure provider 问题 https://github.com/hashicorp/terraform-provider-azuread/issues/40 的启发,我实现了服务主体的部署,并将我的 RBAC 角色分配给该主体,以进行存储读取和写入。由 terraform 执行生成的凭据随后被呈现为文件,成为创建虚拟机时的自定义数据的一部分。使用此解决方案,我可以使用该文件中的凭据登录到 Azure。
az login --service-principal --username ....

完成此操作后,将--auth-mode login标志附加到命令中,原问题的命令即可生效。
由于这种解决方案只是我的问题的一种变通方法,并且通过在运行的 VM 上存储凭据创建了一些安全问题,如果有人可以发布一种直接将 rbac 角色附加到我的 vm 的系统分配身份的解决方案,我会很幸运的。
谢谢,Christian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接