AWSTemplateFormatVersion: "2010-09-09"
Description: >-
  OpstriQ · Team-tier real-time monitoring (§6.50). Creates a
  cross-account IAM role (read-only) plus an EventBridge rule that
  forwards CloudTrail management events to the OpstriQ partner event
  bus. One-time setup · 5 minutes · zero recurring effort. Customer can
  delete this stack at any time to revoke real-time access; polling will
  continue until they disconnect via the OpstriQ dashboard.

Parameters:
  OpstriqAccountId:
    Type: String
    Default: "048109269975"
    Description: OpstriQ AWS account ID (partner event bus owner).

  OpstriqExternalId:
    Type: String
    NoEcho: true
    Description: >-
      Per-tenant external ID provided by OpstriQ on the Connect Account
      screen. Required for STS AssumeRole. Do not share.

  EventBusName:
    Type: String
    Default: "opstriq-events"
    Description: Partner event bus name on the OpstriQ side.

Resources:

  # 1 · Cross-account IAM role · read-only across security + cost services
  OpstriqReadOnlyRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: OpstriQReadOnly
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub "arn:aws:iam::${OpstriqAccountId}:root"
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                sts:ExternalId: !Ref OpstriqExternalId
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/SecurityAudit
        - arn:aws:iam::aws:policy/ReadOnlyAccess
      Path: /
      Tags:
        - { Key: opstriq-managed, Value: "true" }

  # 2 · EventBridge rule forwarding CloudTrail management events
  OpstriqEventForwarder:
    Type: AWS::Events::Rule
    Properties:
      Name: opstriq-cloudtrail-forwarder
      Description: Forward CloudTrail management events to OpstriQ partner bus
      EventPattern:
        source:
          - aws.ec2
          - aws.s3
          - aws.iam
          - aws.rds
          - aws.lambda
          - aws.elasticloadbalancing
          - aws.eks
          - aws.ecs
          - aws.kms
          - aws.secretsmanager
          - aws.cloudformation
          - aws.config
        detail-type:
          - "AWS API Call via CloudTrail"
      State: ENABLED
      Targets:
        - Id: opstriq-target
          Arn: !Sub "arn:aws:events:${AWS::Region}:${OpstriqAccountId}:event-bus/${EventBusName}"
          RoleArn: !GetAtt OpstriqEventBridgeRole.Arn

  # 3 · IAM role granting EventBridge permission to PutEvents on the OpstriQ bus
  OpstriqEventBridgeRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: OpstriQEventBridgeForwarder
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal: { Service: events.amazonaws.com }
            Action: sts:AssumeRole
      Policies:
        - PolicyName: opstriq-put-events
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: events:PutEvents
                Resource: !Sub "arn:aws:events:${AWS::Region}:${OpstriqAccountId}:event-bus/${EventBusName}"

  # 4 · SNS topic for delivery confirmation (optional — customer-side audit)
  OpstriqDeliveryTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: opstriq-event-delivery-audit
      DisplayName: "OpstriQ event delivery audit"
      Tags:
        - { Key: opstriq-managed, Value: "true" }

Outputs:
  RoleArn:
    Description: IAM role ARN to paste into the OpstriQ Connect Account screen
    Value: !GetAtt OpstriqReadOnlyRole.Arn
    Export:
      Name: !Sub "${AWS::StackName}-RoleArn"

  ExternalId:
    Description: External ID echoed back (for confirmation)
    Value: !Ref OpstriqExternalId

  EventForwarderName:
    Description: EventBridge rule name (delete to revoke real-time)
    Value: !Ref OpstriqEventForwarder

  WhatThisStackCreates:
    Description: Plain-English summary
    Value: >-
      One IAM role (read-only, scoped to SecurityAudit + ReadOnlyAccess),
      one EventBridge rule that forwards CloudTrail events for 12 services
      to OpstriQ's partner event bus, and one SNS topic for delivery audit.
      Does NOT enable CloudTrail (uses your existing trail). Does NOT
      enable AWS Config. Does NOT modify any existing resource. Delete
      this stack to instantly revoke real-time access.
