-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sdkv3): migrate IAM client to v3 (#6698)
## Problem Iam uses v2. ## Solution - migrate it. - rename from `DefaultIamClient` -> `IamClient`. - add stronger type assertions on request to avoid weak type assertions later on. (replace `as` with proper type checks when possible) - Continue pattern of wrapping sdk types in "safe" types to avoid assertions. --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
1 parent
25ccd97
commit b7cfeae
Showing
22 changed files
with
741 additions
and
185 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
AttachedPolicy, | ||
AttachRolePolicyCommand, | ||
AttachRolePolicyRequest, | ||
CreateRoleCommand, | ||
CreateRoleCommandOutput, | ||
CreateRoleRequest, | ||
CreateRoleResponse, | ||
EvaluationResult, | ||
GetInstanceProfileCommand, | ||
GetInstanceProfileCommandOutput, | ||
IAMClient, | ||
ListRolesRequest, | ||
paginateListAttachedRolePolicies, | ||
paginateListRoles, | ||
PutRolePolicyCommand, | ||
PutRolePolicyCommandOutput, | ||
Role, | ||
SimulatePolicyResponse, | ||
SimulatePrincipalPolicyCommand, | ||
SimulatePrincipalPolicyRequest, | ||
} from '@aws-sdk/client-iam' | ||
import { AsyncCollection } from '../utilities/asyncCollection' | ||
import { ToolkitError } from '../errors' | ||
import { ClientWrapper } from './clientWrapper' | ||
|
||
export interface IamRole extends Role { | ||
RoleName: string | ||
Arn: string | ||
} | ||
|
||
export interface IamCreateRoleResponse extends CreateRoleResponse { | ||
Role: IamRole | ||
} | ||
|
||
export class IamClient extends ClientWrapper<IAMClient> { | ||
public constructor(public override readonly regionCode: string) { | ||
super(regionCode, IAMClient) | ||
} | ||
|
||
public getRoles(request: ListRolesRequest = {}, maxPages: number = 500): AsyncCollection<IamRole[]> { | ||
return this.makePaginatedRequest(paginateListRoles, request, (p) => p.Roles) | ||
.limit(maxPages) | ||
.map((roles) => roles.filter(hasRequiredFields)) | ||
} | ||
|
||
/** Gets all roles. */ | ||
public async resolveRoles(request: ListRolesRequest = {}): Promise<IamRole[]> { | ||
return this.getRoles(request).flatten().promise() | ||
} | ||
|
||
public async createRole(request: CreateRoleRequest): Promise<IamCreateRoleResponse> { | ||
const response: CreateRoleCommandOutput = await this.makeRequest(CreateRoleCommand, request) | ||
if (!response.Role || !hasRequiredFields(response.Role)) { | ||
throw new ToolkitError('Failed to create IAM role') | ||
} | ||
return response as IamCreateRoleResponse // Safe to assume by check above. | ||
} | ||
|
||
public async attachRolePolicy(request: AttachRolePolicyRequest): Promise<AttachRolePolicyCommand> { | ||
return await this.makeRequest(AttachRolePolicyCommand, request) | ||
} | ||
|
||
public async simulatePrincipalPolicy(request: SimulatePrincipalPolicyRequest): Promise<SimulatePolicyResponse> { | ||
return await this.makeRequest(SimulatePrincipalPolicyCommand, request) | ||
} | ||
|
||
/** | ||
* Attempts to verify if a role has the provided permissions. | ||
*/ | ||
public async getDeniedActions(request: SimulatePrincipalPolicyRequest): Promise<EvaluationResult[]> { | ||
const permissionResponse = await this.simulatePrincipalPolicy(request) | ||
if (!permissionResponse.EvaluationResults) { | ||
throw new Error('No evaluation results found') | ||
} | ||
|
||
// Ignore deny from Organization SCP. These can result in false negatives. | ||
// See https://github.com/aws/aws-sdk/issues/102 | ||
return permissionResponse.EvaluationResults.filter( | ||
(r) => r.EvalDecision !== 'allowed' && r.OrganizationsDecisionDetail?.AllowedByOrganizations !== false | ||
) | ||
} | ||
|
||
public getFriendlyName(arn: string): string { | ||
const tokens = arn.split('/') | ||
if (tokens.length < 2) { | ||
throw new Error(`Invalid IAM role ARN (expected format: arn:aws:iam::{id}/{name}): ${arn}`) | ||
} | ||
return tokens[tokens.length - 1] | ||
} | ||
|
||
public listAttachedRolePolicies(arn: string): AsyncCollection<AttachedPolicy[]> { | ||
return this.makePaginatedRequest( | ||
paginateListAttachedRolePolicies, | ||
{ | ||
RoleName: this.getFriendlyName(arn), | ||
}, | ||
(p) => p.AttachedPolicies | ||
) | ||
} | ||
|
||
public async getIAMRoleFromInstanceProfile(instanceProfileArn: string): Promise<IamRole> { | ||
const response: GetInstanceProfileCommandOutput = await this.makeRequest(GetInstanceProfileCommand, { | ||
InstanceProfileName: this.getFriendlyName(instanceProfileArn), | ||
}) | ||
if ( | ||
!response.InstanceProfile?.Roles || | ||
response.InstanceProfile.Roles.length === 0 || | ||
!hasRequiredFields(response.InstanceProfile.Roles[0]) | ||
) { | ||
throw new ToolkitError(`Failed to find IAM role associated with Instance profile ${instanceProfileArn}`) | ||
} | ||
return response.InstanceProfile.Roles[0] | ||
} | ||
|
||
public async putRolePolicy( | ||
roleArn: string, | ||
policyName: string, | ||
policyDocument: string | ||
): Promise<PutRolePolicyCommandOutput> { | ||
return await this.makeRequest(PutRolePolicyCommand, { | ||
RoleName: this.getFriendlyName(roleArn), | ||
PolicyName: policyName, | ||
PolicyDocument: policyDocument, | ||
}) | ||
} | ||
} | ||
|
||
function hasRequiredFields(role: Role): role is IamRole { | ||
return role.RoleName !== undefined && role.Arn !== undefined | ||
} |
Oops, something went wrong.