Skip to content

Commit

Permalink
fix(iam): fromUserArn returns incorrect principalAccount (#30023)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

Closes #29999

### Reason for this change

As described in the issue [comment](#29999 (comment)).

### Description of changes



### Description of how you validated changes

1. added more unit tests.
2. added a new integ test
3. I have deployed this in my AWS account

```ts
import {
  App, Stack, CfnParameter,
  aws_iam as iam,
  CfnOutput,
} from 'aws-cdk-lib';

const app = new App();
const stack = new Stack(app, 'dummy-stack');

const userArn = 'arn:aws:iam::123456789012:user/OthersExternalIamUser';

const userparam = new CfnParameter(stack, 'UserParameter', {
  default: userArn,
});

const imported = iam.User.fromUserArn(stack, 'imported-user', userArn);
const imported2 = iam.User.fromUserArn(stack, 'imported-user2', userparam.valueAsString );

new CfnOutput(stack, 'User', { value: imported.principalAccount! });
new CfnOutput(stack, 'User2', { value: imported2.principalAccount! });
```
And the output is correct:

```
Outputs:
dummy-stack.User = 123456789012
dummy-stack.User2 = 123456789012
```




### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud committed May 9, 2024
1 parent 7bd76bf commit f9f3681
Show file tree
Hide file tree
Showing 11 changed files with 555 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"Parameters": {
"UserParameter": {
"Type": "String",
"Default": "arn:aws:iam::123456789012:user/OthersExternalIamUser"
},
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Resources": {
"User00B015A1": {
"Type": "AWS::IAM::User"
}
},
"Outputs": {
"UserOutput": {
"Value": "123456789012"
},
"User2Output": {
"Value": {
"Fn::Select": [
4,
{
"Fn::Split": [
":",
{
"Ref": "UserParameter"
}
]
}
]
}
},
"User3Output": {
"Value": {
"Fn::Select": [
4,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"User00B015A1",
"Arn"
]
}
]
}
]
}
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9f3681

Please sign in to comment.