Skip to content

Commit a1434f1

Browse files
author
Justin Kufro
committed
KMS log encryption, break inline permissions into an IAM policy
1 parent c7729a9 commit a1434f1

File tree

2 files changed

+67
-58
lines changed

2 files changed

+67
-58
lines changed

main.tf

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,28 @@ resource "aws_ecr_repository" "mitre_heimdall_pusher" {
3838
resource "aws_kms_key" "HeimdallPassKmsKey" {
3939
description = "The KMS key used to encrypt/decrypt HeimdallPusher's Heimdall account password "
4040
deletion_window_in_days = 10
41+
enable_key_rotation = true
4142

4243
tags = {
4344
Name = "HeimdallPusherPassKmsKey"
4445
}
4546
}
4647

48+
##
49+
# KMS key for encrypting lambda log data
50+
#
51+
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key
52+
#
53+
resource "aws_kms_key" "ServerlessHeimdallPusherLogsKmsKey" {
54+
description = "The KMS key used to encrypt ConfigToHdf's logs"
55+
deletion_window_in_days = 10
56+
enable_key_rotation = true
57+
58+
tags = {
59+
Name = "ServerlessHeimdallPusherLogsKmsKey"
60+
}
61+
}
62+
4763
##
4864
# SSM SecureString parameter for the Heimdall password
4965
#
@@ -58,7 +74,51 @@ resource "aws_ssm_parameter" "heimdall_pass_ssm_param" {
5874
}
5975

6076
##
61-
# HeimdallPusher Role to Invoke HeimdallPusher Lambda function
77+
# HeimdallPusher IAM Policy to Invoke HeimdallPusher Lambda function
78+
#
79+
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy
80+
#
81+
resource "aws_iam_policy" "serverless_heimdall_pusher_lambda_policy" {
82+
name = "ServerlessHeimdallPusherLambdaPolicy"
83+
path = "/"
84+
description = "Policy that provides proper lambda permissions for the serverless_heimdall_pusher_lambda_role"
85+
86+
# Permissions
87+
# - ssm:GetParameter => allows decryption of Heimdall Password
88+
# - kms:Decrypt => allows decryption of Heimdall Password
89+
# - s3:GetObject,PutObject,DeleteObject => Allow interaction with results S3 bucket
90+
policy = jsonencode({
91+
Version = "2012-10-17"
92+
Statement = [
93+
{
94+
Action = [
95+
"ssm:GetParameter"
96+
]
97+
Effect = "Allow"
98+
Resource = aws_ssm_parameter.heimdall_pass_ssm_param.arn
99+
},
100+
{
101+
Action = [
102+
"kms:Decrypt"
103+
]
104+
Effect = "Allow"
105+
Resource = aws_kms_key.HeimdallPassKmsKey.arn
106+
},
107+
{
108+
Action = [
109+
"s3:GetObject",
110+
"s3:PutObject",
111+
"s3:DeleteObject"
112+
]
113+
Effect = "Allow"
114+
Resource = "${data.aws_s3_bucket.results_bucket.arn}/*"
115+
}
116+
]
117+
})
118+
}
119+
120+
##
121+
# HeimdallPusher Role to Invoke HeimdallPusher Lambda function
62122
#
63123
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
64124
#
@@ -68,7 +128,8 @@ resource "aws_iam_role" "serverless_heimdall_pusher_lambda_role" {
68128
# Allow execution of the lambda function
69129
managed_policy_arns = [
70130
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
71-
"arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
131+
"arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole",
132+
aws_iam_policy.serverless_heimdall_pusher_lambda_policy.arn
72133
]
73134

74135
# Allow assume role permission for lambda
@@ -85,61 +146,6 @@ resource "aws_iam_role" "serverless_heimdall_pusher_lambda_role" {
85146
}
86147
]
87148
})
88-
89-
# Allow READ access to Heimdall password SSM parameter
90-
inline_policy {
91-
name = "HeimdallPassSsmReadAccess"
92-
93-
policy = jsonencode({
94-
Version = "2012-10-17"
95-
Statement = [
96-
{
97-
Action = [
98-
"ssm:GetParameter"
99-
]
100-
Effect = "Allow"
101-
Resource = aws_ssm_parameter.heimdall_pass_ssm_param.arn
102-
}
103-
]
104-
})
105-
}
106-
107-
inline_policy {
108-
name = "AllowHeimdallPassKmsKeyDecrypt"
109-
110-
policy = jsonencode({
111-
Version = "2012-10-17"
112-
Statement = [
113-
{
114-
Action = [
115-
"kms:Decrypt"
116-
]
117-
Effect = "Allow"
118-
Resource = aws_kms_key.HeimdallPassKmsKey.arn
119-
}
120-
]
121-
})
122-
}
123-
124-
# Allow S3 read and write access to InSpec results bucket
125-
inline_policy {
126-
name = "S3ResultsAccess"
127-
128-
policy = jsonencode({
129-
Version = "2012-10-17"
130-
Statement = [
131-
{
132-
Action = [
133-
"s3:GetObject",
134-
"s3:PutObject",
135-
"s3:DeleteObject"
136-
]
137-
Effect = "Allow"
138-
Resource = "${data.aws_s3_bucket.results_bucket.arn}/*"
139-
}
140-
]
141-
})
142-
}
143149
}
144150

145151
resource "null_resource" "push_image" {
@@ -189,6 +195,9 @@ module "serverless-heimdall-pusher-lambda" {
189195
image_uri = "${aws_ecr_repository.mitre_heimdall_pusher.repository_url}:${local.image_version}"
190196
package_type = "Image"
191197

198+
cloudwatch_logs_kms_key_id = aws_kms_key.ServerlessHeimdallPusherLogsKmsKey.key_id
199+
cloudwatch_logs_retention_in_days = 30
200+
192201
environment_variables = {
193202
HEIMDALL_URL = var.heimdall_url
194203
HEIMDALL_API_USER = var.heimdall_user

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ variable "image_version" {
5353
variable "lambda_name" {
5454
description = "The name of the lambda function"
5555
type = string
56-
default = "serverless-inspec-lambda"
56+
default = "ServerlessHeimdallPusher"
5757
}

0 commit comments

Comments
 (0)