# hkb-pipeline-cfn-tools

CloudFormation service role for the hkb-tool-* stacks in Security (TL.1, DQ-127). hkb-tool-* CodeBuild projects, roles, log groups and schedules.

- **Lives in:** [Security](/architecture/security/). **Trusted by:** `cloudformation.amazonaws.com`.
- **Boundary:** `hkb-pipeline-boundary`.
- **Reach:** 35 named actions across 5 services (codebuild, iam, logs, scheduler, ssm); 1 statement on all resources.
- **Source:** `baseline/hkb-baseline-iam.yaml`, resource `ToolsCfnRole`.

## Trust policy

```yaml
AssumeRolePolicyDocument:
  Version: "2012-10-17"
  Statement:
    - Effect: Allow
      Principal:
        Service: cloudformation.amazonaws.com
      Action: sts:AssumeRole
      Condition:
        StringEquals:
          aws:SourceAccount: !Ref AWS::AccountId
```

## Permissions

```yaml
Policies:
  - PolicyName: hkb-pipeline-cfn-tools
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Sid: ResolveCatalogParameters
          Effect: Allow
          Action: ssm:GetParameters
          Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/hkb/aws/*
        - Sid: ToolRoles
          Effect: Allow
          Action:
            - iam:CreateRole
            - iam:DeleteRole
            - iam:GetRole
            - iam:UpdateRole
            - iam:UpdateAssumeRolePolicy
            - iam:TagRole
            - iam:UntagRole
            - iam:ListRoleTags
            - iam:PutRolePolicy
            - iam:DeleteRolePolicy
            - iam:GetRolePolicy
            - iam:ListRolePolicies
            - iam:ListAttachedRolePolicies
            - iam:PutRolePermissionsBoundary
          Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/hkb-tool-*
        - Sid: PassToolRoles
          Effect: Allow
          Action: iam:PassRole
          Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/hkb-tool-*
          Condition:
            StringEquals:
              iam:PassedToService:
                - codebuild.amazonaws.com
                - scheduler.amazonaws.com
        - Sid: ToolProjects
          Effect: Allow
          Action:
            - codebuild:CreateProject
            - codebuild:UpdateProject
            - codebuild:DeleteProject
            - codebuild:BatchGetProjects
          Resource: !Sub arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/hkb-tool-*
        - Sid: ToolLogGroups
          Effect: Allow
          Action:
            - logs:CreateLogGroup
            - logs:DeleteLogGroup
            - logs:PutRetentionPolicy
            - logs:DeleteRetentionPolicy
            - logs:TagResource
            - logs:UntagResource
            - logs:ListTagsForResource
            - logs:GetDataProtectionPolicy   # drift detection reads the log group with this and the two below (DQ-145)
          Resource: !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/hkb-tool-*
        - Sid: DescribeLogGroups
          Effect: Allow
          Action:
            - logs:DescribeLogGroups
            - logs:DescribeIndexPolicies
            - logs:DescribeResourcePolicies
          Resource: "*"
        - Sid: ToolSchedules
          Effect: Allow
          Action:
            - scheduler:CreateSchedule
            - scheduler:UpdateSchedule
            - scheduler:DeleteSchedule
            - scheduler:GetSchedule
          Resource: !Sub arn:${AWS::Partition}:scheduler:${AWS::Region}:${AWS::AccountId}:schedule/default/hkb-tool-*
```
