Merge pull request #14735 from jsok/open_url-netrc

Lookup netrc for credentials in open_url
This commit is contained in:
Brian Coca 2016-03-30 07:41:56 -07:00
commit c09e085480

View file

@ -258,6 +258,7 @@ if not HAS_MATCH_HOSTNAME:
import httplib import httplib
import netrc
import os import os
import re import re
import sys import sys
@ -691,6 +692,9 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True,
if parsed[0] != 'ftp': if parsed[0] != 'ftp':
username = url_username username = url_username
if headers is None:
headers = {}
if username: if username:
password = url_password password = url_password
netloc = parsed[1] netloc = parsed[1]
@ -723,10 +727,19 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True,
handlers.append(authhandler) handlers.append(authhandler)
elif username and force_basic_auth: elif username and force_basic_auth:
if headers is None: headers["Authorization"] = basic_auth_header(username, password)
headers = {}
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: if not use_proxy:
proxyhandler = urllib2.ProxyHandler({}) proxyhandler = urllib2.ProxyHandler({})
@ -791,6 +804,9 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True,
# Module-related functions # Module-related functions
# #
def basic_auth_header(username, password):
return "Basic %s" % base64.b64encode("%s:%s" % (username, password))
def url_argument_spec(): def url_argument_spec():
''' '''
Creates an argument spec that can be used with any module Creates an argument spec that can be used with any module