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

    principals {
      type = "AWS"

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

# Jenkins pipeline agents will assume this role in order to access mobile s3 bucket.
resource "aws_iam_role" "jenkins_mobile_deploy_role" {
  name               = "${var.environment}-${var.service_name}-jenkins-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role_policy_document.json
}

# Attach mobile s3 policy to Jenkins role.
resource "aws_iam_role_policy_attachment" "jenkins_mobile_deploy_role_policy_attachment" {
  role       = aws_iam_role.jenkins_mobile_deploy_role.id
  policy_arn = aws_iam_policy.s3_read_write_policy.arn
}
