Validate SSL certs accessed through urllib*

* Adds another module utility file which generalizes the
  access of urls via the urllib* libraries.
* Adds a new spec generator for common arguments.
* Makes the user-agent string configurable.

Fixes #6211
This commit is contained in:
James Cammarata
2014-03-10 16:06:52 -05:00
parent 6577ff5f85
commit 9730157525
23 changed files with 598 additions and 402 deletions

View File

@@ -96,31 +96,12 @@ EXAMPLES = '''
tags=tag1,tag2,tag3
'''
HAS_URLLIB = True
try:
import urllib
except ImportError:
HAS_URLLIB = False
HAS_URLLIB2 = True
try:
import urllib2
except ImportError:
HAS_URLLIB2 = False
# ===========================================
# Module execution.
#
def main():
if not HAS_URLLIB:
module.fail_json(msg="urllib is not installed")
if not HAS_URLLIB2:
module.fail_json(msg="urllib2 is not installed")
module = AnsibleModule(
argument_spec=dict(
token=dict(required=True),
@@ -187,14 +168,16 @@ def main():
module.exit_json(changed=False)
# Send the data to Flowdock
try:
response = urllib2.urlopen(url, urllib.urlencode(params))
except Exception, e:
module.fail_json(msg="unable to send msg: %s" % e)
data = urllib.urlencode(params)
response, info = fetch_url(module, url, data=data)
if info['status'] != 200:
module.fail_json(msg="unable to send msg: %s" % info['msg'])
module.exit_json(changed=False, msg=module.params["msg"])
module.exit_json(changed=True, msg=module.params["msg"])
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()

View File

@@ -41,8 +41,6 @@ EXAMPLES = '''
message=deployed {{ target }}
'''
import urllib
BASE_URL = 'https://grove.io/api/notice/%s/'
# ==============================================================
@@ -57,7 +55,10 @@ def do_notify_grove(module, channel_token, service, message, url=None, icon_url=
if icon_url is not None:
my_data['icon_url'] = icon_url
urllib.urlopen(my_url, urllib.urlencode(my_data))
data = urllib.urlencode(my_data)
response, info = fetch_url(module, my_url, data=data)
if info['status'] != 200:
module.fail_json(msg="failed to send notification: %s" % info['msg'])
# ==============================================================
# main

View File

@@ -60,22 +60,10 @@ EXAMPLES = '''
# HipChat module specific support methods.
#
HAS_URLLIB = True
try:
import urllib
except ImportError:
HAS_URLLIB = False
HAS_URLLIB2 = True
try:
import urllib2
except ImportError:
HAS_URLLIB2 = False
MSG_URI = "https://api.hipchat.com/v1/rooms/message?"
def send_msg(token, room, msg_from, msg, msg_format='text',
def send_msg(module, token, room, msg_from, msg, msg_format='text',
color='yellow', notify=False):
'''sending message to hipchat'''
@@ -92,8 +80,12 @@ def send_msg(token, room, msg_from, msg, msg_format='text',
params['notify'] = 0
url = MSG_URI + "auth_token=%s" % (token)
response = urllib2.urlopen(url, urllib.urlencode(params))
return response.read()
data = urllib.urlencode(params)
response, info = fetch_url(module, url, data=data)
if info['status'] == 200:
return response.read()
else:
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
# ===========================================
@@ -102,11 +94,6 @@ def send_msg(token, room, msg_from, msg, msg_format='text',
def main():
if not HAS_URLLIB:
module.fail_json(msg="urllib is not installed")
if not HAS_URLLIB2:
module.fail_json(msg="urllib2 is not installed")
module = AnsibleModule(
argument_spec=dict(
token=dict(required=True),
@@ -130,15 +117,15 @@ def main():
notify = module.params["notify"]
try:
send_msg(token, room, msg_from, msg, msg_format,
color, notify)
send_msg(module, token, room, msg_from, msg, msg_format, color, notify)
except Exception, e:
module.fail_json(msg="unable to sent msg: %s" % e)
changed = True
module.exit_json(changed=changed, room=room, msg_from=msg_from,
msg=msg)
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()