runbooks 0.7.0__py3-none-any.whl → 0.7.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. runbooks/__init__.py +87 -37
  2. runbooks/cfat/README.md +300 -49
  3. runbooks/cfat/__init__.py +2 -2
  4. runbooks/finops/__init__.py +1 -1
  5. runbooks/finops/cli.py +1 -1
  6. runbooks/inventory/collectors/__init__.py +8 -0
  7. runbooks/inventory/collectors/aws_management.py +791 -0
  8. runbooks/inventory/collectors/aws_networking.py +3 -3
  9. runbooks/main.py +3389 -782
  10. runbooks/operate/__init__.py +207 -0
  11. runbooks/operate/base.py +311 -0
  12. runbooks/operate/cloudformation_operations.py +619 -0
  13. runbooks/operate/cloudwatch_operations.py +496 -0
  14. runbooks/operate/dynamodb_operations.py +812 -0
  15. runbooks/operate/ec2_operations.py +926 -0
  16. runbooks/operate/iam_operations.py +569 -0
  17. runbooks/operate/s3_operations.py +1211 -0
  18. runbooks/operate/tagging_operations.py +655 -0
  19. runbooks/remediation/CLAUDE.md +100 -0
  20. runbooks/remediation/DOME9.md +218 -0
  21. runbooks/remediation/README.md +26 -0
  22. runbooks/remediation/Tests/__init__.py +0 -0
  23. runbooks/remediation/Tests/update_policy.py +74 -0
  24. runbooks/remediation/__init__.py +95 -0
  25. runbooks/remediation/acm_cert_expired_unused.py +98 -0
  26. runbooks/remediation/acm_remediation.py +875 -0
  27. runbooks/remediation/api_gateway_list.py +167 -0
  28. runbooks/remediation/base.py +643 -0
  29. runbooks/remediation/cloudtrail_remediation.py +908 -0
  30. runbooks/remediation/cloudtrail_s3_modifications.py +296 -0
  31. runbooks/remediation/cognito_active_users.py +78 -0
  32. runbooks/remediation/cognito_remediation.py +856 -0
  33. runbooks/remediation/cognito_user_password_reset.py +163 -0
  34. runbooks/remediation/commons.py +455 -0
  35. runbooks/remediation/dynamodb_optimize.py +155 -0
  36. runbooks/remediation/dynamodb_remediation.py +744 -0
  37. runbooks/remediation/dynamodb_server_side_encryption.py +108 -0
  38. runbooks/remediation/ec2_public_ips.py +134 -0
  39. runbooks/remediation/ec2_remediation.py +892 -0
  40. runbooks/remediation/ec2_subnet_disable_auto_ip_assignment.py +72 -0
  41. runbooks/remediation/ec2_unattached_ebs_volumes.py +448 -0
  42. runbooks/remediation/ec2_unused_security_groups.py +202 -0
  43. runbooks/remediation/kms_enable_key_rotation.py +651 -0
  44. runbooks/remediation/kms_remediation.py +717 -0
  45. runbooks/remediation/lambda_list.py +243 -0
  46. runbooks/remediation/lambda_remediation.py +971 -0
  47. runbooks/remediation/multi_account.py +569 -0
  48. runbooks/remediation/rds_instance_list.py +199 -0
  49. runbooks/remediation/rds_remediation.py +873 -0
  50. runbooks/remediation/rds_snapshot_list.py +192 -0
  51. runbooks/remediation/requirements.txt +118 -0
  52. runbooks/remediation/s3_block_public_access.py +159 -0
  53. runbooks/remediation/s3_bucket_public_access.py +143 -0
  54. runbooks/remediation/s3_disable_static_website_hosting.py +74 -0
  55. runbooks/remediation/s3_downloader.py +215 -0
  56. runbooks/remediation/s3_enable_access_logging.py +562 -0
  57. runbooks/remediation/s3_encryption.py +526 -0
  58. runbooks/remediation/s3_force_ssl_secure_policy.py +143 -0
  59. runbooks/remediation/s3_list.py +141 -0
  60. runbooks/remediation/s3_object_search.py +201 -0
  61. runbooks/remediation/s3_remediation.py +816 -0
  62. runbooks/remediation/scan_for_phrase.py +425 -0
  63. runbooks/remediation/workspaces_list.py +220 -0
  64. runbooks/security/__init__.py +9 -10
  65. runbooks/security/security_baseline_tester.py +4 -2
  66. runbooks-0.7.6.dist-info/METADATA +608 -0
  67. {runbooks-0.7.0.dist-info → runbooks-0.7.6.dist-info}/RECORD +84 -76
  68. {runbooks-0.7.0.dist-info → runbooks-0.7.6.dist-info}/entry_points.txt +0 -1
  69. {runbooks-0.7.0.dist-info → runbooks-0.7.6.dist-info}/top_level.txt +0 -1
  70. jupyter-agent/.env +0 -2
  71. jupyter-agent/.env.template +0 -2
  72. jupyter-agent/.gitattributes +0 -35
  73. jupyter-agent/.gradio/certificate.pem +0 -31
  74. jupyter-agent/README.md +0 -16
  75. jupyter-agent/__main__.log +0 -8
  76. jupyter-agent/app.py +0 -256
  77. jupyter-agent/cloudops-agent.png +0 -0
  78. jupyter-agent/ds-system-prompt.txt +0 -154
  79. jupyter-agent/jupyter-agent.png +0 -0
  80. jupyter-agent/llama3_template.jinja +0 -123
  81. jupyter-agent/requirements.txt +0 -9
  82. jupyter-agent/tmp/4ojbs8a02ir/jupyter-agent.ipynb +0 -68
  83. jupyter-agent/tmp/cm5iasgpm3p/jupyter-agent.ipynb +0 -91
  84. jupyter-agent/tmp/crqbsseag5/jupyter-agent.ipynb +0 -91
  85. jupyter-agent/tmp/hohanq1u097/jupyter-agent.ipynb +0 -57
  86. jupyter-agent/tmp/jns1sam29wm/jupyter-agent.ipynb +0 -53
  87. jupyter-agent/tmp/jupyter-agent.ipynb +0 -27
  88. jupyter-agent/utils.py +0 -409
  89. runbooks/aws/__init__.py +0 -58
  90. runbooks/aws/dynamodb_operations.py +0 -231
  91. runbooks/aws/ec2_copy_image_cross-region.py +0 -195
  92. runbooks/aws/ec2_describe_instances.py +0 -202
  93. runbooks/aws/ec2_ebs_snapshots_delete.py +0 -186
  94. runbooks/aws/ec2_run_instances.py +0 -213
  95. runbooks/aws/ec2_start_stop_instances.py +0 -212
  96. runbooks/aws/ec2_terminate_instances.py +0 -143
  97. runbooks/aws/ec2_unused_eips.py +0 -196
  98. runbooks/aws/ec2_unused_volumes.py +0 -188
  99. runbooks/aws/s3_create_bucket.py +0 -142
  100. runbooks/aws/s3_list_buckets.py +0 -152
  101. runbooks/aws/s3_list_objects.py +0 -156
  102. runbooks/aws/s3_object_operations.py +0 -183
  103. runbooks/aws/tagging_lambda_handler.py +0 -183
  104. runbooks/inventory/FAILED_SCRIPTS_TROUBLESHOOTING.md +0 -619
  105. runbooks/inventory/PASSED_SCRIPTS_GUIDE.md +0 -738
  106. runbooks/inventory/aws_organization.png +0 -0
  107. runbooks/inventory/cfn_move_stack_instances.py +0 -1526
  108. runbooks/inventory/delete_s3_buckets_objects.py +0 -169
  109. runbooks/inventory/lockdown_cfn_stackset_role.py +0 -224
  110. runbooks/inventory/update_aws_actions.py +0 -173
  111. runbooks/inventory/update_cfn_stacksets.py +0 -1215
  112. runbooks/inventory/update_cloudwatch_logs_retention_policy.py +0 -294
  113. runbooks/inventory/update_iam_roles_cross_accounts.py +0 -478
  114. runbooks/inventory/update_s3_public_access_block.py +0 -539
  115. runbooks/organizations/__init__.py +0 -12
  116. runbooks/organizations/manager.py +0 -374
  117. runbooks-0.7.0.dist-info/METADATA +0 -375
  118. /runbooks/inventory/{tests → Tests}/common_test_data.py +0 -0
  119. /runbooks/inventory/{tests → Tests}/common_test_functions.py +0 -0
  120. /runbooks/inventory/{tests → Tests}/script_test_data.py +0 -0
  121. /runbooks/inventory/{tests → Tests}/setup.py +0 -0
  122. /runbooks/inventory/{tests → Tests}/src.py +0 -0
  123. /runbooks/inventory/{tests/test_inventory_modules.py → Tests/test_Inventory_Modules.py} +0 -0
  124. /runbooks/inventory/{tests → Tests}/test_cfn_describe_stacks.py +0 -0
  125. /runbooks/inventory/{tests → Tests}/test_ec2_describe_instances.py +0 -0
  126. /runbooks/inventory/{tests → Tests}/test_lambda_list_functions.py +0 -0
  127. /runbooks/inventory/{tests → Tests}/test_moto_integration_example.py +0 -0
  128. /runbooks/inventory/{tests → Tests}/test_org_list_accounts.py +0 -0
  129. /runbooks/inventory/{Inventory_Modules.py → inventory_modules.py} +0 -0
  130. /runbooks/{aws → operate}/tags.json +0 -0
  131. {runbooks-0.7.0.dist-info → runbooks-0.7.6.dist-info}/WHEEL +0 -0
  132. {runbooks-0.7.0.dist-info → runbooks-0.7.6.dist-info}/licenses/LICENSE +0 -0
@@ -1,1215 +0,0 @@
1
- #!/usr/bin/env python3
2
-
3
- import logging
4
- import sys
5
- from queue import Queue
6
- from threading import Thread
7
- from time import sleep, time
8
-
9
- from account_class import aws_acct_access
10
- from ArgumentsClass import CommonArguments
11
- from botocore.exceptions import ClientError
12
- from colorama import Fore, Style, init
13
- from Inventory_Modules import (
14
- check_stack_set_status3,
15
- delete_stack_instances3,
16
- delete_stackset3,
17
- find_stack_instances3,
18
- find_stacksets3,
19
- get_child_access3,
20
- get_ec2_regions3,
21
- get_regions3,
22
- random_string,
23
- )
24
- from tqdm.auto import tqdm
25
-
26
- """
27
- - There are four possible use-cases when trying to modify or remove a stack instance from a stack set:
28
- - The stack exists as an association within the stackset AND it exists within the child account (typical)
29
- - We should remove the stackset-association with "--RetainStacks=False" and that will remove the child stack in the child account.
30
- - The stack exists as an association within the stackset, but has been manually deleted within the child account
31
- - If we remove the stackset-association with "--RetainStacks=False", it won't error, even when the stack doesn't exist within the child.
32
- - There is a special use case here where the child account has been deleted or suspended. This is a difficult use-case to test12.
33
- - The stack doesn't exist within the stackset association, but DOES exist within the child account (because its association was removed from the stackset)
34
- - The only way to remove this is to remove the stack from the child account. This would have to be done after having found the stack within the child account. This will be a ToDo for later...
35
- - The stack doesn't exist within the child account, nor within the stack-set
36
- - Nothing to do here
37
-
38
- TODO:
39
- - Pythonize the whole thing
40
- - More Commenting throughout script
41
- - Make deleting multiple closed accounts easier - needs a parameter that comprises "+delete +force -A -check" all in one - to remove all closed accounts at one time...
42
- - Add a stackset status, instead of just the status for the instances
43
- - Add a "tail" option, so it runs over and over until the stackset is finished
44
- - Make sure that the part where it removes stack instances and WAITS till they're done it working...
45
- """
46
-
47
- init()
48
-
49
- __version__ = "2024.05.31"
50
- ERASE_LINE = "\x1b[2K"
51
- begin_time = time()
52
- sleep_interval = 5
53
- # Seems low, but this fits under the API threshold. Make it too high and it will not.
54
- DefaultMaxWorkerThreads = 5
55
-
56
-
57
- ###################
58
- def parse_args(f_arguments: object):
59
- """
60
- Description: Parses the arguments passed into the script
61
- @param f_arguments: args represents the list of arguments passed in
62
- @return: returns an object namespace that contains the individualized parameters passed in
63
- """
64
- parser = CommonArguments()
65
- parser.singleprofile()
66
- parser.singleregion()
67
- # This next parameter includes picking a specific account, ignoring specific accounts or profiles
68
- parser.extendedargs()
69
- parser.fragment()
70
- parser.roletouse()
71
- # parser.save_to_file()
72
- parser.confirm()
73
- parser.timing()
74
- parser.verbosity()
75
- parser.version(__version__)
76
- operation_group = parser.my_parser.add_mutually_exclusive_group()
77
- operation_group.add_argument(
78
- "+refresh",
79
- help="Use this parameter is you want to re-run the same stackset, over again",
80
- action="store_true",
81
- dest="Refresh",
82
- )
83
- operation_group.add_argument(
84
- "+add",
85
- help="If this parameter is specified, we'll add the specified accounts to the specified stacksets.",
86
- action="store_true",
87
- dest="AddNew",
88
- )
89
- operation_group.add_argument(
90
- "+delete",
91
- "+remove",
92
- help="If this parameter is specified, we'll delete stacksets we find, with no additional confirmation.",
93
- action="store_true",
94
- dest="DryRun",
95
- )
96
- parser.my_parser.add_argument(
97
- "-R",
98
- "--ModifyRegion",
99
- help="The region(s) you want to either remove from or add to all the specified stacksets.",
100
- default=None,
101
- nargs="*",
102
- metavar="region-name",
103
- dest="pRegionModifyList",
104
- )
105
- parser.my_parser.add_argument(
106
- "-c",
107
- "-check",
108
- "--check",
109
- help="Do a comparison of the accounts found in the stacksets to the accounts found in the Organization and list out any that have been closed or suspended, but never removed from the stacksets.",
110
- action="store_true",
111
- dest="AccountCheck",
112
- )
113
- parser.my_parser.add_argument(
114
- "--date",
115
- help="Provide this flag if you want the date of the last operation included with the detailed stack instance output",
116
- action="store_true",
117
- dest="OperationDate",
118
- )
119
- parser.my_parser.add_argument(
120
- "--retain",
121
- "--disassociate",
122
- help="Remove the stack instances from the stackset, but retain the resources. You must also specify '+delete' to use this parameter",
123
- action="store_true",
124
- dest="Retain",
125
- )
126
- return parser.my_parser.parse_args(f_arguments)
127
-
128
-
129
- def setup_auth_and_regions(fProfile: str) -> (aws_acct_access, list):
130
- """
131
- Description: This function takes in a profile, and returns the account object and the regions valid for this account / org.
132
- @param fProfile: A string representing the profile provided by the user. If nothing, then use the default profile or credentials
133
- @return:
134
- - an object of the type "aws_acct_access"
135
- - a list of regions valid for this particular profile/ account.
136
- """
137
- try:
138
- aws_acct = aws_acct_access(fProfile)
139
- except ConnectionError as my_Error:
140
- logging.error(f"Exiting due to error: {my_Error}")
141
- sys.exit(8)
142
-
143
- AllRegions = get_ec2_regions3(aws_acct)
144
-
145
- if pRegion.lower() not in AllRegions:
146
- print()
147
- print(
148
- f"{Fore.RED}You specified '{pRegion}' as the region, but this script only works with a single region.\n"
149
- f"Please run the command again and specify only a single, valid region{Fore.RESET}"
150
- )
151
- print()
152
- sys.exit(9)
153
-
154
- print()
155
- if pdelete:
156
- action = "and delete"
157
- elif pAddNew:
158
- action = "and add to"
159
- elif pRefresh:
160
- action = "and refresh"
161
- else:
162
- action = "but not modify"
163
- print(f"You asked me to find ({action}) stacksets that match the following:")
164
- print(f"\t\tIn the {aws_acct.AccountType} account {aws_acct.acct_number}")
165
- print(f"\t\tIn this Region: {pRegion}")
166
-
167
- RegionList = get_regions3(aws_acct, pRegionModifyList)
168
-
169
- if pRegionModifyList is None:
170
- print(f"\t\tFor stack instances across all enabled Regions")
171
- else:
172
- print(f"\t\tLimiting instance targets to Region{'s' if len(RegionList) > 1 else ''}: {RegionList}")
173
-
174
- if pExact:
175
- print(f"\t\tFor stacksets that {Fore.RED}exactly match{Fore.RESET}: {pStackfrag}")
176
- else:
177
- print(
178
- f"\t\tFor stacksets that contain th{'is fragment' if len(pStackfrag) == 1 else 'ese fragments'}: {pStackfrag}"
179
- )
180
-
181
- if pAccountModifyList is None:
182
- print(f"\t\tFor stack instances across all accounts")
183
- else:
184
- print(
185
- f"\t\tSpecifically to find th{'ese' if len(pAccountModifyList) > 1 else 'is'} account number{'s' if len(pAccountModifyList) > 1 else ''}: {pAccountModifyList}"
186
- )
187
- # print(f"\t\tSpecifically to find th{'ese' if len(pRegionModifyList) > 1 else 'is'} region{'s' if len(pRegionModifyList) > 1 else ''}: {pRegionModifyList}") if pRegionModifyList is not None else ""
188
- print(
189
- f"\t\tWe'll also display those accounts in the stacksets that are no longer part of the organization"
190
- ) if pCheckAccount else ""
191
- print(f"\t\tWe'll refresh the stackset with fragments {pStackfrag}") if pRefresh else ""
192
- print()
193
- return aws_acct, RegionList
194
-
195
-
196
- def _find_stack_set_instances(fStackSetNames: dict, fRegion: str) -> list:
197
- """
198
- Note that this function takes a list of stack set names and finds the stack instances within them
199
- fStackSetNames - This is a list of stackset names to look for. The reserved word "all" will return everything
200
- fRegion - This is a string containing the region in which to look for stacksets.
201
-
202
- """
203
-
204
- class FindStackSets(Thread):
205
- def __init__(self, queue):
206
- Thread.__init__(self)
207
- self.queue = queue
208
-
209
- def run(self):
210
- """
211
- Description: This function simply multi-threads the calls to the Inventory_Scripts function called "find_stack_instances3"
212
- @return: This returns a list with the stackset instance information...
213
- """
214
- while True:
215
- # Get the work from the queue and expand the tuple
216
- c_stacksetname, c_region, c_stackset_info, c_PlaceCount = self.queue.get()
217
- logging.info(f"De-queued info for stack set name {c_stacksetname}")
218
- try:
219
- # Now go through those stacksets and determine the instances, made up of accounts and regions
220
- # Most time spent in this loop
221
- # for i in range(len(fStackSetNames['StackSets'])):
222
- logging.info(
223
- f"{ERASE_LINE}Looking through {c_PlaceCount} of {len(fStackSetNames)} stacksets found with '{pStackfrag}' string in them"
224
- )
225
- # TODO: Creating the list to delete this way prohibits this script from including stacksets that are already empty. This should be fixed.
226
- StackInstances = find_stack_instances3(aws_acct, c_region, c_stacksetname)
227
- logging.warning(
228
- f"Found {len(StackInstances)} Stack Instances within the StackSet {c_stacksetname}, without filtering on the specific accounts we were looking for"
229
- )
230
- if len(StackInstances) == 0 and pAccountModifyList is None and pRegionModifyList is None:
231
- # logging.warning(f"While we didn't find any stack instances within {fStackSetNames['StackSets'][i]['StackSetName']}, we assume you want to delete it, even when it's empty")
232
- logging.warning(
233
- f"While we didn't find any stack instances within {c_stacksetname}, we assume you want to include it, even when it's empty"
234
- )
235
- f_combined_stack_set_instances.append(
236
- {
237
- "ParentAccountNumber": aws_acct.acct_number,
238
- "ChildAccount": None,
239
- "ChildRegion": None,
240
- "StackStatus": None,
241
- "DetailedStatus": None,
242
- "StatusReason": None,
243
- "OrganizationalUnitId": None,
244
- "PermissionModel": c_stackset_info["PermissionModel"]
245
- if "PermissionModel" in c_stackset_info
246
- else "SELF_MANAGED",
247
- "StackSetName": c_stacksetname,
248
- "LastOperationId": None,
249
- }
250
- )
251
- for StackInstance in StackInstances:
252
- if "StackId" not in StackInstance.keys():
253
- logging.info(
254
- f"The stack instance found {StackInstance} doesn't have a stackid associated. Which means it's never been deployed and probably OUTDATED"
255
- )
256
- pass
257
- if pAccountModifyList is None or StackInstance["Account"] in pAccountModifyList:
258
- # This stack instance will be reported if it matches the account they provided,
259
- # or reported on if they didn't provide an account list at all.
260
- # or - it will be removed if they also provided the "+delete" parameter,
261
- # or it will be included since they're trying to ADD accounts to this stackset...
262
- logging.debug(f"This is Instance #: {str(StackInstance)}")
263
- logging.debug(f"This is instance status: {str(StackInstance['Status'])}")
264
- logging.debug(f"This is ChildAccount: {StackInstance['Account']}")
265
- logging.debug(f"This is ChildRegion: {StackInstance['Region']}")
266
- # logging.debug("This is StackId: %s", str(StackInstance['StackId']))
267
-
268
- if pRegionModifyList is None or (StackInstance["Region"] in RegionList) or ChangesRequested:
269
- f_combined_stack_set_instances.append(
270
- {
271
- "ParentAccountNumber": aws_acct.acct_number,
272
- "ChildAccount": StackInstance["Account"],
273
- "ChildRegion": StackInstance["Region"],
274
- "StackStatus": StackInstance["Status"],
275
- "DetailedStatus": StackInstance["StackInstanceStatus"]["DetailedStatus"]
276
- if "DetailedStatus" in StackInstance["StackInstanceStatus"]
277
- else None,
278
- "StatusReason": StackInstance["StatusReason"]
279
- if "StatusReason" in StackInstance
280
- else None,
281
- "OrganizationalUnitId": StackInstance["OrganizationalUnitId"]
282
- if "OrganizationalUnitId" in StackInstance
283
- else None,
284
- "PermissionModel": c_stackset_info["PermissionModel"]
285
- if "PermissionModel" in c_stackset_info
286
- else "SELF_MANAGED",
287
- "StackSetName": c_stacksetname,
288
- "LastOperationId": StackInstance["LastOperationId"],
289
- }
290
- )
291
- elif not (StackInstance["Account"] in pAccountModifyList):
292
- # If the user only wants to remove the stack instances associated with specific accounts,
293
- # then we only want to capture those stack instances where the account number shows up.
294
- # The following code captures this scenario
295
- logging.debug(
296
- f"Found a stack instance, but the account didn't match {pAccountModifyList}... exiting"
297
- )
298
- continue
299
- except KeyError as my_Error:
300
- logging.error(f"Account Access failed - trying to access {c_stacksetname}")
301
- logging.info(f"Actual Error: {my_Error}")
302
- pass
303
- except AttributeError as my_Error:
304
- logging.error(f"Error: Likely that one of the supplied profiles was wrong")
305
- logging.info(f"Actual Error: {my_Error}")
306
- continue
307
- except ClientError as my_Error:
308
- logging.error(f"Error: Likely throttling errors from too much activity")
309
- logging.info(f"Actual Error: {my_Error}")
310
- logging.debug(
311
- f"Operations name: {my_Error.operation_name} | Response: {my_Error.response} | MSG TEMPLATE: {my_Error.MSG_TEMPLATE}"
312
- )
313
- continue
314
- finally:
315
- logging.info(
316
- f"{ERASE_LINE}Finished finding stack instances in stackset {c_stacksetname} in region {c_region} - {c_PlaceCount} / {len(fStackSetNames)}"
317
- )
318
- pbar.update()
319
- self.queue.task_done()
320
-
321
- ###########
322
-
323
- if fRegion is None:
324
- fRegion = "us-east-1"
325
- checkqueue = Queue()
326
-
327
- f_combined_stack_set_instances = []
328
- PlaceCount = 0
329
- WorkerThreads = min(len(fStackSetNames), DefaultMaxWorkerThreads)
330
-
331
- pbar = tqdm(
332
- desc=f"Finding instances from {len(fStackSetNames)} stacksets", total=len(fStackSetNames), unit=" stacksets"
333
- )
334
-
335
- for x in range(WorkerThreads):
336
- worker = FindStackSets(checkqueue)
337
- # Setting daemon to True will let the main thread exit even though the workers are blocking
338
- worker.daemon = True
339
- worker.start()
340
-
341
- for stackset_name, stackset_data in fStackSetNames.items():
342
- logging.debug(f"Beginning to queue data - starting with {stackset_name}")
343
- try:
344
- # I don't know why - but double parens are necessary below. If you remove them, only the first parameter is queued.
345
- PlaceCount += 1
346
- checkqueue.put((stackset_name, fRegion, stackset_data, PlaceCount))
347
- except ClientError as my_Error:
348
- if "AuthFailure" in str(my_Error):
349
- logging.error(f"Authorization Failure accessing stack set {stackset_name} in {fRegion} region")
350
- logging.warning(f"It's possible that the region {fRegion} hasn't been opted-into")
351
- pass
352
- checkqueue.join()
353
- pbar.close()
354
- return f_combined_stack_set_instances
355
-
356
-
357
- def display_stack_set_health(StackSet_Dict: dict, Account_Dict: dict):
358
- """
359
- Description: This function shows off the health of the stacksets at the end of the script
360
- @param StackSet_Dict: Dictionary containing the stacksets that were updated
361
- @param Account_Dict: Dictionary containing the accounts that were used
362
- @return: Nothing - just writes to the screen
363
-
364
- TODO: Since it refers to "combined_stack_set_instances",
365
- there's a edge-case where it skips stacksets that don't appear to have the instance, if it failed to be added.
366
- """
367
- combined_stack_set_instances = StackSet_Dict["combined_stack_set_instances"]
368
- RemovedAccounts = Account_Dict["RemovedAccounts"] if pCheckAccount else None
369
- InaccessibleAccounts = Account_Dict["InaccessibleAccounts"] if pCheckAccount else None
370
- StackSetNames = StackSet_Dict["StackSetNames"]["StackSets"]
371
- summary = {}
372
- stack_set_permission_models = dict()
373
- for record in combined_stack_set_instances:
374
- stack_set_name = record["StackSetName"]
375
- stack_status = record["StackStatus"]
376
- # stackset_status = StackSetNames[stack_set_name]['Status']
377
- # stackset_status_reason = StackSetNames[stack_set_name]['Reason']
378
- detailed_status = record["DetailedStatus"]
379
- status_reason = record["StatusReason"]
380
- stack_region = record["ChildRegion"]
381
- last_operation = record["LastOperationId"]
382
- ou = record["OrganizationalUnitId"]
383
- stack_set_permission_models.update({stack_set_name: record["PermissionModel"]})
384
- if stack_set_name not in summary:
385
- summary[stack_set_name] = {}
386
- if stack_status not in summary[stack_set_name]:
387
- summary[stack_set_name][stack_status] = []
388
- summary[stack_set_name][stack_status].append(
389
- {
390
- "Account": record["ChildAccount"],
391
- "Region": stack_region,
392
- "DetailedStatus": detailed_status,
393
- "StatusReason": status_reason,
394
- "LastOperation": last_operation,
395
- "OrganizationalUnitId": ou,
396
- }
397
- )
398
- summary[stack_set_name]["Status"] = StackSetNames[stack_set_name]["Status"]
399
-
400
- # Print the summary
401
- sorted_summary = dict(sorted(summary.items()))
402
- print()
403
- for stack_set_name, status_counts in sorted_summary.items():
404
- print(
405
- f"{stack_set_name} ({stack_set_permission_models[stack_set_name]}) | Last Operation {sorted_summary[stack_set_name]['Status']}:"
406
- )
407
- for stack_status, instances in status_counts.items():
408
- if stack_status is None:
409
- print(f"\t{Fore.RED}--Empty stackset--{Fore.RESET}")
410
- elif stack_status == "Status":
411
- continue
412
- else:
413
- print(
414
- f"\t{Fore.RED if stack_status not in ['CURRENT'] else ''}{stack_status}: {len(instances)} instances {Fore.RESET}"
415
- )
416
- if verbose < 50:
417
- stack_instances = {}
418
- for stack_instance in instances:
419
- if stack_instance["Account"] not in stack_instances.keys():
420
- stack_instances[stack_instance["Account"]] = {
421
- "Region": [],
422
- "DetailedStatus": stack_instance["DetailedStatus"],
423
- "StatusReason": stack_instance["StatusReason"],
424
- "OperationId": stack_instance["LastOperation"],
425
- }
426
- stack_instances[stack_instance["Account"]]["Region"].append(stack_instance["Region"])
427
- for k, v in stack_instances.items():
428
- if pCheckAccount and k in RemovedAccounts:
429
- print(
430
- f"{Style.BRIGHT}{Fore.MAGENTA}\t\t{k}: {v['Region']}\t <----- Look here for orphaned accounts!{Style.RESET_ALL}"
431
- )
432
- else:
433
- print(f"\t\t{k}: {v['Region']}")
434
- if verbose <= 30 and pOperationDate:
435
- last_stackset_operation = v["OperationId"]
436
- cfn_client = aws_acct.session.client("cloudformation")
437
- last_operation_date = cfn_client.describe_stack_set_operation(
438
- StackSetName=stack_set_name, OperationId=last_stackset_operation
439
- )["StackSetOperation"]["EndTimestamp"]
440
- logging.info(f"Account: {k} | Region: {v['Region']}\n")
441
- print(
442
- f"\t\t\t{Fore.RED if v['DetailedStatus'] != 'SUCCEEDED' else ''}Detailed Status: {v['DetailedStatus']}{Fore.RESET}\n"
443
- f"\t\t\tCompleted: {last_operation_date}\n"
444
- f"\t\t\tStatus Reason: {v['StatusReason']}"
445
- )
446
- elif verbose <= 30 and stack_status != "CURRENT":
447
- logging.info(f"\t\t\tAccount: {k}\n\t\t\tRegion: {v['Region']}\n")
448
- print(
449
- f"\t\t\t{Fore.RED}Detailed Status: {v['DetailedStatus']}{Fore.RESET}\n"
450
- f"\t\t\tStatus Reason: {v['StatusReason']}"
451
- )
452
- pass
453
- if verbose < 50:
454
- print()
455
- print(f"Found {len(StackSetNames)} matching stackset{'' if len(StackSetNames) == 1 else 's'}")
456
- print(
457
- f"Found a total of {len(combined_stack_set_instances)} matching stack instance{'' if len(combined_stack_set_instances) == 1 else 's'}"
458
- )
459
- # print(f"Found {len(summary)} unique stackset names within the Stack Instances")
460
- print()
461
-
462
- # Print the summary of any accounts that were found in the StackSets, but not in the Org.
463
- if pCheckAccount:
464
- print("Displaying accounts within the stacksets that aren't a part of the Organization")
465
- print()
466
- # TODO: Adding together these two lists - which are likely full of the same accounts gives an incorrect number
467
- logging.info(f"Found {len(InaccessibleAccounts) + len(RemovedAccounts)} accounts that don't belong")
468
- print(
469
- f"There were {len(RemovedAccounts)} accounts in the {len(StackSetNames)} Stacksets we looked through, that are not a part of the Organization"
470
- )
471
- for item in RemovedAccounts:
472
- print(f"Account {item} is not in the Organization")
473
- print()
474
- print(f"There are {len(InaccessibleAccounts)} accounts that appear inaccessible, using typical role names")
475
- for item in InaccessibleAccounts:
476
- print(f"Account {item['AccountId']} is unreachable using these roles:\n\t\t{item['RolesTried']}")
477
- print()
478
-
479
-
480
- def get_stack_set_deployment_target_info(
481
- faws_acct: aws_acct_access, fRegion: str, fStackSetName: str, fAccountRemovalList: list = None
482
- ):
483
- """
484
- Required Parameters:
485
- faws_acct - the object containing the account credentials and such
486
- fRegion - the region we're looking to make changes in
487
- fStackSetName - the stackset we're removing stack instances from
488
- fAccountRemvalList - The list of accounts they may have provided to limit the deletion to
489
- """
490
- return_result = {"Success": False, "ErrorMessage": None, "Results": None}
491
- if fAccountRemovalList is None:
492
- deployment_results = find_stack_instances3(faws_acct, fRegion, fStackSetName)
493
- identified_ous = list(set([x["OrganizationalUnitId"] for x in deployment_results]))
494
- DeploymentTargets = {
495
- # 'Accounts' : [
496
- # 'string',
497
- # ],
498
- # 'AccountsUrl' : 'string',
499
- "OrganizationalUnitIds": identified_ous
500
- # 'AccountFilterType' : 'NONE' | 'INTERSECTION' | 'DIFFERENCE' | 'UNION'
501
- }
502
- else:
503
- DeploymentTargets = {
504
- "Accounts": fAccountRemovalList,
505
- # 'AccountsUrl' : 'string',
506
- # 'OrganizationalUnitIds': identified_ous
507
- # 'AccountFilterType' : 'NONE' | 'INTERSECTION' | 'DIFFERENCE' | 'UNION'
508
- }
509
- return_result.update({"Success": True, "ErrorMessage": None, "Results": DeploymentTargets})
510
- return return_result
511
-
512
-
513
- def collect_cfnstacksets(faws_acct: aws_acct_access, fRegion: str) -> dict:
514
- """
515
- Description: This function collects the information about existing stacksets
516
- @param faws_acct: Account Object of type "aws_acct_access"
517
- @param fRegion: String for the region in which to collect the stacksets
518
- @return:
519
- - dict of lists, containing:
520
- 1) Aggregate list of all stack instances found
521
- 2) list of stackset names found
522
- 3) list of stacksets that are in-scope for this script
523
- 4) list of stackset instances that may be acted upon
524
- 5) dict of Accounts found within the applicable stacksets
525
- 6) dict of Regions found within the applicable stacksets
526
- """
527
- # TODO: Wrap in try... except to capture errors here, and when we do - stop using this as a dict!!.
528
- # Get the StackSet names from the Management Account
529
- StackSetNames = find_stacksets3(faws_acct, fRegion, pStackfrag, pExact, fGetHealth=True)
530
- if not StackSetNames["Success"]:
531
- error_message = (
532
- "Something went wrong with the AWS connection. Please check the parameters supplied and try again."
533
- )
534
- sys.exit(error_message)
535
- logging.info(f"Found {len(StackSetNames['StackSets'])} StackSetNames that matched your fragment")
536
-
537
- # This finds ALL stack_set_instances in *all* stack_sets that matched the NAME...
538
- combined_stack_set_instances = _find_stack_set_instances(StackSetNames["StackSets"], fRegion)
539
-
540
- print(ERASE_LINE)
541
- logging.info(f"Found {len(combined_stack_set_instances)} stack instances, filtered on specific accounts.")
542
-
543
- # Assumes that all stacksets found are "Applicable"
544
- ApplicableStackSetsList = sorted(list(set([stackset_name for stackset_name in StackSetNames["StackSets"].keys()])))
545
- # The checks for None below are required in case the stackset has no instances.
546
- FoundAccountList = sorted(
547
- list(set([item["ChildAccount"] for item in combined_stack_set_instances if item["ChildAccount"] is not None]))
548
- )
549
- FoundRegionList = sorted(
550
- list(set([item["ChildRegion"] for item in combined_stack_set_instances if item["ChildRegion"] is not None]))
551
- )
552
- # If no accounts specified, but regions were specified
553
- if pAccountModifyList is None and pRegionModifyList is not None:
554
- ApplicableStackSetInstancesList = list(
555
- filter(lambda n: (n["ChildRegion"] in pRegionModifyList), combined_stack_set_instances)
556
- )
557
- # If accounts were specified, but regions were not specified
558
- elif pAccountModifyList is not None and pRegionModifyList is None:
559
- ApplicableStackSetInstancesList = list(
560
- filter(lambda n: (n["ChildAccount"] in pAccountModifyList), combined_stack_set_instances)
561
- )
562
- # If accounts were specified and regions were specified
563
- elif pAccountModifyList is not None and pRegionModifyList is not None:
564
- ApplicableStackSetInstancesList = list(
565
- filter(
566
- lambda n: (n["ChildAccount"] in pAccountModifyList and n["ChildRegion"] in pRegionModifyList),
567
- combined_stack_set_instances,
568
- )
569
- )
570
- # If no accounts or regions were provided, then apply to everything...
571
- else:
572
- ApplicableStackSetInstancesList = combined_stack_set_instances
573
-
574
- if pAddNew:
575
- # This exists, in case there are EMPTY stacksets, which the user is adding to, which wouldn't have shown up within the combined_stack_set_instances above
576
- ApplicableStackSetsList = [stackset_name for stackset_name in StackSetNames["StackSets"].keys()]
577
-
578
- stackset_instance_count = {}
579
- for _stackset_name in ApplicableStackSetsList:
580
- stackset_instance_count[_stackset_name] = len(
581
- [n["StackSetName"] for n in combined_stack_set_instances if n["StackSetName"] == _stackset_name]
582
- )
583
- for stackset_name, stackset_data in StackSetNames["StackSets"].items():
584
- stackset_data["InstanceCount"] = stackset_instance_count[stackset_name]
585
- """
586
- Within StackSet_Dict, there are six lists:
587
- 1. combined_stack_set_instances: which is a list of all stackset instances from all stacksets that matched the name
588
- 2. StackSetNames: which is a list of the stackset names that matched
589
- 3. ApplicableStackSetsList: which is a list of all stacksets (and their attributes) which matched the name
590
- 4. ApplicableStackSetInstances: which is a list of only those stackset instances which matched the name, account, region to be modified
591
- 5. FoundAccountList is a list of accounts found in all the stackset instances that matched
592
- 6. FoundRegionList is a list of regions found in all the stackset instances that matched
593
- """
594
- StackSet_Dict = {
595
- "combined_stack_set_instances": combined_stack_set_instances,
596
- "StackSetNames": StackSetNames,
597
- "ApplicableStackSetsList": ApplicableStackSetsList,
598
- "ApplicableStackSetInstancesList": ApplicableStackSetInstancesList,
599
- "FoundAccountList": FoundAccountList,
600
- "FoundRegionList": FoundRegionList,
601
- }
602
- return StackSet_Dict
603
-
604
-
605
- def check_accounts(faws_acct: aws_acct_access, AccountList: list):
606
- Account_Dict = {}
607
- InaccessibleAccounts = []
608
- OrgAccountList = [i["AccountId"] for i in faws_acct.ChildAccounts]
609
- logging.info(
610
- f"There are {len(OrgAccountList)} accounts in the Org, and {len(AccountList)} unique accounts in all stacksets found"
611
- )
612
- RemovedAccounts = list(set(AccountList) - set(OrgAccountList))
613
- # TODO: Wrap in Try...Except
614
- for accountnum in AccountList:
615
- logging.info(f"{ERASE_LINE}Trying to gain access to account number {accountnum}")
616
- my_creds = get_child_access3(faws_acct, accountnum)
617
- if my_creds["AccessError"]:
618
- InaccessibleAccounts.append(
619
- {"AccountId": accountnum, "Success": my_creds["Success"], "RolesTried": my_creds["RolesTried"]}
620
- )
621
- Account_Dict.update(
622
- {"InaccessibleAccounts": InaccessibleAccounts, "RemovedAccounts": RemovedAccounts, "AccountList": AccountList}
623
- )
624
- return Account_Dict
625
-
626
-
627
- def check_on_stackset_operations(OpsList: list, f_cfn_client):
628
- """
629
- Description: Showing the health and status of the stacksets operated on within this script.
630
- @param OpsList: Ths list of stacksets that were modified
631
- @param f_cfn_client: The boto client used within the operation, just we don't need to open another
632
- @return: Nothing. Just writes to the screen.
633
- """
634
- print(
635
- f"If this status checking is annoying, you can Ctrl-C to quit out, since the stackset operations are working in the background."
636
- )
637
- StillRunning = True
638
- stackset_results = {}
639
- while StillRunning:
640
- Success = True
641
- StillRunning = False
642
- for operation in OpsList:
643
- print(f"Checking on operations... ")
644
- StackSetStatus = f_cfn_client.describe_stack_set_operation(
645
- StackSetName=operation["StackSetName"], OperationId=operation["OperationId"]
646
- )
647
- print(f"StackSet: {operation['StackSetName']} | Status: {StackSetStatus['StackSetOperation']['Status']}")
648
- # Allows this to run until *all* stacksets are complete - whether successfully or not
649
- StillRunning = StillRunning or StackSetStatus["StackSetOperation"]["Status"] == "RUNNING"
650
- Success = Success and StackSetStatus["StackSetOperation"]["Status"] == "SUCCEEDED"
651
- stackset_results.update({operation["StackSetName"]: StackSetStatus["StackSetOperation"]["Status"]})
652
- stackset_results.update({operation["StackSetName"]: StackSetStatus["StackSetOperation"]["Status"]})
653
- if StillRunning:
654
- print(f"Waiting {sleep_interval} seconds before checking all stacksets again... ")
655
- print()
656
- sleep(sleep_interval)
657
- return stackset_results
658
-
659
-
660
- def _modify_stacksets(StackSet_Dict: dict) -> dict:
661
- applicable_stack_set_instances = StackSet_Dict["ApplicableStackSetInstancesList"]
662
- StackSets: dict = StackSet_Dict["StackSetNames"]["StackSets"]
663
- ApplicableStackSetsList = StackSet_Dict["ApplicableStackSetsList"]
664
- AccountList = StackSet_Dict["FoundAccountList"]
665
- FoundRegionList = StackSet_Dict["FoundRegionList"]
666
- # RemovedAccounts = Account_Dict['RemovedAccounts']
667
-
668
- # If we *are* deleting stack instances, and there's anything to delete...
669
- if pdelete and len(applicable_stack_set_instances) > 0:
670
- ReallyDelete = False
671
- print()
672
- print(
673
- f"Removing {len(applicable_stack_set_instances)} stack instances from the {len(StackSets)} StackSets found"
674
- )
675
- StackInstanceItem = 0
676
- results = dict()
677
- # We need to use the StackSet Data dictionary, since only that dictionary tells us whether this is a SELF_MANAGED or SERVICE_MANAGED stackset
678
- for StackSetName, StackSetData in StackSets.items():
679
- StackSetResult = {"StackSetName": StackSetName, "Success": False, "ErrorMessage": "Haven't started yet..."}
680
- StackInstanceItem += 1
681
- print(
682
- f"About to start deleting stackset instances in {StackSetName}. Beginning {StackInstanceItem} of {len(ApplicableStackSetsList)}"
683
- )
684
- # TODO: This needs to be wrapped in a try...except
685
- # Determine what kind of stackset this is - Self-Managed, or Service-Managed.
686
- # We need to check to see if the 'PermissionModel' key in in the dictionary, since it's only in the dictionary if the permission is 'service_managed',
687
- # but I'm not willing to be it stats that way...
688
- if "PermissionModel" in StackSetData.keys() and StackSetData["PermissionModel"] == "SERVICE_MANAGED":
689
- """
690
- If the StackSet is SERVICE-MANAGED, we need to find more information about the stackset than is returned in the "list-stack-set" call from above
691
- """
692
- if pAccountModifyList is None:
693
- DeploymentTargets = get_stack_set_deployment_target_info(aws_acct, pRegion, StackSetName)
694
- else:
695
- DeploymentTargets = get_stack_set_deployment_target_info(
696
- aws_acct, pRegion, StackSetName, pAccountModifyList
697
- )
698
- if FoundRegionList is None:
699
- print(f"There appear to be no stack instances for this stack-set")
700
- continue
701
- # TODO:
702
- # Have to rethink this part - since we have to be careful how we remove specific accounts or OUs in Service-Managed Stacks
703
- if pAccountModifyList is None: # Remove all instances from the stackset
704
- if pRegionModifyList is not None:
705
- print(
706
- f"About to update stackset {StackSetName} to remove all accounts within {str(FoundRegionList)}"
707
- )
708
- RemoveStackSet = False
709
- else:
710
- print(f"About to update stackset {StackSetName} to remove ALL accounts from all regions")
711
- RemoveStackSet = True
712
- # RemoveStackInstanceResult = _delete_stack_instances(aws_acct, pRegion, AccountList, FoundRegionList, StackSetName, pForce)
713
- else:
714
- print(
715
- f"About to remove account {pAccountModifyList} from stackset {StackSetName} in regions {str(FoundRegionList)}"
716
- )
717
- RemoveStackSet = False
718
- RemoveStackInstanceResult = _delete_stack_instances(
719
- aws_acct,
720
- pRegion,
721
- StackSetName,
722
- pRetain,
723
- AccountList,
724
- FoundRegionList,
725
- StackSetData["PermissionModel"],
726
- DeploymentTargets["Results"],
727
- )
728
- # Section that handles the deletion for the "SELF_MANAGED" type of stackset
729
- else:
730
- # There were no regions found in the stackset, which means there are no instances, which means there's nothing to delete
731
- if StackSetData["Status"] == "Never Run":
732
- print(f"There appear to be no stack instances for this stack-set")
733
- StackSetResult = {"Success": True}
734
- continue
735
- # If the user didn't supply any accounts to be deleted (which means *all* accounts)
736
- if pAccountModifyList is None:
737
- # If they supplied specific regions to be deleted
738
- if pRegionModifyList is not None:
739
- print(
740
- f"About to update stackset {StackSetName} to remove all instances with {str(pRegionModifyList)} "
741
- f"region{'' if len(pRegionModifyList) == 1 else 's'}"
742
- )
743
- RegionsToDelete = pRegionModifyList
744
- RemoveStackSet = False
745
- # If they *didn't* supply specific regions to be deleted - which means all regions.
746
- elif pRegionModifyList is None:
747
- print(f"About to update stackset {StackSetName} to remove ALL accounts from all regions")
748
- RegionsToDelete = FoundRegionList
749
- # Only delete the stackset, if they didn't specify "retain"
750
- RemoveStackSet = True and not pRetain
751
- # If the user *did* supply a list of accounts to be deleted
752
- elif pAccountModifyList is not None:
753
- # If the user specified the regions to be deleted
754
- if pRegionModifyList is not None:
755
- print(
756
- f"About to update stackset {StackSetName} to remove all instances with {str(pRegionModifyList)} "
757
- f"region{'' if len(pRegionModifyList) == 1 else 's'}"
758
- )
759
- RegionsToDelete = pRegionModifyList
760
- RemoveStackSet = False
761
- # If the user *didn't* specify a list of regions (which means *all* regions)
762
- else:
763
- print(f"About to remove ALL instances from stackset {StackSetName}")
764
- RegionsToDelete = FoundRegionList
765
- # Only delete the stackset, if they didn't specify "retain"
766
- RemoveStackSet = True and not pRetain
767
- logging.info(
768
- f"About to remove account {pAccountModifyList} from stackset {StackSetName} in regions {str(RegionsToDelete)}"
769
- )
770
- ReallyDelete = False
771
- # If they didn't specify any accounts
772
- if pAccountModifyList is None:
773
- # AND they didn't specify any regions
774
- if pRegionModifyList is None:
775
- # Assume they meant ALL instances
776
- instances_to_modify = list(
777
- filter(lambda n: n["StackSetName"] == StackSetName, applicable_stack_set_instances)
778
- )
779
- elif len(pRegionModifyList) > 0: # They *did* specify some regions
780
- # Assume they meant, all instances with those regions
781
- instances_to_modify = list(
782
- filter(
783
- lambda n: (n["ChildRegion"] in pRegionModifyList and n["StackSetName"] == StackSetName),
784
- applicable_stack_set_instances,
785
- )
786
- )
787
- # They *did* specify some accounts,
788
- elif pRegionModifyList is None: # But they didn't specify a region
789
- instances_to_modify = list(
790
- filter(
791
- lambda n: (n["ChildAccount"] in pAccountModifyList and n["StackSetName"] == StackSetName),
792
- applicable_stack_set_instances,
793
- )
794
- )
795
- elif len(pRegionModifyList) > 0: # And they specified regions
796
- instances_to_modify = list(
797
- filter(
798
- lambda n: (
799
- n["ChildAccount"] in pAccountModifyList
800
- and n["ChildRegion"] in pRegionModifyList
801
- and n["StackSetName"] == StackSetName
802
- ),
803
- applicable_stack_set_instances,
804
- )
805
- )
806
- else: # Empty list
807
- instances_to_modify = []
808
-
809
- if not pConfirm:
810
- ReallyDelete = input(
811
- f"{Fore.RED}Removing {len(instances_to_modify)} instances within {StackSetName}...{Fore.RESET}\n"
812
- f"Are you still sure? (y/n): "
813
- ) in ["y", "Y"]
814
- if pConfirm or ReallyDelete:
815
- RemoveStackInstanceResult = _delete_stack_instances(
816
- aws_acct, pRegion, StackSetName, pRetain, AccountList, RegionsToDelete
817
- )
818
- else:
819
- RemoveStackInstanceResult = {
820
- "Success": False,
821
- "ErrorMessage": "Customer decided not to delete stack instances",
822
- }
823
-
824
- if RemoveStackInstanceResult["Success"]:
825
- Instances = [item for item in applicable_stack_set_instances if item["StackSetName"] == StackSetName]
826
- print(
827
- f"{ERASE_LINE}Successfully initiated removal of {len(Instances)} instance{'' if len(Instances) == 1 else 's'} from StackSet {StackSetName}"
828
- )
829
- elif RemoveStackInstanceResult["ErrorMessage"] == "Failed-ForceIt" and pRetain:
830
- print("We tried to force the deletion, but some other problem happened.")
831
- elif RemoveStackInstanceResult["ErrorMessage"] == "Failed-ForceIt" and not pRetain:
832
- Decision = input(
833
- "Deletion of Stack Instances failed, but might work if we force it. Shall we force it? (y/n): "
834
- ) in ["y", "Y"]
835
- if Decision:
836
- RemoveStackInstanceResult = _delete_stack_instances(
837
- aws_acct, pRegion, StackSetName, True, AccountList, RegionsToDelete
838
- ) # Try it again, forcing it this time
839
- if RemoveStackInstanceResult["Success"]:
840
- print(f"{ERASE_LINE}Successfully retried StackSet {StackSetName}")
841
- elif pRetain is True and RemoveStackInstanceResult["ErrorMessage"] == "Failed-ForceIt":
842
- print(f"{ERASE_LINE}Some other problem happened on the retry.")
843
- elif RemoveStackInstanceResult["ErrorMessage"] == "Failed-Other":
844
- print(f"{ERASE_LINE}Something else failed on the retry... Please report the error received.")
845
- elif str(RemoveStackInstanceResult["ErrorMessage"]).find("OperationInProgressException") > 0:
846
- print(
847
- f"{Fore.RED}Another operation is running on this StackSet... Please wait for that operation to end and re-run this script{Fore.RESET}"
848
- )
849
- sys.exit(RemoveStackInstanceResult["ErrorMessage"])
850
- elif not ReallyDelete and not pConfirm:
851
- sys.exit(RemoveStackInstanceResult["ErrorMessage"])
852
- else:
853
- print(f"{Fore.RED}Something else failed... Please report the error below{Fore.RESET}")
854
- logging.critical(f"{RemoveStackInstanceResult['ErrorMessage']}")
855
- sys.exit(RemoveStackInstanceResult["ErrorMessage"])
856
- if RemoveStackSet:
857
- logging.info(f"Instances have received the deletion command, continuing to remove the stackset too")
858
- # If there were no Instances to be deleted
859
- if Instances[0]["ChildAccount"] is None:
860
- # skip the check to see if stack instances are gone
861
- RemoveStackInstanceResult["OperationId"] = None
862
- StackInstancesAreGone = dict()
863
- StackInstancesAreGone["Success"] = True
864
- StackInstancesAreGone["OperationId"] = None
865
- StackInstancesAreGone["StackSetStatus"] = "Not yet assigned"
866
- # else if there WERE child stacks that were deleted
867
- else:
868
- StackInstancesAreGone = check_stack_set_status3(
869
- aws_acct, StackSetName, RemoveStackInstanceResult["OperationId"]
870
- )
871
- logging.debug(
872
- f"The operation id {RemoveStackInstanceResult['OperationId']} is {StackInstancesAreGone['StackSetStatus']}"
873
- )
874
- if not StackInstancesAreGone["Success"]:
875
- logging.critical(
876
- f"There was a problem with removing the stack instances from stackset {StackSetName}."
877
- f"Moving to the next stackset in the list"
878
- )
879
- break
880
- intervals_waited = 1
881
- while StackInstancesAreGone["StackSetStatus"] in ["RUNNING"]:
882
- print(
883
- f"Waiting for operation {RemoveStackInstanceResult['OperationId']} to finish",
884
- f"{sleep_interval * intervals_waited} seconds waited so far",
885
- end="\r",
886
- )
887
- sleep(sleep_interval)
888
- intervals_waited += 1
889
- StackInstancesAreGone = check_stack_set_status3(
890
- aws_acct, StackSetName, RemoveStackInstanceResult["OperationId"]
891
- )
892
- if not StackInstancesAreGone["Success"]:
893
- logging.critical(
894
- f"There was a problem with removing the stack instances from stackset {StackSetName}."
895
- )
896
- StackSetResult = delete_stackset3(aws_acct, pRegion, StackSetName)
897
- if StackSetResult["Success"]:
898
- print(
899
- f"{ERASE_LINE}Removal of stackset {StackSetName} took {sleep_interval * intervals_waited} seconds"
900
- )
901
- else:
902
- print(
903
- f"{ERASE_LINE}{Fore.RED}Removal of stackset {StackSetName} {Style.BRIGHT}failed{Style.NORMAL} due to:\n\t{StackSetResult['ErrorMessage']}.{Fore.RESET}"
904
- )
905
- results.update({StackSetName: StackSetResult})
906
- results["ChangesMade"] = True
907
- return results
908
- # If we're supposed to be adding more instances to the existing stacksets
909
- elif pAddNew:
910
- print()
911
- print(f"Adding instances into the specified stacksets")
912
- # Sometimes the user will provide the accounts they want to add, with no additional regions...
913
- if pAccountModifyList is not None and pRegionModifyList is None:
914
- operation_result = _add_instances_to_stacksets(StackSet_Dict, pAccountModifyList)
915
- # Sometimes they want to add a region, but not change the existing accounts...
916
- elif pAccountModifyList is None and pRegionModifyList is not None:
917
- operation_result = _add_instances_to_stacksets(StackSet_Dict, AccountList, pRegionModifyList)
918
- # They want to change both the accounts AND the regions
919
- elif pAccountModifyList is not None and pRegionModifyList is not None:
920
- operation_result = _add_instances_to_stacksets(StackSet_Dict, pAccountModifyList, pRegionModifyList)
921
- # They provided the "+add" parameter, but didn't specify any accounts or regions to add
922
- else:
923
- logging.error(
924
- f"You specified to '+add', but didn't specify any new accounts or regions to add... exiting... "
925
- )
926
- sys.exit(95)
927
- operation_result["ChangesMade"] = True
928
- return operation_result
929
- # If we are just refreshing the specific stacksets (within the "ApplicableStackSetsList")
930
- elif pRefresh and len(applicable_stack_set_instances) > 0:
931
- operation_result = _refresh_stacksets(StackSet_Dict)
932
- operation_result["ChangesMade"] = True
933
- return operation_result
934
- # No matching stacksets, or no matching stackset instances
935
- else:
936
- print(
937
- f"{Fore.RED}You asked us to make a change, but there are no matching stacksets or stackset instances to modify...{Fore.RESET}"
938
- )
939
- operation_result = dict()
940
- operation_result["ChangesMade"] = False
941
- for stackset_name, stackset_data in StackSets.items():
942
- operation_result[stackset_name] = stackset_data["Status"]
943
- return operation_result
944
-
945
-
946
- def _delete_stack_instances(
947
- faws_acct: aws_acct_access,
948
- fRegion: str,
949
- fStackSetName: str,
950
- fRetain: bool,
951
- fAccountList: list = None,
952
- fRegionList: list = None,
953
- fPermissionModel="SELF_MANAGED",
954
- fDeploymentTargets=None,
955
- ) -> dict:
956
- """
957
- Required Parameters:
958
- faws_acct - the object containing the account credentials and such
959
- fRegion - the region we're looking to make changes in
960
- fAccountList - this is the listing of accounts that were FOUND to be within stack instances
961
- fRegionList - The list of regions within the stackset to remove as well
962
- fStackSetName - the stackset we're removing stack instances from
963
- fRetain - By passing a "True" here, the API will pass on "RetainStacks" to the child stack - which will allow the stackset to be deleted more easily,
964
- but also leaves a remnant in the child account to clean up later..
965
- fPermissionModel - Whether the StackSet is using SELF_MANAGED or SERVICE_MANAGED permission model (associating the stack with individual accounts, or with an OU itself)
966
- fDeploymentTargets - When fPermissionModel is 'SELF_MANAGED', this should be None.
967
- When fPermissionModel is 'SERVICE_MANAGED', this is a dictionary specifying which OUs, or accounts should be impacted"
968
- """
969
- logging.info(f"Removing instances from {fStackSetName} StackSet")
970
- StackSetOpId = f"DeleteInstances-{random_string(5)}"
971
- if ((fAccountList is None or fAccountList == []) and fPermissionModel.upper() == "SELF_MANAGED") or (
972
- fRegionList is None or fRegionList == []
973
- ):
974
- logging.error(f"AccountList and RegionList cannot be null")
975
- logging.warning(f"AccountList: {fAccountList}")
976
- logging.warning(f"RegionList: {fRegionList}")
977
- # Note: The "Success" is True below to show that the calling function can move forward, even though the Account / Regions are null
978
- return_response = {"Success": True, "ErrorMessage": "Failed - Account List or Region List was null"}
979
- return return_response
980
- elif fPermissionModel == "SERVICE_MANAGED" and fDeploymentTargets is None:
981
- logging.error(
982
- f"You can't provide a stackset that is self-managed, and not supply the deployment targets it's supposed to delete"
983
- )
984
- # Note: The "Success" is True below to show that the calling function can move forward, even though the Account / Regions are null
985
- return_response = {
986
- "Success": False,
987
- "ErrorMessage": "Failed - StackSet is 'Service_Managed' but no deployment target was provided",
988
- }
989
- return return_response
990
- try:
991
- delete_stack_instance_response = delete_stack_instances3(
992
- faws_acct,
993
- fRegion,
994
- fRegionList,
995
- fStackSetName,
996
- fRetain,
997
- StackSetOpId,
998
- fAccountList,
999
- fPermissionModel,
1000
- fDeploymentTargets,
1001
- )
1002
- if delete_stack_instance_response["Success"]:
1003
- return_response = {"Success": True, "OperationId": delete_stack_instance_response["OperationId"]}
1004
- else:
1005
- return_response = {"Success": False, "ErrorMessage": delete_stack_instance_response["ErrorMessage"]}
1006
- return return_response
1007
- except Exception as my_Error:
1008
- logging.error(f"Error: {my_Error}")
1009
- if my_Error.response["Error"]["Code"] == "StackSetNotFoundException":
1010
- logging.info("Caught exception 'StackSetNotFoundException', ignoring the exception...")
1011
- return_response = {"Success": False, "ErrorMessage": "Failed - StackSet not found"}
1012
- return return_response
1013
- else:
1014
- print("Failure to run: ", my_Error)
1015
- return_response = {"Success": False, "ErrorMessage": "Failed-Other"}
1016
- return return_response
1017
-
1018
-
1019
- def _refresh_stacksets(StackSet_Dict: dict) -> dict:
1020
- ApplicableStackSetsList = StackSet_Dict["ApplicableStackSetsList"]
1021
- RefreshOpsList = []
1022
- cfn_client = aws_acct.session.client("cloudformation")
1023
- print(f"Found {len(ApplicableStackSetsList)} stacksets that matched {pStackfrag}")
1024
- print()
1025
- for stackset in ApplicableStackSetsList:
1026
- print(f"Beginning to refresh stackset {Fore.RED}{stackset}{Fore.RESET} as you requested...")
1027
- # Get current attributes for the stacksets we've found...
1028
- stacksetAttributes = cfn_client.describe_stack_set(StackSetName=stackset)
1029
- # Then re-run those same stacksets, supplying the same information back to them -
1030
- ReallyRefresh = (
1031
- (
1032
- input(
1033
- f"Refresh of {Fore.RED}{stackset}{Fore.RESET} has been requested.\n"
1034
- f"Drift Status of the stackset is: {stacksetAttributes['StackSet']['StackSetDriftDetectionDetails']['DriftStatus']}\n"
1035
- f"Are you still sure? (y/n): "
1036
- )
1037
- in ["y", "Y"]
1038
- )
1039
- if not pRetain
1040
- else False
1041
- )
1042
- if ReallyRefresh or pRetain:
1043
- # WE have to separate the use-cases here, since the "Service Managed" update operation won't accept a "AdministrationRoleArn",
1044
- # but the "Self Managed" *requires* it.
1045
- if (
1046
- "PermissionModel" in stacksetAttributes["StackSet"].keys()
1047
- and stacksetAttributes["StackSet"]["PermissionModel"] == "SERVICE_MANAGED"
1048
- ):
1049
- refresh_stack_set = cfn_client.update_stack_set(
1050
- StackSetName=stacksetAttributes["StackSet"]["StackSetName"],
1051
- UsePreviousTemplate=True,
1052
- Capabilities=stacksetAttributes["StackSet"]["Capabilities"],
1053
- OperationPreferences={
1054
- "RegionConcurrencyType": "PARALLEL",
1055
- "FailureToleranceCount": 0,
1056
- "MaxConcurrentPercentage": 100,
1057
- },
1058
- )
1059
- else:
1060
- refresh_stack_set = cfn_client.update_stack_set(
1061
- StackSetName=stacksetAttributes["StackSet"]["StackSetName"],
1062
- UsePreviousTemplate=True,
1063
- Capabilities=stacksetAttributes["StackSet"]["Capabilities"],
1064
- OperationPreferences={
1065
- "RegionConcurrencyType": "PARALLEL",
1066
- "FailureToleranceCount": 0,
1067
- "MaxConcurrentPercentage": 100,
1068
- },
1069
- AdministrationRoleARN=stacksetAttributes["StackSet"]["AdministrationRoleARN"],
1070
- )
1071
- RefreshOpsList.append({"StackSetName": stackset, "OperationId": refresh_stack_set["OperationId"]})
1072
- OperationResult = check_on_stackset_operations(RefreshOpsList, cfn_client)
1073
- return OperationResult
1074
-
1075
-
1076
- def _add_instances_to_stacksets(StackSet_Dict: dict, accounts_to_add: list, regions_to_add: list = None) -> dict:
1077
- ApplicableStackSetsList = StackSet_Dict["ApplicableStackSetsList"]
1078
- AddStacksList = []
1079
- cfn_client = aws_acct.session.client("cloudformation")
1080
- print(f"Found {len(ApplicableStackSetsList)} stacksets that matched {pStackfrag}")
1081
- print()
1082
- instances_to_add = (len(accounts_to_add) if accounts_to_add is not None else 1) * (
1083
- len(regions_to_add) if regions_to_add is not None else 1
1084
- )
1085
- for stackset in ApplicableStackSetsList:
1086
- ReallyAdd = False
1087
- if not pConfirm:
1088
- ReallyAdd = input(
1089
- f"{Fore.RED}Adding {instances_to_add} instances to {stackset}...{Fore.RESET}\n"
1090
- f"Are you still sure? (y/n): "
1091
- ) in ["y", "Y"]
1092
- if pConfirm or ReallyAdd:
1093
- OperationId = "Add-Instances--" + random_string(6)
1094
- # This is not wrapped in a try/except, because the "create_stack_instances" operation doesn't actually return anything useful.
1095
- stackset_add = cfn_client.create_stack_instances(
1096
- StackSetName=stackset,
1097
- Accounts=accounts_to_add,
1098
- Regions=RegionList,
1099
- OperationPreferences={
1100
- "RegionConcurrencyType": "PARALLEL",
1101
- "MaxConcurrentPercentage": 100,
1102
- "FailureToleranceCount": 0,
1103
- },
1104
- OperationId=OperationId,
1105
- CallAs="SELF",
1106
- )
1107
- AddStacksList.append({"StackSetName": stackset, "OperationId": OperationId})
1108
- else:
1109
- print(f"{Fore.RED}Skipping {stackset}...{Fore.RESET}")
1110
- OperationResult = check_on_stackset_operations(AddStacksList, cfn_client)
1111
- return OperationResult
1112
-
1113
-
1114
- ##########################
1115
-
1116
- if __name__ == "__main__":
1117
- args = parse_args(sys.argv[1:])
1118
- pProfile = args.Profile
1119
- pRegion = args.Region
1120
- pStackfrag = args.Fragments
1121
- pExact = args.Exact
1122
- pTiming = args.Time
1123
- pAccountModifyList = args.Accounts
1124
- verbose = args.loglevel
1125
- pCheckAccount = args.AccountCheck
1126
- pRole = args.AccessRole
1127
- pdelete = args.DryRun
1128
- pAddNew = args.AddNew
1129
- pRetain = args.Retain
1130
- pOperationDate = args.OperationDate
1131
- pRegionModifyList = args.pRegionModifyList
1132
- pRefresh = args.Refresh
1133
- pConfirm = args.Confirm
1134
- ChangesRequested = pdelete or pAddNew or pRefresh
1135
- # pSaveFilename = args.Filename
1136
- logging.basicConfig(level=verbose, format="[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s")
1137
- logging.getLogger("boto3").setLevel(logging.CRITICAL)
1138
- logging.getLogger("botocore").setLevel(logging.CRITICAL)
1139
- logging.getLogger("s3transfer").setLevel(logging.CRITICAL)
1140
- logging.getLogger("urllib3").setLevel(logging.CRITICAL)
1141
-
1142
- # Setup the aws_acct object
1143
- aws_acct, RegionList = setup_auth_and_regions(pProfile)
1144
- # Collect the stacksets, AccountList and RegionList involved
1145
- # StackSets, Accounts, Regions = collect_cfnstacksets(aws_acct, pRegion)
1146
- StackSet_Info = collect_cfnstacksets(aws_acct, pRegion)
1147
- # Once we have all the stacksets found, determine what to do with them...
1148
- if ChangesRequested:
1149
- operation_result = _modify_stacksets(StackSet_Info)
1150
- ChangesMade = operation_result["ChangesMade"]
1151
- else:
1152
- ChangesMade = False
1153
- # Handle the checking of accounts to see if there any that don't belong in the Org.
1154
- Account_Dict = {}
1155
- if pCheckAccount:
1156
- Account_Dict = check_accounts(aws_acct, StackSet_Info["FoundAccountList"])
1157
- # If we changed anything, get a refreshed view before we display health of stacksets
1158
- if ChangesRequested and ChangesMade:
1159
- print()
1160
- print(
1161
- f"{Fore.RED}Since changes were requested, we're getting the updated view of the environment (post-changes){Fore.RESET}"
1162
- )
1163
- print()
1164
- StackSet_Info = collect_cfnstacksets(aws_acct, pRegion)
1165
- # Display results
1166
- # If the stacksets were found, but the account number they provided isn't in any of the stacksets found
1167
- if (
1168
- len(StackSet_Info["ApplicableStackSetInstancesList"]) == 0
1169
- and len(StackSet_Info["StackSetNames"]["StackSets"]) > 0
1170
- ):
1171
- # print()
1172
- print(
1173
- f"While we found {len(StackSet_Info['ApplicableStackSetsList'])} stackset{'' if len(StackSet_Info['StackSetNames']['StackSets']) == 1 else 's'} that matched your request, "
1174
- f"we found no instances matching your criteria - {pRegionModifyList} - in them"
1175
- )
1176
- ChangesMade = False
1177
- # If the stacksets were not found...
1178
- elif len(StackSet_Info["ApplicableStackSetsList"]) == 0:
1179
- print()
1180
- print(f"We found no stacksets that matched your request... ")
1181
- ChangesMade = False
1182
- # If there is is nothing to display, the function below will print nothing, so it's safe to run it anyway...
1183
- display_stack_set_health(StackSet_Info, Account_Dict)
1184
-
1185
- if ChangesRequested:
1186
- if pAddNew:
1187
- operation = "add"
1188
- elif pdelete:
1189
- operation = "delete"
1190
- elif pRefresh:
1191
- operation = "refresh"
1192
- if ChangesMade:
1193
- print(
1194
- f"Results of {operation} operation on {len(operation_result) - 1} stackset{'' if len(operation_result) == 1 else 's'} found with {pStackfrag} fragment:"
1195
- )
1196
- for k, v in operation_result.items():
1197
- if k == "ChangesMade":
1198
- continue
1199
- else:
1200
- print(f"\t{Fore.RED if v == 'FAILED' else ''}{k}: {v} {Fore.RESET}")
1201
- else:
1202
- print(
1203
- f"While a {Fore.RED}{operation}{Fore.RESET} was requested, no stacksets were found matching your naming criteria {Fore.RED}{pStackfrag}{Fore.RESET}, "
1204
- f"the accounts you wanted {Fore.RED}{'[all]' if pAccountModifyList is None else pAccountModifyList}{Fore.RESET} and the regions you specified "
1205
- f"{Fore.RED}{'[all]' if pRegionModifyList is None else pRegionModifyList}{Fore.RESET}, so no changes were made."
1206
- )
1207
- print()
1208
-
1209
- if pTiming:
1210
- print(ERASE_LINE)
1211
- print(f"{Fore.GREEN}This script took {time() - begin_time:.2f} seconds{Fore.RESET}")
1212
-
1213
- print()
1214
- print("Thanks for using this script...")
1215
- print()