Skip to content

Commit fbfe057

Browse files
refactor: use custom KMS and region replication
1 parent 781fc78 commit fbfe057

File tree

5 files changed

+54
-30
lines changed

5 files changed

+54
-30
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ repos:
125125
- id: terraform_tflint
126126
args:
127127
- --hook-config=--tf-path=tofu
128-
exclude: (modules/integrations/splunk_cloud_data_manager|modules/infra/forge_subscription/)
128+
exclude: (modules/integrations/splunk_cloud_data_manager|modules/infra/forge_subscription|modules/integrations/splunk_secrets/)
129129
always_run: true
130130
- id: terraform_validate
131131
args:
@@ -140,7 +140,7 @@ repos:
140140
args:
141141
- --hook-config=--tf-path=tofu
142142
- --args=--config=.terraform-docs.yml
143-
exclude: (modules/integrations/splunk_cloud_data_manager|modules/infra/forge_subscription/)
143+
exclude: (modules/integrations/splunk_cloud_data_manager|modules/infra/forge_subscription|modules/integrations/splunk_secrets/)
144144

145145
# Security Hooks
146146
- repo: https://github.com/gitleaks/gitleaks

modules/integrations/splunk_secrets/main.tf

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
11
locals {
22
cicd_secrets_prefix = "/cicd/common"
3-
app_tf_prefix = "/app/tf"
43

54
secrets = [
6-
7-
{
8-
name = "${local.cicd_secrets_prefix}/github_repo_access"
9-
description = "GitHub personal access token (PAT) for pulling repos from GitHub Cloud."
10-
recovery_days = 7
11-
},
125
{
13-
name = "${local.cicd_secrets_prefix}/github_private_ssh_key"
14-
description = "GitHub private SSH key used for pushing and signing commits in GitHub Cloud."
15-
recovery_days = 7
16-
},
17-
{
18-
name = "${local.app_tf_prefix}/splunk_access_ingest_token"
6+
name = "${local.cicd_secrets_prefix}/splunk_access_ingest_token"
197
description = "Splunk Observability Cloud Access Token for Ingest"
208
recovery_days = 7
219
},
2210
{
23-
name = "${local.app_tf_prefix}/splunk_access_api_token"
24-
description = "Splunk Observability Cloud Access Token for API"
25-
recovery_days = 7
26-
},
27-
{
28-
name = "${local.app_tf_prefix}/splunk_o11y_username"
11+
name = "${local.cicd_secrets_prefix}/splunk_o11y_username"
2912
description = "Splunk o11y Username"
3013
recovery_days = 7
3114
},
3215
{
33-
name = "${local.app_tf_prefix}/splunk_o11y_password"
16+
name = "${local.cicd_secrets_prefix}/splunk_o11y_password"
3417
description = "Splunk o11y Password"
3518
recovery_days = 7
3619
},
3720
{
38-
name = "${local.app_tf_prefix}/splunk_cloud_username"
21+
name = "${local.cicd_secrets_prefix}/splunk_cloud_username"
3922
description = "Splunk Cloud Username"
4023
recovery_days = 7
4124
},
4225
{
43-
name = "${local.app_tf_prefix}/splunk_cloud_password"
26+
name = "${local.cicd_secrets_prefix}/splunk_cloud_password"
4427
description = "Splunk Cloud Password"
4528
recovery_days = 7
4629
},
4730
{
48-
name = "${local.app_tf_prefix}/splunk_cloud_api_token"
31+
name = "${local.cicd_secrets_prefix}/splunk_cloud_api_token"
4932
description = "Splunk Cloud API token"
5033
recovery_days = 7
5134
},
5235
{
53-
name = "${local.app_tf_prefix}/splunk_cloud_hec_token_eks"
36+
name = "${local.cicd_secrets_prefix}/splunk_cloud_hec_token_eks"
5437
description = "Splunk Cloud HEC token for eks"
5538
recovery_days = 7
5639
},
5740
]
41+
42+
all_regions = toset(concat([var.aws_region], var.replica_regions))
5843
}
5944

6045
# Psuedo-random seeds we use for initializing the secrets. If we don't do this,
@@ -68,6 +53,14 @@ data "aws_secretsmanager_random_password" "secret_seeds" {
6853
password_length = 16
6954
}
7055

56+
resource "aws_kms_key" "regional" {
57+
for_each = local.all_regions
58+
provider = aws.by_region[each.value]
59+
60+
description = "Customer managed CMK for SecretsManager in ${each.key}"
61+
enable_key_rotation = true
62+
}
63+
7164
# Actual object containing the secret.
7265
resource "aws_secretsmanager_secret" "cicd_secrets" {
7366
for_each = {
@@ -76,26 +69,35 @@ resource "aws_secretsmanager_secret" "cicd_secrets" {
7669

7770
name = each.value.name
7871
description = each.value.description
72+
kms_key_id = aws_kms_key.regional[var.aws_region].arn
7973
recovery_window_in_days = each.value.recovery_days
8074
tags = local.all_security_tags
8175
tags_all = local.all_security_tags
8276

77+
dynamic "replica" {
78+
for_each = var.replica_regions
79+
content {
80+
region = replica.value
81+
kms_key_id = aws_kms_key.regional[replica.value].arn
82+
}
83+
}
84+
8385
}
8486

8587
# Force a delay between secret creation and seeding. We only need a few
8688
# seconds, but if we don't do this, we get into a bad state requiring manual
8789
# intervention and/or manual forced-deletion of secrets.
88-
resource "time_sleep" "wait_30_seconds" {
90+
resource "time_sleep" "wait_60_seconds" {
8991
depends_on = [
9092
aws_secretsmanager_secret.cicd_secrets,
9193
]
92-
create_duration = "30s"
94+
create_duration = "60s"
9395
}
9496

9597
# Only used for seeding purposes. Will not clobber/overwrite secrets afterward
9698
# (i.e. if/when we set them manually via the AWS CLI or management console).
9799
resource "aws_secretsmanager_secret_version" "cicd_secrets" {
98-
depends_on = [time_sleep.wait_30_seconds]
100+
depends_on = [time_sleep.wait_60_seconds]
99101
for_each = {
100102
for key, val in local.secrets : val.name => val
101103
}

modules/integrations/splunk_secrets/providers.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ provider "aws" {
88
tags = var.default_tags
99
}
1010
}
11+
12+
13+
provider "aws" {
14+
alias = "by_region"
15+
# supported by opentofu >= 1.9.0
16+
for_each = toset(local.all_regions)
17+
profile = var.aws_profile
18+
region = each.key
19+
20+
# Required, as per security guidelines.
21+
default_tags {
22+
tags = var.default_tags
23+
}
24+
}

modules/integrations/splunk_secrets/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ variable "aws_profile" {
44
}
55

66
variable "aws_region" {
7-
description = "Default AWS region."
87
type = string
8+
description = "Default AWS region."
9+
default = "us-east-1"
10+
}
11+
12+
variable "replica_regions" {
13+
description = "List of regions to replicate the secret"
14+
type = list(string)
15+
default = ["value"]
916
}
1017

1118
variable "tags" {

scripts/update-terraform-docs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ find modules/* -type f -name "*.tf" \
1919
-not -path '*/.*' \
2020
-not -path 'modules/integrations/splunk_cloud_data_manager/*' \
2121
-not -path 'modules/infra/forge_subscription/*' \
22+
-not -path 'modules/integrations/splunk_secrets/*' \
2223
-exec dirname {} \; | sort -u | xargs -I {} terraform-docs -c .terraform-docs.yml {}

0 commit comments

Comments
 (0)