aws / aws/aws-cdk

(aws-iam): Inconsistency in resource grant permissions when importing a role using fromRoleName() and fromRoleArn()

Open
#28,363 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-iam bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

The grant permissions are producing different synth outputs/missing permissions when using fromRoleName() vs fromRoleArn()

Example -

Bug behaviour

```ts
const s3KmsKey = new Key(this, 'TestDataBucketKey')
const bucket = new s3.Bucket(this, 'TestDataBucket', {
encryption: s3.BucketEncryption.KMS,
encryptionKey: s3KmsKey
})
const role_from_name = iam.Role.fromRoleName(this, 'CDKRole', 'cdk-role')
s3KmsKey.grantEncryptDecrypt(role_from_name)
bucket.grantReadWrite(role_from_name)
```

In the resultant template, the bucket policy and KMS key policy will be missing permissions for the role. But an IAM policy will be created and attached to the role.

Expected -

```ts
const s3KmsKey = new Key(this, 'TestDataBucketKey')
const bucket = new s3.Bucket(this, 'TestDataBucket', {
encryption: s3.BucketEncryption.KMS,
encryptionKey: s3KmsKey
})
const role_from_arn = iam.Role.fromRoleArn(
this,
'CDKRole',
'arn:aws:iam::xxxxxxxxxxx:role/cdk-role'
)
s3KmsKey.grantEncryptDecrypt(role_from_arn)
bucket.grantReadWrite(role_from_arn)
```

In the above example, both s3 policy and KMS policy is updated with role permissions as well as an IAM policy will be created and attached to the role.

### Expected Behavior

When using fromRoleName(), resource policy as well as IAM policy be updated/modified.

### Current Behavior

Resource policy is missing when using fromRoleName()

### Reproduction Steps

In Description

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.114.1

### Framework Version

_No response_

### Node.js Version

18.18.2

### OS

macos

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the TypeScript example from the issue with iam.Role.fromRoleName() and fromRoleArn(), then compare the synthesized S3 bucket policy, KMS key policy, and IAM policy. Trace the grantEncryptDecrypt() and grantReadWrite() calls to determine why the imported role forms produce different resource-policy permissions. Done means fromRoleName() generates the same resource-policy permissions as fromRoleArn() while retaining the IAM policy attachment.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
authorization, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.