From e6bf08d9ec50854b8060a76c9f8c71cd4288e812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20REY?= Date: Mon, 25 Mar 2019 22:04:32 +0100 Subject: [PATCH] grafana_datasource: use the Ansible helpers to get basic auth header (#54183) * grafana_datasource: use the Ansible helpers to get basic auth header Module was not using the helpers, so an error occured in python3. Fixes: #49147 * Update grafana_datasource_fix_basic_auth_python3_issue.yaml --- .../grafana_datasource_fix_basic_auth_python3_issue.yaml | 3 +++ lib/ansible/modules/monitoring/grafana_datasource.py | 8 +++----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/grafana_datasource_fix_basic_auth_python3_issue.yaml diff --git a/changelogs/fragments/grafana_datasource_fix_basic_auth_python3_issue.yaml b/changelogs/fragments/grafana_datasource_fix_basic_auth_python3_issue.yaml new file mode 100644 index 00000000000..c06bf91be81 --- /dev/null +++ b/changelogs/fragments/grafana_datasource_fix_basic_auth_python3_issue.yaml @@ -0,0 +1,3 @@ +bugfixes: + - grafana_datasource - Fixed an issue when running Python3 and using basic auth + (https://github.com/ansible/ansible/issues/49147) diff --git a/lib/ansible/modules/monitoring/grafana_datasource.py b/lib/ansible/modules/monitoring/grafana_datasource.py index 00d8c6f087c..ff30e04fc6a 100644 --- a/lib/ansible/modules/monitoring/grafana_datasource.py +++ b/lib/ansible/modules/monitoring/grafana_datasource.py @@ -259,7 +259,7 @@ import json import base64 from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.urls import fetch_url +from ansible.module_utils.urls import fetch_url, basic_auth_header from ansible.module_utils._text import to_bytes __metaclass__ = type @@ -373,8 +373,7 @@ def grafana_create_datasource(module, data): if 'grafana_api_key' in data and data['grafana_api_key'] is not None: headers['Authorization'] = "Bearer %s" % data['grafana_api_key'] else: - auth = base64.b64encode(to_bytes('%s:%s' % (data['grafana_user'], data['grafana_password'])).replace('\n', '')) - headers['Authorization'] = 'Basic %s' % auth + headers['Authorization'] = basic_auth_header(data['grafana_user'], data['grafana_password']) grafana_switch_organisation(module, data['grafana_url'], data['org_id'], headers) # test if datasource already exists @@ -433,8 +432,7 @@ def grafana_delete_datasource(module, data): if 'grafana_api_key' in data and data['grafana_api_key']: headers['Authorization'] = "Bearer %s" % data['grafana_api_key'] else: - auth = base64.b64encode(to_bytes('%s:%s' % (data['grafana_user'], data['grafana_password'])).replace('\n', '')) - headers['Authorization'] = 'Basic %s' % auth + headers['Authorization'] = basic_auth_header(data['grafana_user'], data['grafana_password']) grafana_switch_organisation(module, data['grafana_url'], data['org_id'], headers) # test if datasource already exists