1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Lookup netrc for credentials in open_url

This commit is contained in:
Jonathan Sokolowski 2016-03-02 09:26:26 +11:00
parent d60f0c94f8
commit 5ec7adc64d

View file

@ -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