AWSTemplateFormatVersion: "2010-09-09"
Description: >
  ShieldFlow read-only audit role (template v1). SecurityAudit plus a
  few newer read calls, with an explicit deny on reading any customer data.
Parameters:
  ShieldFlowAccountId:
    Type: String
    AllowedPattern: "[0-9]{12}"
    Description: ShieldFlow's AWS account (shown on the Connect AWS screen)
  ExternalId:
    Type: String
    MinLength: 16
    Description: Your workspace's External ID (shown on the Connect AWS screen)
Resources:
  ShieldFlowAuditRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: ShieldFlowAudit
      MaxSessionDuration: 3600
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub "arn:aws:iam::${ShieldFlowAccountId}:root"
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                sts:ExternalId: !Ref ExternalId
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/SecurityAudit
      Policies:
        - PolicyName: ShieldFlowAuditExtras
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Sid: ReadOnlyExtras
                Effect: Allow
                Action:
                  - eks:DescribeClusterVersions
                  - inspector2:BatchGetAccountStatus
                  - inspector2:ListFindingAggregations
                  - lambda:ListFunctionUrlConfigs
                  - ecr:DescribeImages
                  - ecr:DescribeRepositories
                  - dynamodb:DescribeContinuousBackups
                  - rds:DescribeDBMajorEngineVersions
                  - cloudtrail:LookupEvents
                  - backup:ListBackupPlans
                  - backup:ListProtectedResources
                  - backup:ListRestoreJobs
                  - backup:ListRestoreTestingPlans
                  - backup:ListCopyJobs
                  - identitystore:ListUsers
                  - identitystore:ListGroups
                  - identitystore:ListGroupMemberships
                  - sso:ListInstances
                  - sso:ListPermissionSets
                  - sso:DescribePermissionSet
                  - sso:ListAccountAssignments
                  - sso:ListPermissionSetsProvisionedToAccount
                  - sso:ListManagedPoliciesInPermissionSet
                  - sso:GetInlinePolicyForPermissionSet
                  - sso:ListAccountsForProvisionedPermissionSet
                  - organizations:DescribeOrganization
                  - organizations:ListAccounts
                Resource: "*"
              - Sid: NeverReadCustomerData
                Effect: Deny
                Action:
                  - s3:GetObject
                  - s3:GetObjectVersion
                  - dynamodb:GetItem
                  - dynamodb:BatchGetItem
                  - dynamodb:Query
                  - dynamodb:Scan
                  - rds-data:*
                  - secretsmanager:GetSecretValue
                  - ssm:GetParameter
                  - ssm:GetParameters
                  - ssm:GetParametersByPath
                  - kms:Decrypt
                  - lambda:GetFunction
                  - logs:GetLogEvents
                  - logs:FilterLogEvents
                  - ecr:GetDownloadUrlForLayer
                  - ecr:BatchGetImage
                  - athena:GetQueryResults
                Resource: "*"
Outputs:
  RoleArn:
    Description: Paste this into ShieldFlow
    Value: !GetAtt ShieldFlowAuditRole.Arn
