# Bootstrap: the policy document references the CloudFront distribution data
# source, which is commented out in cloudfront.tf until the us-east-1 apply
# creates the distribution. Re-enable (and re-add bucket_policy_overrides +
# apply_replication_destination_configuration below) once us-east-1 is applied.
data "aws_iam_policy_document" "s3_hot_updater_server_bucket_policy" {
  statement {
    actions = [
      "s3:GetObject",
    ]

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

    condition {
      test     = "StringEquals"
      variable = "AWS:SourceArn"
      values = [
        data.aws_cloudfront_distribution.hot_updater_server_cdn.arn
      ]
    }

    principals {
      type        = "Service"
      identifiers = ["cloudfront.amazonaws.com"]
    }
  }
}

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

  env                = var.environment
  bucket_name        = var.hot_updater_server_bucket_name
  application_family = var.application_family

  bucket_policy_overrides = [
    data.aws_iam_policy_document.s3_hot_updater_server_bucket_policy.json
  ]

  apply_server_side_encryption_by_default = {
    sse_algorithm = "AES256"
  }

  apply_replication_destination_configuration = [{
    replication_source_iam_role_arn = data.aws_iam_role.replication_source_iam_role.arn
  }]
}
