# hkb-pipeline-cfn-self

CloudFormation service role for the hkb-pipeline stack (pipeline and CodeBuild projects). Cannot edit IAM, the key, the bucket or this stack.

- **Lives in:** [Shared Services](/architecture/shared-services/). **Trusted by:** `cloudformation.amazonaws.com`.
- **Boundary:** `hkb-pipeline-boundary`.
- **Reach:** 17 named actions across 6 services (codebuild, codeconnections, codepipeline, codestar-connections, iam, ssm); 0 statements on all resources.
- **Source:** `pipeline/hkb-pipeline-foundation.yaml`, resource `CfnSelfRole`.

## 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-self
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Sid: ManagePipeline
          Effect: Allow
          Action:
            - codepipeline:CreatePipeline
            - codepipeline:UpdatePipeline
            - codepipeline:GetPipeline
            - codepipeline:GetPipelineState   # drift detection reads the pipeline with this (DQ-145)
            - codepipeline:DeletePipeline
            - codepipeline:TagResource
            - codepipeline:UntagResource
            - codepipeline:ListTagsForResource
          Resource:
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${PipelineName}/*
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${SitePipelineName}
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${SitePipelineName}/*
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${ToolsPipelineName}
            - !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${ToolsPipelineName}/*
        - Sid: ManageValidateProjects
          Effect: Allow
          Action:
            - codebuild:CreateProject
            - codebuild:UpdateProject
            - codebuild:DeleteProject
            - codebuild:BatchGetProjects
          Resource: !Sub arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/hkb-pipeline-*
        - Sid: PassOnlyThePipelineRoles
          Effect: Allow
          Action: iam:PassRole
          Resource:
            - !GetAtt OrchestratorRole.Arn
            - !GetAtt SiteOrchestratorRole.Arn
            - !GetAtt ToolsOrchestratorRole.Arn
            - !GetAtt ValidateRole.Arn
            - !GetAtt SyncRole.Arn
          Condition:
            StringEquals:
              iam:PassedToService:
                - codepipeline.amazonaws.com
                - codebuild.amazonaws.com
        - Sid: PassThePipelineActionRoles
          Effect: Allow
          Action: iam:PassRole
          Resource:
            - !GetAtt SelfRole.Arn
            - !GetAtt StackSetsRole.Arn
            # Cross-account action roles are governed by their trust policies; listed here so
            # the pipeline update can't fail on a PassRole check (the 2.10 lesson).
            - !Sub arn:${AWS::Partition}:iam::${SandboxAccountId}:role/hkb-pipeline-site
            - !Sub arn:${AWS::Partition}:iam::${DevAccountId}:role/hkb-pipeline-site
            - !Sub arn:${AWS::Partition}:iam::${ProdAccountId}:role/hkb-pipeline-site
            - !Sub arn:${AWS::Partition}:iam::${SecurityAccountId}:role/hkb-pipeline-security
            - !Sub arn:${AWS::Partition}:iam::${SandboxAccountId}:role/hkb-pipeline-network
            - !Sub arn:${AWS::Partition}:iam::${DevAccountId}:role/hkb-pipeline-network
            - !Sub arn:${AWS::Partition}:iam::${ProdAccountId}:role/hkb-pipeline-network
            - !Sub arn:${AWS::Partition}:iam::${SecurityAccountId}:role/hkb-pipeline-tools
          Condition:
            StringEquals:
              iam:PassedToService: codepipeline.amazonaws.com
        - Sid: PassTheCloudFormationServiceRoles
          Effect: Allow
          Action: iam:PassRole
          Resource:
            - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/hkb-pipeline-cfn-self
            - !GetAtt CfnStackSetsRole.Arn
            # The site stages name these member service roles; CodePipeline checks PassRole on them
            # when the pipeline definition is saved (step 4.4, the 2.10 lesson again).
            - !Sub arn:${AWS::Partition}:iam::${SandboxAccountId}:role/hkb-pipeline-cfn-site
            - !Sub arn:${AWS::Partition}:iam::${DevAccountId}:role/hkb-pipeline-cfn-site
            - !Sub arn:${AWS::Partition}:iam::${ProdAccountId}:role/hkb-pipeline-cfn-site
            - !Sub arn:${AWS::Partition}:iam::${SecurityAccountId}:role/hkb-pipeline-cfn-security
            # The network stages (step 5.5, DQ-110).
            - !Sub arn:${AWS::Partition}:iam::${SandboxAccountId}:role/hkb-pipeline-cfn-network
            - !Sub arn:${AWS::Partition}:iam::${DevAccountId}:role/hkb-pipeline-cfn-network
            - !Sub arn:${AWS::Partition}:iam::${ProdAccountId}:role/hkb-pipeline-cfn-network
            # The tools stages (TL.2, DQ-127).
            - !Sub arn:${AWS::Partition}:iam::${SecurityAccountId}:role/hkb-pipeline-cfn-tools
          Condition:
            StringEqualsIfExists:
              iam:PassedToService:
                - codepipeline.amazonaws.com
                - cloudformation.amazonaws.com
        - Sid: UseGitHubConnectionInPipeline
          Effect: Allow
          Action:
            - codeconnections:PassConnection
            - codeconnections:GetConnection
            - codestar-connections:PassConnection
          Resource:
            - !Sub arn:${AWS::Partition}:codeconnections:${AWS::Region}:${AWS::AccountId}:connection/*
            - !Sub arn:${AWS::Partition}:codestar-connections:${AWS::Region}:${AWS::AccountId}:connection/*
        - Sid: ResolveIdParameters
          Effect: Allow
          Action: ssm:GetParameters
          Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/hkb/aws/*
```
