# breakglass-identity / aws / t0 "Quick start"
#
# An IAM user for when single sign-on is down: full access, but only after
# signing in with MFA, and an alert every time it signs in or does anything,
# in any region.
#
# Terraform never sets its password or MFA, so neither is stored in state.
# After apply, as an administrator:
#   1. In the IAM console, set the user's console password.
#   2. Register a hardware security key as its MFA device.
#   3. Seal both where your runbook says, such as a safe that needs two people to open.
#   Never create access keys for this user.
#
# Needs a multi-region CloudTrail trail that delivers to CloudWatch Logs, such
# as the Standard audit log template builds. Apply in that log group's region.
#
# Change the two values below, then:
#   terraform init && terraform apply
#
# Verify: sign in as the user in a drill. The email arrives within about ten
# minutes.

locals {
  trail_log_group = "your-cloudtrail-log-group" # change me
  alert_email     = "oncall@example.com"        # change me
  user_name       = "break-glass"
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  default_tags {
    tags = {
      owner               = "security-engineering"
      environment         = "prod"
      data_classification = "restricted"
      managed_by          = "terraform"
    }
  }
}

resource "aws_iam_user" "break_glass" {
  name = local.user_name
  path = "/break-glass/"
}

resource "aws_iam_user_policy_attachment" "admin" {
  user       = aws_iam_user.break_glass.name
  policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}

# Everything is refused until the session has MFA, except what is needed to
# finish signing in with it.
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"]
    }
  }
}

resource "aws_iam_user_policy" "require_mfa" {
  name   = "require-mfa"
  user   = aws_iam_user.break_glass.name
  policy = data.aws_iam_policy_document.require_mfa.json
}

resource "aws_sns_topic" "alerts" {
  name = "break-glass-used"
}

resource "aws_sns_topic_subscription" "email" {
  topic_arn = aws_sns_topic.alerts.arn
  protocol  = "email"
  endpoint  = local.alert_email
}

# The trail's log group contains every region's events, so one filter sees a
# sign-in or a call anywhere.
resource "aws_cloudwatch_log_metric_filter" "used" {
  name           = "break-glass-used"
  log_group_name = local.trail_log_group
  pattern        = "{ ($.userIdentity.type = \"IAMUser\") && ($.userIdentity.userName = \"${local.user_name}\") }"
  metric_transformation {
    name          = "BreakGlassUsed"
    namespace     = "Security/BreakGlass"
    value         = "1"
    default_value = "0"
  }
}

resource "aws_cloudwatch_metric_alarm" "used" {
  alarm_name          = "break-glass-used"
  alarm_description   = "The break-glass user signed in or made a call. Search the trail for userIdentity.userName = ${local.user_name}."
  namespace           = "Security/BreakGlass"
  metric_name         = "BreakGlassUsed"
  statistic           = "Sum"
  period              = 60
  evaluation_periods  = 1
  threshold           = 1
  comparison_operator = "GreaterThanOrEqualToThreshold"
  treat_missing_data  = "notBreaching"
  alarm_actions       = [aws_sns_topic.alerts.arn]
  depends_on          = [aws_cloudwatch_log_metric_filter.used]
}

output "user_arn" {
  description = "Add this to the exempt list in the guardrail template, so the guardrails do not lock it out."
  value       = aws_iam_user.break_glass.arn
}
