AWSTemplateFormatVersion: "2010-09-09"
Description: "IAM Managed Policies/Role for Material Providers Library"

Parameters:
  ProjectName:
    Type: String
    Description: A prefix that will be applied to any names
    Default: MPL-Dafny
  GitHubRepo:
    Type: String
    Description: GitHub Repo that invokes CI
    Default: aws/aws-cryptographic-material-providers-library

Resources:
  GitHubCIRole:
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: !Sub "GitHub-CI-${ProjectName}-Role-${AWS::Region}"
      Description: "Access DDB, KMS, Resources for CI from GitHub, and Prod CodeBuild"
      ManagedPolicyArns:
        - "arn:aws:iam::370957321024:policy/PolymorphTestModels-KMS-us-west-2"
        - "arn:aws:iam::370957321024:policy/RSA-GitHub-KMS-Key-Policy"
        - "arn:aws:iam::370957321024:policy/Hierarchical-GitHub-KMS-Key-Policy"
        - "arn:aws:iam::370957321024:policy/PolymorphTestModels-DDB-ReadWriteDelete-us-west-2"
        - "arn:aws:iam::370957321024:policy/ESDK-Dafny-DDB-ReadWriteDelete-us-west-2"
        - "arn:aws:iam::370957321024:policy/ESDK-Dafny-DDB-KeyStoreStaticTable-ReadOnly-us-west-2"
        - "arn:aws:iam::370957321024:policy/Github-ECDH-KMS"
      AssumeRolePolicyDocument: !Sub |
        {
          "Version": "2012-10-17",   
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": { "Federated": "arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com" },
              "Action": "sts:AssumeRoleWithWebIdentity",
              "Condition": {
                "StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" },
                "StringLike": {
                  "token.actions.githubusercontent.com:sub": [
                    "repo:aws/aws-cryptographic-material-providers-library:*",
                    "repo:aws/private-aws-cryptographic-material-providers-library-dafny-staging:*"
                  ]
                }
              }
            },
            {
              "Effect": "Allow",
              "Principal": {
                "AWS": "*"
              },
              "Action": "sts:AssumeRole",
              "Condition": {
                "StringEquals": {
                  "aws:PrincipalArn": [
                    "arn:aws:iam::587316601012:role/service-role/codebuild-AWS-MPL-Java-service-role-release",
                    "arn:aws:iam::587316601012:role/service-role/codebuild-AWS-MPL-DotNet-service-role-release",
                    "arn:aws:iam::587316601012:role/service-role/codebuild-AWS-ESDK-DotNet-service-role-release",
                    "arn:aws:iam::370957321024:role/ToolsDevelopment"
                  ]
                }
              }
            }
          ]
        }
