【问题标题】:private_endpoint between two resource group services两个资源组服务之间的 private_endpoint
【发布时间】:2022-01-27 08:51:52
【问题描述】:

我在 azure 中有一个容器注册表,由 terraform 实现,您可以在以下代码中看到:

resource "azurerm_container_registry" "container_registry" {
  name                = replace("${var.name}${var.namespace}", "-", "")
  location            = var.location
  resource_group_name = azurerm_resource_group.resource_group.name

  sku = "Premium"

  public_network_access_enabled = false

  network_rule_set = [
    {
      default_action = "Deny"
      ip_rule = []
      virtual_network = [
        for subnet_id in var.allowed_subnets : {
          action    = "Allow"
          subnet_id = subnet_id
        }
      ]
    }
  ]
}

另一方面,我有一个具有不同资源组的虚拟机。因为我想将 VM 中创建的 docker-images 推送到 registry 我想在这两者之间建立一个私人连接。我的terraform 项目如下:

resource "azurerm_subnet" "subnet" {
  name                = "${var.name}-${var.namespace}"
  resource_group_name = azurerm_resource_group.resource_group.name

  virtual_network_name = azurerm_virtual_network.virtual_network.name
  address_prefixes     = ["10.0.1.0/24"]
  service_endpoints    = ["Microsoft.ContainerRegistry"]

  enforce_private_link_endpoint_network_policies = true
}

resource "azurerm_role_assignment" "role_assignment" {
  scope                = data.terraform_remote_state.container_registry.outputs.container_registry_id
  role_definition_name = "AcrPush"
  principal_id         = azurerm_linux_virtual_machine.virtual_machine.identity[0].principal_id
}

resource "azurerm_private_endpoint" "private_endpoint" {
  name                = "${var.name}-${var.namespace}"
  location            = var.location
  resource_group_name = azurerm_resource_group.resource_group.name
  subnet_id           = azurerm_subnet.subnet.id

  private_service_connection {
    name                           = "container-private-connection"
    subresource_names              = ["registry"] 
    private_connection_resource_id = data.terraform_remote_state.container_registry.outputs.container_registry_id
    is_manual_connection           = false

  }
}

我想通过private_endpoint 我可以为az acr login --name 建立安全连接,但它不起作用并返回 403。你能帮我解决这个问题吗?

Unable to get AAD authorization tokens with message: An error occurred: CONNECTIVITY_REFRESH_TOKEN_ERROR
Access to registry 'tempregistry.azurecr.io' was denied. Response code: 403. Please try running 'az login' again to refresh permissions.
WARNING: Unable to get admin user credentials with message: The resource with name 'tempregistry' and type 'Microsoft.ContainerRegistry/registries' could not be found in subscription 'temp-dev'.
ERROR: Unable to authenticate using AAD or admin login credentials. Please specify both username and password in non-interactive mode

(我删除了额外的代码,例如资源定义和 vnet)

【问题讨论】:

  • 您好!完成。@Ans
  • 谢谢,您认为使用 DNS 区域为容器注册创建虚拟专用链接是个好主意吗?@AnsumanBal-MT
  • 不只是 Run command 在 Azure 控制台中!我不想拥有公共 IP。
  • 它可以工作,但我不想让管理员访问它!只是我想为 VM 设置 acrPush 角色并为另一个 VM 拉取访问权限!
  • 不,您没有授予管理员访问权限,您正在创建私有 acr 访问的用户名和密码。因此,当您将 acr 推送角色分配给 vm 的托管身份时,它将搜索该用户名和密码并登录,但它仅具有推送和拉取权限。您还可以测试其他操作,这些操作被允许担任 acroowner 角色.. 如果这回答了您的问题,请告诉我

标签: azure terraform azure-container-registry


【解决方案1】:

我尝试了代码并收到与您相同的错误:

Unable to get AAD authorization tokens with message: An error occurred: CONNECTIVITY_REFRESH_TOKEN_ERROR
Access to registry 'ansumanacrtest.azurecr.io' was denied. Response code: 403. Please try running 'az login' again to refresh permissions.
WARNING: Unable to get admin user credentials with message: The resource with name 'ansumanacrtest' and type 'Microsoft.ContainerRegistry/registries' could not be found in subscription 'xxxxx'.
ERROR: Unable to authenticate using AAD or admin login credentials. Please specify both username and password in non-interactive mode

要解决上述错误,我们必须在 ACR 所在的同一资源组中提供 VM Reader 角色的托管身份,以读取详细信息ACR 和启用 ACR 中的管理员以创建用户名和密码。

但是当我再次尝试从 Run Command in Portal 进行相同操作时,它给了我 login failed 错误 作为 不允许运行命令使用的 IP 如下:

所以,一旦我获得了 Portal 使用的 IP,然后我将它添加到下面代码中的网络规则集中,我之前使用它来创建资源:

resource "azurerm_container_registry" "container_registry" {
  name                = "ansumanacrtest"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  sku = "Premium"
  admin_enabled = true
  public_network_access_enabled = true # as we are running commands from Run Command Script , we have to allow selected public access
#network rule set can be added after you get the IP from portal Run Command
  network_rule_set { 
      default_action = "Deny"
      ip_rule = [{
          action = "Allow"
          ip_range = "20.127.31.73"## IP received from the above error 
      }]
  }
}
resource "azurerm_role_assignment" "role_assignment" {
  scope                = azurerm_container_registry.container_registry.id
  role_definition_name = "AcrPush"
  principal_id         = azurerm_linux_virtual_machine.example.identity[0].principal_id
}

resource "azurerm_role_assignment" "VMcontributor" {
  scope = data.azurerm_resource_group.example.id
  role_definition_name = "Reader"
  principal_id = azurerm_linux_virtual_machine.example.identity[0].principal_id
}


resource "azurerm_private_endpoint" "private_endpoint" {
  name                = "pe-ansumanacr"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  subnet_id           = azurerm_subnet.subnet.id

  private_dns_zone_group {
    name = "container-dns-group"
    private_dns_zone_ids = ["${azurerm_private_dns_zone.example.id}"]
  }

  private_service_connection {
    name                           = "container-private-connection"
    subresource_names              = ["registry"] 
    private_connection_resource_id = azurerm_container_registry.container_registry.id
    is_manual_connection           = false

  }
}
resource "azurerm_private_dns_zone" "example" {
  name                = "privatelink.azureacr.io"
  resource_group_name = data.azurerm_resource_group.example.name
}

#private DNS Link
resource "azurerm_private_dns_zone_virtual_network_link" "example" {
  name                  = "${azurerm_container_registry.container_registry.name}-dnslink"
  resource_group_name   = data.azurerm_resource_group.example.name
  private_dns_zone_name = azurerm_private_dns_zone.example.name
  virtual_network_id    = azurerm_virtual_network.example.id
  registration_enabled = false
} 

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2019-10-08
    • 2018-06-01
    • 2017-09-23
    • 2019-03-24
    相关资源
    最近更新 更多