-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
16888: comment module_utils/urls.py #16987
Merged
+56
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4bc2d62
16888: comment module_utils/urls.py
kontrafiktion 5f3c667
improved documentation of module (mentioning ‘requests’)
kontrafiktion 3ebeb76
remove mentioning of implementation details (urllib)
kontrafiktion cbfa571
improved parameter and return value documentation
kontrafiktion File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,21 @@ | |
# agrees to be bound by the terms and conditions of this License | ||
# Agreement. | ||
|
||
''' | ||
The **urls** utils module offers a replacement for the urllib2 python library. | ||
|
||
urllib2 is the python stdlib way to retrieve files from the Internet but it | ||
lacks some security features (around verifying SSL certificates) that users | ||
should care about in most situations. Using the functions in this module corrects | ||
deficiencies in the urllib2 module wherever possible. | ||
|
||
There are also third-party libraries (for instance, requests) which can be used | ||
to replace urllib2 with a more secure library. However, all third party libraries | ||
require that the library be installed on the managed machine. That is an extra step | ||
for users making use of a module. If possible, avoid third party libraries by using | ||
this code instead. | ||
''' | ||
|
||
import netrc | ||
import os | ||
import re | ||
|
@@ -728,11 +743,11 @@ def maybe_add_ssl_handler(url, validate_certs): | |
|
||
|
||
def open_url(url, data=None, headers=None, method=None, use_proxy=True, | ||
force=False, last_mod_time=None, timeout=10, validate_certs=True, | ||
url_username=None, url_password=None, http_agent=None, | ||
force_basic_auth=False, follow_redirects='urllib2'): | ||
force=False, last_mod_time=None, timeout=10, validate_certs=True, | ||
url_username=None, url_password=None, http_agent=None, | ||
force_basic_auth=False, follow_redirects='urllib2'): | ||
''' | ||
Fetches a file from an HTTP/FTP server using urllib2 | ||
Sends a request via HTTP(S) or FTP using urllib2 (Python2) or urllib (Python3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as you're clarifying the documentation, we could drop urllib2/urllib mention here. It's not important to users of the function. |
||
|
||
Does not require the module environment | ||
''' | ||
|
@@ -870,23 +885,49 @@ def url_argument_spec(): | |
that will be requesting content via urllib/urllib2 | ||
''' | ||
return dict( | ||
url = dict(), | ||
force = dict(default='no', aliases=['thirsty'], type='bool'), | ||
http_agent = dict(default='ansible-httpget'), | ||
use_proxy = dict(default='yes', type='bool'), | ||
validate_certs = dict(default='yes', type='bool'), | ||
url_username = dict(required=False), | ||
url_password = dict(required=False), | ||
force_basic_auth = dict(required=False, type='bool', default='no'), | ||
url=dict(), | ||
force=dict(default='no', aliases=['thirsty'], type='bool'), | ||
http_agent=dict(default='ansible-httpget'), | ||
use_proxy=dict(default='yes', type='bool'), | ||
validate_certs=dict(default='yes', type='bool'), | ||
url_username=dict(required=False), | ||
url_password=dict(required=False), | ||
force_basic_auth=dict(required=False, type='bool', default='no'), | ||
|
||
) | ||
|
||
|
||
def fetch_url(module, url, data=None, headers=None, method=None, | ||
use_proxy=True, force=False, last_mod_time=None, timeout=10): | ||
''' | ||
Fetches a file from an HTTP/FTP server using urllib2. Requires the module environment | ||
''' | ||
'''Sends a request via HTTP(S) or FTP (needs the module as parameter) | ||
|
||
:arg module: The AnsibleModule (used to get username, password etc. (s.b.). | ||
:arg url: The url to use. | ||
|
||
:kwarg data: The data to be sent (in case of POST/PUT). | ||
:kwarg headers: A dict with the request headers. | ||
:kwarg method: "POST", "PUT", etc. | ||
:kwarg boolean use_proxy: Default: True | ||
:kwarg boolean force: If True: Do not get a cached copy (Default: False) | ||
:kwarg last_mod_time: Default: None | ||
:kwarg int timeout: Default: 10 | ||
|
||
:returns: A tuple of (**response**, **info**). Use ``response.body()`` to read the data. | ||
The **info** contains the 'status' and other meta data. When a HttpError (status > 400) | ||
occurred then ``info['body']`` contains the error response data:: | ||
|
||
Example:: | ||
|
||
data={...} | ||
resp, info = fetch_url("http://example.com", | ||
data=module.jsonify(data) | ||
header={Content-type': 'application/json'}, | ||
method="POST") | ||
status_code = info["status"] | ||
body = resp.read() | ||
if status_code >= 400 : | ||
body = info['body'] | ||
''' | ||
|
||
if not HAS_URLPARSE: | ||
module.fail_json(msg='urlparse is not installed') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module is really a replacement for
urllib.openurl()
which should never be used because it suffers from security limitation. requests has similar security features as this module_util but is an external dependency and therefore would be an additional requirement on the remote machine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.... I see in the original bug that ansible-modules-validate is throwing an error for using requests and that was part of what prompted this doc update. That does make it a good idea to mention requests here.... What about something like: