Skip to content

Conversation

@StealthBadger747
Copy link

@StealthBadger747 StealthBadger747 commented Jan 28, 2025

what

  • Describe high-level what changed as a result of these commits (i.e. in plain-english, what do these changes mean?)
  • Use bullet points to be concise and to the point.

I changed how the IAM Bucket policy is reference in order to fix an error I was getting.

Additionally for compliance I have included changes to use CMK KMS as well.

why

  • Provide the justifications for the changes (e.g. business case).
  • Describe why these changes were made (e.g. why do these commits fix the problem?)
  • Use bullet points to be concise and to the point.

I was experiencing this error:

Error: Invalid value for input variable

  on main.tf line 143, in module "bucket_policy":
  143:   iam_policy_statements = try(lookup(local.policy, "Statement"), null)

The given value is not suitable for module.bucket_policy.var.iam_policy_statements declared at .terraform/modules/bucket_policy/variables.tf:37,1-33: map of object required.

And because https://github.com/cloudposse/terraform-aws-iam-policy/blob/660de2d894c7b4aebd8f98bd061faad5b028a107/variables.tf#L37-L65 requires a map of objects, I needed to change how the policy was being referenced in order to fulfill that requirement.

Other details

I also in my stack needed to add a name, otherwise I'd get the following errors:

    datadog-logs-archive:
      vars:
        enabled: true
        name: datadog-logs-archive
        catchall_enabled: true
        enable_glacier_transition: true
        glacier_transition_days: 365

Errors:

module.cloudtrail_s3_bucket[0].aws_s3_bucket_lifecycle_configuration.default[0]: Creation complete after 32s [id=hl-core-uw2-audit-datadog-logs-archive-cloudtrail]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [40s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [50s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m0s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m10s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m20s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m30s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m40s elapsed]
module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0]: Still creating... [1m50s elapsed]
╷
│ Error: putting S3 Bucket (hl-core-uw2-audit-datadog-logs-archive-cloudtrail) Policy: operation error S3: PutBucketPolicy, https response error StatusCode: 400, RequestID: N4BWPTREK6FJWBPK, HostID: an/L2NP8qnWpmFMujsJPJsu1LgCaLHKmEDMffuZDlM5webavxIIRQjRLOcI+Vqx1/XJfUI0iYr0=, api error MalformedPolicy: Policy has invalid resource
│ 
│   with module.cloudtrail_s3_bucket[0].aws_s3_bucket_policy.default[0],
│   on .terraform/modules/cloudtrail_s3_bucket/main.tf line 461, in resource "aws_s3_bucket_policy" "default":
│  461: resource "aws_s3_bucket_policy" "default" {
│ 
╵
exit status 1

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added encryption with AWS KMS for S3 buckets and CloudTrail logs.
    • Introduced a dedicated KMS key for Datadog Log Archives with detailed access policies.
    • Made Glacier transition days configurable for CloudTrail bucket lifecycle rules.
  • Refactor
    • Updated IAM policy configuration to a conditional and structured format.

@StealthBadger747 StealthBadger747 requested review from a team as code owners January 28, 2025 21:15
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 28, 2025

Walkthrough

The pull request modifies src/main.tf to replace the bucket_policy module's iam_policy_statements parameter with a conditional iam_policy object, adds KMS encryption parameters to archive_bucket and cloudtrail_s3_bucket modules, and makes lifecycle rules configurable. It also updates the cloudtrail module to use the KMS key. A new kms_key_archive module is introduced in src/kms-key.tf to create a KMS key with a detailed policy for Datadog Log Archives.

Changes

File Change Summary
src/main.tf - Replaced iam_policy_statements with conditional iam_policy in bucket_policy
- Added kms_master_key_arn and sse_algorithm to archive_bucket and cloudtrail_s3_bucket
- Updated lifecycle rules to use variable var.glacier_transition_days
- Added kms_key_arn to cloudtrail module
src/kms-key.tf - Added module "kms_key_archive" for KMS key creation
- Added data "aws_iam_policy_document" "kms_key_archive" for KMS key policy with permissions for AWS root, CloudTrail, and Datadog role

Poem

🐇✨
A key to guard the logs so bright,
Policies crafted just right,
Buckets wrapped in encrypted light,
Datadog’s watchful eyes in sight,
Terraform hops with all its might,
Secure and sound, day and night! 🔐🌿

Suggested labels

migration


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mergify
Copy link

mergify bot commented Jan 28, 2025

Important

Description is necessary and should not be empty.

Kindly provide details with what was changed, why it was changed.

@mergify
Copy link

mergify bot commented Jan 28, 2025

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟠 Require terratest

Waiting checks: test/terratest.

This rule require terratest status

  • check-success = test/terratest

@mergify mergify bot added the needs-test Needs testing label Jan 29, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/kms-key.tf (1)

1-11: Consider updating the KMS key module version.

The configuration looks good with appropriate security settings (key rotation and deletion window). However, consider updating to the latest version 0.12.3 of the cloudposse/kms-key/aws module for potential bug fixes and improvements.

 module "kms_key_archive" {
   source  = "cloudposse/kms-key/aws"
-  version = "0.12.2"
+  version = "0.12.3"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1995c9e and f32c0c2.

📒 Files selected for processing (2)
  • src/kms-key.tf (1 hunks)
  • src/main.tf (6 hunks)
🧰 Additional context used
🪛 Checkov (3.2.334)
src/kms-key.tf

[HIGH] 13-90: Ensure no IAM policies documents allow "*" as a statement's resource for restrictable actions

(CKV_AWS_356)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (7)
src/kms-key.tf (3)

16-27: LGTM! Root account permissions are properly configured.

The statement follows AWS best practices by allowing root account access to the KMS key.


29-48: LGTM! CloudTrail permissions follow least privilege principle.

The statement correctly restricts CloudTrail's KMS permissions to only necessary encryption operations and uses appropriate conditions to limit scope.


50-89: LGTM! Datadog role permissions are properly secured.

The statements provide necessary KMS permissions for Datadog while implementing proper security controls:

  • Key usage permissions are limited to essential operations
  • Grant management is restricted to AWS services via the kms:GrantIsForAWSResource condition
src/main.tf (4)

143-145: LGTM! IAM policy reference has been fixed.

The change correctly implements the required map of objects structure for the IAM policy, addressing the original issue mentioned in the PR objectives.


172-174: LGTM! Server-side encryption is properly configured.

The archive bucket is correctly configured to use KMS encryption with the newly created key.


313-313: LGTM! CloudTrail KMS integration is complete.

The CloudTrail module is correctly configured to use the new KMS key for encryption, completing the end-to-end encryption setup.


Line range hint 226-239: Verify the impact of the major version upgrade.

While the KMS encryption configuration looks good, upgrading the s3-bucket module from 3.1.2 to 4.10.0 is a major version jump that might include breaking changes.

✅ Verification successful

The version upgrade from 3.1.2 to 4.10.0 is safe to proceed.

The code review shows that none of the breaking changes introduced in version 4.0.0 affect this implementation:

  • No usage of deprecated policy input
  • No logging configuration to migrate
  • No replication rules to update
  • KMS encryption is properly configured
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for breaking changes in the s3-bucket module upgrade

# Fetch the changelog
curl -s https://raw.githubusercontent.com/cloudposse/terraform-aws-s3-bucket/master/CHANGELOG.md | grep -A 10 "## \[4.0.0\]"

Length of output: 126


Script:

#!/bin/bash
# Description: Fetch release information using GitHub API for version comparison

# Fetch releases between 3.1.2 and 4.10.0
curl -s -H "Accept: application/vnd.github.v3+json" \
     "https://api.github.com/repos/cloudposse/terraform-aws-s3-bucket/releases" | \
jq -r '.[] | select(.tag_name | startswith("4.") or startswith("3.")) | "\(.tag_name)\n\(.body)"'

Length of output: 45008

@StealthBadger747
Copy link
Author

Updated with KMS as per our discussion in office hours.

@StealthBadger747 StealthBadger747 changed the title Fix iam policy referencing Fix iam policy referencing and add KMS Jan 29, 2025
@@ -0,0 +1,90 @@
module "kms_key_archive" {
source = "cloudposse/kms-key/aws"
version = "0.12.2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for folks who don't want to kms encrypt this. could we add a variable that sets the enabled field for this module?

Suggested change
version = "0.12.2"
version = "0.12.2"
enabled = var.enable_kms_encryption

Also, you'll probably want to output the arn of the kms key

@mergify
Copy link

mergify bot commented Feb 27, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Feb 27, 2025
@mergify
Copy link

mergify bot commented Mar 6, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Mar 6, 2025
@mergify
Copy link

mergify bot commented Mar 13, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Mar 13, 2025
@mergify
Copy link

mergify bot commented Mar 20, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Mar 20, 2025
@mergify
Copy link

mergify bot commented Mar 27, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Mar 27, 2025
@mergify
Copy link

mergify bot commented Apr 3, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Apr 3, 2025
@mergify
Copy link

mergify bot commented Apr 10, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify
Copy link

mergify bot commented Aug 7, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Aug 7, 2025
@mergify
Copy link

mergify bot commented Aug 7, 2025

Thanks @StealthBadger747 for creating this pull request!

A maintainer will review your changes shortly. Please don't be discouraged if it takes a while.

While you wait, make sure to review our contributor guidelines.

Tip

Need help or want to ask for a PR review to be expedited?

Join us on Slack in the #pr-reviews channel.

@mergify
Copy link

mergify bot commented Aug 14, 2025

💥 This pull request now has conflicts. Could you fix it @StealthBadger747? 🙏

@mergify
Copy link

mergify bot commented Aug 14, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added conflict This PR has conflicts stale This PR has gone stale and removed stale This PR has gone stale labels Aug 14, 2025
@mergify
Copy link

mergify bot commented Aug 21, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Aug 21, 2025
@mergify
Copy link

mergify bot commented Aug 28, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Aug 28, 2025
@mergify
Copy link

mergify bot commented Sep 4, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Sep 4, 2025
@mergify
Copy link

mergify bot commented Sep 11, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Sep 11, 2025
@mergify
Copy link

mergify bot commented Sep 18, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Sep 18, 2025
@mergify
Copy link

mergify bot commented Sep 25, 2025

Heads up! This pull request looks stale. It will be closed soon, if there are no new commits. ⏳

@mergify mergify bot added stale This PR has gone stale and removed stale This PR has gone stale labels Sep 25, 2025
@StealthBadger747
Copy link
Author

I no longer have the setup to test this, feel free to take what is here and make it work though

@mergify mergify bot removed the conflict This PR has conflicts label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-test Needs testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants