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
Extend ProxyHandler to support CIDR ranges for no_proxy #83085
Open
sivel
wants to merge
7
commits into
ansible:devel
Choose a base branch
from
sivel:urls-no-proxy-cidr
base: devel
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e06e888
Extend ProxyHandler to support CIDR ranges for no_proxy
sivel 58a1b91
Also skip rhel testing for tinyproxy
sivel 6d6b0f4
Switch to proxy.py
sivel b484277
Use var for http_proxy
sivel 2447f66
improve plugin
sivel c206d4f
Derive with improvements instead of straight copy
sivel 6367c69
Set fact for proxy.py pid
sivel 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- urls.py - Support CIDR ranges for no_proxy |
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from proxy.http.proxy import HttpProxyBasePlugin | ||
from proxy.http.parser import HttpParser, httpParserStates | ||
|
||
|
||
class HamSandwichPlugin(HttpProxyBasePlugin): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.parser = None | ||
self.parsable = True | ||
|
||
def handle_upstream_chunk(self, chunk): | ||
if not self.parsable: | ||
return chunk | ||
|
||
if not self.parser: | ||
self.parser = HttpParser.response(chunk) | ||
else: | ||
self.parser.parse(chunk) | ||
|
||
if self.parser.state == httpParserStates.INITIALIZED: | ||
# This is likely TLS without interception | ||
self.parsable = False | ||
return chunk | ||
|
||
if not self.parser.is_complete: | ||
return None | ||
|
||
self.parser.add_header(b'X-Sandwich', b'ham') | ||
return memoryview(bytearray(self.parser.build_response())) |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- name: stop proxy.py | ||
command: kill {{ proxy_py_pid }} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dependencies: | ||
- setup_remote_tmp_dir |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
- name: install proxy.py | ||
pip: | ||
name: proxy.py | ||
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. It might be a good idea to pin it to the most recent release candidate (otherwise it may end up being pretty old). FYI. |
||
virtualenv: '{{ remote_tmp_dir }}/proxy_py' | ||
virtualenv_command: "{{ ansible_python_interpreter }} -m venv" | ||
notify: stop proxy.py | ||
|
||
- name: get venv site-packages | ||
command: >- | ||
{{ remote_tmp_dir }}/proxy_py/bin/python -c 'import site; print(site.getsitepackages()[0])' | ||
register: proxy_py_site_packages | ||
|
||
- name: install proxy.py plugin | ||
copy: | ||
src: hamsandwich.py | ||
dest: '{{ proxy_py_site_packages.stdout }}/hamsandwich.py' | ||
|
||
- name: start proxy.py | ||
command: >- | ||
{{ remote_tmp_dir }}/proxy_py/bin/proxy --port 8080 --log-file "{{ remote_tmp_dir }}/proxy_py/proxy_py.log" | ||
--plugins hamsandwich.HamSandwichPlugin --pid-file "{{ remote_tmp_dir }}/proxy_py/proxy_py.pid" | ||
async: 120 | ||
poll: 0 | ||
register: proxy_py | ||
|
||
- name: wait for proxy.py to start | ||
wait_for: | ||
port: 8080 | ||
connect_timeout: 1 | ||
timeout: 10 | ||
|
||
- name: get proxy.py pid | ||
slurp: | ||
path: '{{ remote_tmp_dir }}/proxy_py/proxy_py.pid' | ||
register: proxy_py_slurp_pid | ||
|
||
- name: set fact for proxy.py pid | ||
set_fact: | ||
proxy_py_pid: '{{ proxy_py_slurp_pid.content|b64decode }}' | ||
|
||
- name: set fact for proxy host | ||
set_fact: | ||
http_proxy: 'http://127.0.0.1:8080' |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
destructive | ||
shippable/posix/group1 | ||
needs/httptester | ||
needs/target/setup_proxy |
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
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
- include_role: | ||
name: setup_proxy | ||
|
||
- name: Get IP address for ansible.http.tests | ||
command: >- | ||
{{ ansible_python_interpreter }} -c 'import socket; print(socket.gethostbyname("{{ httpbin_host }}"))' | ||
register: httpbin_ip | ||
|
||
- name: Test http over http proxy | ||
uri: | ||
url: http://{{ httpbin_host }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
register: http_over_http | ||
failed_when: http_over_http.x_sandwich is undefined | ||
|
||
- name: Test https over http proxy | ||
uri: | ||
url: https://{{ httpbin_host }}/get | ||
environment: | ||
https_proxy: '{{ http_proxy }}' | ||
register: https_over_http | ||
# failed_when: | ||
# failure checking is handled by the assert at the bottom comparing logs | ||
# because we aren't running a proxy that can inspect the https stream | ||
# there won't be added headers | ||
|
||
- name: Test request without a proxy | ||
uri: | ||
url: http://{{ httpbin_host }}/get | ||
register: request_without_proxy | ||
failed_when: request_without_proxy.x_sandwich is defined | ||
|
||
- name: Test request with proxy and no_proxy=hostname | ||
uri: | ||
url: http://{{ httpbin_host }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_host }}' | ||
register: no_proxy_hostname | ||
failed_when: no_proxy_hostname.x_sandwich is defined | ||
|
||
- name: Test request with proxy and no_proxy=ip | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_ip.stdout }}' | ||
register: no_proxy_ip | ||
failed_when: no_proxy_ip.x_sandwich is defined | ||
|
||
- name: Test request with proxy and no_proxy=cidr/32 | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_ip.stdout }}/32' | ||
register: no_proxy_cidr_32 | ||
failed_when: no_proxy_cidr_32.x_sandwich is defined | ||
|
||
- name: Test request with proxy and no_proxy=cidr/24 | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_cidr }}' | ||
register: no_proxy_cidr_24 | ||
vars: | ||
httpbin_cidr: "{{ httpbin_ip.stdout.split('.')[:3]|join('.') }}.0/24" | ||
failed_when: no_proxy_cidr_24.x_sandwich is defined | ||
|
||
- name: Test request with proxy and non-matching no_proxy=cidr | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: 1.2.3.0/24 | ||
register: no_proxy_non_matching_cidr | ||
failed_when: no_proxy_non_matching_cidr.x_sandwich is undefined | ||
|
||
- name: Test request with proxy and no_proxy=cidr:port | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_ip.stdout }}/32:80' | ||
register: no_proxy_cidr_port | ||
failed_when: no_proxy_cidr_port.x_sandwich is defined | ||
|
||
- name: Test request with proxy and non-matching no_proxy=cidr:port | ||
uri: | ||
url: http://{{ httpbin_ip.stdout }}/get | ||
environment: | ||
http_proxy: '{{ http_proxy }}' | ||
no_proxy: '{{ httpbin_ip.stdout }}/32:8080' | ||
register: no_proxy_non_matching_cidr_port | ||
failed_when: no_proxy_non_matching_cidr_port.x_sandwich is undefined | ||
|
||
- slurp: | ||
path: "{{ remote_tmp_dir }}/proxy_py/proxy_py.log" | ||
register: proxy_py_logs | ||
|
||
- debug: | ||
msg: '{{ proxy_py_logs.content|b64decode }}' | ||
|
||
- assert: | ||
that: | ||
- >- | ||
log_content is contains "CONNECT " ~ httpbin_host ~ ":443" | ||
# https over http | ||
- >- | ||
log_content|regex_findall("CONNECT ")|length == 1 | ||
# 3 http over http | ||
- >- | ||
log_content|regex_findall('GET')|length == 3 | ||
vars: | ||
log_content: '{{ proxy_py_logs.content|b64decode }}' |
Oops, something went wrong.
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.
The biggest concern I have about this implementation, is in overriding this method.
proxy_open
is not a documented method at https://docs.python.org/3/library/urllib.request.htmlAt closest it the
<protocol>_open
methods are described as part of https://docs.python.org/3/library/urllib.request.html#protocol-open butproxy
isn't really a protocol likehttp
,https
,file
, etc are.And further more, it's highly dependent on
urllib.request.ProxyHandler.__init__
: https://github.com/python/cpython/blob/8f25cc992021d6ffc62bb110545b97a92f7cb295/Lib/urllib/request.py#L770-L774However, the use of
proxy_open
has been around, unchanged for 25+ years, so it seems relatively safe and stable: python/cpython@6d7e47bI guess at a minimum, this is why we have tests, which this PR includes.