AWSTemplateFormatVersion: '2010-09-09'
Description: Hoogly - Create read-only audit role (AssumeRole + ExternalId)

Parameters:
  HooglyAccountId:
    Type: String
    Description: Hoogly AWS account ID (the trusted principal)
    AllowedPattern: '^[0-9]{12}$'
  ExternalId:
    Type: String
    Description: ExternalId provided by Hoogly (prevents confused deputy)
    MinLength: 6
    MaxLength: 64
  RoleName:
    Type: String
    Default: HooglyAuditRole2
    Description: Name of the role Hoogly will assume

Resources:
  HooglyAuditRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Ref RoleName
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub arn:aws:iam::${HooglyAccountId}:root
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                sts:ExternalId: !Ref ExternalId
      ManagedPolicyArns:
        # AWS managed read-only audit policy. For best results with Hoogly scans,
        # you may add extra read-only policies later if needed.
        - arn:aws:iam::aws:policy/SecurityAudit
      Policies:
        - PolicyName: HooglyExtraReadOnly
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              # Optional extra read-only actions that some checks may call.
              - Effect: Allow
                Action:
                  - account:GetAccountInformation
                  - account:GetRegionOptStatus
                  - iam:GenerateCredentialReport
                  - iam:GetAccountSummary
                  - iam:GetCredentialReport
                Resource: "*"

Outputs:
  RoleArn:
    Description: Role ARN to paste into Hoogly
    Value: !GetAtt HooglyAuditRole.Arn
  ExternalId:
    Description: External ID (for reference) - should match Hoogly
    Value: !Ref ExternalId
