provider "aws" {
  region = var.aws_region
}

terraform {
  backend "s3" {
    bucket  = "backup-orcd-terraform-state"
    key     = "prod/vault-backup/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

data "aws_iam_policy_document" "bucket_policy" {
  statement {
    sid = "DenyDestructiveActions"

    effect = "Deny"

    actions = [
      "s3:DeleteBucket",
      "s3:DeleteBucketPolicy",
      "s3:DeleteObject*"
    ]

    resources = [
      module.s3_bucket.s3_bucket_arn_output,
      "${module.s3_bucket.s3_bucket_arn_output}/*"
    ]

    principals {
      type        = "*"
      identifiers = ["*"]
    }
  }

  statement {
    sid    = "AllowVaultBackupRoleActions"
    effect = "Allow"

    actions = [
      "s3:PutObject",
    ]

    resources = [
      "${module.s3_bucket.s3_bucket_arn_output}/*"
    ]

    principals {
      type        = "AWS"
      identifiers = var.vault_backup_role_arns
    }
  }
}

module "s3_bucket" {
  source = "git@github.com:theorchard/terraform-s3.git//modules/s3_bucket?ref=3.1.5"

  env                = var.environment
  bucket_name        = var.service_name
  application_family = var.application_family
  bucket_policy_overrides = [
    data.aws_iam_policy_document.bucket_policy.json
  ]

  apply_server_side_encryption_by_default = {
    sse_algorithm = "aws:kms"
  }

  kms_key_enabled  = true
  kms_key_required = true
  kms_key_name     = "${var.service_name}-kms-key"

  lifecycle_rules_options_current_version_transition = [
    {
      prefix        = ""
      enabled       = true
      days          = 30
      storage_class = "DEEP_ARCHIVE"
    }
  ]

  lifecycle_rules_options_current_version_expiration = [
    {
      prefix  = ""
      enabled = true
      days    = 30
    },
  ]

  lifecycle_rules_options_noncurrent_version_transition = [
    {
      prefix        = ""
      enabled       = true
      days          = 30
      storage_class = "DEEP_ARCHIVE"
    }
  ]

  lifecycle_rules_options_noncurrent_version_expiration = [
    {
      prefix  = ""
      enabled = true
      days    = 60
    }
  ]
}

data "aws_iam_policy_document" "kms_policy" {
  # checkov:skip=CKV_AWS_109:It is a KMS key policy, so it applies only to a specific key.
  # checkov:skip=CKV_AWS_111:Same as above.

  # Statement from default KMS 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 Vault backup role to use key"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = var.vault_backup_role_arns
    }

    actions = [
      "kms:Encrypt",
      "kms:GenerateDataKey",
    ]
    resources = ["*"]
  }
}

resource "aws_kms_key_policy" "kms_policy" {
  key_id = module.s3_bucket.s3_kms_encryption_key_id_output
  policy = data.aws_iam_policy_document.kms_policy.json
}
