forked from cloudposse/terraform-aws-transfer-sftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
258 lines (191 loc) · 6.56 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
locals {
enabled = module.this.enabled
s3_arn_prefix = "arn:${one(data.aws_partition.default[*].partition)}:s3:::"
is_vpc = var.vpc_id != null
user_names = keys(var.sftp_users)
user_names_map = {
for user, val in var.sftp_users :
user => merge(val, {
s3_bucket_arn = lookup(val, "s3_bucket_name", null) != null ? "${local.s3_arn_prefix}${lookup(val, "s3_bucket_name")}" : one(data.aws_s3_bucket.landing[*].arn)
})
}
}
data "aws_partition" "default" {
count = local.enabled ? 1 : 0
}
data "aws_s3_bucket" "landing" {
count = local.enabled ? 1 : 0
bucket = var.s3_bucket_name
}
resource "aws_transfer_server" "default" {
count = local.enabled ? 1 : 0
identity_provider_type = "SERVICE_MANAGED"
protocols = ["SFTP"]
domain = var.domain
endpoint_type = local.is_vpc ? "VPC" : "PUBLIC"
force_destroy = var.force_destroy
security_policy_name = var.security_policy_name
logging_role = join("", aws_iam_role.logging[*].arn)
dynamic "endpoint_details" {
for_each = local.is_vpc ? [1] : []
content {
subnet_ids = var.subnet_ids
security_group_ids = var.vpc_security_group_ids
vpc_id = var.vpc_id
address_allocation_ids = var.eip_enabled ? aws_eip.sftp.*.id : var.address_allocation_ids
}
}
tags = module.this.tags
}
resource "aws_transfer_user" "default" {
for_each = local.enabled ? var.sftp_users : {}
server_id = join("", aws_transfer_server.default[*].id)
role = aws_iam_role.s3_access_for_sftp_users[each.value.user_name].arn
user_name = each.value.user_name
home_directory_type = lookup(each.value, "home_directory_type", null) != null ? lookup(each.value, "home_directory_type") : (var.restricted_home ? "LOGICAL" : "PATH")
home_directory = lookup(each.value, "home_directory", null) != null ? lookup(each.value, "home_directory") : (!var.restricted_home ? "/${lookup(each.value, "s3_bucket_name", var.s3_bucket_name)}" : null)
dynamic "home_directory_mappings" {
for_each = var.restricted_home ? (
lookup(each.value, "home_directory_mappings", null) != null ? lookup(each.value, "home_directory_mappings") : [
{
entry = "/"
# Specifically do not use $${Transfer:UserName} since subsequent terraform plan/applies will try to revert
# the value back to $${Tranfer:*} value
target = format("/%s/%s", lookup(each.value, "s3_bucket_name", var.s3_bucket_name), each.value.user_name)
}
]
) : toset([])
content {
entry = lookup(home_directory_mappings.value, "entry")
target = lookup(home_directory_mappings.value, "target")
}
}
tags = module.this.tags
}
resource "aws_transfer_ssh_key" "default" {
for_each = local.enabled ? var.sftp_users : {}
server_id = join("", aws_transfer_server.default[*].id)
user_name = each.value.user_name
body = each.value.public_key
depends_on = [
aws_transfer_user.default
]
}
resource "aws_eip" "sftp" {
count = local.enabled && var.eip_enabled ? length(var.subnet_ids) : 0
vpc = local.is_vpc
tags = module.this.tags
}
# Custom Domain
data "aws_region" "current" {}
data "aws_vpc_endpoint" "sftp_vpc_endpoint" {
count = length(aws_transfer_server.default)
vpc_id = var.vpc_id
filter {
name = "vpc-endpoint-id"
values = [aws_transfer_server.default[count.index].endpoint_details[0].vpc_endpoint_id]
}
depends_on = [aws_transfer_server.default]
}
resource "aws_route53_record" "main" {
count = local.enabled && length(var.domain_name) > 0 && length(var.zone_id) > 0 ? 1 : 0
name = var.domain_name
zone_id = var.zone_id
type = "CNAME"
ttl = "300"
records = [
data.aws_vpc_endpoint.sftp_vpc_endpoint[count.index].dns_entry[0].dns_name
]
}
module "logging_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["transfer", "cloudwatch"]
context = module.this.context
}
data "aws_iam_policy_document" "assume_role_policy" {
count = local.enabled ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["transfer.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "s3_access_for_sftp_users" {
for_each = local.enabled ? local.user_names_map : {}
statement {
sid = "AllowListingOfUserFolder"
effect = "Allow"
actions = [
"s3:ListBucket"
]
resources = [
each.value.s3_bucket_arn,
]
}
statement {
sid = "HomeDirObjectAccess"
effect = "Allow"
actions = [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:GetObjectVersion",
"s3:GetObjectACL",
"s3:PutObjectACL"
]
resources = [
var.restricted_home ? "${each.value.s3_bucket_arn}/${each.value.user_name}/*" : "${each.value.s3_bucket_arn}/*"
]
}
}
data "aws_iam_policy_document" "logging" {
count = local.enabled ? 1 : 0
statement {
sid = "CloudWatchAccessForAWSTransfer"
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:CreateLogGroup",
"logs:PutLogEvents"
]
resources = ["*"]
}
}
module "iam_label" {
for_each = local.enabled ? local.user_names_map : {}
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["transfer", "s3", each.value.user_name]
context = module.this.context
}
resource "aws_iam_policy" "s3_access_for_sftp_users" {
for_each = local.enabled ? local.user_names_map : {}
name = module.iam_label[each.value.user_name].id
policy = data.aws_iam_policy_document.s3_access_for_sftp_users[each.value.user_name].json
tags = module.this.tags
}
resource "aws_iam_role" "s3_access_for_sftp_users" {
for_each = local.enabled ? local.user_names_map : {}
name = module.iam_label[each.value.user_name].id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy[*].json)
managed_policy_arns = [aws_iam_policy.s3_access_for_sftp_users[each.value.user_name].arn]
tags = module.this.tags
}
resource "aws_iam_policy" "logging" {
count = local.enabled ? 1 : 0
name = module.logging_label.id
policy = join("", data.aws_iam_policy_document.logging[*].json)
tags = module.this.tags
}
resource "aws_iam_role" "logging" {
count = local.enabled ? 1 : 0
name = module.logging_label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy[*].json)
managed_policy_arns = [join("", aws_iam_policy.logging[*].arn)]
tags = module.this.tags
}