gpt4 book ai didi

azure - 使用 terraform 将 Azure rbac 角色分配给虚拟机

转载 作者:行者123 更新时间:2023-12-04 17:15:17 28 4
gpt4 key购买 nike

我的 Azure 订阅中有一个虚拟机,它应该能够读取和写入同一订阅中的存储容器。

因此,我创建了一个 rbac 角色,允许在存储容器中读写。为了将角色分配给 VM,我在 VM 中启用系统分配的身份,并将 rbac 角色分配给系统分配的身份,并以容器的资源管理器 ID 作为范围。

VM 启动后,我假设我应该能够在 VM 中执行一些 az storage blob ... 命令,例如列出给定容器中的 blob,但该命令因身份验证问题而失败。

az storage blob list --account-name examplestorage --container-name example-container

There are no credentials provided in your command and environment, we will query for account key for your storage account.
It is recommended to provide --connection-string, --account-key or --sas-token in your command as credentials.

You also can add --auth-mode login in your command to use Azure Active Directory (Azure AD) for authorization if your login account is assigned required RBAC roles.
For more information about RBAC roles in storage, visit https://learn.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli.

In addition, setting the corresponding environment variables can avoid inputting credentials in your command. Please use --help to get more information about environment variable usage.

Skip querying account key due to failure: Please run 'az login' to setup account.
Public access is not permitted on this storage account.
RequestId:a3f2a5ce-301e-00a6-315b-931f48000000
Time:2021-08-17T11:30:31.2242111Z
ErrorCode:PublicAccessNotPermitted
Error:None

以下是我用来创建资源和分配角色的 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 门户中,我看到系统为我的虚拟机分配了一个身份。该身份具有以存储容器作为范围分配的已定义角色。

我做了以下测试来检查权限:

  • 使用定义的 rbac 角色创建一个服务主体 --> 使用此服务主体凭据从我的虚拟机登录到 azure,我可以列出容器中的 blob。
  • 将贡献者角色分配给系统分配的身份,并将我的订阅作为范围 --> 但错误仍然相同。

我的 terraform 版本是:0.13.04我的 terraform 提供程序的版本是:正在初始化提供程序插件...

  • 使用之前安装的 hashcorp/cloudinit v2.2.0
  • terraform.io/builtin/terraform 内置于 Terraform
  • 使用之前安装的 hashcorp/azurerm v2.46.0
  • 使用之前安装的 hashcorp/template v2.2

有什么建议我在这里遗漏或做错了吗?如有任何帮助,我们将不胜感激。

谢谢克里斯蒂安

最佳答案

我决定使用一些解决方法来实现从虚拟机到 Azure 存储的访问。受到 terraform Azure 提供程序问题的启发 https://github.com/hashicorp/terraform-provider-azuread/issues/40我实现了服务主体的部署,并将用于存储读写的 rbac 角色分配给该主体。然后,由 terraform 执行生成的凭据将呈现到一个文件中,该文件是创建虚拟机时自定义数据的一部分。使用此解决方案,我可以使用此文件中的凭据登录 Azure 使用

az login --service-principal --username ....

完成此操作后,我原来问题中的命令在附加建议时起作用--auth-mode登录标记命令。

由于此解决方案只是解决我的问题的方法,并且通过在正在运行的虚拟机上存储凭据而产生一些安全问题,如果有人可以发布一个将 rbac 角色直接附加到我的虚拟机的系统分配身份的解决方案,我会很幸运。

谢谢克里斯蒂安

关于azure - 使用 terraform 将 Azure rbac 角色分配给虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68818141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com