forked from paulbayer/Inventory_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_my_saml_providers.py
More file actions
executable file
·170 lines (157 loc) · 6.28 KB
/
all_my_saml_providers.py
File metadata and controls
executable file
·170 lines (157 loc) · 6.28 KB
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
#!/usr/bin/env python3
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os, sys, pprint, boto3
import Inventory_Modules
import argparse
from colorama import init,Fore,Back,Style
from botocore.exceptions import ClientError, NoCredentialsError
import logging
init()
# UsageMsg="You can provide a level to determine whether this script considers only the 'credentials' file, the 'config' file, or both."
parser = argparse.ArgumentParser(
description="We\'re going to find all saml identity providers within any of the child accounts within the organization.",
prefix_chars='-+/')
parser.add_argument(
"-p","--profile",
dest="pProfile",
metavar="profile to use",
default="default",
help="To specify a specific profile, use this parameter. Default will be your default profile.")
parser.add_argument(
"-k","--skip",
dest="pSkipAccounts",
nargs="*",
metavar="Accounts to leave alone",
default=[],
help="These are the account numbers you don't want to screw with. Likely the core accounts. Separate them by a space.")
parser.add_argument(
"-r","--pRegion",
# nargs="*",
dest="pRegion",
metavar="pRegion name string",
default="us-east-1",
help="String fragment of the pRegion(s) you want to check for resources.")
parser.add_argument(
"+delete","+forreal",
dest="DeletionRun",
const=True,
default=False,
action="store_const",
help="This will delete the identity providers found - without any opportunity to confirm. Be careful!!")
parser.add_argument(
'-v',
help="Be verbose",
action="store_const",
dest="loglevel",
const=logging.ERROR, # args.loglevel = 40
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-vv', '--verbose',
help="Be MORE verbose",
action="store_const",
dest="loglevel",
const=logging.WARNING, # args.loglevel = 30
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-vvv',
help="Print INFO level statements",
action="store_const",
dest="loglevel",
const=logging.INFO, # args.loglevel = 20
default=logging.CRITICAL) # args.loglevel = 50
parser.add_argument(
'-d', '--debug',
help="Print LOTS of debugging statements",
action="store_const",
dest="loglevel",
const=logging.DEBUG, # args.loglevel = 10
default=logging.CRITICAL) # args.loglevel = 50
args = parser.parse_args()
pProfile=args.pProfile
pRegion=args.pRegion
AccountsToSkip=args.pSkipAccounts
verbose=args.loglevel
DeletionRun=args.DeletionRun
logging.basicConfig(level=args.loglevel, format="[%(filename)s:%(lineno)s:%(levelname)s - %(funcName)30s() ] %(message)s")
##########################
ERASE_LINE = '\x1b[2K'
print()
fmt='%-20s %-15s %-15s'
print(fmt % ("Account","pRegion","IDP Name"))
print(fmt % ("-------","------","--------"))
# pRegionList=Inventory_Modules.get_ec2_pRegions(ppRegionList)
ChildAccounts=Inventory_Modules.find_child_accounts2(pProfile)
ChildAccounts=Inventory_Modules.RemoveCoreAccounts(ChildAccounts,AccountsToSkip)
# pprint.pprint(AccountsToSkip)
# pprint.pprint(ChildAccounts)
# sys.exit(1)
NumofAccounts=len(ChildAccounts)
IdpsFound=[]
for account in ChildAccounts:
try:
account_credentials,role_arn = Inventory_Modules.get_child_access2(pProfile, account['AccountId'])
logging.info("Role ARN: %s" % role_arn)
account_credentials['AccountNumber']=account['AccountId']
except ClientError as my_Error:
if str(my_Error).find("AuthFailure") > 0:
print(pProfile+": Authorization Failure for account {}".format(account['AccountId']))
else:
print(pProfile+": Other kind of failure for account {}".format(account['AccountId']))
print (my_Error)
break
# for pRegion in pRegionList:
try:
idpNum=0
Idps=Inventory_Modules.find_saml_components_in_acct(account_credentials,pRegion)
# pprint.pprint(Stacks)
idpNum=len(Idps)
logging.warning("Account: %s | Region: %s | Found %s Idps", account['AccountId'], pRegion, idpNum )
logging.warning(ERASE_LINE+Fore.RED+"Account: %s pRegion: %s Found %s Idps. Only %s accounts left to go",account['AccountId'],pRegion,idpNum,NumofAccounts+Fore.RESET)
except ClientError as my_Error:
if str(my_Error).find("AuthFailure") > 0:
print(account['AccountId']+": Authorization Failure")
if idpNum > 0:
for y in range(len(Idps)):
logging.warning("Arn: %s",Idps[y]['Arn'])
NameStart=Idps[y]['Arn'].find('/')+1
logging.debug("Name starts at character: %s",NameStart)
IdpName=Idps[y]['Arn'][NameStart:]
print(fmt % (account['AccountId'],pRegion,IdpName))
IdpsFound.append({
'AccountId':account['AccountId'],
'pRegion':pRegion,
'IdpName':IdpName,
'Arn':Idps[y]['Arn'] })
NumofAccounts-=1
print(ERASE_LINE)
print(Fore.RED+"Found {} Idps across {} accounts in region {}".format(len(IdpsFound),len(ChildAccounts),pRegion)+Fore.RESET)
print()
# pprint.pprint(IdpsFound)
if DeletionRun:
logging.warning("Deleting %s Idps",len(IdpsFound))
for y in range(len(IdpsFound)):
account_credentials,role_arn = Inventory_Modules.get_child_access2(pProfile, IdpsFound[y]['AccountId'])
session_aws=boto3.Session(pRegion_name=IdpsFound[y]['pRegion'],
aws_access_key_id = account_credentials['AccessKeyId'],
aws_secret_access_key = account_credentials['SecretAccessKey'],
aws_session_token = account_credentials['SessionToken']
)
iam_client=session_aws.client('iam')
print("Deleting Idp {} from account {} in pRegion {}".format(IdpsFound[y]['IdpName'],IdpsFound[y]['AccountId'],IdpsFound[y]['pRegion']))
response=iam_client.delete_saml_provider(SAMLProviderArn=IdpsFound[y]['Arn'])
print()
print("Thanks for using this script...")
print()