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

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

data "aws_iam_policy_document" "auth_task_role" {
  statement {
    sid = "AuthSecretsSecretsAccess"
    effect = "Allow"
    actions = [
      "secretsmanager:GetSecretValue"
    ]
    resources = [
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:mysql_pass_permission_server-??????",
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:docker-hub-mop-deploy-??????",
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:auth-service-client-secret-??????"
    ]
  }
  statement {
    sid = "BillingECSTaskExecution"
    effect = "Allow"
    actions = [
      "ecr:GetAuthorizationToken",
      "ecr:BatchCheckLayerAvailability",
      "ecr:GetDownloadUrlForLayer",
      "ecr:BatchGetImage",
      "logs:CreateLogStream",
      "logs:PutLogEvents"
    ]
    resources = ["*"]
  }
}


resource "aws_iam_policy" "auth_task_role" {
  name = "${local.service_name}${local.iam_suffix}"
  policy = data.aws_iam_policy_document.auth_task_role.json
}

resource "aws_iam_role" "auth_task_role" {
  name = "${local.service_name}-task-role${local.iam_suffix}"
  assume_role_policy = data.aws_iam_policy_document.ecs_assume_role.json
}

resource "aws_iam_role_policy_attachment" "auth_task_role_policy_attach" {
  role = aws_iam_role.auth_task_role.name
  policy_arn = aws_iam_policy.auth_task_role.arn
}

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

resource "aws_iam_policy" "internal_profile_policy_copy" {
  name = "auth-profile-internal${local.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" "authorization_service" {
  name = "authorization_service"
  count = local.deploy_env_name == "sandbox" ? 1 : 0
}
# this credential lives in sandbox-credentials
resource "aws_iam_access_key" "authorization_service" {
  user = aws_iam_user.authorization_service[0].name
  count = local.deploy_env_name == "sandbox" ? 1 : 0
}

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

/*
output authorization_aws_access_key_id {
  value = "${aws_iam_access_key.authorization_service.*.id}"
}
output authorization_aws_secret_access_key {
  value = "${aws_iam_access_key.authorization_service.*.secret}"
}
 */
