resource "aws_iam_role" "codebuild_role" {
  name     = "codebuild_role"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": {
          "Service": [
            "codebuild.amazonaws.com"
            ]
        },
        "Action": "sts:AssumeRole"
      }
    ]
  }
EOF
}

resource "aws_iam_role_policy_attachment" "codebuild_role_policy_attach" {
  role       = aws_iam_role.codebuild_role.name
  policy_arn = aws_iam_policy.codebuild_policy.arn
}

# Some things about this policy I'm not perfectly sure about, like
# should the account number be hardcoded?  Also, it reads like we'll have to
# update it each time we have a new repository added to codecommit - that
# or we'll need to authorize the codebuild role to be able to pull from any 
# codecommit repo.  Which may be fine?
resource "aws_iam_policy" "codebuild_policy" {
  name        = "codebuild_policy"
  description = "Policy for AWS codebuild to build and store artifacts"

  policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Resource": [
                "arn:${var.aws_partition}:logs:${var.aws_region}:${var.common_services_account}:log-group:/aws/codebuild/*"
            ],
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ]
        },
        {
            "Effect": "Allow",
            "Resource": [
                "arn:${var.aws_partition}:s3:::codepipeline-${var.aws_region}-*"
            ],
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:GetObjectVersion"
            ]
        },
        {
            "Effect": "Allow",
            "Resource": [
                "arn:${var.aws_partition}:codecommit:${var.aws_region}:${var.common_services_account}:*"
            ],
            "Action": [
                "codecommit:GitPull"
            ]
        },
        {
            "Effect": "Allow",
            "Resource": [
                "arn:${var.aws_partition}:s3:::xdr-codebuild-artifacts/*",
                "arn:${var.aws_partition}:s3:::*"
            ],
            "Action": [
                "s3:PutObject",
                "s3:GetObject*",
                "s3:ListBucket"
            ]
        },
        {
            "Effect": "Allow",
            "Resource": [
                "*"
            ],
            "Action": [
              "ecr:GetAuthorizationToken",
              "ecr:BatchCheckLayerAvailability",
              "ecr:CompleteLayerUpload",
              "ecr:GetAuthorizationToken",
              "ecr:InitiateLayerUpload",
              "ecr:PutImage",
              "ecr:UploadLayerPart"
            ]
        }
    ]
}
EOF
}

# !!!!!     RETAINED FOR FUTURE USE   !!!!!
# Defines an IAM user that can only download ECR images, intended for
# use in POP nodes where we need containers, but won't necessarily have
# EC2 instance role credentials.  Maybe one day this goes to vault, I
# hope.   It would be nice.

# data "aws_iam_policy_document" "ecr_policy_pop" {
#   statement {
#     sid       = "AllowECRReadOnly"
#     effect    = "Allow"

#     actions   = [
#       "ecr:GetAuthorizationToken",
#       "ecr:BatchCheckLayerAvailability",
#       "ecr:GetDownloadUrlForLayer",
#       "ecr:GetRepositoryPolicy",
#       "ecr:DescribeRepositories",
#       "ecr:ListImages",
#       "ecr:DescribeImages",
#       "ecr:BatchGetImage"
#     ]
    
#     resources = [
#       "*"
#     ]

#   }
# }

# resource "aws_iam_policy" "ecr_policy_pop" {
#   name   = "ecr_policy_pop"
#   path   = "/"
#   policy = "${data.aws_iam_policy_document.ecr_policy_pop.json}"
# }

# resource "aws_iam_user" "pop_service_account" {
#   name = "svc-mdrpop"
#   path = "/service/"
# }

# resource "aws_iam_user_policy_attachment" "pop_service_account_1" {
#   user       = "${aws_iam_user.pop_service_account.name}"
#   policy_arn = "${aws_iam_policy.ecr_policy_pop.arn}"
# }


# resource "aws_iam_access_key" "pop_service_account" {
#   user    = "${aws_iam_user.pop_service_account.name}"
#   pgp_key = "${file("../00-organizations-and-iam/duane_waddle.pgp")}"
# }

# output "pop_service_account_key_id" {
#   value = "${aws_iam_access_key.pop_service_account.id}"
# }

# output "pop_service_account_secret" {
#   value = "${aws_iam_access_key.pop_service_account.encrypted_secret}"
# }

# !!!!!    END OF RETAINED FOR FUTURE USE   !!!!!