|
6 | 6 | import re |
7 | 7 |
|
8 | 8 | import requests |
| 9 | +from urllib.parse import urljoin |
9 | 10 |
|
10 | 11 | from ansible.plugins.action import ActionBase |
11 | 12 |
|
@@ -34,27 +35,73 @@ def write_version(self, proj_path, etag): |
34 | 35 | with open(file_path, 'w') as f: |
35 | 36 | f.write(etag) |
36 | 37 |
|
| 38 | + def _obtain_auth_token(self, oidc_endpoint, client_id, client_secret): |
| 39 | + main_url = urljoin(oidc_endpoint, '/.well-known/openid-configuration') |
| 40 | + response = requests.get(url=main_url, headers={'Accept': 'application/json'}) |
| 41 | + data = {} |
| 42 | + if response.status_code != 200: |
| 43 | + data['failed'] = True |
| 44 | + data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format( |
| 45 | + main_url, response.status_code, response.content |
| 46 | + ) |
| 47 | + return data |
| 48 | + |
| 49 | + auth_url = response.json().get('token_endpoint', None) |
| 50 | + data = { |
| 51 | + 'grant_type': 'client_credentials', |
| 52 | + 'scope': 'api.console', |
| 53 | + 'client_id': client_id, |
| 54 | + 'client_secret': client_secret, |
| 55 | + } |
| 56 | + response = requests.post(url=auth_url, data=data) |
| 57 | + |
| 58 | + if response.status_code != 200: |
| 59 | + data['failed'] = True |
| 60 | + data['msg'] = 'Expected {} to return a status code of 200 but returned status code "{}" instead with content "{}".'.format( |
| 61 | + auth_url, response.status_code, response.content |
| 62 | + ) |
| 63 | + else: |
| 64 | + data['token'] = response.json().get('access_token', None) |
| 65 | + data['token_type'] = response.json().get('token_type', None) |
| 66 | + return data |
| 67 | + |
37 | 68 | def run(self, tmp=None, task_vars=None): |
38 | 69 | self._supports_check_mode = False |
39 | 70 |
|
| 71 | + session = requests.Session() |
40 | 72 | result = super(ActionModule, self).run(tmp, task_vars) |
41 | 73 |
|
42 | 74 | insights_url = self._task.args.get('insights_url', None) |
43 | | - username = self._task.args.get('username', None) |
44 | | - password = self._task.args.get('password', None) |
45 | 75 | proj_path = self._task.args.get('project_path', None) |
46 | 76 | license = self._task.args.get('awx_license_type', None) |
47 | 77 | awx_version = self._task.args.get('awx_version', None) |
| 78 | + authentication = self._task.args.get('authentication', None) |
| 79 | + username = self._task.args.get('username', None) |
| 80 | + password = self._task.args.get('password', None) |
| 81 | + client_id = self._task.args.get('client_id', None) |
| 82 | + client_secret = self._task.args.get('client_secret', None) |
| 83 | + oidc_endpoint = self._task.args.get('oidc_endpoint', None) |
| 84 | + |
| 85 | + session.headers.update( |
| 86 | + { |
| 87 | + 'Content-Type': 'application/json', |
| 88 | + 'User-Agent': '{} {} ({})'.format('AWX' if license == 'open' else 'Red Hat Ansible Automation Platform', awx_version, license), |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + if authentication == 'service_account' or (client_id and client_secret): |
| 93 | + data = self._obtain_auth_token(oidc_endpoint, client_id, client_secret) |
| 94 | + if 'token' not in data: |
| 95 | + result['failed'] = data['failed'] |
| 96 | + result['msg'] = data['msg'] |
| 97 | + return result |
| 98 | + session.headers.update({'Authorization': f'{result['token_type']} {result['token']}'}) |
| 99 | + elif authentication == 'basic' or (username and password): |
| 100 | + session.auth = requests.auth.HTTPBasicAuth(username, password) |
48 | 101 |
|
49 | | - session = requests.Session() |
50 | | - session.auth = requests.auth.HTTPBasicAuth(username, password) |
51 | | - headers = { |
52 | | - 'Content-Type': 'application/json', |
53 | | - 'User-Agent': '{} {} ({})'.format('AWX' if license == 'open' else 'Red Hat Ansible Automation Platform', awx_version, license), |
54 | | - } |
55 | 102 | url = '/api/remediations/v1/remediations' |
56 | 103 | while url: |
57 | | - res = session.get('{}{}'.format(insights_url, url), headers=headers, timeout=120) |
| 104 | + res = session.get('{}{}'.format(insights_url, url), timeout=120) |
58 | 105 |
|
59 | 106 | if res.status_code != 200: |
60 | 107 | result['failed'] = True |
|
0 commit comments