data "aws_iam_policy_document" "ecs_assume_role" {
  statement {
    sid     = "AllowAssumeRoleFromECSTasks"
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ecs-tasks.amazonaws.com"]
    }
  }
}

data "aws_iam_policy_document" "customer_task_role" {
  statement {
    sid = "CustomerSecretsSecretsAccess"
    effect = "Allow"
    actions = [
      "secretsmanager:GetSecretValue"
    ]
    resources = [
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:spring_cloud_config-??????",
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:${local.service_name}-client-secret-??????"
    ]
  }
  statement {
    sid = "customerECSTaskExecution"
    effect = "Allow"
    actions = [
      "ecr:GetAuthorizationToken",
      "ecr:BatchCheckLayerAvailability",
      "ecr:GetDownloadUrlForLayer",
      "ecr:BatchGetImage",
      "logs:CreateLogStream",
      "logs:PutLogEvents"
    ]
    resources = ["*"]
  }
  statement {
    sid = "customerDynamoDBAccess"
    effect = "Allow"
    actions = [
      "dynamodb:BatchGet*",
      "dynamodb:Describe*",
      "dynamodb:Get*",
      "dynamodb:List*",
      "dynamodb:Query",
      "dynamodb:Scan"
    ]
    resources = local.workspace["dynamodb_access"]
  }
}

resource "aws_iam_policy" "customer_task_role" {
  name = "${var.service_name}${local.workspace["iam_suffix"]}"
  policy = data.aws_iam_policy_document.customer_task_role.json
}

resource "aws_iam_role" "customer_task_role" {
  name = "${var.service_name}-task-role${local.workspace["iam_suffix"]}"
  assume_role_policy = data.aws_iam_policy_document.ecs_assume_role.json
}

resource "aws_iam_role_policy_attachment" "customer_task_role_policy_attach" {
  role = aws_iam_role.customer_task_role.name
  policy_arn = aws_iam_policy.customer_task_role.arn
}


data "aws_iam_policy_document" "customer_task_metering_policydoc" {
  statement {
    sid = "MarketplaceMeteringPolicyDoc"
    effect = "Allow"
    actions = [
      "aws-marketplace:*"
    ]
    resources = ["*"]
  }
}

resource "aws_iam_policy" "customer_task_metering_policy" {
  name = "${local.service_name}-task-metering-policy${local.workspace["iam_suffix"]}"
  policy = data.aws_iam_policy_document.customer_task_metering_policydoc.json
}

resource "aws_iam_role_policy_attachment" "customer_task_metering_policy_attach" {
  role = aws_iam_role.customer_task_role.name
  policy_arn = aws_iam_policy.customer_task_metering_policy.arn
}

resource "aws_iam_role_policy_attachment" "profile_internal_policy_attach" {
  role = aws_iam_role.customer_task_role.name
  policy_arn = aws_iam_policy.internal_profile_policy_copy.arn
}

resource "aws_iam_policy" "internal_profile_policy_copy" {
  name = "${var.service_name}-profile-internal${local.workspace["iam_suffix"]}"
  policy = data.terraform_remote_state.mop_core.outputs.tf_profile_internal_policy_document
}

#
##
### if Sandbox then create a user with the same policies for localhost MOP
##
#

# create an IAM user for the localhost service
resource "aws_iam_user" "customer_management_service" {
  name = "customer_management_service"
  count = local.workspace["deploy_env_name"] == "sandbox" ? 1 : 0
}
# this credential lives in sandbox-credentials
resource "aws_iam_access_key" "customer_management_service" {
  user = aws_iam_user.customer_management_service[0].name
  count = local.workspace["deploy_env_name"] == "sandbox" ? 1 : 0
}

# attach the same policies held by the task role
resource "aws_iam_user_policy_attachment" "profile_internal_core" {
  user       = aws_iam_user.customer_management_service[0].name
  policy_arn = aws_iam_policy.internal_profile_policy_copy.arn
  count = local.workspace["deploy_env_name"] == "sandbox" ? 1 : 0
}

resource "aws_iam_user_policy_attachment" "task_role_customer_management" {
  user       = aws_iam_user.customer_management_service[0].name
  policy_arn = aws_iam_policy.customer_task_role.arn
  count = local.workspace["deploy_env_name"] == "sandbox" ? 1 : 0
}
