From 5ec7adc64d5846a2af8ffda78102979534bf645c Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Wed, 2 Mar 2016 09:26:26 +1100 Subject: [PATCH] Lookup netrc for credentials in open_url --- lib/ansible/module_utils/urls.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 0d0965e3abe..5d4219baa07 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -258,6 +258,7 @@ if not HAS_MATCH_HOSTNAME: import httplib +import netrc import os import re import sys @@ -691,6 +692,9 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True, if parsed[0] != 'ftp': username = url_username + if headers is None: + headers = {} + if username: password = url_password netloc = parsed[1] @@ -723,10 +727,19 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True, handlers.append(authhandler) elif username and force_basic_auth: - if headers is None: - headers = {} + headers["Authorization"] = basic_auth_header(username, password) - headers["Authorization"] = "Basic %s" % base64.b64encode("%s:%s" % (username, password)) + else: + try: + rc = netrc.netrc(os.environ.get('NETRC')) + login = rc.authenticators(parsed[1]) + except IOError: + login = None + + if login: + username, _, password = login + if username and password: + headers["Authorization"] = basic_auth_header(username, password) if not use_proxy: proxyhandler = urllib2.ProxyHandler({}) @@ -791,6 +804,9 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True, # Module-related functions # +def basic_auth_header(username, password): + return "Basic %s" % base64.b64encode("%s:%s" % (username, password)) + def url_argument_spec(): ''' Creates an argument spec that can be used with any module