-
Notifications
You must be signed in to change notification settings - Fork 1
/
r-sftp-users.tf
103 lines (87 loc) · 4.14 KB
/
r-sftp-users.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# https://learn.microsoft.com/en-us/azure/storage/blobs/secure-file-transfer-protocol-support#supported-algorithms
resource "tls_private_key" "main" {
for_each = local.sftp_users_with_ssh_key_enabled
algorithm = "RSA"
rsa_bits = 4096
}
moved {
from = tls_private_key.sftp_users_keys
to = tls_private_key.main
}
moved {
from = azurerm_storage_account_local_user.sftp_users
to = azurerm_storage_account_local_user.main
}
resource "azurerm_storage_account_local_user" "main" {
for_each = local.sftp_users
name = each.key
storage_account_id = module.storage_account.id
ssh_key_enabled = each.value.ssh_key_enabled
ssh_password_enabled = each.value.ssh_password_enabled
# The first container in the `permissions_scopes` list will always be the default home directory
home_directory = coalesce(each.value.home_directory, each.value.permissions_scopes[0].target_container)
# https://learn.microsoft.com/en-us/azure/storage/blobs/secure-file-transfer-protocol-support#container-permissions
dynamic "permission_scope" {
for_each = each.value.permissions_scopes
content {
service = "blob"
resource_name = permission_scope.value.target_container
permissions {
create = contains(permission_scope.value.permissions, "All") || contains(permission_scope.value.permissions, "Create")
delete = contains(permission_scope.value.permissions, "All") || contains(permission_scope.value.permissions, "Delete")
list = contains(permission_scope.value.permissions, "All") || contains(permission_scope.value.permissions, "List")
read = contains(permission_scope.value.permissions, "All") || contains(permission_scope.value.permissions, "Read")
write = contains(permission_scope.value.permissions, "All") || contains(permission_scope.value.permissions, "Write")
}
}
}
dynamic "ssh_authorized_key" {
for_each = each.value.ssh_key_enabled ? ["auto"] : []
content {
key = tls_private_key.main[each.key].public_key_openssh
description = "Automatically generated by Terraform"
}
}
dynamic "ssh_authorized_key" {
for_each = each.value.ssh_key_enabled ? each.value.ssh_authorized_keys : []
content {
key = ssh_authorized_key.value.key
description = ssh_authorized_key.value.description
}
}
lifecycle {
precondition {
condition = alltrue([
for scope in each.value.permissions_scopes : contains(keys(module.storage_account.storage_blob_containers), scope.target_container)
])
error_message = format("At least one target container does not exist (or is being deleted) for user %s.", each.key)
}
precondition {
condition = alltrue(flatten([
for scope in each.value.permissions_scopes : [
for permission in scope.permissions : contains(local.sftp_users_permissions, permission)
]
]))
error_message = format("One or more permissions are wrong for user %s. Allowed values in the list are: %s.", each.key, join(", ", [
for permission in local.sftp_users_permissions : "'${permission}'"
]))
}
# Required because otherwise Terraform will apply successfully but the SFTP connection will fail when using the default SFTP connection command line
postcondition {
condition = contains(self.permission_scope[*].resource_name, split("/", self.home_directory)[0])
error_message = format("The home directory of user %s does not refer to any container in its permissions scopes.", self.name)
}
}
}
resource "local_sensitive_file" "sftp_users_private_keys" {
for_each = var.create_sftp_users_keys ? tls_private_key.main : {}
content = each.value.private_key_pem
filename = pathexpand(format("%s/%s_%s.pem", var.sftp_users_keys_path, module.storage_account.name, each.key))
file_permission = "0600"
}
resource "local_sensitive_file" "sftp_users_public_keys" {
for_each = var.create_sftp_users_keys ? tls_private_key.main : {}
content = each.value.public_key_openssh
filename = pathexpand(format("%s/%s_%s.pub", var.sftp_users_keys_path, module.storage_account.name, each.key))
file_permission = "0644"
}