-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathaccount_aliases_fetcher.py
55 lines (45 loc) · 1.77 KB
/
account_aliases_fetcher.py
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
import logging
import re
import lxml.etree as ET
_account_alias_pattern = re.compile("Account: *([^(]+) *\(([0-9]+)\)")
_account_without_alias_pattern = re.compile("Account: *\(?([0-9]+)\)?")
def account_aliases(session, username, password, auth_method, saml_response, config):
alias_response = session.post(
'https://signin.aws.amazon.com/saml',
verify=config.ssl_verification,
headers={
'Accept-Language': 'en',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/60.0',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'text/plain, */*; q=0.01',
},
auth=None,
data={
'SAMLResponse': saml_response,
}
)
logging.debug(u'''Request:
* url: {}
* headers: {}
Response:
* status: {}
* headers: {}
* body: {}
'''.format('https://signin.aws.amazon.com/saml',
alias_response.request.headers,
alias_response.status_code,
alias_response.headers,
alias_response.text))
html_response = ET.fromstring(alias_response.text, ET.HTMLParser())
accounts = {}
account_element_query = './/div[@class="saml-account-name"]'
for account_element in html_response.iterfind(account_element_query):
logging.debug(u'Found SAML account name: {}'.format(account_element.text))
m = _account_alias_pattern.search(account_element.text)
if m is not None:
accounts[m.group(2)] = m.group(1).strip()
if m is None:
m = _account_without_alias_pattern.search(account_element.text)
if m is not None:
accounts[m.group(1)] = m.group(0).strip()
return accounts