Skip to content

Commit 2bf8a16

Browse files
authored
Merge pull request #1 from mitre/v0.2
V0.2.0
2 parents 918ed22 + a1434f1 commit 2bf8a16

File tree

4 files changed

+91
-66
lines changed

4 files changed

+91
-66
lines changed

main.tf

Lines changed: 73 additions & 62 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,10 +195,14 @@ 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
195204
HEIMDALL_PASS_SSM_PARAM = aws_ssm_parameter.heimdall_pass_ssm_param.name
205+
HEIMDALL_PUBLIC = var.heimdall_public
196206
}
197207
}
198208

@@ -211,11 +221,12 @@ data "aws_s3_bucket" "results_bucket" {
211221
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission
212222
#
213223
resource "aws_lambda_permission" "allow_bucket" {
214-
statement_id = "AllowHeimdallPusherExecutionFromS3Bucket"
215-
action = "lambda:InvokeFunction"
216-
function_name = module.serverless-heimdall-pusher-lambda.lambda_function_arn
217-
principal = "s3.amazonaws.com"
218-
source_arn = data.aws_s3_bucket.results_bucket.arn
224+
statement_id = "AllowHeimdallPusherExecutionFromS3Bucket"
225+
action = "lambda:InvokeFunction"
226+
function_name = module.serverless-heimdall-pusher-lambda.lambda_function_arn
227+
principal = "s3.amazonaws.com"
228+
source_arn = data.aws_s3_bucket.results_bucket.arn
229+
source_account = var.results_bucket_source_account_id != null ? var.results_bucket_source_account_id : data.aws_caller_identity.current.account_id
219230
}
220231

221232
##

src/lambda_function.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def process_record(_event, bucket_name, object_key)
6666
# Save to Heimdall
6767
heimdall_user_password = heimdall_password
6868
user_id, token = get_heimdall_api_token(heimdall_user_password)
69-
push_to_heimdall(filename, hdf, user_id, token, record_contents['eval_tags'])
69+
push_to_heimdall(filename, hdf, user_id, token, record_contents['eval_tags'], record_contents['groups'])
7070

7171
# Save to S3
7272
save_results_to_bucket(record_contents, bucket_name, filename)
@@ -203,7 +203,7 @@ def get_heimdall_api_token(heimdall_user_password)
203203
# -H "Authorization: Bearer <token>" \
204204
# "http://my-heimdall/evaluations"
205205
#
206-
def push_to_heimdall(filename, hdf, user_id, token, eval_tags)
206+
def push_to_heimdall(filename, hdf, user_id, token, eval_tags, groups)
207207
$logger.info('Pushing HDF results to Heimdall Server...')
208208
url = URI("#{ENV['HEIMDALL_URL']}/evaluations")
209209
payload = {
@@ -213,6 +213,8 @@ def push_to_heimdall(filename, hdf, user_id, token, eval_tags)
213213
public: ENV['HEIMDALL_PUBLIC'] || 'true',
214214
evaluationTags: eval_tags
215215
}
216+
# Groups are broken out separately because groups may be nil/omitted
217+
payload['groups'] = groups if groups
216218
request = Net::HTTP::Post::Multipart.new(url.path, payload)
217219
request['Authorization'] = "Bearer #{token}"
218220
response = Net::HTTP.start(url.host, url.port) do |http|

variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
variable "heimdall_public" {
3+
description = "Set this to 'true' if results should be visible by anyone in Heimdall"
4+
type = string
5+
default = "false"
6+
}
7+
28
variable "heimdall_url" {
39
description = "The url to the Heimdall server in http://... format"
410
type = string
@@ -20,6 +26,12 @@ variable "results_bucket_id" {
2026
type = string
2127
}
2228

29+
variable "results_bucket_source_account_id" {
30+
description = "The AWS account ID (without a hyphen) of the results S3 bucket source owner."
31+
type = string
32+
default = null
33+
}
34+
2335
variable "subnet_ids" {
2436
description = "The subnet ids to deploy the lambda to."
2537
type = list(string)
@@ -41,5 +53,5 @@ variable "image_version" {
4153
variable "lambda_name" {
4254
description = "The name of the lambda function"
4355
type = string
44-
default = "serverless-inspec-lambda"
56+
default = "ServerlessHeimdallPusher"
4557
}

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.1
1+
0.2.0

0 commit comments

Comments
 (0)