|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + AttachedPolicy, |
| 8 | + AttachRolePolicyCommand, |
| 9 | + AttachRolePolicyRequest, |
| 10 | + CreateRoleCommand, |
| 11 | + CreateRoleCommandOutput, |
| 12 | + CreateRoleRequest, |
| 13 | + CreateRoleResponse, |
| 14 | + EvaluationResult, |
| 15 | + GetInstanceProfileCommand, |
| 16 | + GetInstanceProfileCommandOutput, |
| 17 | + IAMClient, |
| 18 | + ListRolesRequest, |
| 19 | + paginateListAttachedRolePolicies, |
| 20 | + paginateListRoles, |
| 21 | + PutRolePolicyCommand, |
| 22 | + PutRolePolicyCommandOutput, |
| 23 | + Role, |
| 24 | + SimulatePolicyResponse, |
| 25 | + SimulatePrincipalPolicyCommand, |
| 26 | + SimulatePrincipalPolicyRequest, |
| 27 | +} from '@aws-sdk/client-iam' |
| 28 | +import { AsyncCollection } from '../utilities/asyncCollection' |
| 29 | +import { ToolkitError } from '../errors' |
| 30 | +import { ClientWrapper } from './clientWrapper' |
| 31 | + |
| 32 | +export interface IamRole extends Role { |
| 33 | + RoleName: string |
| 34 | + Arn: string |
| 35 | +} |
| 36 | + |
| 37 | +export interface IamCreateRoleResponse extends CreateRoleResponse { |
| 38 | + Role: IamRole |
| 39 | +} |
| 40 | + |
| 41 | +export class IamClient extends ClientWrapper<IAMClient> { |
| 42 | + public constructor(public override readonly regionCode: string) { |
| 43 | + super(regionCode, IAMClient) |
| 44 | + } |
| 45 | + |
| 46 | + public getRoles(request: ListRolesRequest = {}, maxPages: number = 500): AsyncCollection<IamRole[]> { |
| 47 | + return this.makePaginatedRequest(paginateListRoles, request, (p) => p.Roles) |
| 48 | + .limit(maxPages) |
| 49 | + .map((roles) => roles.filter(hasRequiredFields)) |
| 50 | + } |
| 51 | + |
| 52 | + /** Gets all roles. */ |
| 53 | + public async resolveRoles(request: ListRolesRequest = {}): Promise<IamRole[]> { |
| 54 | + return this.getRoles(request).flatten().promise() |
| 55 | + } |
| 56 | + |
| 57 | + public async createRole(request: CreateRoleRequest): Promise<IamCreateRoleResponse> { |
| 58 | + const response: CreateRoleCommandOutput = await this.makeRequest(CreateRoleCommand, request) |
| 59 | + if (!response.Role || !hasRequiredFields(response.Role)) { |
| 60 | + throw new ToolkitError('Failed to create IAM role') |
| 61 | + } |
| 62 | + return response as IamCreateRoleResponse // Safe to assume by check above. |
| 63 | + } |
| 64 | + |
| 65 | + public async attachRolePolicy(request: AttachRolePolicyRequest): Promise<AttachRolePolicyCommand> { |
| 66 | + return await this.makeRequest(AttachRolePolicyCommand, request) |
| 67 | + } |
| 68 | + |
| 69 | + public async simulatePrincipalPolicy(request: SimulatePrincipalPolicyRequest): Promise<SimulatePolicyResponse> { |
| 70 | + return await this.makeRequest(SimulatePrincipalPolicyCommand, request) |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Attempts to verify if a role has the provided permissions. |
| 75 | + */ |
| 76 | + public async getDeniedActions(request: SimulatePrincipalPolicyRequest): Promise<EvaluationResult[]> { |
| 77 | + const permissionResponse = await this.simulatePrincipalPolicy(request) |
| 78 | + if (!permissionResponse.EvaluationResults) { |
| 79 | + throw new Error('No evaluation results found') |
| 80 | + } |
| 81 | + |
| 82 | + // Ignore deny from Organization SCP. These can result in false negatives. |
| 83 | + // See https://github.com/aws/aws-sdk/issues/102 |
| 84 | + return permissionResponse.EvaluationResults.filter( |
| 85 | + (r) => r.EvalDecision !== 'allowed' && r.OrganizationsDecisionDetail?.AllowedByOrganizations !== false |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + public getFriendlyName(arn: string): string { |
| 90 | + const tokens = arn.split('/') |
| 91 | + if (tokens.length < 2) { |
| 92 | + throw new Error(`Invalid IAM role ARN (expected format: arn:aws:iam::{id}/{name}): ${arn}`) |
| 93 | + } |
| 94 | + return tokens[tokens.length - 1] |
| 95 | + } |
| 96 | + |
| 97 | + public listAttachedRolePolicies(arn: string): AsyncCollection<AttachedPolicy[]> { |
| 98 | + return this.makePaginatedRequest( |
| 99 | + paginateListAttachedRolePolicies, |
| 100 | + { |
| 101 | + RoleName: this.getFriendlyName(arn), |
| 102 | + }, |
| 103 | + (p) => p.AttachedPolicies |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + public async getIAMRoleFromInstanceProfile(instanceProfileArn: string): Promise<IamRole> { |
| 108 | + const response: GetInstanceProfileCommandOutput = await this.makeRequest(GetInstanceProfileCommand, { |
| 109 | + InstanceProfileName: this.getFriendlyName(instanceProfileArn), |
| 110 | + }) |
| 111 | + if ( |
| 112 | + !response.InstanceProfile?.Roles || |
| 113 | + response.InstanceProfile.Roles.length === 0 || |
| 114 | + !hasRequiredFields(response.InstanceProfile.Roles[0]) |
| 115 | + ) { |
| 116 | + throw new ToolkitError(`Failed to find IAM role associated with Instance profile ${instanceProfileArn}`) |
| 117 | + } |
| 118 | + return response.InstanceProfile.Roles[0] |
| 119 | + } |
| 120 | + |
| 121 | + public async putRolePolicy( |
| 122 | + roleArn: string, |
| 123 | + policyName: string, |
| 124 | + policyDocument: string |
| 125 | + ): Promise<PutRolePolicyCommandOutput> { |
| 126 | + return await this.makeRequest(PutRolePolicyCommand, { |
| 127 | + RoleName: this.getFriendlyName(roleArn), |
| 128 | + PolicyName: policyName, |
| 129 | + PolicyDocument: policyDocument, |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function hasRequiredFields(role: Role): role is IamRole { |
| 135 | + return role.RoleName !== undefined && role.Arn !== undefined |
| 136 | +} |
0 commit comments