Skip to content

aws-iam: Assigning the same user policies in multiple stacks can cause previous policies to be overwritten #37682

@TheRealMintd

Description

@TheRealMintd

Describe the bug

When a user is imported into multiple CDK stacks, and is granted policies through ISecret.grantRead (or similar), the last deployed stack's policy overrides whatever was granted before on the user.

Seems to be similar to #23080, but for users instead of roles.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

Each policy should be unique on a per-stack basis, such that each stack's policies don't overwrite one another.

Current Behavior

Only the last deployed stack's policy is attached to the user, overwriting all previous policies.

Reproduction Steps

With a created user, bucket, and function, deploy these two stacks:

export class FirstStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: FirstStackProps) {
    super(scope, id, props);

    const bucket = new Bucket(this, 'Bucket', {
      autoDeleteObjects: true,
      removalPolicy: RemovalPolicy.DESTROY
    })
    const user = User.fromUserArn(this, "User", "<userArn>")
    bucket.grantWrite(user)
  }
}

export class SecondStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: SecondStackProps) {
    super(scope, id, props);

    const fn = new Function(this, 'MyFn', ...)
    const user = User.fromUserArn(this, "User", "<userArn>")
    fn.grantInvoke(user)
  }
}

Possible Solution

Perhaps a similar fix to what was done for #23080?

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.235.1

AWS CDK CLI version

2.1118.4

Node.js Version

18.20.8

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-iamRelated to AWS Identity and Access ManagementbugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions