Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform/nixpkgs-tarballs: use cloudtrails without cloudwatch #467

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 58 additions & 41 deletions terraform/nixpkgs-tarballs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,69 @@ resource "aws_s3_bucket" "nixpkgs-tarballs-cloudtrail-logs" {
}
}

# Attach a policy to the CloudTrail logs S3 bucket
data "aws_iam_policy_document" "nixpkgs-tarballs-cloudtrail-logs-policy" {
statement {
sid = "AWSCloudTrailAclCheck"
effect = "Allow"

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}

actions = ["s3:GetBucketAcl"]
resources = [aws_s3_bucket.nixpkgs-tarballs-cloudtrail-logs.arn]
condition {
test = "StringEquals"
variable = "aws:SourceArn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/nixpkgs-tarballs"]
}
}

statement {
sid = "AWSCloudTrailWrite"
effect = "Allow"

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}

actions = ["s3:PutObject"]
resources = ["${aws_s3_bucket.nixpkgs-tarballs-cloudtrail-logs.arn}/*"]

condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
condition {
test = "StringEquals"
variable = "aws:SourceArn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/nixpkgs-tarballs"]
}
}
}

data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}

resource "aws_s3_bucket_policy" "nixpkgs-tarballs-cloudtrail-logs-policy" {
bucket = aws_s3_bucket.nixpkgs-tarballs-cloudtrail-logs.id
policy = data.aws_iam_policy_document.nixpkgs-tarballs-cloudtrail-logs-policy.json
}

# Create a CloudTrail
resource "aws_cloudtrail" "nixpkgs-tarballs" {
name = "nixpkgs-tarballs"
s3_bucket_name = aws_s3_bucket.nixpkgs-tarballs-cloudtrail-logs.bucket
enable_log_file_validation = true
cloud_watch_logs_role_arn = aws_iam_role.nixpkgs-tarballs-cloudtrail.arn
depends_on = [aws_s3_bucket.nixpkgs-tarballs-cloudtrail-logs]
depends_on = [
aws_s3_bucket_policy.nixpkgs-tarballs-cloudtrail-logs-policy
]
# You must specify a log group and a role ARN.

event_selector {
read_write_type = "WriteOnly"
Expand All @@ -317,42 +373,3 @@ resource "aws_cloudtrail" "nixpkgs-tarballs" {
}
}
}

# Create an IAM role for CloudTrail to write logs to CloudWatch
resource "aws_iam_role" "nixpkgs-tarballs-cloudtrail" {
name = "nixpkgs-tarballs-cloudtrail"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "cloudtrail.amazonaws.com"
}
},
]
})
}

# Attach the policy to allow CloudTrail to publish to CloudWatch Logs
resource "aws_iam_role_policy" "nixpkgs-tarballs-cloudtrail" {
name = "cloudtrail-nixpkgs-policy"
role = aws_iam_role.nixpkgs-tarballs-cloudtrail.id

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
Effect = "Allow"
Resource = "*"
},
]
})
}
Loading