Skip to content

Commit 50bb0c1

Browse files
fix: Adding permission management for BYO networks #450 (#477)
* Adding permission management for BYO networks #450 Signed-off-by: frozentank <[email protected]> * Reversing check that looks for when BYO networks are being used. Signed-off-by: frozentank <[email protected]> --------- Signed-off-by: frozentank <[email protected]> Co-authored-by: Chris Miller <[email protected]>
1 parent 09b7f71 commit 50bb0c1

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

docs/CONFIG-VARS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Note: All of the following resources are expected to be in the Resource Group se
177177
| subnet_names | Existing subnets mapped to desired usage. | map(string) | null | Only required if deploying into existing subnets. See the example that follows. |
178178
| nsg_name | Name of pre-existing network security group. | string | null | Only required if deploying into existing NSG. |
179179
| aks_uai_name | Name of existing User Assigned Identity for the cluster | string | null | This Identity will need permissions as listed in [AKS Cluster Identity Permissions](https://docs.microsoft.com/en-us/azure/aks/concepts-identity#aks-cluster-identity-permissions) and [Additional Cluster Identity Permissions](https://docs.microsoft.com/en-us/azure/aks/concepts-identity#additional-cluster-identity-permissions). Alternatively, use can use the [Contributor](https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#contributor) role for this Identity. |
180+
| msi_network_roles | Roles that will be assigned to the vnet and route table | list of strings | ["Network Contributor"] | This field will only be used in the event that the User Assigned Identity is created by IaC. If this case the authenticating identity used to run Terraform must have [Permissions for Assigning Roles](https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal#prerequisites) scoped to the vnet and route table. |
180181

181182
Example for the `subnet_names` variable:
182183

docs/user/BYOnetwork.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ values set by the [`vm_public_access_cidrs`/`postgres_public_access_cidrs`](../C
3535
## Cluster Identity
3636

3737
When creating an AKS cluster, Azure associates an Identity with the cluster. Any resources created on behalf of the cluster (e.g. VMs for the Node Pools etc.) will use the permissions associated with that Identity.
38-
By default, an Identity with the same permissions as the [Identity used for authenticating to the Terraform script](TerraformAzureAuthentication.md) will be used. You can choose to use the Service Principal directly (if used), or bring your own User Assigned Identity, depending on the setting of the [`aks_identity`](../CONFIG-VARS.md#general) variable.
38+
By default, an Identity with the same permissions as the [Identity used for authenticating to the Terraform script](TerraformAzureAuthentication.md) will be used. However, the new Identity may not have the same scope as the authenticating Identity. You can choose to use the Service Principal directly (if used), or bring your own User Assigned Identity, depending on the setting of the [`aks_identity`](../CONFIG-VARS.md#general) variable.
3939

40-
When providing your own networking, the AKS cluster identity will need write access to the aks subnet and the associated routing table.
40+
When providing your own networking, the AKS cluster identity will need write access to the aks subnet and the associated routing table. If an alternate identity is not provided then the resulting identity will have permissions assigned to the networking components directly (by default Network Contributor). This will require your authenticating identity to have [Permissions for Assigning Roles](https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal#prerequisites).
4141

4242
See [AKS Cluster Identity Permissions](https://docs.microsoft.com/en-us/azure/aks/concepts-identity#aks-cluster-identity-permissions) and [Additional Cluster Identity Permissions](https://docs.microsoft.com/en-us/azure/aks/concepts-identity#additional-cluster-identity-permissions) for details.
4343

locals.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ locals {
6464
: null
6565
)
6666

67+
aks_uai_principal_id = (var.aks_identity == "uai"
68+
? (var.aks_uai_name == null
69+
? azurerm_user_assigned_identity.uai[0].principal_id
70+
: data.azurerm_user_assigned_identity.uai[0].principal_id
71+
)
72+
: null
73+
)
74+
6775
cluster_egress_type = (var.cluster_egress_type == null
6876
? (var.egress_public_ip_name == null
6977
? "loadBalancer"

main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ module "vnet" {
8787
resource_group_name = local.network_rg.name
8888
location = var.location
8989
subnets = local.subnets
90+
roles = var.msi_network_roles
91+
aks_uai_principal_id = local.aks_uai_principal_id
92+
add_uai_permissions = (var.aks_uai_name == null)
9093
existing_subnets = var.subnet_names
9194
address_space = [var.vnet_address_space]
9295
tags = var.tags

modules/azurerm_vnet/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,16 @@ resource "azurerm_subnet" "subnet" {
5858
depends_on = [data.azurerm_virtual_network.vnet, azurerm_virtual_network.vnet]
5959
}
6060

61+
resource "azurerm_role_assignment" "existing_network_assignment" {
62+
count = length(var.existing_subnets) == 0 ? 0 : (var.add_uai_permissions ? length(var.roles) : 0)
63+
scope = data.azurerm_subnet.subnet["aks"].route_table_id
64+
role_definition_name = var.roles[count.index]
65+
principal_id = var.aks_uai_principal_id
66+
}
67+
68+
resource "azurerm_role_assignment" "existing_vnet_assignment" {
69+
count = var.name != null ? (var.add_uai_permissions ? length(var.roles) : 0) : 0
70+
scope = data.azurerm_virtual_network.vnet[0].id
71+
role_definition_name = var.roles[count.index]
72+
principal_id = var.aks_uai_principal_id
73+
}

modules/azurerm_vnet/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,20 @@ variable "tags" {
6161
description = "The tags to associate with your network and subnets."
6262
type = map(string)
6363
}
64+
65+
variable "roles" {
66+
description = "Managed Identity permissions for VNet and Route Table"
67+
type = list(string)
68+
default = ["Network Contributor"]
69+
}
70+
71+
variable "aks_uai_principal_id" {
72+
description = "Managed Identity Principal ID used to associate permissions to network and route table"
73+
type = string
74+
}
75+
76+
variable "add_uai_permissions" {
77+
description = "True if we should add roles to network objects"
78+
default = false
79+
type = bool
80+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ variable "use_msi" {
3030
default = false
3131
}
3232

33+
variable "msi_network_roles" {
34+
description = "Managed Identity permissions for VNet and Route Table"
35+
type = list(string)
36+
default = ["Network Contributor"]
37+
}
38+
3339
variable "iac_tooling" {
3440
description = "Value used to identify the tooling used to generate this providers infrastructure."
3541
type = string

0 commit comments

Comments
 (0)