|
| 1 | +import click |
| 2 | +import lxml.etree as ET |
| 3 | + |
| 4 | +import logging |
| 5 | +import re |
| 6 | + |
| 7 | +from . import run_command |
| 8 | + |
| 9 | +try: |
| 10 | + # Python 3 |
| 11 | + from urllib.parse import urlparse, parse_qs |
| 12 | +except ImportError: |
| 13 | + # Python 2 |
| 14 | + from urlparse import urlparse, parse_qs |
| 15 | + |
| 16 | +from . import roles_assertion_extractor |
| 17 | +from .helpers import trace_http_request |
| 18 | + |
| 19 | + |
| 20 | +def extract(html_response, ssl_verification_enabled, mfa_token_command, mfa_token, session): |
| 21 | + """ |
| 22 | + :param response: raw http response |
| 23 | + :param html_response: html result of parsing http response |
| 24 | + :return: |
| 25 | + """ |
| 26 | + |
| 27 | + roles_page_url = _action_url_on_validation_success(html_response) |
| 28 | + |
| 29 | + if mfa_token_command: |
| 30 | + data = run_command.run_command(mfa_token_command) |
| 31 | + safenet_mfa_code = data['mfa_token'] |
| 32 | + logging.debug(f"using SafeNet MFA token from command: {safenet_mfa_code}") |
| 33 | + elif mfa_token: |
| 34 | + safenet_mfa_code = mfa_token |
| 35 | + logging.debug(f"using SafeNet MFA token from env: {safenet_mfa_code}") |
| 36 | + else: |
| 37 | + safenet_mfa_code = click.prompt(text='Enter your SafeNet MFA token', type=str, hide_input=True) |
| 38 | + |
| 39 | + click.echo('Going for aws roles', err=True) |
| 40 | + return _retrieve_roles_page( |
| 41 | + roles_page_url, |
| 42 | + _context(html_response), |
| 43 | + session, |
| 44 | + ssl_verification_enabled, |
| 45 | + safenet_mfa_code, |
| 46 | + ) |
| 47 | + |
| 48 | +def _context(html_response): |
| 49 | + context_query = './/input[@name="Context"]' |
| 50 | + element = html_response.find(context_query) |
| 51 | + return element.get('value') |
| 52 | + |
| 53 | + |
| 54 | +def _retrieve_roles_page(roles_page_url, context, session, ssl_verification_enabled, |
| 55 | + safenet_mfa_code): |
| 56 | + response = session.post( |
| 57 | + roles_page_url, |
| 58 | + verify=ssl_verification_enabled, |
| 59 | + allow_redirects=True, |
| 60 | + data={ |
| 61 | + 'AuthMethod': 'SafeNet-MFA', |
| 62 | + 'Context': context, |
| 63 | + 'SAFENET_PASSWORD': safenet_mfa_code, |
| 64 | + } |
| 65 | + ) |
| 66 | + trace_http_request(response) |
| 67 | + |
| 68 | + if response.status_code != 200: |
| 69 | + raise click.ClickException( |
| 70 | + u'Issues during redirection to aws roles page. The error response {}'.format( |
| 71 | + response |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + # Save session cookies to avoid having to repeat MFA on each login |
| 76 | + session.cookies.save(ignore_discard=True) |
| 77 | + |
| 78 | + html_response = ET.fromstring(response.text, ET.HTMLParser()) |
| 79 | + return roles_assertion_extractor.extract(html_response) |
| 80 | + |
| 81 | +def _action_url_on_validation_success(html_response): |
| 82 | + safenet_mfa_auth_method = './/form[@id="options"]' |
| 83 | + element = html_response.find(safenet_mfa_auth_method) |
| 84 | + return element.get('action') |
0 commit comments