Skip to content

Commit 5ae8197

Browse files
authored
Update policy and secret access for opensearch (#11)
1 parent 33afa18 commit 5ae8197

File tree

6 files changed

+79
-20
lines changed

6 files changed

+79
-20
lines changed

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ locals {
7777
module.postgres_admin_login[*],
7878
module.redis_token[*],
7979
module.secret_key[*],
80+
module.opensearch[0].secret_details,
8081
values(module.developer_managed_secrets),
8182
)
8283
}

modules/opensearch/main.tf

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ resource "aws_opensearch_domain" "this" {
9696
}
9797
}
9898

99-
dedicated_master_count = try(cluster_config.value.dedicated_master_count, 3)
100-
dedicated_master_enabled = try(cluster_config.value.dedicated_master_enabled, true)
101-
dedicated_master_type = try(cluster_config.value.dedicated_master_type, "c6g.large.search")
102-
instance_count = try(cluster_config.value.instance_count, 3)
103-
instance_type = try(cluster_config.value.instance_type, "r6g.large.search")
104-
warm_count = try(cluster_config.value.warm_count, null)
105-
warm_enabled = try(cluster_config.value.warm_enabled, null)
106-
warm_type = try(cluster_config.value.warm_type, null)
99+
dedicated_master_count = try(cluster_config.value.dedicated_master_count, 3)
100+
dedicated_master_enabled = try(cluster_config.value.dedicated_master_enabled, true)
101+
dedicated_master_type = try(cluster_config.value.dedicated_master_type, "c6g.large.search")
102+
instance_count = try(cluster_config.value.instance_count, 3)
103+
instance_type = try(cluster_config.value.instance_type, "r6g.large.search")
104+
warm_count = try(cluster_config.value.warm_count, null)
105+
warm_enabled = try(cluster_config.value.warm_enabled, null)
106+
warm_type = try(cluster_config.value.warm_type, null)
107107

108108
dynamic "zone_awareness_config" {
109109
for_each = try([cluster_config.value.zone_awareness_config], [])
@@ -163,7 +163,7 @@ resource "aws_opensearch_domain" "this" {
163163
}
164164
}
165165

166-
engine_version = var.engine_version
166+
engine_version = var.engine_version
167167

168168
dynamic "log_publishing_options" {
169169
for_each = { for opt in var.log_publishing_options : opt.log_type => opt }
@@ -305,7 +305,7 @@ resource "aws_opensearch_domain_saml_options" "this" {
305305
resource "aws_opensearch_outbound_connection" "this" {
306306
for_each = { for k, v in var.outbound_connections : k => v if var.create }
307307

308-
connection_alias = try(each.value.connection_alias, each.key)
308+
connection_alias = try(each.value.connection_alias, each.key)
309309

310310
local_domain_info {
311311
owner_id = try(each.value.local_domain_info.owner_id, local.account_id)
@@ -447,3 +447,20 @@ resource "aws_vpc_security_group_egress_rule" "this" {
447447

448448
tags = merge(local.tags, var.security_group_tags, try(each.value.tags, {}))
449449
}
450+
451+
module "elasticsearch_secret" {
452+
source = "github.com/thoughtbot/terraform-aws-secrets//secret?ref=v0.4.0"
453+
454+
admin_principals = var.admin_principals
455+
description = "Elastisearch secrets for: ${local.name}"
456+
name = "${local.name}-secret"
457+
read_principals = var.read_principals
458+
resource_tags = var.tags
459+
460+
initial_value = jsonencode({
461+
ES_ENDPOINT = module.opensearch[0].domain_endpoint
462+
ES_DASHBOARD_ENDPOINT = module.opensearch[0].domain_dashboard_endpoint
463+
ES_DOMAIN_ID = module.opensearch[0].domain_id
464+
ES_PASSWORD = random_password.es.result
465+
})
466+
}

modules/opensearch/outputs.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,21 @@ output "security_group_arn" {
5252
output "security_group_id" {
5353
description = "ID of the security group"
5454
value = try(aws_security_group.this[0].id, null)
55+
}
56+
57+
################################################################################
58+
# Secret details
59+
################################################################################
60+
61+
output "secret_details" {
62+
description = "Map containing secret details for opensearch credentials"
63+
value = [
64+
{
65+
name = secret.secret_name
66+
environment_variables = ["ES_ENDPOINT", "ES_DASHBOARD_ENDPOINT", "ES_DOMAIN_ID", "ES_PASSWORD"]
67+
policy_json = module.elasticsearch_secret.policy_json
68+
kms_key_arn = module.elasticsearch_secret.kms_key_arn
69+
secret_arn = module.elasticsearch_secret.arn
70+
}
71+
]
5572
}

modules/opensearch/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "application_name" {
2+
type = string
3+
description = "Unique name for the opensearch instance"
4+
default = ""
5+
}
6+
17
variable "create" {
28
description = "Determines whether resources will be created (affects all resources)"
39
type = bool
@@ -271,3 +277,15 @@ variable "security_group_tags" {
271277
type = map(string)
272278
default = {}
273279
}
280+
281+
variable "admin_principals" {
282+
description = "Principals allowed to peform admin actions (default: current account)"
283+
type = list(string)
284+
default = null
285+
}
286+
287+
variable "read_principals" {
288+
description = "Principals allowed to read the secret (default: current account)"
289+
type = list(string)
290+
default = null
291+
}

opensearch-variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ variable "es_engine_version" {
4444
description = "Version of Elasticsearch to deploy."
4545
}
4646

47-
variable "admin_principals" {
47+
variable "es_admin_principals" {
4848
description = "Principals allowed to peform admin actions (default: current account)"
4949
type = list(string)
5050
default = null
5151
}
5252

53-
variable "read_principals" {
53+
variable "es_read_principals" {
5454
description = "Principals allowed to read the secret (default: current account)"
5555
type = list(string)
5656
default = null

opensearch.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ module "opensearch" {
9090
{ log_type = "SEARCH_SLOW_LOGS" },
9191
]
9292

93+
application_name = var.es_application_name
94+
95+
admin_principals = var.es_admin_principals
96+
97+
read_principals = var.es_read_principals
98+
9399
node_to_node_encryption = {
94100
enabled = true
95101
}
@@ -140,41 +146,41 @@ resource "random_password" "es" {
140146
special = false
141147
}
142148

143-
module "secret" {
149+
module "elasticsearch_secret" {
144150
count = var.elasticsearch_enabled ? 1 : 0
145151
source = "github.com/thoughtbot/terraform-aws-secrets//secret?ref=v0.4.0"
146152

147153
admin_principals = var.admin_principals
148-
description = "Elastisearch password for: ${local.name}"
154+
description = "Elastisearch secrets for: ${local.name}"
149155
name = "${local.name}-secret"
150156
read_principals = var.read_principals
151157
resource_tags = var.tags
152158

153159
initial_value = jsonencode({
154160
ES_ENDPOINT = module.opensearch[0].domain_endpoint
155161
ES_DASHBOARD_ENDPOINT = module.opensearch[0].domain_dashboard_endpoint
156-
DOMAIN_ID = module.opensearch[0].domain_id
157-
PASSWORD = random_password.es.result
162+
ES_DOMAIN_ID = module.opensearch[0].domain_id
163+
ES_PASSWORD = random_password.es.result
158164
})
159165
}
160166

161167
resource "aws_iam_role_policy_attachment" "test-attach" {
162-
count = var.elasticsearch_enabled ? 1 : 0
168+
count = var.elasticsearch_enabled ? 1 : 0
163169

164170
role = module.pod_role.name
165171
policy_arn = "arn:aws:iam::aws:policy/aws-service-role/AmazonElasticsearchServiceRolePolicy"
166172

167-
depends_on = [ module.pod_policy ]
173+
depends_on = [module.pod_policy]
168174
}
169175

170176
module "pod_policy" {
171177
count = var.elasticsearch_enabled ? 1 : 0
172178
source = "github.com/thoughtbot/flightdeck//aws/service-account-policy?ref=v0.9.0"
173179

174180
name = "es-${var.es_application_name}-pods"
175-
policy_documents = module.secret[*].policy_json
181+
policy_documents = module.opensearch[*].secret_details.policy_json
176182

177-
role_names = [module.pod_role.name]
183+
role_names = [module.pod_role.name]
178184
}
179185

180186
data "aws_region" "current" {}

0 commit comments

Comments
 (0)