@@ -8,6 +8,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
8
8
// input/aws-iam/policy.ts
9
9
const base = require ( "aws-iam/base" ) ;
10
10
const AWSIAMEntity = base . AWSIAMEntity ;
11
+ const common = require ( "aws-iam/common" ) ;
12
+ const IAM_ACTIONS = common . IAM_ACTIONS ;
11
13
const cli = require ( "cli" ) ;
12
14
var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
13
15
getPolicyName ( ) {
@@ -84,7 +86,7 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
84
86
}
85
87
}
86
88
try {
87
- const response = this . makeAWSRequest ( "POST" , "CreatePolicy" , params ) ;
89
+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . CREATE_POLICY , params ) ;
88
90
if ( response . Policy ) {
89
91
this . state = {
90
92
policy_arn : response . Policy . Arn ,
@@ -112,7 +114,7 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
112
114
SetAsDefault : true
113
115
} ;
114
116
try {
115
- const response = this . makeAWSRequest ( "POST" , "CreatePolicyVersion" , params ) ;
117
+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . CREATE_POLICY_VERSION , params ) ;
116
118
if ( response . PolicyVersion ) {
117
119
this . state . default_version_id = response . PolicyVersion . VersionId ;
118
120
this . state . update_date = response . PolicyVersion . CreateDate ;
@@ -127,26 +129,87 @@ var _IAMPolicy = class _IAMPolicy extends AWSIAMEntity {
127
129
if ( ! this . state . policy_arn ) {
128
130
return ;
129
131
}
132
+ if ( this . state . existing ) {
133
+ return ;
134
+ }
130
135
try {
131
- if ( this . state . attachment_count && this . state . attachment_count > 0 ) {
132
- cli . output ( `Warning: Policy has ${ this . state . attachment_count } attachments. You may need to detach it first.` ) ;
133
- }
136
+ this . detachPolicyFromAllEntities ( ) ;
134
137
this . deleteNonDefaultVersions ( ) ;
135
138
this . deletePolicy ( this . state . policy_arn , this . definition . policy_name ) ;
136
139
} catch ( error ) {
137
140
throw new Error ( `Failed to delete IAM Policy ${ this . definition . policy_name } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
138
141
}
139
142
}
143
+ detachPolicyFromAllEntities ( ) {
144
+ if ( ! this . state . policy_arn ) {
145
+ return ;
146
+ }
147
+ try {
148
+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . LIST_ENTITIES_FOR_POLICY , {
149
+ PolicyArn : this . state . policy_arn
150
+ } ) ;
151
+ if ( response . PolicyUsers ) {
152
+ const users = Array . isArray ( response . PolicyUsers ) ? response . PolicyUsers : [ response . PolicyUsers ] ;
153
+ for ( const user of users ) {
154
+ const userName = typeof user === "string" ? user : user . UserName ;
155
+ if ( userName ) {
156
+ try {
157
+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_USER_POLICY , {
158
+ UserName : userName ,
159
+ PolicyArn : this . state . policy_arn
160
+ } ) ;
161
+ } catch ( error ) {
162
+ cli . output ( `Warning: Failed to detach policy from user ${ userName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
163
+ }
164
+ }
165
+ }
166
+ }
167
+ if ( response . PolicyRoles ) {
168
+ const roles = Array . isArray ( response . PolicyRoles ) ? response . PolicyRoles : [ response . PolicyRoles ] ;
169
+ for ( const role of roles ) {
170
+ const roleName = typeof role === "string" ? role : role . RoleName ;
171
+ if ( roleName ) {
172
+ try {
173
+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_ROLE_POLICY , {
174
+ RoleName : roleName ,
175
+ PolicyArn : this . state . policy_arn
176
+ } ) ;
177
+ } catch ( error ) {
178
+ cli . output ( `Warning: Failed to detach policy from role ${ roleName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ if ( response . PolicyGroups ) {
184
+ const groups = Array . isArray ( response . PolicyGroups ) ? response . PolicyGroups : [ response . PolicyGroups ] ;
185
+ for ( const group of groups ) {
186
+ const groupName = typeof group === "string" ? group : group . GroupName ;
187
+ if ( groupName ) {
188
+ try {
189
+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DETACH_GROUP_POLICY , {
190
+ GroupName : groupName ,
191
+ PolicyArn : this . state . policy_arn
192
+ } ) ;
193
+ } catch ( error ) {
194
+ cli . output ( `Warning: Failed to detach policy from group ${ groupName } : ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ } catch ( error ) {
200
+ cli . output ( `Warning: Failed to list policy attachments: ${ error instanceof Error ? error . message : "Unknown error" } ` ) ;
201
+ }
202
+ }
140
203
deleteNonDefaultVersions ( ) {
141
204
try {
142
- const response = this . makeAWSRequest ( "POST" , "ListPolicyVersions" , {
205
+ const response = this . makeAWSRequest ( "POST" , IAM_ACTIONS . LIST_POLICY_VERSIONS , {
143
206
PolicyArn : this . state . policy_arn
144
207
} ) ;
145
208
if ( response . Versions ) {
146
209
const nonDefaultVersions = response . Versions . filter ( ( v ) => ! v . IsDefaultVersion ) ;
147
210
for ( const version of nonDefaultVersions ) {
148
211
try {
149
- this . makeAWSRequest ( "POST" , "DeletePolicyVersion" , {
212
+ this . makeAWSRequest ( "POST" , IAM_ACTIONS . DELETE_POLICY_VERSION , {
150
213
PolicyArn : this . state . policy_arn ,
151
214
VersionId : version . VersionId
152
215
} ) ;
0 commit comments