Skip to content
Merged
9 changes: 7 additions & 2 deletions packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,21 @@ export class Pipeline extends PipelineBase {
* @param action the action to return/create a role for
* @param actionScope the scope, unique to the action, to create new resources in
*/

private getRoleForAction(stage: Stage, action: RichAction, actionScope: Construct): iam.IRole | undefined {
const pipelineStack = Stack.of(this);

let actionRole = this.getRoleFromActionPropsOrGenerateIfCrossAccount(stage, action);

if (!actionRole && this.isAwsOwned(action)) {
// generate a Role for this specific Action
actionRole = new iam.Role(actionScope, 'CodePipelineActionRole', {
const isRemoveRootPrincipal = FeatureFlags.of(this).isEnabled(cxapi.PIPELINE_REDUCE_STAGE_ROLE_TRUST_SCOPE);
const roleProps = isRemoveRootPrincipal? {
assumedBy: new iam.ArnPrincipal(this.role.roleArn), // Allow only the pipeline execution role
} : {
assumedBy: new iam.AccountPrincipal(pipelineStack.account),
});
};
actionRole = new iam.Role(actionScope, 'CodePipelineActionRole', roleProps);
}

// the pipeline role needs assumeRole permissions to the action role
Expand Down
18 changes: 18 additions & 0 deletions packages/aws-cdk-lib/cx-api/FEATURE_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Flags come in three types:
| [@aws-cdk/core:enableAdditionalMetadataCollection](#aws-cdkcoreenableadditionalmetadatacollection) | When enabled, CDK will expand the scope of usage data collected to better inform CDK development and improve communication for security concerns and emerging issues. | 2.178.0 | (config) |
| [@aws-cdk/aws-lambda:createNewPoliciesWithAddToRolePolicy](#aws-cdkaws-lambdacreatenewpolicieswithaddtorolepolicy) | When enabled, Lambda will create new inline policies with AddToRolePolicy instead of adding to the Default Policy Statement | 2.180.0 | (fix) |
| [@aws-cdk/aws-s3:setUniqueReplicationRoleName](#aws-cdkaws-s3setuniquereplicationrolename) | When enabled, CDK will automatically generate a unique role name that is used for s3 object replication. | 2.182.0 | (fix) |
| [@aws-cdk/pipelines:reduceStageRoleTrustScope](#aws-cdkpipelinesreducestageroletrustscope) | Remove the root account principal from Stage addActions trust policy | V2NEXT | (default) |

<!-- END table -->

Expand Down Expand Up @@ -215,6 +216,7 @@ are migrating a v1 CDK project to v2, explicitly set any of these flags which do
| [@aws-cdk/pipelines:reduceAssetRoleTrustScope](#aws-cdkpipelinesreduceassetroletrustscope) | Remove the root account principal from PipelineAssetsFileRole trust policy | (default) | | `false` | `true` |
| [@aws-cdk/aws-stepfunctions-tasks:useNewS3UriParametersForBedrockInvokeModelTask](#aws-cdkaws-stepfunctions-tasksusenews3uriparametersforbedrockinvokemodeltask) | When enabled, use new props for S3 URI field in task definition of state machine for bedrock invoke model. | (fix) | | `false` | `true` |
| [@aws-cdk/core:aspectStabilization](#aws-cdkcoreaspectstabilization) | When enabled, a stabilization loop will be run when invoking Aspects during synthesis. | (config) | | `false` | `true` |
| [@aws-cdk/pipelines:reduceStageRoleTrustScope](#aws-cdkpipelinesreducestageroletrustscope) | Remove the root account principal from Stage addActions trust policy | (default) | | `false` | `true` |

<!-- END diff -->

Expand Down Expand Up @@ -1722,4 +1724,20 @@ When disabled, 'CDKReplicationRole' is always specified.
| 2.182.0 | `false` | `true` |


### @aws-cdk/pipelines:reduceStageRoleTrustScope

*Remove the root account principal from Stage addActions trust policy* (default)

When this feature flag is enabled, the root account principal will not be added to the trust policy of stage role.
When this feature flag is disabled, it will keep the root account principal in the trust policy.


| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| V2NEXT | `true` | `true` |

**Compatibility with old behavior:** Disable the feature flag to add the root account principal back


<!-- END details -->
17 changes: 16 additions & 1 deletion packages/aws-cdk-lib/cx-api/lib/features.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FlagInfo, FlagType } from './private/flag-modeling';
import { FlagInfo, FlagType, MAGIC_V2NEXT } from './private/flag-modeling';

////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -125,6 +125,7 @@ export const IAM_OIDC_REJECT_UNAUTHORIZED_CONNECTIONS = '@aws-cdk/aws-iam:oidcRe
export const ENABLE_ADDITIONAL_METADATA_COLLECTION = '@aws-cdk/core:enableAdditionalMetadataCollection';
export const LAMBDA_CREATE_NEW_POLICIES_WITH_ADDTOROLEPOLICY = '@aws-cdk/aws-lambda:createNewPoliciesWithAddToRolePolicy';
export const SET_UNIQUE_REPLICATION_ROLE_NAME = '@aws-cdk/aws-s3:setUniqueReplicationRoleName';
export const PIPELINE_REDUCE_STAGE_ROLE_TRUST_SCOPE = '@aws-cdk/pipelines:reduceStageRoleTrustScope';

export const FLAGS: Record<string, FlagInfo> = {
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1410,6 +1411,20 @@ export const FLAGS: Record<string, FlagInfo> = {
introducedIn: { v2: '2.182.0' },
recommendedValue: true,
},
//////////////////////////////////////////////////////////////////////
[PIPELINE_REDUCE_STAGE_ROLE_TRUST_SCOPE]: {
type: FlagType.ApiDefault,
summary: 'Remove the root account principal from Stage addActions trust policy',
detailsMd: `
When this feature flag is enabled, the root account principal will not be added to the trust policy of stage role.
When this feature flag is disabled, it will keep the root account principal in the trust policy.
`,
introducedIn: { v2: MAGIC_V2NEXT },
defaults: { v2: true },
recommendedValue: true,
compatibilityWithOldBehaviorMd: 'Disable the feature flag to add the root account principal back',
},

};

const CURRENT_MV = 'v2';
Expand Down
Loading