forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_app.py
252 lines (206 loc) · 9.17 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import runpy
import sys
import unittest
from datetime import timedelta
from unittest import mock
import pytest
from werkzeug.routing import Rule
from werkzeug.test import create_environ
from werkzeug.wrappers import Response
from airflow.www import app as application
from tests.test_utils.config import conf_vars
from tests.test_utils.decorators import dont_initialize_flask_app_submodules
class TestApp(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
from airflow import settings
settings.configure_orm()
@conf_vars(
{
('webserver', 'enable_proxy_fix'): 'True',
('webserver', 'proxy_fix_x_for'): '1',
('webserver', 'proxy_fix_x_proto'): '1',
('webserver', 'proxy_fix_x_host'): '1',
('webserver', 'proxy_fix_x_port'): '1',
('webserver', 'proxy_fix_x_prefix'): '1',
}
)
@dont_initialize_flask_app_submodules
def test_should_respect_proxy_fix(self):
app = application.cached_app(testing=True)
app.url_map.add(Rule("/debug", endpoint="debug"))
def debug_view():
from flask import request
# Should respect HTTP_X_FORWARDED_FOR
assert request.remote_addr == '192.168.0.1'
# Should respect HTTP_X_FORWARDED_PROTO, HTTP_X_FORWARDED_HOST, HTTP_X_FORWARDED_PORT,
# HTTP_X_FORWARDED_PREFIX
assert request.url == 'https://valid:445/proxy-prefix/debug'
return Response("success")
app.view_functions['debug'] = debug_view
new_environ = {
"PATH_INFO": "/debug",
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "invalid:9000",
"HTTP_X_FORWARDED_FOR": "192.168.0.1",
"HTTP_X_FORWARDED_PROTO": "https",
"HTTP_X_FORWARDED_HOST": "valid",
"HTTP_X_FORWARDED_PORT": "445",
"HTTP_X_FORWARDED_PREFIX": "/proxy-prefix",
}
environ = create_environ(environ_overrides=new_environ)
response = Response.from_app(app, environ)
assert b"success" == response.get_data()
assert response.status_code == 200
@conf_vars(
{
('webserver', 'base_url'): 'http://localhost:8080/internal-client',
}
)
@dont_initialize_flask_app_submodules
def test_should_respect_base_url_ignore_proxy_headers(self):
app = application.cached_app(testing=True)
app.url_map.add(Rule("/debug", endpoint="debug"))
def debug_view():
from flask import request
# Should ignore HTTP_X_FORWARDED_FOR
assert request.remote_addr == '192.168.0.2'
# Should ignore HTTP_X_FORWARDED_PROTO, HTTP_X_FORWARDED_HOST, HTTP_X_FORWARDED_PORT,
# HTTP_X_FORWARDED_PREFIX
assert request.url == 'http://invalid:9000/internal-client/debug'
return Response("success")
app.view_functions['debug'] = debug_view
new_environ = {
"PATH_INFO": "/internal-client/debug",
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "invalid:9000",
"HTTP_X_FORWARDED_FOR": "192.168.0.1",
"HTTP_X_FORWARDED_PROTO": "https",
"HTTP_X_FORWARDED_HOST": "valid",
"HTTP_X_FORWARDED_PORT": "445",
"HTTP_X_FORWARDED_PREFIX": "/proxy-prefix",
}
environ = create_environ(environ_overrides=new_environ)
response = Response.from_app(app, environ)
assert b"success" == response.get_data()
assert response.status_code == 200
@conf_vars(
{
('webserver', 'base_url'): 'http://localhost:8080/internal-client',
('webserver', 'enable_proxy_fix'): 'True',
('webserver', 'proxy_fix_x_for'): '1',
('webserver', 'proxy_fix_x_proto'): '1',
('webserver', 'proxy_fix_x_host'): '1',
('webserver', 'proxy_fix_x_port'): '1',
('webserver', 'proxy_fix_x_prefix'): '1',
}
)
@dont_initialize_flask_app_submodules
def test_should_respect_base_url_when_proxy_fix_and_base_url_is_set_up_but_headers_missing(self):
app = application.cached_app(testing=True)
app.url_map.add(Rule("/debug", endpoint="debug"))
def debug_view():
from flask import request
# Should use original REMOTE_ADDR
assert request.remote_addr == '192.168.0.1'
# Should respect base_url
assert request.url == "http://invalid:9000/internal-client/debug"
return Response("success")
app.view_functions['debug'] = debug_view
new_environ = {
"PATH_INFO": "/internal-client/debug",
"REMOTE_ADDR": "192.168.0.1",
"HTTP_HOST": "invalid:9000",
}
environ = create_environ(environ_overrides=new_environ)
response = Response.from_app(app, environ)
assert b"success" == response.get_data()
assert response.status_code == 200
@conf_vars(
{
('webserver', 'base_url'): 'http://localhost:8080/internal-client',
('webserver', 'enable_proxy_fix'): 'True',
('webserver', 'proxy_fix_x_for'): '1',
('webserver', 'proxy_fix_x_proto'): '1',
('webserver', 'proxy_fix_x_host'): '1',
('webserver', 'proxy_fix_x_port'): '1',
('webserver', 'proxy_fix_x_prefix'): '1',
}
)
@dont_initialize_flask_app_submodules
def test_should_respect_base_url_and_proxy_when_proxy_fix_and_base_url_is_set_up(self):
app = application.cached_app(testing=True)
app.url_map.add(Rule("/debug", endpoint="debug"))
def debug_view():
from flask import request
# Should respect HTTP_X_FORWARDED_FOR
assert request.remote_addr == '192.168.0.1'
# Should respect HTTP_X_FORWARDED_PROTO, HTTP_X_FORWARDED_HOST, HTTP_X_FORWARDED_PORT,
# HTTP_X_FORWARDED_PREFIX and use base_url
assert request.url == "https://valid:445/proxy-prefix/internal-client/debug"
return Response("success")
app.view_functions['debug'] = debug_view
new_environ = {
"PATH_INFO": "/internal-client/debug",
"REMOTE_ADDR": "192.168.0.2",
"HTTP_HOST": "invalid:9000",
"HTTP_X_FORWARDED_FOR": "192.168.0.1",
"HTTP_X_FORWARDED_PROTO": "https",
"HTTP_X_FORWARDED_HOST": "valid",
"HTTP_X_FORWARDED_PORT": "445",
"HTTP_X_FORWARDED_PREFIX": "/proxy-prefix",
}
environ = create_environ(environ_overrides=new_environ)
response = Response.from_app(app, environ)
assert b"success" == response.get_data()
assert response.status_code == 200
@conf_vars(
{
('webserver', 'session_lifetime_minutes'): '3600',
}
)
@dont_initialize_flask_app_submodules
def test_should_set_permanent_session_timeout(self):
app = application.cached_app(testing=True)
assert app.config['PERMANENT_SESSION_LIFETIME'] == timedelta(minutes=3600)
@conf_vars({('webserver', 'cookie_samesite'): ''})
@dont_initialize_flask_app_submodules
def test_correct_default_is_set_for_cookie_samesite(self):
"""An empty 'cookie_samesite' should be corrected to 'Lax' with a deprecation warning."""
with pytest.deprecated_call():
app = application.cached_app(testing=True)
assert app.config['SESSION_COOKIE_SAMESITE'] == 'Lax'
class TestFlaskCli:
@dont_initialize_flask_app_submodules(skip_all_except=['init_appbuilder'])
def test_flask_cli_should_display_routes(self, capsys):
with mock.patch.dict("os.environ", FLASK_APP="airflow.www.app:cached_app"), mock.patch.object(
sys, 'argv', ['flask', 'routes']
), pytest.raises(SystemExit):
runpy.run_module('flask', run_name='__main__')
output = capsys.readouterr()
assert "/login/" in output.out
def test_app_can_json_serialize_k8s_pod():
# This is mostly testing that we have correctly configured the JSON provider to use. Testing the k8s pos
# is a side-effect of that.
k8s = pytest.importorskip('kubernetes.client.models')
pod = k8s.V1Pod(spec=k8s.V1PodSpec(containers=[k8s.V1Container(name="base")]))
app = application.cached_app(testing=True)
assert app.json.dumps(pod) == '{"spec": {"containers": [{"name": "base"}]}}'