Skip to content

Commit 22e6b41

Browse files
committed
refactor: migrate the core client
1 parent 3779e71 commit 22e6b41

File tree

1 file changed

+51
-57
lines changed
  • packages/core/src/shared/clients

1 file changed

+51
-57
lines changed

packages/core/src/shared/clients/iam.ts

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,59 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { IAM } from 'aws-sdk'
7-
import globals from '../extensionGlobals'
6+
import {
7+
AttachedPolicy,
8+
AttachRolePolicyCommand,
9+
AttachRolePolicyRequest,
10+
CreateRoleCommand,
11+
CreateRoleRequest,
12+
CreateRoleResponse,
13+
EvaluationResult,
14+
GetInstanceProfileCommand,
15+
IAMClient,
16+
ListRolesRequest,
17+
paginateListAttachedRolePolicies,
18+
paginateListRoles,
19+
PutRolePolicyCommand,
20+
Role,
21+
SimulatePolicyResponse,
22+
SimulatePrincipalPolicyCommand,
23+
SimulatePrincipalPolicyRequest,
24+
} from '@aws-sdk/client-iam'
825
import { AsyncCollection } from '../utilities/asyncCollection'
9-
import { pageableToCollection } from '../utilities/collectionUtils'
1026
import { ToolkitError } from '../errors'
27+
import { ClientWrapper } from './clientWrapper'
1128

12-
/** Do not pull more than this many pages. */
13-
const maxPages = 500
14-
15-
export class IamClient {
16-
public constructor(public readonly regionCode: string) {}
17-
18-
public getRoles(request: IAM.ListRolesRequest = {}): AsyncCollection<IAM.Role[]> {
19-
const requester = async (request: IAM.ListRolesRequest) =>
20-
(await this.createSdkClient()).listRoles(request).promise()
21-
const collection = pageableToCollection(requester, request, 'Marker', 'Roles')
29+
export class IamClient extends ClientWrapper<IAMClient> {
30+
public constructor(public override readonly regionCode: string) {
31+
super(regionCode, IAMClient)
32+
}
2233

23-
return collection.limit(maxPages)
34+
public getRoles(request: ListRolesRequest = {}, maxPages: number = 500): AsyncCollection<Role[]> {
35+
return this.makePaginatedRequest(paginateListRoles, request, (p) => p.Roles).limit(maxPages)
2436
}
2537

2638
/** Gets all roles. */
27-
public async listRoles(request: IAM.ListRolesRequest = {}): Promise<IAM.Role[]> {
39+
public async resolveRoles(request: ListRolesRequest = {}): Promise<Role[]> {
2840
return this.getRoles(request).flatten().promise()
2941
}
3042

31-
public async createRole(request: IAM.CreateRoleRequest): Promise<IAM.CreateRoleResponse> {
32-
const sdkClient = await this.createSdkClient()
33-
const response = await sdkClient.createRole(request).promise()
34-
35-
return response
43+
public async createRole(request: CreateRoleRequest): Promise<CreateRoleResponse> {
44+
return await this.makeRequest(CreateRoleCommand, request)
3645
}
3746

38-
public async attachRolePolicy(request: IAM.AttachRolePolicyRequest): Promise<void> {
39-
const sdkClient = await this.createSdkClient()
40-
await sdkClient.attachRolePolicy(request).promise()
47+
public async attachRolePolicy(request: AttachRolePolicyRequest): Promise<AttachRolePolicyCommand> {
48+
return await this.makeRequest(AttachRolePolicyCommand, request)
4149
}
4250

43-
public async simulatePrincipalPolicy(
44-
request: IAM.SimulatePrincipalPolicyRequest
45-
): Promise<IAM.SimulatePolicyResponse> {
46-
const sdkClient = await this.createSdkClient()
47-
return await sdkClient.simulatePrincipalPolicy(request).promise()
51+
public async simulatePrincipalPolicy(request: SimulatePrincipalPolicyRequest): Promise<SimulatePolicyResponse> {
52+
return await this.makeRequest(SimulatePrincipalPolicyCommand, request)
4853
}
4954

5055
/**
5156
* Attempts to verify if a role has the provided permissions.
5257
*/
53-
public async getDeniedActions(request: IAM.SimulatePrincipalPolicyRequest): Promise<IAM.EvaluationResult[]> {
58+
public async getDeniedActions(request: SimulatePrincipalPolicyRequest): Promise<EvaluationResult[]> {
5459
const permissionResponse = await this.simulatePrincipalPolicy(request)
5560
if (!permissionResponse.EvaluationResults) {
5661
throw new Error('No evaluation results found')
@@ -63,10 +68,6 @@ export class IamClient {
6368
)
6469
}
6570

66-
private async createSdkClient(): Promise<IAM> {
67-
return await globals.sdkClientBuilder.createAwsService(IAM, undefined, this.regionCode)
68-
}
69-
7071
public getFriendlyName(arn: string): string {
7172
const tokens = arn.split('/')
7273
if (tokens.length < 2) {
@@ -75,38 +76,31 @@ export class IamClient {
7576
return tokens[tokens.length - 1]
7677
}
7778

78-
public async listAttachedRolePolicies(arn: string): Promise<IAM.AttachedPolicy[]> {
79-
const client = await this.createSdkClient()
80-
const roleName = this.getFriendlyName(arn)
81-
82-
const requester = async (request: IAM.ListAttachedRolePoliciesRequest) =>
83-
client.listAttachedRolePolicies(request).promise()
84-
85-
const collection = pageableToCollection(requester, { RoleName: roleName }, 'Marker', 'AttachedPolicies')
86-
.flatten()
87-
.filter((p) => p !== undefined)
88-
.map((p) => p!)
89-
90-
const policies = await collection.promise()
91-
92-
return policies
79+
public listAttachedRolePolicies(arn: string): AsyncCollection<AttachedPolicy[]> {
80+
return this.makePaginatedRequest(
81+
paginateListAttachedRolePolicies,
82+
{
83+
RoleName: this.getFriendlyName(arn),
84+
},
85+
(p) => p.AttachedPolicies
86+
)
9387
}
9488

95-
public async getIAMRoleFromInstanceProfile(instanceProfileArn: string): Promise<IAM.Role> {
96-
const client = await this.createSdkClient()
97-
const instanceProfileName = this.getFriendlyName(instanceProfileArn)
98-
const response = await client.getInstanceProfile({ InstanceProfileName: instanceProfileName }).promise()
89+
public async getIAMRoleFromInstanceProfile(instanceProfileArn: string): Promise<Role> {
90+
const response = await this.makeRequest(GetInstanceProfileCommand, {
91+
InstanceProfileName: this.getFriendlyName(instanceProfileArn),
92+
})
9993
if (response.InstanceProfile.Roles.length === 0) {
10094
throw new ToolkitError(`Failed to find IAM role associated with Instance profile ${instanceProfileArn}`)
10195
}
10296
return response.InstanceProfile.Roles[0]
10397
}
10498

10599
public async putRolePolicy(roleArn: string, policyName: string, policyDocument: string): Promise<void> {
106-
const client = await this.createSdkClient()
107-
const roleName = this.getFriendlyName(roleArn)
108-
await client
109-
.putRolePolicy({ RoleName: roleName, PolicyName: policyName, PolicyDocument: policyDocument })
110-
.promise()
100+
return await this.makeRequest(PutRolePolicyCommand, {
101+
RoleName: this.getFriendlyName(roleArn),
102+
PolicyName: policyName,
103+
PolicyDocument: policyDocument,
104+
})
111105
}
112106
}

0 commit comments

Comments
 (0)