Skip to content

Commit ecb9297

Browse files
authored
Add read permissions for blacklist file (#16)
* Add read permissions for blacklist file * fmt * remove redudant if statements * remove empty string/ comparison
1 parent 6e71c1a commit ecb9297

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

locals.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ locals {
1313
# Please do not change or replace the 'frontend' suffix since there a logic in the bot based in it
1414
api_triggered_function_name = local.single_lambda_integration ? local.resource_name_pattern : "${local.resource_name_pattern}-frontend"
1515
# Merge user env vars with env vars which are not based on user input
16-
env_vars = merge(var.env_vars, { HOME = "/tmp" })
16+
env_vars = merge(var.env_vars, { HOME = "/tmp" })
17+
blacklist_file_arn = contains(keys(var.env_vars), "S3_BLACK_LIST_OBJECT_KEY") && contains(keys(var.env_vars), "S3_BLACK_LIST_BUCKET_NAME") ? "arn:aws:s3:::${var.env_vars.S3_BLACK_LIST_BUCKET_NAME}/${var.env_vars.S3_BLACK_LIST_OBJECT_KEY}" : null
1718
}

modules/role/role.tf

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
locals {
2+
should_create_s3_policy = var.blacklist_object_arn != null ? 1 : 0
3+
}
4+
5+
16
data "aws_iam_policy_document" "assume_role_policy" {
27
statement {
38
sid = ""
@@ -22,6 +27,32 @@ resource "aws_iam_role" "lambda_execution_role" {
2227
)
2328
}
2429

30+
data "aws_iam_policy_document" "s3_policy_document" {
31+
count = local.should_create_s3_policy
32+
statement {
33+
sid = ""
34+
effect = "Allow"
35+
actions = ["s3:GetObject"]
36+
resources = [var.blacklist_object_arn]
37+
}
38+
}
39+
40+
resource "aws_iam_policy" "s3_iam_policy" {
41+
count = local.should_create_s3_policy
42+
policy = data.aws_iam_policy_document.s3_policy_document[count.index].json
43+
44+
tags = merge(
45+
var.global_tags,
46+
lookup(var.tags, "iam", {}),
47+
)
48+
}
49+
50+
resource "aws_iam_role_policy_attachment" "s3_policy_attachment" {
51+
count = local.should_create_s3_policy
52+
role = aws_iam_role.lambda_execution_role.name
53+
policy_arn = aws_iam_policy.s3_iam_policy[count.index].arn
54+
}
55+
2556
data "aws_iam_policy_document" "secrets_policy_document" {
2657
statement {
2758
sid = ""

modules/role/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ variable "secrets_arns" {
99
default = []
1010
}
1111

12+
variable "blacklist_object_arn" {
13+
description = "Arn of the blacklist file"
14+
type = string
15+
default = null
16+
}
17+
1218
variable "global_tags" {
1319
type = map(string)
1420
description = "A list of tags to apply on all newly created resources."

shared.tf

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ module "lambda_role" {
3030
tags = var.tags
3131
global_tags = var.global_tags
3232
multiple_lambda_integration = local.multiple_lambda_integration
33+
blacklist_object_arn = local.blacklist_file_arn
3334
}

0 commit comments

Comments
 (0)