Skip to content

Commit 6b12830

Browse files
authored
Enhance Entra ID login configuration and role assignments (#79)
Signed-off-by: Roman Schwarz <rs@cloudeteer.de>
1 parent 6266601 commit 6b12830

6 files changed

Lines changed: 63 additions & 24 deletions

File tree

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ The following resources are used by this module:
135135
- [azurerm_managed_disk.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk) (resource)
136136
- [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) (resource)
137137
- [azurerm_public_ip.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) (resource)
138-
- [azurerm_role_assignment.entra_id_login](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
138+
- [azurerm_role_assignment.entra_id_login_admin](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
139+
- [azurerm_role_assignment.entra_id_login_user](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) (resource)
139140
- [azurerm_user_assigned_identity.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) (resource)
140141
- [azurerm_virtual_machine_data_disk_attachment.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_data_disk_attachment) (resource)
141142
- [azurerm_virtual_machine_extension.domain_join](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension) (resource)
@@ -502,15 +503,22 @@ Default: `true`
502503

503504
### <a name="input_entra_id_login"></a> [entra\_id\_login](#input\_entra\_id\_login)
504505

505-
Description: Configures Entra ID-based login for virtual machines. Set `enabled` to `true` to activate this feature and define the list of `principal_ids` permitted to log in.
506+
Description: Configures Entra ID-based login for virtual machines. Set `enabled` to `true` to activate this feature and specify the lists of `admin_login_principal_ids` and `user_login_principal_ids` that are permitted to log in.
507+
506508
**Note**: This feature requires at least 1GB of memory and is not supported on certain SKUs, including `Standard_B1ls`, `Basic_A0`, and `Standard_A0`.
507509

510+
**Note**: When specifying `admin_login_principal_ids` or `user_login_principal_ids`, this module will create Azure role assignments for those principals. The user deploying this module must have sufficient permissions to create role assignments (typically the `Owner` role or a custom role with the necessary permissions).
511+
512+
**Deprecation Notice**: The `principal_ids` argument is deprecated and will be removed in version 2.0 of this module. Please use `admin_login_principal_ids` and `user_login_principal_ids` instead.
513+
508514
Type:
509515

510516
```hcl
511517
object({
512-
enabled = optional(bool)
513-
principal_ids = optional(list(string), [])
518+
enabled = optional(bool)
519+
principal_ids = optional(list(string), []) # !! DEPRECATED !!
520+
admin_login_principal_ids = optional(list(string), [])
521+
user_login_principal_ids = optional(list(string), [])
514522
})
515523
```
516524

r-extensions.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ resource "azurerm_virtual_machine_extension" "entra_id_login" {
8282
}
8383
}
8484

85+
resource "azurerm_role_assignment" "entra_id_login_admin" {
86+
for_each = (
87+
var.entra_id_login.enabled
88+
? toset(concat(var.entra_id_login.principal_ids, var.entra_id_login.admin_login_principal_ids))
89+
: []
90+
)
91+
92+
principal_id = each.value
93+
role_definition_name = "Virtual Machine Administrator Login"
94+
scope = local.virtual_machine.id
95+
}
96+
97+
resource "azurerm_role_assignment" "entra_id_login_user" {
98+
for_each = (
99+
var.entra_id_login.enabled ? toset(var.entra_id_login.user_login_principal_ids) : []
100+
)
101+
102+
principal_id = each.value
103+
role_definition_name = "Virtual Machine User Login"
104+
scope = local.virtual_machine.id
105+
}
106+
107+
85108
resource "azurerm_virtual_machine_extension" "domain_join" {
86109
count = var.domain_join.enabled ? 1 : 0
87110

r-identity.tf

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ resource "azurerm_user_assigned_identity" "this" {
1414
tags = var.tags
1515
}
1616

17-
resource "azurerm_role_assignment" "entra_id_login" {
18-
for_each = var.entra_id_login.enabled ? toset(var.entra_id_login.principal_ids) : []
19-
20-
principal_id = each.value
21-
role_definition_name = "Virtual Machine Administrator Login"
22-
scope = local.virtual_machine.id
17+
moved {
18+
from = azurerm_role_assignment.entra_id_login
19+
to = azurerm_role_assignment.entra_id_login_admin
2320
}

tests/local/input_entra_id.tftest.hcl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ run "entra_id_extension_and_identity_type_should_be_created" {
2020
error_message = "It is not possible to install the AAD-Extension without a given 'principal_id'."
2121
}
2222

23-
assert {
24-
condition = length(azurerm_role_assignment.entra_id_login) != 0
25-
error_message = "It is not possible to install the AAD-Extension without a given 'principal_id'."
26-
}
27-
2823
assert {
2924
condition = local.identity_type == "SystemAssigned"
3025
error_message = "It is not possible to install the EntraID-Extension without setting the Idenity to 'SystemAssigned' OR 'SystemAssigned, UserAssigned'."
@@ -51,11 +46,6 @@ run "entra_id_extension_and_add_identity_type_should_be_created" {
5146
error_message = "It is not possible to install the AAD-Extension without a given 'principal_id'."
5247
}
5348

54-
assert {
55-
condition = length(azurerm_role_assignment.entra_id_login) != 0
56-
error_message = "It is not possible to install the AAD-Extension without a given 'principal_id'."
57-
}
58-
5949
assert {
6050
condition = local.identity_type == "SystemAssigned, UserAssigned"
6151
error_message = "It is not possible to install the EntraID-Extension without setting the Idenity to 'SystemAssigned' OR 'SystemAssigned, UserAssigned'."
@@ -71,7 +61,12 @@ run "nothing_should_be_created_regarding_entra_id_login" {
7161
}
7262

7363
assert {
74-
condition = length(azurerm_role_assignment.entra_id_login) == 0
64+
condition = length(azurerm_role_assignment.entra_id_login_admin) == 0
65+
error_message = "No role_assignment for EntraID should be created when 'entra_id_login' is disabled."
66+
}
67+
68+
assert {
69+
condition = length(azurerm_role_assignment.entra_id_login_user) == 0
7570
error_message = "No role_assignment for EntraID should be created when 'entra_id_login' is disabled."
7671
}
7772

tests/remote/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ module "tftest_02" {
7575
image = "Ubuntu2204"
7676
key_vault_id = local.key_vault_id
7777
subnet_id = local.subnet_id
78+
79+
entra_id_login = {
80+
enabled = true
81+
principal_ids = ["52c45cde-8aa6-45a7-b717-686d20a2fbbf"]
82+
admin_login_principal_ids = ["52c45cde-8aa6-45a7-b717-686d20a2fbbf"]
83+
user_login_principal_ids = ["52c45cde-8aa6-45a7-b717-686d20a2fbbf"]
84+
}
7885
}

variables.tf

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,22 @@ variable "encryption_at_host_enabled" {
311311

312312
variable "entra_id_login" {
313313
description = <<-EOD
314-
Configures Entra ID-based login for virtual machines. Set `enabled` to `true` to activate this feature and define the list of `principal_ids` permitted to log in.
314+
Configures Entra ID-based login for virtual machines. Set `enabled` to `true` to activate this feature and specify the lists of `admin_login_principal_ids` and `user_login_principal_ids` that are permitted to log in.
315+
315316
**Note**: This feature requires at least 1GB of memory and is not supported on certain SKUs, including `Standard_B1ls`, `Basic_A0`, and `Standard_A0`.
317+
318+
**Note**: When specifying `admin_login_principal_ids` or `user_login_principal_ids`, this module will create Azure role assignments for those principals. The user deploying this module must have sufficient permissions to create role assignments (typically the `Owner` role or a custom role with the necessary permissions).
319+
320+
**Deprecation Notice**: The `principal_ids` argument is deprecated and will be removed in version 2.0 of this module. Please use `admin_login_principal_ids` and `user_login_principal_ids` instead.
316321
EOD
322+
317323
type = object({
318-
enabled = optional(bool)
319-
principal_ids = optional(list(string), [])
324+
enabled = optional(bool)
325+
principal_ids = optional(list(string), []) # !! DEPRECATED !!
326+
admin_login_principal_ids = optional(list(string), [])
327+
user_login_principal_ids = optional(list(string), [])
320328
})
329+
321330
default = ({
322331
enabled = false
323332
})

0 commit comments

Comments
 (0)