# breakglass-identity / aws / t1 "Standard" # # Break-glass users with alerts on their use and on any change to them: a new # access key, a new password, MFA removed, policies or groups changed. Each of # those is how someone would quietly make the account their own. data "aws_caller_identity" "current" {} data "aws_partition" "current" {} locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) topic_arn = var.alert_topic_arn != "" ? var.alert_topic_arn : aws_sns_topic.alerts[0].arn names = join(" || ", [for n in var.user_names : "($.userIdentity.userName = \"${n}\")"]) targets = join(" || ", [for n in var.user_names : "($.requestParameters.userName = \"${n}\")"]) changes = join(" || ", [for e in [ "CreateAccessKey", "CreateLoginProfile", "UpdateLoginProfile", "DeleteLoginProfile", "DeactivateMFADevice", "EnableMFADevice", "AttachUserPolicy", "DetachUserPolicy", "PutUserPolicy", "DeleteUserPolicy", "AddUserToGroup", "RemoveUserFromGroup", "DeleteUser", ] : "($.eventName = \"${e}\")"]) detections = { used = { pattern = "{ ($.userIdentity.type = \"IAMUser\") && (${local.names}) }" description = "A break-glass user signed in or made a call." } changed = { pattern = "{ ($.eventSource = \"iam.amazonaws.com\") && (${local.targets}) && (${local.changes}) }" description = "Someone changed a break-glass user's keys, password, MFA, policies, or groups." } } } resource "aws_iam_user" "break_glass" { for_each = toset(var.user_names) name = each.value path = "/break-glass/" } resource "aws_iam_user_policy_attachment" "admin" { for_each = aws_iam_user.break_glass user = each.value.name policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess" } data "aws_iam_policy_document" "require_mfa" { statement { sid = "DenyEverythingWithoutMFA" effect = "Deny" not_actions = [ "iam:ListMFADevices", "iam:ListVirtualMFADevices", "iam:GetUser", "sts:GetSessionToken", "iam:ChangePassword", ] resources = ["*"] condition { test = "BoolIfExists" variable = "aws:MultiFactorAuthPresent" values = ["false"] } } # The user cannot create a key for itself. An administrator still can, which # the change alert catches, and the key would be useless: every call without # MFA is refused, and a hardware key cannot supply MFA to the command line. statement { sid = "NoAccessKeysForSelf" effect = "Deny" actions = ["iam:CreateAccessKey"] resources = ["arn:aws:iam::*:user/break-glass/*"] } } resource "aws_iam_user_policy" "require_mfa" { for_each = aws_iam_user.break_glass name = "require-mfa-no-keys" user = each.value.name policy = data.aws_iam_policy_document.require_mfa.json } # The topic gets its own key. CloudWatch alarms can only publish to an # encrypted topic when the key policy names them, and the AWS managed SNS key # cannot be changed to do that, so alarms on a topic using it fail without an # error anyone sees. data "aws_iam_policy_document" "alerts_key" { count = var.alert_topic_arn == "" ? 1 : 0 statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] } } statement { sid = "CloudWatchAlarmsPublish" actions = ["kms:Decrypt", "kms:GenerateDataKey*"] resources = ["*"] principals { type = "Service" identifiers = ["cloudwatch.amazonaws.com"] } } } resource "aws_kms_key" "alerts" { count = var.alert_topic_arn == "" ? 1 : 0 description = "Break-glass alert topic" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.alerts_key[0].json } resource "aws_sns_topic" "alerts" { count = var.alert_topic_arn == "" ? 1 : 0 name = "break-glass-alerts" kms_master_key_id = aws_kms_key.alerts[0].arn # A topic nobody is subscribed to turns every alarm into silence. lifecycle { precondition { condition = length(var.alert_emails) > 0 error_message = "Set alert_emails, or alert_topic_arn for a topic that reaches on-call. Otherwise the alarms reach nobody." } } } resource "aws_sns_topic_subscription" "email" { for_each = var.alert_topic_arn == "" ? toset(var.alert_emails) : toset([]) topic_arn = aws_sns_topic.alerts[0].arn protocol = "email" endpoint = each.value } resource "aws_cloudwatch_log_metric_filter" "detection" { for_each = local.detections name = "break-glass-${each.key}" log_group_name = var.trail_log_group pattern = each.value.pattern metric_transformation { name = "BreakGlass-${each.key}" namespace = "Security/BreakGlass" value = "1" default_value = "0" } } resource "aws_cloudwatch_metric_alarm" "detection" { for_each = local.detections alarm_name = "break-glass-${each.key}" alarm_description = "${each.value.description} Search the trail for the user name to see what happened." namespace = "Security/BreakGlass" metric_name = "BreakGlass-${each.key}" statistic = "Sum" period = 60 evaluation_periods = 1 threshold = 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] depends_on = [aws_cloudwatch_log_metric_filter.detection] }