-
Notifications
You must be signed in to change notification settings - Fork 13.9k
/
test_serve_logs.py
237 lines (204 loc) · 7.37 KB
/
test_serve_logs.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
# 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
from datetime import timedelta
from pathlib import Path
from typing import TYPE_CHECKING
import jwt
import pytest
import time_machine
from airflow.config_templates.airflow_local_settings import DEFAULT_LOGGING_CONFIG
from airflow.utils import timezone
from airflow.utils.jwt_signer import JWTSigner
from airflow.utils.serve_logs import create_app
from tests.test_utils.config import conf_vars
if TYPE_CHECKING:
from flask.testing import FlaskClient
LOG_DATA = "Airflow log data" * 20
@pytest.fixture
def client_without_config(tmp_path):
with conf_vars({("logging", "base_log_folder"): tmp_path.as_posix()}):
app = create_app()
yield app.test_client()
@pytest.fixture
def client_with_config():
with conf_vars(
{
(
"logging",
"logging_config_class",
): "airflow.config_templates.airflow_local_settings.DEFAULT_LOGGING_CONFIG"
}
):
app = create_app()
yield app.test_client()
@pytest.fixture(params=["client_without_config", "client_with_config"])
def client(request):
return request.getfixturevalue(request.param)
@pytest.fixture
def sample_log(request, tmp_path):
client = request.getfixturevalue("client")
if client == request.getfixturevalue("client_without_config"):
base_log_dir = tmp_path
elif client == request.getfixturevalue("client_with_config"):
base_log_dir = Path(DEFAULT_LOGGING_CONFIG["handlers"]["task"]["base_log_folder"])
else:
raise ValueError(f"Unknown client fixture: {client}")
f = base_log_dir.joinpath("sample.log")
f.write_text(LOG_DATA)
return f
@pytest.fixture
def signer(secret_key):
return JWTSigner(
secret_key=secret_key,
expiration_time_in_seconds=30,
audience="task-instance-logs",
)
@pytest.fixture
def different_audience(secret_key):
return JWTSigner(
secret_key=secret_key,
expiration_time_in_seconds=30,
audience="different-audience",
)
@pytest.mark.usefixtures("sample_log")
class TestServeLogs:
def test_forbidden_no_auth(self, client: FlaskClient):
assert 403 == client.get("/log/sample.log").status_code
def test_should_serve_file(self, client: FlaskClient, signer):
response = client.get(
"/log/sample.log",
headers={
"Authorization": signer.generate_signed_token({"filename": "sample.log"}),
},
)
assert response.data.decode() == LOG_DATA
assert response.status_code == 200
def test_forbidden_different_logname(self, client: FlaskClient, signer):
response = client.get(
"/log/sample.log",
headers={
"Authorization": signer.generate_signed_token({"filename": "different.log"}),
},
)
assert response.status_code == 403
def test_forbidden_expired(self, client: FlaskClient, signer):
with time_machine.travel("2010-01-14"):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 403
)
def test_forbidden_future(self, client: FlaskClient, signer):
with time_machine.travel(timezone.utcnow() + timedelta(seconds=3600)):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 403
)
def test_ok_with_short_future_skew(self, client: FlaskClient, signer):
with time_machine.travel(timezone.utcnow() + timedelta(seconds=1)):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 200
)
def test_ok_with_short_past_skew(self, client: FlaskClient, signer):
with time_machine.travel(timezone.utcnow() - timedelta(seconds=31)):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 200
)
def test_forbidden_with_long_future_skew(self, client: FlaskClient, signer):
with time_machine.travel(timezone.utcnow() + timedelta(seconds=10)):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 403
)
def test_forbidden_with_long_past_skew(self, client: FlaskClient, signer):
with time_machine.travel(timezone.utcnow() - timedelta(seconds=40)):
token = signer.generate_signed_token({"filename": "sample.log"})
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 403
)
def test_wrong_audience(self, client: FlaskClient, different_audience):
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": different_audience.generate_signed_token({"filename": "sample.log"}),
},
).status_code
== 403
)
@pytest.mark.parametrize("claim_to_remove", ["iat", "exp", "nbf", "aud"])
def test_missing_claims(self, claim_to_remove: str, client: FlaskClient, secret_key):
jwt_dict = {
"aud": "task-instance-logs",
"iat": timezone.utcnow(),
"nbf": timezone.utcnow(),
"exp": timezone.utcnow() + timedelta(seconds=30),
}
del jwt_dict[claim_to_remove]
jwt_dict.update({"filename": "sample.log"})
token = jwt.encode(
jwt_dict,
secret_key,
algorithm="HS512",
)
assert (
client.get(
"/log/sample.log",
headers={
"Authorization": token,
},
).status_code
== 403
)