Skip to content

Commit 5e64f54

Browse files
add ACLs for VM users automatically (#6)
1 parent 02529bd commit 5e64f54

File tree

7 files changed

+53
-32
lines changed

7 files changed

+53
-32
lines changed

modules/vm/main.tf

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,45 @@ resource "proxmox_virtual_environment_vm" "vm" {
9191
}
9292
}
9393

94-
dynamic "initialization" {
95-
for_each = var.ssh_keys != null ? [var.ssh_keys] : []
96-
content {
97-
datastore_id = var.disk.storage
94+
initialization {
95+
datastore_id = var.disk.storage
9896

99-
# this is necessary when using machine type q35 or ovmf bios
100-
interface = "scsi1"
97+
# this is necessary when using machine type q35 or ovmf bios
98+
interface = "scsi1"
10199

102-
user_account {
103-
password = random_password.default_root_password.result
104-
keys = initialization.value
105-
}
100+
user_account {
101+
password = random_password.default_root_password.result
102+
keys = [for _, user in var.admins : user.ssh_key]
103+
}
106104

107-
ip_config {
108-
ipv4 {
109-
address = "dhcp"
110-
}
105+
ip_config {
106+
ipv4 {
107+
address = "dhcp"
108+
}
111109

112-
ipv6 {
113-
address = "dhcp"
114-
}
110+
ipv6 {
111+
address = "dhcp"
115112
}
113+
}
116114

117-
ip_config {
118-
ipv4 {
119-
address = "10.23.42.${var.vm_id}/24"
120-
gateway = "10.23.42.1"
121-
}
115+
ip_config {
116+
ipv4 {
117+
address = "10.23.42.${var.vm_id}/24"
118+
gateway = "10.23.42.1"
122119
}
123120
}
124121
}
125122
}
126123

124+
resource "proxmox_virtual_environment_acl" "admin_acl" {
125+
for_each = { for user in var.admins : user.username => user }
126+
127+
user_id = each.value.username
128+
role_id = "VMUser"
129+
path = "/vms/${proxmox_virtual_environment_vm.vm.vm_id}"
130+
}
131+
132+
127133
resource "random_password" "default_root_password" {
128134
length = 16
129135
override_special = "_%@"

modules/vm/variables.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ variable "clone" {
6767
description = "clone the vm with the given ID"
6868
}
6969

70-
variable "ssh_keys" {
71-
type = list(string)
72-
default = null
73-
description = "clone the vm with the given ID"
70+
variable "admins" {
71+
type = list(object({
72+
username = string
73+
ssh_key = string
74+
}))
7475
}
7576

7677
variable "template" {

proxmox/docker.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "docker_vm" {
2222
internal_bridge = proxmox_virtual_environment_network_linux_bridge.internal_bridge.name
2323
}
2424

25-
ssh_keys = [
26-
var.users["hrmny"].ssh_key,
25+
admins = [
26+
local.users["hrmny"]
2727
]
2828
}

proxmox/home-assistant.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ module "home_assistant_vm" {
1919
bridge = var.network.bridge
2020
internal_bridge = proxmox_virtual_environment_network_linux_bridge.internal_bridge.name
2121
}
22+
23+
admins = [
24+
local.users["hrmny"]
25+
]
2226
}

proxmox/kristall-miai-vm.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "kristall-miai_vm" {
2222
internal_bridge = proxmox_virtual_environment_network_linux_bridge.internal_bridge.name
2323
}
2424

25-
ssh_keys = [
26-
var.users["kristall"].ssh_key,
25+
admins = [
26+
local.users["kristall"]
2727
]
2828
}

proxmox/netboot.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "netboot_vm" {
2222
internal_bridge = proxmox_virtual_environment_network_linux_bridge.internal_bridge.name
2323
}
2424

25-
ssh_keys = [
26-
var.users["saces"].ssh_key,
25+
admins = [
26+
local.users["saces"]
2727
]
2828
}

proxmox/users.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ resource "proxmox_virtual_environment_user" "ijon" {
2121
role_id = data.proxmox_virtual_environment_role.admin_role.role_id
2222
}
2323
}
24+
25+
resource "proxmox_virtual_environment_user" "member" {
26+
for_each = local.users
27+
user_id = each.value.username
28+
}
29+
30+
// add pve username to user object
31+
locals {
32+
users = { for name, user in var.users : name => { username = "${name}@ldap", ssh_key = user.ssh_key } }
33+
}

0 commit comments

Comments
 (0)