forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecrets.tf
More file actions
119 lines (101 loc) · 4.38 KB
/
Copy pathsecrets.tf
File metadata and controls
119 lines (101 loc) · 4.38 KB
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
# ── Random master password ────────────────────────────────────────────────────
resource "random_password" "rds_master" {
length = 32
special = true
override_special = "!#$%&*()-_=+[]{}<>:?"
}
# ── Secrets Manager secret (stores master credentials) ───────────────────────
resource "aws_secretsmanager_secret" "rds_master" {
name = "nova-rewards/${var.environment}/rds-master"
description = "Nova Rewards RDS master credentials"
recovery_window_in_days = 7
tags = {
Environment = var.environment
Project = "nova-rewards"
}
}
resource "aws_secretsmanager_secret_version" "rds_master" {
secret_id = aws_secretsmanager_secret.rds_master.id
secret_string = jsonencode({
username = var.db_master_username
password = random_password.rds_master.result
host = aws_db_instance.nova.address
port = 5432
dbname = var.db_name
})
# Replaced by rotation after first apply; ignore drift
lifecycle {
ignore_changes = [secret_string]
}
}
# ── Automatic 90-day rotation ─────────────────────────────────────────────────
resource "aws_secretsmanager_secret_rotation" "rds_master" {
secret_id = aws_secretsmanager_secret.rds_master.id
rotation_lambda_arn = aws_lambda_function.secret_rotation.arn
rotation_rules {
automatically_after_days = 90
}
}
# ── Rotation Lambda (uses AWS-managed SecretsManager rotation for RDS PG) ────
data "aws_iam_policy_document" "lambda_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "rotation_lambda" {
name = "nova-rewards-secret-rotation-${var.environment}"
assume_role_policy = data.aws_iam_policy_document.lambda_assume.json
}
resource "aws_iam_role_policy_attachment" "rotation_lambda_basic" {
role = aws_iam_role.rotation_lambda.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "rotation_lambda_vpc" {
role = aws_iam_role.rotation_lambda.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
data "aws_iam_policy_document" "rotation_lambda_secrets" {
statement {
actions = ["secretsmanager:*", "rds-db:connect"]
resources = [aws_secretsmanager_secret.rds_master.arn]
}
}
resource "aws_iam_role_policy" "rotation_lambda_secrets" {
name = "allow-secret-rotation"
role = aws_iam_role.rotation_lambda.id
policy = data.aws_iam_policy_document.rotation_lambda_secrets.json
}
# Deploy the AWS-provided Serverless Application for RDS PostgreSQL rotation
resource "aws_serverlessapplicationrepository_cloudformation_stack" "rotation" {
name = "nova-rewards-rds-rotation-${var.environment}"
application_id = "arn:aws:serverlessrepo:us-east-1:297356227824:applications/SecretsManagerRDSPostgreSQLRotationSingleUser"
# Use latest semantic version — pin in production
semantic_version = "1.1.367"
parameters = {
functionName = "nova-rewards-rds-rotation-${var.environment}"
endpoint = "https://secretsmanager.${var.aws_region}.amazonaws.com"
}
capabilities = ["CAPABILITY_IAM", "CAPABILITY_RESOURCE_POLICY"]
}
locals {
rotation_lambda_arn = aws_serverlessapplicationrepository_cloudformation_stack.rotation.outputs["RotationLambdaARN"]
}
# Override the placeholder used above with the real ARN from SAR
resource "aws_lambda_function" "secret_rotation" {
# This is a data-only reference; the actual function is managed by SAR above.
# We create a minimal shim so the rotation resource has a valid ARN to reference
# before SAR output is available. In practice, reference locals.rotation_lambda_arn.
function_name = "nova-rewards-rds-rotation-${var.environment}"
role = aws_iam_role.rotation_lambda.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.11"
filename = "${path.module}/rotation_placeholder.zip"
lifecycle {
# SAR manages the actual code; ignore all changes after initial creation
ignore_changes = all
}
}