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

Fix urlopen usage to use open_url instead

Add a travis test for urlopen usage
This commit is contained in:
Toshio Kuratomi 2015-09-15 07:58:52 -07:00
parent e97d448838
commit 3db8070aa3
9 changed files with 52 additions and 54 deletions

View file

@ -14,6 +14,8 @@ addons:
install: install:
- pip install tox PyYAML Jinja2 sphinx - pip install tox PyYAML Jinja2 sphinx
script: script:
# urllib2's defaults are not secure enough for us
- ./test/code-smell/replace-urlopen.sh .
- if test x"$TOKENV" != x'py24' ; then tox ; fi - if test x"$TOKENV" != x'py24' ; then tox ; fi
- if test x"$TOKENV" = x'py24' ; then python2.4 -V && python2.4 -m compileall -fq -x 'module_utils/(a10|rax|openstack|ec2|gce).py' lib/ansible/module_utils ; fi - if test x"$TOKENV" = x'py24' ; then python2.4 -V && python2.4 -m compileall -fq -x 'module_utils/(a10|rax|openstack|ec2|gce).py' lib/ansible/module_utils ; fi
#- make -C docsite all #- make -C docsite all

View file

@ -45,26 +45,24 @@ import os
import sys import sys
import time import time
import ConfigParser import ConfigParser
import urllib2
import base64
try: try:
import json import json
except ImportError: except ImportError:
import simplejson as json import simplejson as json
from ansible.module_utils.urls import open_url
def api_get(link, config): def api_get(link, config):
try: try:
if link == None: if link == None:
request = urllib2.Request(config.get('api','uri')+config.get('api','login_path')) url = config.get('api','uri') + config.get('api','login_path')
request.add_header("Accept",config.get('api','login_type')) headers = {"Accept": config.get('api','login_type')}
else: else:
request = urllib2.Request(link['href']+'?limit=0') url = link['href'] + '?limit=0'
request.add_header("Accept",link['type']) headers = {"Accept": link['type']}
# Auth result = open_url(url, headers=headers, url_username=config.get('auth','apiuser').replace('\n', ''),
base64string = base64.encodestring('%s:%s' % (config.get('auth','apiuser'),config.get('auth','apipass'))).replace('\n', '') url_password=config.get('auth','apipass').replace('\n', ''))
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
return json.loads(result.read()) return json.loads(result.read())
except: except:
return None return None

View file

@ -67,7 +67,6 @@ Tested against Ansible 1.8.2 and Collins 1.3.0.
import argparse import argparse
import base64
import ConfigParser import ConfigParser
import logging import logging
import os import os
@ -76,7 +75,6 @@ import sys
from time import time from time import time
import traceback import traceback
import urllib import urllib
import urllib2
try: try:
import json import json
@ -85,6 +83,7 @@ except ImportError:
from six import iteritems from six import iteritems
from ansible.module_utils.urls import open_url
class CollinsDefaults(object): class CollinsDefaults(object):
ASSETS_API_ENDPOINT = '%s/api/assets' ASSETS_API_ENDPOINT = '%s/api/assets'
@ -198,10 +197,11 @@ class CollinsInventory(object):
(CollinsDefaults.ASSETS_API_ENDPOINT % self.collins_host), (CollinsDefaults.ASSETS_API_ENDPOINT % self.collins_host),
urllib.urlencode(query_parameters, doseq=True) urllib.urlencode(query_parameters, doseq=True)
) )
request = urllib2.Request(query_url)
request.add_header('Authorization', self.basic_auth_header)
try: try:
response = urllib2.urlopen(request, timeout=self.collins_timeout_secs) response = open_url(query_url,
timeout=self.collins_timeout_secs,
url_username=self.collins_username,
url_password=self.collins_password)
json_response = json.loads(response.read()) json_response = json.loads(response.read())
# Adds any assets found to the array of assets. # Adds any assets found to the array of assets.
assets += json_response['data']['Data'] assets += json_response['data']['Data']
@ -261,8 +261,6 @@ class CollinsInventory(object):
log_path = config.get('collins', 'log_path') log_path = config.get('collins', 'log_path')
self.log_location = log_path + '/ansible-collins.log' self.log_location = log_path + '/ansible-collins.log'
self.basic_auth_header = "Basic %s" % base64.encodestring(
'%s:%s' % (self.collins_username, self.collins_password))[:-1]
def parse_cli_args(self): def parse_cli_args(self):
""" Command line argument processing """ """ Command line argument processing """

View file

@ -28,7 +28,6 @@ version_added: None
author: Michael Scherer author: Michael Scherer
''' '''
import urllib2
try: try:
import json import json
except ImportError: except ImportError:
@ -39,6 +38,8 @@ import sys
import ConfigParser import ConfigParser
import StringIO import StringIO
from ansible.module_utils.urls import open_url
configparser = None configparser = None
@ -66,34 +67,21 @@ def get_config(env_var, config_var):
return result return result
def get_json_from_api(url): def get_json_from_api(url, username, password):
req = urllib2.Request(url, None, {'Accept': 'application/json; version=1.5'}) headers = {'Accept': 'application/json; version=1.5'}
response = urllib2.urlopen(req) response = open_url(url, headers=headers, url_username=username, url_password=password)
return json.loads(response.read())['data'] return json.loads(response.read())['data']
def passwd_setup(top_level_url, username, password):
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
username = get_config('ANSIBLE_OPENSHIFT_USERNAME', 'default_rhlogin') username = get_config('ANSIBLE_OPENSHIFT_USERNAME', 'default_rhlogin')
password = get_config('ANSIBLE_OPENSHIFT_PASSWORD', 'password') password = get_config('ANSIBLE_OPENSHIFT_PASSWORD', 'password')
broker_url = 'https://%s/broker/rest/' % get_config('ANSIBLE_OPENSHIFT_BROKER', 'libra_server') broker_url = 'https://%s/broker/rest/' % get_config('ANSIBLE_OPENSHIFT_BROKER', 'libra_server')
passwd_setup(broker_url, username, password) response = get_json_from_api(broker_url + '/domains', username, password)
response = get_json_from_api(broker_url + '/domains')
response = get_json_from_api("%s/domains/%s/applications" % response = get_json_from_api("%s/domains/%s/applications" %
(broker_url, response[0]['id'])) (broker_url, response[0]['id']), username, password)
result = {} result = {}
for app in response: for app in response:

View file

@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib import urllib
import urllib2
try: try:
import json import json
except ImportError: except ImportError:
@ -27,6 +26,7 @@ from optparse import OptionParser
from six import iteritems from six import iteritems
from ansible.module_utils.urls import open_url
class ProxmoxNodeList(list): class ProxmoxNodeList(list):
def get_names(self): def get_names(self):
@ -86,7 +86,7 @@ class ProxmoxAPI(object):
'password': self.options.password, 'password': self.options.password,
}) })
data = json.load(urllib2.urlopen(request_path, request_params)) data = json.load(open_url(request_path, data=request_params))
self.credentials = { self.credentials = {
'ticket': data['data']['ticket'], 'ticket': data['data']['ticket'],
@ -94,11 +94,10 @@ class ProxmoxAPI(object):
} }
def get(self, url, data=None): def get(self, url, data=None):
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', 'PVEAuthCookie={}'.format(self.credentials['ticket'])))
request_path = '{}{}'.format(self.options.url, url) request_path = '{}{}'.format(self.options.url, url)
request = opener.open(request_path, data)
headers = {'Cookie': 'PVEAuthCookie={}'.format(self.credentials['ticket'])}
request = open_url(request_path, data=data, headers=headers)
response = json.load(request) response = json.load(request)
return response['data'] return response['data']

View file

@ -21,10 +21,11 @@
# #
######################################################################## ########################################################################
import json import json
from urllib2 import urlopen, quote as urlquote, HTTPError from urllib2 import quote as urlquote, HTTPError
from urlparse import urlparse from urlparse import urlparse
from ansible.errors import AnsibleError, AnsibleOptionsError from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.module_utils.urls import open_url
class GalaxyAPI(object): class GalaxyAPI(object):
''' This class is meant to be used as a API client for an Ansible Galaxy server ''' ''' This class is meant to be used as a API client for an Ansible Galaxy server '''
@ -61,7 +62,7 @@ class GalaxyAPI(object):
return 'v1' return 'v1'
try: try:
data = json.load(urlopen(api_server)) data = json.load(open_url(api_server))
return data.get("current_version", 'v1') return data.get("current_version", 'v1')
except Exception as e: except Exception as e:
# TODO: report error # TODO: report error
@ -85,7 +86,7 @@ class GalaxyAPI(object):
url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name) url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name)
self.galaxy.display.vvvv("- %s" % (url)) self.galaxy.display.vvvv("- %s" % (url))
try: try:
data = json.load(urlopen(url)) data = json.load(open_url(url))
if len(data["results"]) != 0: if len(data["results"]) != 0:
return data["results"][0] return data["results"][0]
except: except:
@ -102,13 +103,13 @@ class GalaxyAPI(object):
try: try:
url = '%s/roles/%d/%s/?page_size=50' % (self.baseurl, int(role_id), related) url = '%s/roles/%d/%s/?page_size=50' % (self.baseurl, int(role_id), related)
data = json.load(urlopen(url)) data = json.load(open_url(url))
results = data['results'] results = data['results']
done = (data.get('next', None) == None) done = (data.get('next', None) == None)
while not done: while not done:
url = '%s%s' % (self.baseurl, data['next']) url = '%s%s' % (self.baseurl, data['next'])
self.galaxy.display.display(url) self.galaxy.display.display(url)
data = json.load(urlopen(url)) data = json.load(open_url(url))
results += data['results'] results += data['results']
done = (data.get('next', None) == None) done = (data.get('next', None) == None)
return results return results
@ -122,7 +123,7 @@ class GalaxyAPI(object):
try: try:
url = '%s/%s/?page_size' % (self.baseurl, what) url = '%s/%s/?page_size' % (self.baseurl, what)
data = json.load(urlopen(url)) data = json.load(open_url(url))
if "results" in data: if "results" in data:
results = data['results'] results = data['results']
else: else:
@ -133,7 +134,7 @@ class GalaxyAPI(object):
while not done: while not done:
url = '%s%s' % (self.baseurl, data['next']) url = '%s%s' % (self.baseurl, data['next'])
self.galaxy.display.display(url) self.galaxy.display.display(url)
data = json.load(urlopen(url)) data = json.load(open_url(url))
results += data['results'] results += data['results']
done = (data.get('next', None) == None) done = (data.get('next', None) == None)
return results return results
@ -165,7 +166,7 @@ class GalaxyAPI(object):
self.galaxy.display.debug("Executing query: %s" % search_url) self.galaxy.display.debug("Executing query: %s" % search_url)
try: try:
data = json.load(urlopen(search_url)) data = json.load(open_url(search_url))
except HTTPError as e: except HTTPError as e:
raise AnsibleError("Unsuccessful request to server: %s" % str(e)) raise AnsibleError("Unsuccessful request to server: %s" % str(e))

View file

@ -26,10 +26,10 @@ import tarfile
import tempfile import tempfile
import yaml import yaml
from shutil import rmtree from shutil import rmtree
from urllib2 import urlopen
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansilbe.module_utils.urls import open_url
try: try:
from __main__ import display from __main__ import display
@ -162,7 +162,7 @@ class GalaxyRole(object):
display.display("- downloading role from %s" % archive_url) display.display("- downloading role from %s" % archive_url)
try: try:
url_file = urlopen(archive_url) url_file = open_url(archive_url)
temp_file = tempfile.NamedTemporaryFile(delete=False) temp_file = tempfile.NamedTemporaryFile(delete=False)
data = url_file.read() data = url_file.read()
while data: while data:

View file

@ -17,7 +17,6 @@
import os import os
import urllib import urllib
import urllib2
try: try:
import prettytable import prettytable
@ -26,6 +25,7 @@ except ImportError:
HAS_PRETTYTABLE = False HAS_PRETTYTABLE = False
from ansible.plugins.callback import CallbackBase from ansible.plugins.callback import CallbackBase
from ansible.module_utils.urls import open_url
class CallbackModule(CallbackBase): class CallbackModule(CallbackBase):
"""This is an example ansible callback plugin that sends status """This is an example ansible callback plugin that sends status
@ -82,7 +82,7 @@ class CallbackModule(CallbackBase):
url = ('%s?auth_token=%s' % (self.msg_uri, self.token)) url = ('%s?auth_token=%s' % (self.msg_uri, self.token))
try: try:
response = urllib2.urlopen(url, urllib.urlencode(params)) response = open_url(url, data=urllib.urlencode(params))
return response.read() return response.read()
except: except:
self.display.warning('Could not submit message to hipchat') self.display.warning('Could not submit message to hipchat')

View file

@ -0,0 +1,12 @@
#!/bin/sh
BASEDIR=${1-"."}
URLLIB_USERS=$(find "$BASEDIR" -name '*.py' -exec grep -H urlopen \{\} \;)
URLLIB_USERS=$(echo "$URLLIB_USERS" | sed '/\(\n\|lib\/ansible\/module_utils\/urls.py\)/d')
if test -n "$URLLIB_USERS" ; then
printf "$URLLIB_USERS"
exit 1
else
exit 0
fi