-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
data "aws_region" "current" {} | ||
|
||
data "aws_iam_policy_document" "bucket_pol" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
statement { | ||
sid = "Allow PutObject" | ||
actions = [ | ||
"s3:PutObject" | ||
] | ||
|
||
resources = [ | ||
"${aws_s3_bucket.gd_bucket[0].arn}/*" | ||
] | ||
|
||
principals { | ||
type = "Service" | ||
identifiers = ["guardduty.amazonaws.com"] | ||
} | ||
} | ||
|
||
statement { | ||
sid = "Allow GetBucketLocation" | ||
actions = [ | ||
"s3:GetBucketLocation" | ||
] | ||
|
||
resources = [ | ||
aws_s3_bucket.gd_bucket[0].arn | ||
] | ||
|
||
principals { | ||
type = "Service" | ||
identifiers = ["guardduty.amazonaws.com"] | ||
} | ||
} | ||
} | ||
|
||
data "aws_iam_policy_document" "kms_pol" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
statement { | ||
sid = "Allow GuardDuty to encrypt findings" | ||
actions = [ | ||
"kms:GenerateDataKey" | ||
] | ||
|
||
resources = [ | ||
"arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/*" | ||
] | ||
|
||
principals { | ||
type = "Service" | ||
identifiers = ["guardduty.amazonaws.com"] | ||
} | ||
} | ||
|
||
statement { | ||
sid = "Allow all users to modify/delete key (test only)" | ||
actions = [ | ||
"kms:*" | ||
] | ||
|
||
resources = [ | ||
"arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/*" | ||
] | ||
|
||
principals { | ||
type = "AWS" | ||
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] | ||
} | ||
} | ||
|
||
} | ||
|
||
resource "aws_guardduty_detector" "gd" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
enable = true | ||
} | ||
|
||
resource "aws_s3_bucket" "gd_bucket" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
bucket = format("%s-gd-findingd-%s", var.name, data.aws_caller_identity.current.account_id) | ||
force_destroy = true | ||
} | ||
|
||
resource "aws_s3_bucket_policy" "gd_bucket_policy" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
bucket = aws_s3_bucket.gd_bucket[0].id | ||
policy = data.aws_iam_policy_document.bucket_pol[0].json | ||
} | ||
|
||
resource "aws_kms_key" "gd_key" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
description = "Temporary key for AccTest of TF" | ||
deletion_window_in_days = 7 | ||
policy = data.aws_iam_policy_document.kms_pol[0].json | ||
} | ||
|
||
resource "aws_guardduty_publishing_destination" "gd_destination" { | ||
count = var.enable_guard_duty ? 1 : 0 | ||
detector_id = aws_guardduty_detector.gd[0].id | ||
destination_arn = aws_s3_bucket.gd_bucket[0].arn | ||
kms_key_arn = aws_kms_key.gd_key[0].arn | ||
|
||
depends_on = [ | ||
aws_s3_bucket_policy.gd_bucket_policy, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import boto3 | ||
|
||
def lambda_handler(event, context): | ||
# Initialize the SNS client | ||
sns_client = boto3.client('sns') | ||
|
||
# Get the list of active AWS regions | ||
ec2_client = boto3.client('ec2') | ||
regions = [region['RegionName'] for region in ec2_client.describe_regions()['Regions']] | ||
|
||
# Iterate through each region | ||
for region in regions: | ||
# Initialize the ACM client for the current region | ||
acm_client = boto3.client('acm', region_name=region) | ||
|
||
try: | ||
# List the ACM certificates in the current region | ||
response = acm_client.list_certificates() | ||
|
||
# Process each certificate in the region | ||
for certificate in response['CertificateSummaryList']: | ||
certificate_arn = certificate['CertificateArn'] | ||
|
||
# Get the details of the ACM certificate | ||
certificate_details = acm_client.describe_certificate(CertificateArn=certificate_arn) | ||
certificate_name = certificate_details['Certificate']['DomainName'] | ||
expiration_date = certificate_details['Certificate']['NotAfter'] | ||
|
||
# Compose the email message | ||
subject = f'ACM Certificate Expiration Alert' | ||
message = f'The ACM certificate "{certificate_name}" with ARN {certificate_arn} ' \ | ||
f'in region {region} will expire on {expiration_date}.' | ||
|
||
# Replace 'your-topic-arn' with the ARN of your SNS topic | ||
topic_arn = 'arn:aws:sns:us-east-1:309017165673:acm-sns-alert' | ||
|
||
# Publish the message to the SNS topic | ||
sns_client.publish(TopicArn=topic_arn, Subject=subject, Message=message) | ||
|
||
except Exception as e: | ||
print(f'Error in region {region}: {e}') | ||
# Handle any errors that occur during the process | ||
# You can choose to log the error or take appropriate actions based on your use case. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters