Skip to content

Commit

Permalink
fixes test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jordicasas1987 committed Jun 30, 2023
1 parent d68048a commit 37002ac
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions awx/main/tests/functional/api/test_generic.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
import pytest
from unittest.mock import MagicMock

from awx.api.versioning import reverse
from awx.main.middleware import SetRemoteAddrFromRemoteHostHeader


@pytest.mark.django_db
def test_proxy_ip_allowed(get, patch, admin):
get_response = MagicMock()
url = reverse('api:setting_singleton_detail', kwargs={'category_slug': 'system'})
patch(url, user=admin, data={'REMOTE_HOST_HEADERS': ['HTTP_X_FROM_THE_LOAD_BALANCER', 'REMOTE_ADDR', 'REMOTE_HOST']})
middleware = SetRemoteAddrFromRemoteHostHeader(get_response)

# By default, `PROXY_IP_ALLOWED_LIST` is disabled, so custom `REMOTE_HOST_HEADERS`
# should just pass through
middleware = SetRemoteAddrFromRemoteHostHeader()
patch(url, user=admin, data={'REMOTE_HOST_HEADERS': ['HTTP_X_FROM_THE_LOAD_BALANCER', 'REMOTE_ADDR', 'REMOTE_HOST']})
get(url, user=admin, middleware=middleware, HTTP_X_FROM_THE_LOAD_BALANCER='some-actual-ip')
assert middleware.environ['HTTP_X_FROM_THE_LOAD_BALANCER'] == 'some-actual-ip'

# If `PROXY_IP_ALLOWED_LIST` is restricted to 10.0.1.100 and we make a request
# from 8.9.10.11, the custom `HTTP_X_FROM_THE_LOAD_BALANCER` header should
# be stripped
patch(url, user=admin, data={'PROXY_IP_ALLOWED_LIST': ['10.0.1.100']})
middleware = SetRemoteAddrFromRemoteHostHeader()
get(url, user=admin, middleware=middleware, REMOTE_ADDR='8.9.10.11', HTTP_X_FROM_THE_LOAD_BALANCER='some-actual-ip')
assert 'HTTP_X_FROM_THE_LOAD_BALANCER' not in middleware.environ

# If 8.9.10.11 is added to `PROXY_IP_ALLOWED_LIST` the
# `HTTP_X_FROM_THE_LOAD_BALANCER` header should be passed through again
patch(url, user=admin, data={'PROXY_IP_ALLOWED_LIST': ['10.0.1.100', '8.9.10.11']})
middleware = SetRemoteAddrFromRemoteHostHeader()
get(url, user=admin, middleware=middleware, REMOTE_ADDR='8.9.10.11', HTTP_X_FROM_THE_LOAD_BALANCER='some-actual-ip')
assert middleware.environ['HTTP_X_FROM_THE_LOAD_BALANCER'] == 'some-actual-ip'

# Allow allowed list of proxy hostnames in addition to IP addresses
patch(url, user=admin, data={'PROXY_IP_ALLOWED_LIST': ['my.proxy.example.org']})
middleware = SetRemoteAddrFromRemoteHostHeader()
get(url, user=admin, middleware=middleware, REMOTE_ADDR='8.9.10.11', REMOTE_HOST='my.proxy.example.org', HTTP_X_FROM_THE_LOAD_BALANCER='some-actual-ip')
assert middleware.environ['HTTP_X_FROM_THE_LOAD_BALANCER'] == 'some-actual-ip'

Expand Down

0 comments on commit 37002ac

Please sign in to comment.