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          = var.team_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

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

data "aws_caller_identity" "current" {}

# Create IAM user
resource "aws_iam_user" "iam_user" {
  name = "cdn-deploy"
  tags = {
    role        = "service-user"
    terraformed = true
  }
}

data "aws_iam_policy_document" "cloudfront_deployment_policy_document" {
  # checkov:skip=CKV_AWS_111:This deployment role needs access to all distributions
  statement {
    effect = "Allow"

    actions = [
      "cloudfront:CreateInvalidation",
      "cloudfront:GetDistributionConfig",
      "cloudfront:GetInvalidation",
      "cloudfront:ListDistributions",
      "cloudfront:ListInvalidations",
      "cloudfront:ListStreamingDistributions",
    ]

    resources = [
      "*",
    ]
  }
}

data "aws_iam_policy_document" "s3_cdn_deployment_policy_document" {
  statement {
    effect = "Allow"

    actions = [
      "s3:ListAllMyBuckets",
    ]

    resources = [
      "*",
    ]
  }

  statement {
    effect = "Allow"

    actions = [
      "s3:Get*",
      "s3:List*",
      "s3:PutObject",
      "s3:PutObjectAcl",
      "s3:PutObjectTagging",
      "s3:PutObjectVersionAcl",
      "s3:PutObjectVersionTagging",
      "s3:HeadObject",
    ]

    resources = [
      "arn:aws:s3:::prod-orcd-cdn",
      "arn:aws:s3:::prod-orcd-cdn/*",
      "arn:aws:s3:::prod-orcd-cdn-*/*",
      "arn:aws:s3:::prod-overdrive-cdn",
      "arn:aws:s3:::prod-overdrive-cdn/*",
      "arn:aws:s3:::qa-orcd-cdn",
      "arn:aws:s3:::qa-orcd-cdn/*",
      "arn:aws:s3:::qa-orcd-cdn-*/*",
      "arn:aws:s3:::qa-overdrive-cdn",
      "arn:aws:s3:::qa-overdrive-cdn/*",
      "arn:aws:s3:::uat-orcd-cdn",
      "arn:aws:s3:::uat-orcd-cdn/*",
      "arn:aws:s3:::uat-orcd-cdn-*/*",
    ]
  }
}

resource "aws_iam_policy" "cloudfront_deployment_policy" {
  name        = "Cloudfront-create-invalidation"
  description = "Cloudfront-create-invalidation"
  policy      = data.aws_iam_policy_document.cloudfront_deployment_policy_document.json
}

resource "aws_iam_policy" "s3_cdn_deployment_policy" {
  name        = "S3-${var.service_name}-RW"
  description = "S3-${var.service_name}-RW"
  policy      = data.aws_iam_policy_document.s3_cdn_deployment_policy_document.json
}

resource "aws_iam_user_policy_attachment" "cloudfront_deployment_policy_attachment" {
  policy_arn = aws_iam_policy.cloudfront_deployment_policy.arn
  user       = aws_iam_user.iam_user.name
}

resource "aws_iam_user_policy_attachment" "s3_cdn_deployment_policy_attachment" {
  policy_arn = aws_iam_policy.s3_cdn_deployment_policy.arn
  user       = aws_iam_user.iam_user.name
}

# Create IAM role to replace the IAM user
data "aws_iam_policy_document" "cdn_deploy_assume_role_policy" {
  statement {
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/prod-jenkins-aws-pipeline-agent"]
    }

    actions = ["sts:AssumeRole"]
  }
}

resource "aws_iam_role" "cdn_deploy_role" {
  name               = "prod-cdn-deploy-role"
  assume_role_policy = data.aws_iam_policy_document.cdn_deploy_assume_role_policy.json
}

resource "aws_iam_role_policy_attachment" "cdn_deploy_cloudfront_policy_attachment" {
  role       = aws_iam_role.cdn_deploy_role.name
  policy_arn = aws_iam_policy.cloudfront_deployment_policy.arn
}

resource "aws_iam_role_policy_attachment" "cdn_deploy_s3_policy_attachment" {
  role       = aws_iam_role.cdn_deploy_role.name
  policy_arn = aws_iam_policy.s3_cdn_deployment_policy.arn
}
