forked from cloudposse/terraform-aws-transfer-sftp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
265 lines (205 loc) · 6.86 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
259
260
261
262
263
264
265
locals {
enabled = module.this.enabled
is_vpc = var.vpc_id != null
security_group_enabled = module.this.enabled && var.security_group_enabled
user_names = keys(var.sftp_users)
user_names_map = { for idx, user in local.user_names : idx => user }
}
data "aws_region" "current" {}
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
depends_on = [aws_eip.sftp]
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)
pre_authentication_login_banner = var.pre_authentication_login_banner
dynamic "endpoint_details" {
for_each = local.is_vpc ? [1] : []
content {
subnet_ids = var.subnet_ids
security_group_ids = local.security_group_enabled ? module.security_group.*.id : 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[index(local.user_names, each.value.user_name)].arn
user_name = each.value.user_name
home_directory_type = var.restricted_home ? "LOGICAL" : "PATH"
dynamic "home_directory_mappings" {
for_each = var.restricted_home ? [1] : []
content {
entry = "/"
target = "/${var.s3_bucket_name}/$${Transfer:UserName}"
}
}
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
}
module "security_group" {
source = "cloudposse/security-group/aws"
version = "0.3.1"
use_name_prefix = var.security_group_use_name_prefix
rules = var.security_group_rules
description = var.security_group_description
vpc_id = local.is_vpc ? var.vpc_id : null
enabled = local.security_group_enabled
context = module.this.context
}
# Custom Domain
resource "aws_route53_record" "main" {
count = var.route53enabled ? 1 : 0
name = var.domain_name
zone_id = var.zone_id
type = "CNAME"
ttl = "300"
records = [
join("", aws_transfer_server.default[*].endpoint)
]
}
# Add tags for DNS info and link in AWS console
resource "null_resource" "transfer_server_dns_tags" {
count = var.route53enabled ? 1 : 0
depends_on = [aws_transfer_server.default, aws_route53_record.main]
triggers = {
aws_profile = var.aws_profile
aws_region = data.aws_region.current.name
hostname = var.domain_name
zone_id = var.zone_id
transfer_server_arn = aws_transfer_server.default[0].arn
}
provisioner "local-exec" {
command = <<EOF
aws --profile ${var.aws_profile} --region ${data.aws_region.current.name} transfer tag-resource \
--arn '${aws_transfer_server.default[0].arn}' \
--tags \
Key=aws:transfer:route53HostedZoneId,Value=/hostedzone/${var.zone_id} \
Key=aws:transfer:customHostname,Value=${var.domain_name}
EOF
}
provisioner "local-exec" {
when = destroy
command = <<EOD
echo "Skip"
EOD
}
}
module "logging_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["transfer", "cloudwatch"]
context = module.this.context
}
resource "aws_cloudwatch_log_group" "transfer_server" {
#checkov:skip=CKV_AWS_158:Log encryption not needed
count = local.enabled ? 1 : 0
name = "/aws/transfer/${aws_transfer_server.default[0].id}"
retention_in_days = var.log_retention
tags = module.logging_label.tags
}
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 = [
join("", data.aws_s3_bucket.landing[*].arn)
]
}
statement {
sid = "HomeDirObjectAccess"
effect = "Allow"
actions = [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:GetObjectVersion",
"s3:GetObjectACL",
"s3:PutObjectACL"
]
resources = [
"${join("", data.aws_s3_bucket.landing[*].arn)}/${each.value}/*"
]
}
}
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]
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[index(local.user_names, each.value)].id
policy = data.aws_iam_policy_document.s3_access_for_sftp_users[index(local.user_names, each.value)].json
}
resource "aws_iam_role" "s3_access_for_sftp_users" {
for_each = local.enabled ? local.user_names_map : {}
name = module.iam_label[index(local.user_names, each.value)].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[index(local.user_names, each.value)].arn]
}
resource "aws_iam_policy" "logging" {
count = local.enabled ? 1 : 0
name = module.logging_label.id
policy = join("", data.aws_iam_policy_document.logging[*].json)
}
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)]
}