-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathiam-roles.ts
256 lines (245 loc) · 9.67 KB
/
iam-roles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as cdk from 'aws-cdk-lib';
import {
AccountPrincipal,
ArnPrincipal,
CfnInstanceProfile,
CfnManagedPolicy,
Effect,
PolicyDocument,
PolicyStatement,
ServicePrincipal,
} from 'aws-cdk-lib/aws-iam';
import { RoleConfig, AseaResourceType } from '@aws-accelerator/config';
import { SsmResourceType } from '@aws-accelerator/utils/lib/ssm-parameter-path';
import { pascalCase } from 'pascal-case';
import { ImportAseaResourcesStack, LogLevel } from '../stacks/import-asea-resources-stack';
import { AseaResource, AseaResourceProps } from './resource';
const CFN_ROLE_TYPE = 'AWS::IAM::Role';
const ASEA_PHASE_NUMBER = '1';
export interface RolesProps extends AseaResourceProps {
/**
* Policy constructs defined in configuration
*/
policies: { [key: string]: CfnManagedPolicy };
}
/**
* Handles IAM Roles created by ASEA.
* All IAM Roles driven by ASEA configuration are deployed in Phase-1
*/
export class Roles extends AseaResource {
readonly props: RolesProps;
constructor(scope: ImportAseaResourcesStack, props: RolesProps) {
super(scope, props);
this.props = props;
const prefix = this.props.globalConfig.externalLandingZoneResources?.acceleratorPrefix ?? 'ASEA';
if (props.stackInfo.phase !== ASEA_PHASE_NUMBER) {
this.scope.addLogs(LogLevel.INFO, `No ${CFN_ROLE_TYPE}s to handle in stack ${props.stackInfo.stackName}`);
return;
}
if (props.globalConfig.homeRegion !== this.scope.region) {
return;
}
const roleSetItemsInScope = props.iamConfig.roleSets.filter(roleSet =>
this.scope.isIncluded(roleSet.deploymentTargets),
);
const rolesInScope = roleSetItemsInScope.map(roleSetItem => roleSetItem.roles).flat();
this.addDeletionFlagForRoles(rolesInScope, CFN_ROLE_TYPE, prefix);
this.updateRoles(rolesInScope);
}
private getManagedPolicies(roleItem: RoleConfig) {
const managedPolicies: string[] = [];
for (const policyItem of roleItem.policies?.awsManaged ?? []) {
this.scope.addLogs(LogLevel.INFO, `Role - aws managed policy ${policyItem}`);
managedPolicies.push(`arn:${cdk.Aws.PARTITION}:iam::aws:policy/${policyItem}`);
}
for (const policyItem of roleItem.policies?.customerManaged ?? []) {
this.scope.addLogs(LogLevel.INFO, `Role - customer managed policy ${policyItem}`);
const managedPolicy =
this.props.policies[policyItem]?.ref ??
this.resourceSsmParameters[this.scope.getSsmPath(SsmResourceType.IAM_POLICY, [policyItem])];
if (managedPolicy) {
managedPolicies.push(managedPolicy);
}
}
return managedPolicies;
}
private getAssumeRolePolicy(roleItem: RoleConfig) {
const statements: PolicyStatement[] = [];
for (const assumedByItem of roleItem.assumedBy ?? []) {
if (assumedByItem.type === 'service' || assumedByItem.type === 'account') {
statements.push(
new PolicyStatement({
actions: ['sts:AssumeRole'],
effect: Effect.ALLOW,
principals: [new ServicePrincipal(assumedByItem.principal)],
}),
);
}
if (assumedByItem.type === 'principalArn') {
statements.push(
new PolicyStatement({
actions: ['sts:AssumeRole'],
effect: Effect.ALLOW,
principals: [new ArnPrincipal(assumedByItem.principal)],
}),
);
}
if (assumedByItem.type === 'account' && assumedByItem.principal) {
const partition = this.props.partition;
const accountIdRegex = /^\d{12}$/;
const accountArnRegex = new RegExp('^arn:' + partition + ':iam::(\\d{12}):root$');
if (accountIdRegex.test(assumedByItem.principal)) {
statements.push(
new PolicyStatement({
actions: ['sts:AssumeRole'],
effect: Effect.ALLOW,
principals: [new AccountPrincipal(assumedByItem.principal)],
}),
);
} else if (accountArnRegex.test(assumedByItem.principal)) {
const accountId = accountArnRegex.exec(assumedByItem.principal);
statements.push(
new PolicyStatement({
actions: ['sts:AssumeRole'],
effect: Effect.ALLOW,
principals: [new AccountPrincipal(accountId![1])],
}),
);
} else {
statements.push(
new PolicyStatement({
actions: ['sts:AssumeRole'],
effect: Effect.ALLOW,
principals: [
new cdk.aws_iam.AccountPrincipal(this.props.accountsConfig.getAccountId(assumedByItem.principal)),
],
}),
);
}
}
}
return new PolicyDocument({
statements,
});
}
private setResourceBoundaryPolicy(roleItem: RoleConfig, resource: cdk.aws_iam.CfnRole) {
if (roleItem.boundaryPolicy) {
const boundaryPolicy =
this.props.policies[roleItem.boundaryPolicy]?.ref ??
this.resourceSsmParameters[this.scope.getSsmPath(SsmResourceType.IAM_POLICY, [roleItem.boundaryPolicy])];
if (boundaryPolicy) {
resource.permissionsBoundary = boundaryPolicy;
}
} else if (resource.permissionsBoundary) {
resource.permissionsBoundary = undefined;
}
}
private setInstanceProfile(roleItem: RoleConfig, resource: cdk.aws_iam.CfnRole) {
const existingInstanceProfile = this.scope.importStackResources.getResourceByName(
'InstanceProfileName',
`${roleItem.name}-ip`,
);
if (existingInstanceProfile && !roleItem.instanceProfile) {
this.scope.node.tryRemoveChild(existingInstanceProfile.logicalResourceId);
return;
}
if (!roleItem.instanceProfile) {
return;
}
let instanceProfile: CfnInstanceProfile;
if (!existingInstanceProfile) {
// Creating instance profile since, Role is managed by ASEA and "instanceProfile" flag is changed to true
instanceProfile = new CfnInstanceProfile(this.scope, `${pascalCase(roleItem.name)}InstanceProfile`, {
roles: [resource.ref],
instanceProfileName: `${roleItem.name}-ip`,
});
} else {
instanceProfile = this.scope.getResource(existingInstanceProfile.logicalResourceId) as CfnInstanceProfile;
instanceProfile.instanceProfileName = `${roleItem.name}-ip`;
}
}
// add the delete flag to roles that are no longer in the config file
private addDeletionFlagForRoles(roleItems: RoleConfig[], resourceType: string, acceleratorPrefix: string) {
const rolesPrefixesToExclude = [
`${acceleratorPrefix}-VPC-FlowLog`,
`${acceleratorPrefix}-VPC-PCX`,
`${acceleratorPrefix}-Reports`,
];
const importRoles = this.scope.importStackResources.getResourcesByType(resourceType);
for (const importRole of importRoles) {
const roleResource = this.scope.getResource(importRole.logicalResourceId) as cdk.aws_iam.CfnRole;
const roleName = roleResource.roleName;
if (!roleName) {
continue;
}
const excludedRole = rolesPrefixesToExclude.filter(prefix => {
return roleName!.startsWith(prefix);
});
if (excludedRole.length > 0) {
continue;
}
const roleExistsInConfig = roleItems.find(item => item.name === roleName);
if (!roleExistsInConfig) {
importRole.isDeleted = true;
this.scope.addDeleteFlagForAseaResource({
type: resourceType,
identifier: roleName,
logicalId: importRole.logicalResourceId,
});
// Add the delete flag to the ssm parameter created with the role.
const ssmResource = this.scope.importStackResources.getSSMParameterByName(
this.scope.getSsmPath(SsmResourceType.IAM_ROLE, [roleName]),
);
if (ssmResource) {
ssmResource.isDeleted = true;
}
const existingInstanceProfile = this.scope.importStackResources.getResourceByName(
'InstanceProfileName',
`${roleName}-ip`,
);
if (existingInstanceProfile) {
existingInstanceProfile.isDeleted = true;
}
}
}
}
private updateRoles(roleItems: RoleConfig[]) {
if (roleItems.length === 0) {
this.scope.addLogs(LogLevel.INFO, `No ${CFN_ROLE_TYPE}s to handle in stack.`);
return;
}
for (const roleItem of roleItems) {
this.scope.addLogs(LogLevel.INFO, `Add role ${roleItem.name}`);
const role = this.scope.importStackResources.getResourceByName('RoleName', roleItem.name);
if (!role) {
continue;
}
const resource = this.scope.getResource(role.logicalResourceId) as cdk.aws_iam.CfnRole;
if (!resource) {
continue;
}
resource.managedPolicyArns = this.getManagedPolicies(roleItem);
resource.assumeRolePolicyDocument = this.getAssumeRolePolicy(roleItem);
this.setResourceBoundaryPolicy(roleItem, resource);
this.setInstanceProfile(roleItem, resource);
this.scope.addSsmParameter({
logicalId: pascalCase(`SsmParam${pascalCase(roleItem.name)}RoleArn`),
parameterName: this.scope.getSsmPath(SsmResourceType.IAM_ROLE, [roleItem.name]),
stringValue: resource.attrArn,
});
this.scope.addAseaResource(AseaResourceType.IAM_ROLE, roleItem.name);
}
}
}