Skip to content

Commit 608b67f

Browse files
authored
Allow authenticated access from anywhere (#75)
1 parent b30e765 commit 608b67f

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ Available targets:
146146
| Name | Version |
147147
|------|---------|
148148
| aws | >= 2.0 |
149-
| null | >= 2.0 |
150149

151150
## Inputs
152151

docs/terraform.md

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
| Name | Version |
1414
|------|---------|
1515
| aws | >= 2.0 |
16-
| null | >= 2.0 |
1716

1817
## Inputs
1918

examples/complete/fixtures.us-east-2.tfvars

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ name = "es-test"
1010

1111
availability_zones = ["us-east-2a", "us-east-2b"]
1212

13-
instance_type = "t2.small.elasticsearch"
13+
instance_type = "t3.small.elasticsearch"
1414

15-
elasticsearch_version = "6.5"
15+
elasticsearch_version = "7.7"
1616

1717
instance_count = 2
1818

@@ -26,7 +26,7 @@ elasticsearch_subdomain_name = ""
2626

2727
kibana_subdomain_name = ""
2828

29-
ebs_volume_size = 10
29+
ebs_volume_size = 20
3030

3131
create_iam_service_linked_role = false
3232

main.tf

+35-29
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
module "user_label" {
22
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
33

4-
enabled = module.this.enabled
5-
attributes = concat(module.this.attributes, ["user"])
4+
attributes = compact(concat(module.this.attributes, ["user"]))
65

76
context = module.this.context
87
}
98

109
module "kibana_label" {
1110
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
1211

13-
enabled = module.this.enabled
14-
attributes = concat(module.this.attributes, ["kibana"])
12+
attributes = compact(concat(module.this.attributes, ["kibana"]))
1513

1614
context = module.this.context
1715
}
@@ -97,20 +95,6 @@ data "aws_iam_policy_document" "assume_role" {
9795
}
9896
}
9997

100-
# inspired by https://github.com/hashicorp/terraform/issues/20692
101-
# I use 0.12 new "dynamic" block - https://www.terraform.io/docs/configuration/expressions.html
102-
# If we have 1 az - the count of this resource equals 0, hence no config
103-
# block appears in the `aws_elasticsearch_domain`
104-
# If we have more than 1 - we set the trigger to the actual value of
105-
# `availability_zone_count`
106-
# and `dynamic` block kicks in
107-
resource "null_resource" "azs" {
108-
count = var.availability_zone_count > 1 ? 1 : 0
109-
triggers = {
110-
availability_zone_count = var.availability_zone_count
111-
}
112-
}
113-
11498
resource "aws_elasticsearch_domain" "default" {
11599
count = module.this.enabled ? 1 : 0
116100
domain_name = module.this.id
@@ -153,13 +137,13 @@ resource "aws_elasticsearch_domain" "default" {
153137
dedicated_master_type = var.dedicated_master_type
154138
zone_awareness_enabled = var.zone_awareness_enabled
155139
warm_enabled = var.warm_enabled
156-
warm_count = var.warm_count
157-
warm_type = var.warm_type
140+
warm_count = var.warm_enabled ? var.warm_count : null
141+
warm_type = var.warm_enabled ? var.warm_type : null
158142

159143
dynamic "zone_awareness_config" {
160-
for_each = null_resource.azs[*].triggers
144+
for_each = var.availability_zone_count > 1 ? [true] : []
161145
content {
162-
availability_zone_count = zone_awareness_config.value.availability_zone_count
146+
availability_zone_count = var.availability_zone_count
163147
}
164148
}
165149
}
@@ -218,6 +202,9 @@ data "aws_iam_policy_document" "default" {
218202
count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
219203

220204
statement {
205+
sid = "AllowEsAccessToSpecifiedRoles"
206+
effect = "Allow"
207+
221208
actions = distinct(compact(var.iam_actions))
222209

223210
resources = [
@@ -229,14 +216,30 @@ data "aws_iam_policy_document" "default" {
229216
type = "AWS"
230217
identifiers = distinct(compact(concat(var.iam_role_arns, aws_iam_role.elasticsearch_user.*.arn)))
231218
}
219+
}
232220

233-
# This condition is for non VPC ES to allow anonymous access from whitelisted IP ranges without requests signing
234-
# https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
235-
# https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
236-
dynamic "condition" {
237-
for_each = ! var.vpc_enabled && length(var.allowed_cidr_blocks) > 0 ? [true] : []
221+
# This statement is for non VPC ES to allow anonymous access from whitelisted IP ranges without requests signing
222+
# https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
223+
# https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
224+
dynamic "statement" {
225+
for_each = length(var.allowed_cidr_blocks) > 0 && ! var.vpc_enabled ? [true] : []
226+
content {
227+
sid = "AllowAnonymousEsAccessFromCIDR"
228+
effect = "Allow"
238229

239-
content {
230+
actions = distinct(compact(var.iam_actions))
231+
232+
resources = [
233+
join("", aws_elasticsearch_domain.default.*.arn),
234+
"${join("", aws_elasticsearch_domain.default.*.arn)}/*"
235+
]
236+
237+
principals {
238+
type = "AWS"
239+
identifiers = ["*"]
240+
}
241+
242+
condition {
240243
test = "IpAddress"
241244
values = var.allowed_cidr_blocks
242245
variable = "aws:SourceIp"
@@ -270,7 +273,10 @@ module "kibana_hostname" {
270273
dns_name = var.kibana_subdomain_name == "" ? module.kibana_label.id : var.kibana_subdomain_name
271274
ttl = 60
272275
zone_id = var.dns_zone_id
273-
records = [join("", aws_elasticsearch_domain.default.*.kibana_endpoint)]
276+
# Note: kibana_endpoint is not just a domain name, it includes a path component,
277+
# and as such is not suitable for a DNS record. The plain endpoint is the
278+
# hostname portion and should be used for DNS.
279+
records = [join("", aws_elasticsearch_domain.default.*.endpoint)]
274280

275281
context = module.this.context
276282
}

0 commit comments

Comments
 (0)