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

Feature/kmskeyrotation #27

Open
wants to merge 23 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion deployment/build_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
PACKAGES_DIR="${SCRIPT_PATH}/packages/"
LIBRARY="${SCRIPT_PATH}/../hammer/library"

LAMBDAS="ami-info logs-forwarder ddb-tables-backup sg-issues-identification s3-acl-issues-identification s3-policy-issues-identification iam-keyrotation-issues-identification iam-user-inactive-keys-identification cloudtrails-issues-identification ebs-unencrypted-volume-identification ebs-public-snapshots-identification rds-public-snapshots-identification sqs-public-policy-identification s3-unencrypted-bucket-issues-identification rds-unencrypted-instance-identification"
LAMBDAS="ami-info logs-forwarder ddb-tables-backup sg-issues-identification s3-acl-issues-identification s3-policy-issues-identification iam-keyrotation-issues-identification iam-user-inactive-keys-identification cloudtrails-issues-identification ebs-unencrypted-volume-identification ebs-public-snapshots-identification rds-public-snapshots-identification sqs-public-policy-identification s3-unencrypted-bucket-issues-identification rds-unencrypted-instance-identification kms-keyrotation-issues-identification"

pushd "${SCRIPT_PATH}" > /dev/null
pushd ../hammer/identification/lambdas > /dev/null
Expand Down
32 changes: 32 additions & 0 deletions deployment/cf-templates/ddb.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,38 @@
},
"TableName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" }, "rds-unencrypted" ] ]}
}
},
"DynamoDBKMSKeyRotation": {
"Type": "AWS::DynamoDB::Table",
"DeletionPolicy": "Retain",
"DependsOn": ["DynamoDBCredentials"],
"Properties": {
"AttributeDefinitions": [
{
"AttributeName": "account_id",
"AttributeType": "S"
},
{
"AttributeName": "issue_id",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "account_id",
"KeyType": "HASH"
},
{
"AttributeName": "issue_id",
"KeyType": "RANGE"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": "10",
"WriteCapacityUnits": "2"
},
"TableName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" }, "kms-key-rotation" ] ]}
}
}
}
}
11 changes: 11 additions & 0 deletions deployment/cf-templates/identification-crossaccount-role.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
],
"Resource": "*"
},
{
"Action": [
"kms:ListKeys",
"kms:GetKeyRotationStatus",
"kms:DescribeKey",
"kms:ListResourceTags"
],
"Resource": "*",
"Effect": "Allow",
"Sid": "KmsIssues"
},
{
"Sid": "CloudTrailIssues",
"Effect": "Allow",
Expand Down
11 changes: 11 additions & 0 deletions deployment/cf-templates/identification-role.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@
],
"Resource": "*"
},
{
"Action": [
"kms:ListKeys",
"kms:GetKeyRotationStatus",
"kms:DescribeKey",
"kms:ListResourceTags"
],
"Resource": "*",
"Effect": "Allow",
"Sid": "KmsIssues"
},
{
"Sid": "CloudTrailIssues",
"Effect": "Allow",
Expand Down
228 changes: 227 additions & 1 deletion deployment/cf-templates/identification.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"SourceIdentificationIAMUserInactiveKeys",
"SourceIdentificationEBSVolumes",
"SourceIdentificationEBSSnapshots",
"SourceIdentificationRDSSnapshots"
"SourceIdentificationRDSSnapshots",
"SourceIdentificationKMSKeyRotation"
]
},
{
Expand Down Expand Up @@ -88,6 +89,9 @@
},
"SourceIdentificationRDSSnapshots": {
"default": "Relative path to public RDS snapshots lambda sources"
},
"SourceIdentificationKMSKeyRotation": {
"default": "Relative path to KMS key rotation sources"
}
}
}
Expand Down Expand Up @@ -165,6 +169,10 @@
"Type": "String",
"Default": "rds-public-snapshots-identification.zip"
},
"SourceIdentificationKMSKeyRotation": {
"Type": "String",
"Default": "kms-keyrotation-issues-identification.zip"
},
"SourceIdentificationSQSPublicPolicy": {
"Type": "String",
"Default": "sqs-public-policy-identification.zip"
Expand Down Expand Up @@ -221,6 +229,9 @@
"IdentificationMetricRDSSnapshotsError": {
"value": "RDSSnapshotsError"
},
"IdentificationMetricKMSKeyRotationError": {
"value": "KMSKeyRotationError"
},
"IdentificationMetricSQSPublicPolicyError": {
"value": "SQSPublicPolicyError"
},
Expand Down Expand Up @@ -284,6 +295,12 @@
"SNSTopicNameRDSSnapshots": {
"value": "describe-rds-public-snapshots-lambda"
},
"SNSDisplayNameKMSKeyRotation": {
"value": "describe-kms-key-rotation-sns"
},
"SNSTopicNameKMSKeyRotation": {
"value": "describe-kms-key-rotation-lambda"
},
"SNSDisplayNameSQSPublicPolicy": {
"value": "describe-sqs-public-policy-sns"
},
Expand Down Expand Up @@ -362,6 +379,12 @@
"IdentifyRDSSnapshotsLambdaFunctionName": {
"value": "describe-rds-public-snapshots"
},
"InitiateKMSKeyRotationLambdaFunctionName": {
"value": "initiate-kms-key-rotation"
},
"IdentifyKMSKeyRotationLambdaFunctionName": {
"value": "describe-kms-key-rotation"
},
"InitiateSQSPublicPolicyLambdaFunctionName": {
"value": "initiate-sqs-public-policy"
},
Expand Down Expand Up @@ -1839,6 +1862,106 @@
"LogGroupName" : { "Ref": "LogGroupLambdaEvaluateRDSEncryption" }
}
},
"LambdaInitiateKMSKeyRotationEvaluation": {
"Type": "AWS::Lambda::Function",
"DependsOn": ["SNSNotifyLambdaEvaluateKMSKeyRotation", "LogGroupLambdaInitiateKMSKeyRotationEvaluation"],
"Properties": {
"Code": {
"S3Bucket": { "Ref": "SourceS3Bucket" },
"S3Key": { "Ref": "SourceIdentificationKMSKeyRotation" }
},
"Environment": {
"Variables": {
"SNS_KMS_KEY_ROTATION_ARN": { "Ref": "SNSNotifyLambdaEvaluateKMSKeyRotation" }
}
},
"Description": "Lambda function for initiate to identify disabled kms key rotation issues.",
"FunctionName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards", "InitiateKMSKeyRotationLambdaFunctionName", "value"] } ]
]},
"Handler": "initiate_to_desc_kms_key_rotation.lambda_handler",
"MemorySize": 128,
"Timeout": "300",
"Role": {"Fn::Join" : ["", [ "arn:aws:iam::",
{ "Ref": "AWS::AccountId" },
":role/",
{ "Ref": "ResourcesPrefix" },
{ "Ref": "IdentificationIAMRole" }
] ]},
"Runtime": "python3.6"
}
},
"LogGroupLambdaInitiateKMSKeyRotationEvaluation": {
"Type" : "AWS::Logs::LogGroup",
"Properties" : {
"LogGroupName": {"Fn::Join": ["", [ "/aws/lambda/",
{ "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards",
"InitiateKMSKeyRotationLambdaFunctionName",
"value"]
} ] ] },
"RetentionInDays": "7"
}
},
"SubscriptionFilterLambdaInitiateKMSKeyRotationEvaluation": {
"Type" : "AWS::Logs::SubscriptionFilter",
"DependsOn": ["LambdaLogsForwarder",
"PermissionToInvokeLambdaLogsForwarderCloudWatchLogs",
"LogGroupLambdaInitiateKMSKeyRotationEvaluation"],
"Properties" : {
"DestinationArn" : { "Fn::GetAtt" : [ "LambdaLogsForwarder", "Arn" ] },
"FilterPattern" : "[level != START && level != END && level != DEBUG, ...]",
"LogGroupName" : { "Ref": "LogGroupLambdaInitiateKMSKeyRotationEvaluation" }
}
},

"LambdaEvaluateKMSKeyRotation": {
"Type": "AWS::Lambda::Function",
"DependsOn": ["LogGroupLambdaEvaluateKMSKeyRotation"],
"Properties": {
"Code": {
"S3Bucket": { "Ref": "SourceS3Bucket" },
"S3Key": { "Ref": "SourceIdentificationKMSKeyRotation" }
},
"Description": "Lambda function to describe disabled KMS key rotation issues.",
"FunctionName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards", "IdentifyKMSKeyRotationLambdaFunctionName", "value"] } ]
]},
"Handler": "describe_kms_key_rotation.lambda_handler",
"MemorySize": 256,
"Timeout": "300",
"Role": {"Fn::Join" : ["", [ "arn:aws:iam::",
{ "Ref": "AWS::AccountId" },
":role/",
{ "Ref": "ResourcesPrefix" },
{ "Ref": "IdentificationIAMRole" }
] ]},
"Runtime": "python3.6"
}
},
"LogGroupLambdaEvaluateKMSKeyRotation": {
"Type" : "AWS::Logs::LogGroup",
"Properties" : {
"LogGroupName": {"Fn::Join": ["", [ "/aws/lambda/",
{ "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards",
"IdentifyKMSKeyRotationLambdaFunctionName",
"value"]
} ] ] },
"RetentionInDays": "7"
}
},
"SubscriptionFilterLambdaEvaluateKMSKeyRotation": {
"Type" : "AWS::Logs::SubscriptionFilter",
"DependsOn": ["LambdaLogsForwarder",
"PermissionToInvokeLambdaLogsForwarderCloudWatchLogs",
"LogGroupLambdaEvaluateKMSKeyRotation"],
"Properties" : {
"DestinationArn" : { "Fn::GetAtt" : [ "LambdaLogsForwarder", "Arn" ] },
"FilterPattern" : "[level != START && level != END && level != DEBUG, ...]",
"LogGroupName" : { "Ref": "LogGroupLambdaEvaluateKMSKeyRotation" }
}
},

"EventBackupDDB": {
"Type": "AWS::Events::Rule",
Expand Down Expand Up @@ -2005,6 +2128,23 @@
}
},

"EventInitiateEvaluationKMSKeyRotation": {
"Type": "AWS::Events::Rule",
"DependsOn": ["LambdaInitiateKMSKeyRotationEvaluation"],
"Properties": {
"Description": "Hammer ScheduledRule to initiate kms key rotation evaluations",
"Name": {"Fn::Join" : ["", [{ "Ref": "ResourcesPrefix" }, "InitiateEvaluationKMSKeyRotation"] ] },
"ScheduleExpression": {"Fn::Join": ["", [ "cron(", "35 ", { "Ref": "IdentificationCheckRateExpression" }, ")" ] ]},
"State": "ENABLED",
"Targets": [
{
"Arn": { "Fn::GetAtt": ["LambdaInitiateKMSKeyRotationEvaluation", "Arn"] },
"Id": "LambdaInitiateKMSKeyRotationEvaluation"
}
]
}
},

"PermissionToInvokeLambdaLogsForwarderCloudWatchLogs": {
"Type": "AWS::Lambda::Permission",
"DependsOn": ["LambdaLogsForwarder"],
Expand Down Expand Up @@ -2148,6 +2288,17 @@
}
},

"PermissionToInvokeLambdaInitiateKMSKeyRotationEvaluationCloudWatchEvents": {
"Type": "AWS::Lambda::Permission",
"DependsOn": ["LambdaInitiateKMSKeyRotationEvaluation", "EventInitiateEvaluationKMSKeyRotation"],
"Properties": {
"FunctionName": { "Ref": "LambdaInitiateKMSKeyRotationEvaluation" },
"Action": "lambda:InvokeFunction",
"Principal": "events.amazonaws.com",
"SourceArn": { "Fn::GetAtt": ["EventInitiateEvaluationKMSKeyRotation", "Arn"] }
}
},

"SNSNotifyLambdaEvaluateSG": {
"Type": "AWS::SNS::Topic",
"DependsOn": ["LambdaEvaluateSG"],
Expand Down Expand Up @@ -2365,6 +2516,25 @@
}
},

"SNSNotifyLambdaEvaluateKMSKeyRotation": {
"Type": "AWS::SNS::Topic",
"DependsOn": "LambdaEvaluateKMSKeyRotation",
"Properties": {
"DisplayName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards", "SNSDisplayNameKMSKeyRotation", "value"] } ]
]},
"TopicName": {"Fn::Join" : ["", [ { "Ref": "ResourcesPrefix" },
{ "Fn::FindInMap": ["NamingStandards", "SNSTopicNameKMSKeyRotation", "value"] } ]
]},
"Subscription": [{
"Endpoint": {
"Fn::GetAtt": ["LambdaEvaluateKMSKeyRotation", "Arn"]
},
"Protocol": "lambda"
}]
}
},

"PermissionToInvokeLambdaEvaluateSgSNS": {
"Type": "AWS::Lambda::Permission",
"DependsOn": ["SNSNotifyLambdaEvaluateSG", "LambdaEvaluateSG"],
Expand Down Expand Up @@ -2485,6 +2655,16 @@
"FunctionName": { "Fn::GetAtt": ["LambdaEvaluateRDSEncryption", "Arn"] }
}
},
"PermissionToInvokeLambdaEvaluateKMSKeyRotationSNS": {
"Type": "AWS::Lambda::Permission",
"DependsOn": ["SNSNotifyLambdaEvaluateKMSKeyRotation", "LambdaEvaluateKMSKeyRotation"],
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "sns.amazonaws.com",
"SourceArn": { "Ref": "SNSNotifyLambdaEvaluateKMSKeyRotation" },
"FunctionName": { "Fn::GetAtt": ["LambdaEvaluateKMSKeyRotation", "Arn"] }
}
},

"SNSIdentificationErrors": {
"Type": "AWS::SNS::Topic",
Expand Down Expand Up @@ -3088,6 +3268,52 @@
"Threshold": 0,
"TreatMissingData": "notBreaching"
}
},
"AlarmErrorsLambdaInitiateKMSKeyRotationEvaluation": {
"Type": "AWS::CloudWatch::Alarm",
"DependsOn": ["SNSIdentificationErrors", "LambdaInitiateKMSKeyRotationEvaluation"],
"Properties": {
"AlarmActions": [ { "Ref": "SNSIdentificationErrors" } ],
"OKActions": [ { "Ref": "SNSIdentificationErrors" } ],
"AlarmName": {"Fn::Join": ["/", [ { "Ref": "LambdaInitiateKMSKeyRotationEvaluation" }, "LambdaError" ] ]},
"EvaluationPeriods": 1,
"Namespace": "AWS/Lambda",
"MetricName": "Errors",
"Dimensions": [
{
"Name": "FunctionName",
"Value": { "Ref": "LambdaInitiateKMSKeyRotationEvaluation" }
}
],
"Period": 3600,
"Statistic": "Maximum",
"ComparisonOperator" : "GreaterThanThreshold",
"Threshold": 0,
"TreatMissingData": "notBreaching"
}
},
"AlarmErrorsLambdaKMSKeyRotationEvaluation": {
"Type": "AWS::CloudWatch::Alarm",
"DependsOn": ["SNSIdentificationErrors", "LambdaEvaluateKMSKeyRotation"],
"Properties": {
"AlarmActions": [ { "Ref": "SNSIdentificationErrors" } ],
"OKActions": [ { "Ref": "SNSIdentificationErrors" } ],
"AlarmName": {"Fn::Join": ["/", [ { "Ref": "LambdaEvaluateKMSKeyRotation" }, "LambdaError" ] ]},
"EvaluationPeriods": 1,
"Namespace": "AWS/Lambda",
"MetricName": "Errors",
"Dimensions": [
{
"Name": "FunctionName",
"Value": { "Ref": "LambdaEvaluateKMSKeyRotation" }
}
],
"Period": 3600,
"Statistic": "Maximum",
"ComparisonOperator" : "GreaterThanThreshold",
"Threshold": 0,
"TreatMissingData": "notBreaching"
}
}
},
"Outputs": {
Expand Down
Loading