module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = "devops"
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "shared-orcd-terraform-state"
    key     = "shared/base-amis/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}
data "aws_caller_identity" "current" {}

data "aws_iam_role" "autoscaling_role" {
  name = "AWSServiceRoleForAutoScaling"
}

data "aws_iam_policy_document" "ami_key" {
  # checkov:skip=CKV_AWS_109:the wildcard is not a problem for key policy
  # checkov:skip=CKV_AWS_111:the wildcard is not a problem for key policy
  statement {
    sid    = "Enable IAM policies"
    effect = "Allow"
    principals {
      type = "AWS"
      identifiers = [
        "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
      ]
    }
    actions   = ["kms:*"]
    resources = ["*"]
  }

  statement {
    sid    = "Allow other accounts to describe and decrypt with this key"
    effect = "Allow"
    principals {
      type        = "AWS"
      identifiers = local.ami_consumer_account_arns
    }
    actions = [
      "kms:Decrypt",
      "kms:DescribeKey",
      "kms:Encrypt",
      "kms:ReEncrypt*",
      "kms:GenerateDataKey",
      "kms:GenerateDataKeyWithoutPlaintext",
      "kms:List*",
    ]
    resources = ["*"]
  }

  statement {
    sid    = "Allow attachment of persistent resources in other accounts"
    effect = "Allow"
    principals {
      type        = "AWS"
      identifiers = local.ami_consumer_account_arns
    }
    actions = [
      "kms:CreateGrant",
    ]
    resources = ["*"]
  }
}

resource "aws_kms_key" "ami" {
  description         = "KMS key to encrypt base amis, will be shared to all accounts"
  policy              = data.aws_iam_policy_document.ami_key.json
  enable_key_rotation = true
  tags                = module.default_tags.tags
}

resource "aws_kms_alias" "ami" {
  name          = "alias/shared-base-ami-key"
  target_key_id = aws_kms_key.ami.key_id
}

# Data resource for access to the shared AMI KMS key
data "aws_iam_policy_document" "ami_kms_policy_document" {
  statement {
    effect = "Allow"

    actions = [
      "kms:CreateGrant",
    ]
    resources = [
      aws_kms_key.ami.arn,
    ]
  }
}

resource "aws_iam_policy" "ami_kms_policy" {
  name   = "KMS-shared-${var.service_name}-RO"
  policy = data.aws_iam_policy_document.ami_kms_policy_document.json
}

resource "aws_kms_grant" "autoscaling_kms_grant" {
  name              = "autoscaling-kms-grant"
  key_id            = aws_kms_key.ami.arn
  grantee_principal = data.aws_iam_role.autoscaling_role.arn
  operations        = ["Encrypt", "Decrypt", "ReEncryptFrom", "ReEncryptTo", "GenerateDataKey", "GenerateDataKeyWithoutPlaintext", "DescribeKey", "CreateGrant", ]
}
