Skip to content

Commit 05e13f3

Browse files
rix0rrrmergify[bot]
authored andcommitted
fix(iam): make User implement IUser (#3738)
* fix(iam): make User implement IUser Languages using nominal typing (Java, C#) require this annotation to be able to pass a User object where an IUser is expected. Fixes #3490. * Fix linter errors
1 parent 77f5d30 commit 05e13f3

File tree

1 file changed

+56
-2
lines changed
  • packages/@aws-cdk/aws-iam/lib

1 file changed

+56
-2
lines changed

packages/@aws-cdk/aws-iam/lib/user.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Construct, Lazy, Resource, SecretValue } from '@aws-cdk/core';
1+
import { Construct, Lazy, Resource, SecretValue, Stack } from '@aws-cdk/core';
22
import { IGroup } from './group';
33
import { CfnUser } from './iam.generated';
44
import { IIdentity } from './identity-base';
@@ -10,7 +10,15 @@ import { IPrincipal } from './principals';
1010
import { AttachedPolicies, undefinedIfEmpty } from './util';
1111

1212
export interface IUser extends IIdentity {
13+
/**
14+
* The user's name
15+
* @attribute
16+
*/
1317
readonly userName: string;
18+
19+
/**
20+
* Adds this user to a group.
21+
*/
1422
addToGroup(group: IGroup): void;
1523
}
1624

@@ -97,7 +105,53 @@ export interface UserProps {
97105
readonly passwordResetRequired?: boolean;
98106
}
99107

100-
export class User extends Resource implements IIdentity {
108+
/**
109+
* Define a new IAM user
110+
*/
111+
export class User extends Resource implements IIdentity, IUser {
112+
/**
113+
* Import an existing user given a username
114+
*/
115+
public static fromUserName(scope: Construct, id: string, userName: string): IUser {
116+
const arn = Stack.of(scope).formatArn({
117+
service: 'iam',
118+
region: '',
119+
resource: 'user',
120+
resourceName: userName
121+
});
122+
123+
class Import extends Resource implements IUser {
124+
public readonly grantPrincipal: IPrincipal = this;
125+
public readonly userName: string = userName;
126+
public readonly assumeRoleAction: string = 'sts:AssumeRole';
127+
public readonly policyFragment: PrincipalPolicyFragment = new ArnPrincipal(arn).policyFragment;
128+
private defaultPolicy?: Policy;
129+
130+
public addToPolicy(statement: PolicyStatement): boolean {
131+
if (!this.defaultPolicy) {
132+
this.defaultPolicy = new Policy(this, 'Policy');
133+
this.defaultPolicy.attachToUser(this);
134+
}
135+
this.defaultPolicy.addStatements(statement);
136+
return true;
137+
}
138+
139+
public addToGroup(_group: IGroup): void {
140+
throw new Error('Cannot add imported User to Group');
141+
}
142+
143+
public attachInlinePolicy(_policy: Policy): void {
144+
throw new Error('Cannot add inline policy to imported User');
145+
}
146+
147+
public addManagedPolicy(_policy: IManagedPolicy): void {
148+
throw new Error('Cannot add managed policy to imported User');
149+
}
150+
}
151+
152+
return new Import(scope, id);
153+
}
154+
101155
public readonly grantPrincipal: IPrincipal = this;
102156
public readonly assumeRoleAction: string = 'sts:AssumeRole';
103157

0 commit comments

Comments
 (0)