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" "sds_task_role" {

  statement {
    sid = "sdsDataServiceSecretsAccess"
    effect = "Allow"
    actions = [
      "secretsmanager:Get*",
      "secretsmanager:List*",
      "secretsmanager:Describe*"
    ]
    resources = [
      "arn:aws:secretsmanager:*:*:secret:network_*",
      "arn:aws:secretsmanager:*:*:secret:nc_*",
      "arn:aws:secretsmanager:us-east-1:${data.aws_caller_identity.current.account_id}:secret:${local.service_name}-client-secret-??????"
    ]
  }
  statement {
    sid = "sdsDataServiceRoute53ZoneAccess"
    effect = "Allow"
    actions = [
      "route53:ChangeResourceRecordSets",
      "route53:ChangeTagsForResource",
      "route53:ListResourceRecordSets",
      "route53:TestDNSAnswer"
    ]
    resources = [
      "arn:aws:route53:::hostedzone/${data.terraform_remote_state.mop_core.outputs.zone_id}",
      "arn:aws:route53:::hostedzone/${data.terraform_remote_state.mop_core.outputs.browzer_zone_id}"
    ]
  }
  statement {
    sid = "sdsDataServiceRoute53ChangeAccess"
    effect = "Allow"
    actions = [
      "route53:GetChange"
    ]
    resources = [
      "*"
    ]
  }
    statement {
    sid = "sdsECSTaskExecution"
    effect = "Allow"
    actions = [
      "ecr:GetAuthorizationToken",
      "ecr:BatchCheckLayerAvailability",
      "ecr:GetDownloadUrlForLayer",
      "ecr:BatchGetImage",
      "logs:CreateLogStream",
      "logs:PutLogEvents"
    ]
    resources = ["*"]
  }
}

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

resource "aws_iam_role" "sds_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" "sds_task_role_policy_attach" {
  role = aws_iam_role.sds_task_role.name
  policy_arn = aws_iam_policy.sds_task_role.arn
}


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

resource "aws_iam_policy" "internal_profile_policy_copy" {
  name = "${local.service_name}-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" "sds_service" {
  name = "sds_service"
  count = local.deploy_env_name == "sandbox" ? 1 : 0
}
# this credential lives in sandbox-credentials
resource "aws_iam_access_key" "sds_service" {
  user = aws_iam_user.sds_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_sds" {
  user       = aws_iam_user.sds_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_sds" {
  user       = aws_iam_user.sds_service[0].name
  policy_arn = aws_iam_policy.sds_task_role.arn
  count = local.deploy_env_name == "sandbox" ? 1 : 0
}
