Skip to content

Commit

Permalink
fix: DBTP-1089 Move to shared log resource policy (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Kate Sugden <[email protected]>
  • Loading branch information
WillGibson and ksugden authored Jun 27, 2024
1 parent 81c7bd7 commit 9527e75
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 48 deletions.
7 changes: 0 additions & 7 deletions extensions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,3 @@ resource "aws_ssm_parameter" "addons" {
value = jsonencode(var.args.services)
tags = local.tags
}

module "logs" {
source = "../logs"

application = var.args.application
environment = var.environment
}
8 changes: 4 additions & 4 deletions logs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data "aws_iam_policy_document" "log-resource-policy" {
"logs:PutLogEvents",
]

resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/copilot/${var.application}-${var.environment}-*:log-stream:*"]
resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/copilot/*:log-stream:*"]

condition {
test = "StringEquals"
Expand All @@ -36,7 +36,7 @@ data "aws_iam_policy_document" "log-resource-policy" {
"logs:PutLogEvents",
]

resources = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/elasticache/${var.application}/${var.environment}/*"]
resources = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/elasticache/*"]

condition {
test = "ArnLike"
Expand All @@ -62,7 +62,7 @@ data "aws_iam_policy_document" "log-resource-policy" {
"logs:PutLogEvents",
]

resources = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/opensearch/${var.application}/${var.environment}/*"]
resources = ["arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/opensearch/*/*"]

condition {
test = "StringEquals"
Expand All @@ -79,5 +79,5 @@ data "aws_iam_policy_document" "log-resource-policy" {

resource "aws_cloudwatch_log_resource_policy" "log-resource-policy" {
policy_document = data.aws_iam_policy_document.log-resource-policy.json
policy_name = "${var.application}-${var.environment}-LogResourcePolicy"
policy_name = "${var.name_prefix}-LogResourcePolicy"
}
3 changes: 3 additions & 0 deletions logs/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "log-resource-policy" {
value = aws_cloudwatch_log_resource_policy.log-resource-policy.policy_name
}
15 changes: 7 additions & 8 deletions logs/tests/unit.tftest.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
variables {
application = "log-test-application"
environment = "log-test-environment"
name_prefix = "test-name"
}

run "log_resource_policy_unit_test" {
Expand All @@ -22,8 +21,8 @@ run "log_resource_policy_unit_test" {
}

assert {
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[0].Resource, "log-group:/copilot/log-test-application-log-test-environment-*:log-stream:*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/copilot/log-test-application-log-test-environment-*:log-stream:*"
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[0].Resource, "log-group:/copilot/*:log-stream:*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/copilot/*:log-stream:*"
}

assert {
Expand All @@ -42,8 +41,8 @@ run "log_resource_policy_unit_test" {
}

assert {
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[1].Resource, "log-group:/aws/elasticache/log-test-application/log-test-environment/*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/aws/elasticache/log-test-application/log-test-environment/*"
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[1].Resource, "log-group:/aws/elasticache/*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/aws/elasticache/*"
}

assert {
Expand All @@ -57,7 +56,7 @@ run "log_resource_policy_unit_test" {
}

assert {
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[2].Resource, "log-group:/aws/opensearch/log-test-application/log-test-environment/*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/aws/opensearch/log-test-application/log-test-environment/*"
condition = strcontains(jsondecode(data.aws_iam_policy_document.log-resource-policy.json).Statement[2].Resource, "log-group:/aws/opensearch/*")
error_message = "Invalid value for aws_iam_policy_document log_resource_policy statement resource should contain log-group:/aws/opensearch/*"
}
}
9 changes: 3 additions & 6 deletions logs/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
variable "application" {
type = string
}

variable "environment" {
type = string
variable "name_prefix" {
default = null
type = string
}
23 changes: 0 additions & 23 deletions opensearch/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ resource "aws_cloudwatch_log_group" "opensearch_log_group_audit_logs" {
retention_in_days = coalesce(var.config.audit_log_retention_in_days, 7)
}

resource "aws_cloudwatch_log_resource_policy" "opensearch_log_group_policy" {
policy_name = "opensearch_log_group_policy"
policy_document = <<CONFIG
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "es.amazonaws.com"
},
"Action": [
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
"logs:CreateLogStream"
],
"Resource": "arn:aws:logs:*"
}
]
}
CONFIG
}

resource "aws_security_group" "opensearch-security-group" {
name = local.domain_name
vpc_id = data.aws_vpc.vpc.id
Expand Down
5 changes: 5 additions & 0 deletions vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,8 @@ data "aws_subnets" "private-subnets" {
values = ["${var.arg_name}-private-*"]
}
}

module "logs" {
source = "../logs"
name_prefix = var.arg_name
}
9 changes: 9 additions & 0 deletions vpc/tests/unit.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,12 @@ run "aws_default_network_acl_unit_test" {
error_message = "Invalid default network ACL"
}
}

run "log_resource_policy_unit_test" {
command = plan

assert {
condition = module.logs.log-resource-policy == "vpc-test-name-LogResourcePolicy"
error_message = "Should be: vpc-test-name-LogResourcePolicy"
}
}

0 comments on commit 9527e75

Please sign in to comment.