provider "aws" {
  region = var.aws_region
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/auth0-hosted-pages/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

locals {
  secret_names = concat(
    var.secrets_manager_secret_names,
    var.secrets_manager_integration_tests_secret_names,
  )
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.3"
  for_each = toset(local.secret_names)

  environment                    = var.environment
  service_name                   = var.service_name
  secret_name                    = each.value
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}

# -------------------- deploy role --------------------
data "aws_iam_policy_document" "build_policy_document" {
  statement {
    effect = "Allow"

    actions = [
      "secretsmanager:GetSecretValue",
    ]

    resources = concat(
      [for k in var.secrets_manager_secret_names : module.secrets[k].secret_arn],
      [for s in values(module.m2m_secrets) : s.auth0_client_credentials_secret_arn],
    )
  }
}

# Assume role policy used by Jenkins pipeline agent role
data "aws_iam_policy_document" "assume_deploy_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type = "AWS"

      identifiers = [
        "arn:aws:iam::437795906767:role/prod-jenkins-aws-pipeline-agent",
      ]
    }
  }
}

# Access Policy
resource "aws_iam_policy" "deploy_policy" {
  name   = "${var.environment}-${var.service_name}-deploy-policy"
  policy = data.aws_iam_policy_document.build_policy_document.json
}

# Jenkins pipeline agents will assume this role
resource "aws_iam_role" "deploy_role" {
  name               = "${var.environment}-${var.service_name}-deploy-role"
  assume_role_policy = data.aws_iam_policy_document.assume_deploy_role_policy.json
}

# Attach access policy to Jenkins role.
resource "aws_iam_role_policy_attachment" "deploy_policy_attachment" {
  role       = aws_iam_role.deploy_role.id
  policy_arn = aws_iam_policy.deploy_policy.arn
}

# -------------------- integration test role --------------------
data "aws_s3_bucket" "orcdbucket" {
  bucket = "${var.environment}-orcdbucket"
}

data "aws_iam_policy_document" "integration_test_policy_document" {
  # Get secrets
  statement {
    effect = "Allow"

    actions = [
      "secretsmanager:GetSecretValue",
    ]

    resources = [for k in var.secrets_manager_integration_tests_secret_names : module.secrets[k].secret_arn]
  }

  # List S3 objects
  statement {
    effect = "Allow"

    resources = [
      data.aws_s3_bucket.orcdbucket.arn
    ]

    condition {
      test     = "StringLike"
      variable = "s3:prefix"
      values = [
        var.bucket_prefix,
        "${var.bucket_prefix}/*"
      ]
    }

    actions = [
      "s3:ListBucket"
    ]
  }

  # Get and delete S3 objects
  statement {
    effect = "Allow"

    resources = [
      "${data.aws_s3_bucket.orcdbucket.arn}/${var.bucket_prefix}/*"
    ]

    actions = [
      "s3:GetObject",
      "s3:DeleteObject"
    ]
  }

  # Pull image to run integration tests
  statement {
    actions = [
      "ecr:BatchCheckLayerAvailability",
      "ecr:GetDownloadUrlForLayer",
      "ecr:BatchGetImage",
    ]

    resources = [
      "arn:aws:ecr:*:086679231553:repository/docker-parent-images"
    ]
  }

  statement {
    actions = [
      "ecr:GetAuthorizationToken"
    ]

    resources = [
      "*",
    ]
  }
}

# Assume role policy used by Jenkins pipeline agent role
data "aws_iam_policy_document" "assume_test_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type = "AWS"

      identifiers = [
        "arn:aws:iam::437795906767:role/prod-jenkins-aws-pipeline-agent",
      ]
    }
  }
}

# Access Policy
resource "aws_iam_policy" "integration_test_policy" {
  name   = "${var.environment}-${var.service_name}-integration-test-policy"
  policy = data.aws_iam_policy_document.integration_test_policy_document.json
}

# Jenkins pipeline agents will assume this role
resource "aws_iam_role" "integration_test_role" {
  name               = "${var.environment}-${var.service_name}-integration-test-role"
  assume_role_policy = data.aws_iam_policy_document.assume_test_role_policy.json
}

# Attach access policy to Jenkins role.
resource "aws_iam_role_policy_attachment" "test_policy_attachment" {
  role       = aws_iam_role.integration_test_role.id
  policy_arn = aws_iam_policy.integration_test_policy.arn
}
