# yaml-language-server: $schema=../../schemas/@aws-mdaa/roles.json
generatePolicies:
  GlueJobLogPolicy:
    # TODO: Review the below policyDocument permissions and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM5"
    #     reason: "Wildcard in log-group ARN (/aws-glue/*) required because Glue dynamically generates log group names at runtime."
    policyDocument:
      Statement:
        - SID: GlueCloudwatch
          Effect: Allow
          Resource:
            - "arn:{{partition}}:logs:{{region}}:{{account}}:log-group:/aws-glue/*"
          Action:
            - logs:CreateLogStream
            - logs:AssociateKmsKey
            - logs:CreateLogGroup
            - logs:PutLogEvents

  DataAdminPolicy:
    # TODO: Review the below policyDocument permissions and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM5"
    #     reason: "s3:ListAllMyBuckets, s3:GetAccountPublicAccessBlock, s3:ListAccessPoints, and kms:ListAliases are account-level actions that do not support resource-level permissions."
    policyDocument:
      Statement:
        - Sid: BasicS3Access
          Effect: Allow
          Action:
            - s3:ListAllMyBuckets
            - s3:GetAccountPublicAccessBlock
            - s3:GetBucketPublicAccessBlock
            - s3:GetBucketPolicyStatus
            - s3:GetBucketAcl
            - s3:ListAccessPoints
            - s3:GetBucketLocation
          Resource: "*"

        # Allows basic listing of KMS keys (required for up)
        - Sid: BasicKMSAccess
          Effect: Allow
          Action:
            - kms:ListAliases
          Resource: "*"

  DataUserPolicy:
    # TODO: Review the below policyDocument permissions and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM5"
    #     reason: "Glue database/table wildcards are intentional — fine-grained data access is governed by Lake Formation grants, not IAM resource ARNs. athena:ListWorkGroups does not support resource-level permissions."
    policyDocument:
      Statement:
        # This statement allows coarse-grained access to Glue catalog resources, but does not itself grant any access to data.
        # Effective permissions are the intersection between IAM Glue Permissions and LF Grants. By establishing broad, coarse-grained permissions here,
        # we are effectively concentrating effective permissions management in LF Grants.
        - SID: GlueCoarseGrainedAccess
          Effect: Allow
          Resource:
            - arn:{{partition}}:glue:{{region}}:{{account}}:catalog
            - arn:{{partition}}:glue:{{region}}:{{account}}:database/*
            - arn:{{partition}}:glue:{{region}}:{{account}}:table/*
          Action:
            - glue:GetDatabase
            - glue:GetDatabases
            - glue:GetCatalogImportStatus
            - glue:GetTable
            - glue:GetTables
            - glue:GetPartition
            - glue:GetPartitions
            - glue:SearchTables

        # This statement allows the basic listing of Athena workgroups
        # Specific Athena accesses are granted by the Athena Workgroup module itself.
        - SID: BasicAthenaAccess
          Effect: Allow
          Action:
            - athena:ListWorkGroups
          Resource: "*"

  FileProcessorDdbPolicy:
    # TODO: Review the below policyDocument permissions and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM5"
    #     reason: "DynamoDB table wildcard required because table names are generated dynamically by MDAA and not known at policy authoring time."
    policyDocument:
      Statement:
        - SID: DynamoDBWriter
          Effect: Allow
          Resource:
            - "arn:{{partition}}:dynamodb:{{region}}:{{account}}:table/*"
          Action:
            - dynamodb:PutItem
            - dynamodb:DeleteItem
            - dynamodb:GetItem
            - dynamodb:Scan
            - dynamodb:Query
            - dynamodb:UpdateItem

# The list of roles which will be generated
generateRoles:
  transformation-glue-job-role:
    # TODO: Review the below awsManagedPolicies and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM4"
    #     reason: "AWSGlueServiceRole is required for Glue ETL jobs to access S3, CloudWatch Logs, and EC2 networking. AmazonEC2ContainerRegistryReadOnly provides read access to ECR for custom Glue container images."
    trustedPrincipal: service:glue.amazonaws.com
    # A list of AWS managed policies which will be added to the role
    awsManagedPolicies:
      - AmazonEC2ContainerRegistryReadOnly
      - service-role/AWSGlueServiceRole
    generatedPolicies:
      - GlueJobLogPolicy

  file-processor-glue-job-role:
    # TODO: Review the below awsManagedPolicies and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM4"
    #     reason: "AWSGlueServiceRole is required for Glue ETL jobs to access S3, CloudWatch Logs, and EC2 networking. AmazonEC2ContainerRegistryReadOnly provides read access to ECR for custom Glue container images."
    trustedPrincipal: service:glue.amazonaws.com
    # A list of AWS managed policies which will be added to the role
    awsManagedPolicies:
      - AmazonEC2ContainerRegistryReadOnly
      - service-role/AWSGlueServiceRole
    generatedPolicies:
      - FileProcessorDdbPolicy
      - GlueJobLogPolicy

  dms:
    trustedPrincipal: service:dms.{{region}}.amazonaws.com
    generatedPolicies:
      # needed for the S3 target
      - DataAdminPolicy

  glue-etl:
    # TODO: Review the below awsManagedPolicies and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM4"
    #     reason: "AWSGlueServiceRole is required for Glue ETL jobs to access S3, CloudWatch Logs, and EC2 networking for job execution. See https://docs.aws.amazon.com/glue/latest/dg/create-an-iam-role.html"
    trustedPrincipal: service:glue.amazonaws.com
    # A list of AWS managed policies which will be added to the role
    awsManagedPolicies:
      - service-role/AWSGlueServiceRole
    generatedPolicies:
      - GlueJobLogPolicy

  data-admin:
    # TODO: Review the below awsManagedPolicies and suppression. Uncomment the suppression prior to deployment.
    # suppressions:
    #   - id: "AwsSolutions-IAM4"
    #     reason: "AWSGlueConsoleFullAccess provides the data-admin role with full Glue console access for managing ETL jobs, crawlers, and catalog resources. See https://docs.aws.amazon.com/glue/latest/dg/security-iam-awsmanpol.html"
    trustedPrincipal: this_account
    awsManagedPolicies:
      - AWSGlueConsoleFullAccess
    generatedPolicies:
      - DataUserPolicy
      - DataAdminPolicy

  data-user:
    trustedPrincipal: this_account
    generatedPolicies:
      - DataUserPolicy
