-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
test_relay_projectconfigs_v2.py
381 lines (291 loc) · 13.3 KB
/
test_relay_projectconfigs_v2.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from __future__ import annotations
import re
from typing import Any
import pytest
from django.urls import reverse
from sentry import quotas
from sentry.constants import ObjectStatus
from sentry.models.projectkey import ProjectKey, ProjectKeyStatus
from sentry.testutils.helpers import Feature
from sentry.testutils.pytest.fixtures import django_db_all
from sentry.utils import json, safe
_date_regex = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$")
def _get_all_keys(config):
for key in config:
yield key
if key == "breakdownsV2":
# Breakdown keys are not field names and may contain underscores,
# e.g. span_ops
continue
if isinstance(config[key], dict):
for key in _get_all_keys(config[key]):
yield key
@pytest.fixture(autouse=True)
def setup_relay(default_project):
default_project.update_option("sentry:scrub_ip_address", True)
@pytest.fixture
def call_endpoint(client, relay, private_key, default_projectkey):
def inner(full_config, public_keys=None, version="2"):
path = reverse("sentry-api-0-relay-projectconfigs") + f"?version={version}"
if public_keys is None:
public_keys = [str(default_projectkey.public_key)]
if full_config is None:
raw_json, signature = private_key.pack({"publicKeys": public_keys})
else:
raw_json, signature = private_key.pack(
{"publicKeys": public_keys, "fullConfig": full_config}
)
resp = client.post(
path,
data=raw_json,
content_type="application/json",
HTTP_X_SENTRY_RELAY_ID=relay.relay_id,
HTTP_X_SENTRY_RELAY_SIGNATURE=signature,
)
return json.loads(resp.content), resp.status_code
return inner
@pytest.fixture
def add_org_key(default_organization, relay):
default_organization.update_option(
"sentry:trusted-relays", [{"public_key": relay.public_key, "name": "main-relay"}]
)
@pytest.fixture
def no_internal_networks(monkeypatch):
"""
Disable is_internal_ip functionality (make all requests appear to be from external networks)
"""
monkeypatch.setattr("sentry.auth.system.INTERNAL_NETWORKS", ())
@django_db_all
def test_internal_relays_should_receive_minimal_configs_if_they_do_not_explicitly_ask_for_full_config(
call_endpoint, default_project, default_projectkey
):
result, status_code = call_endpoint(full_config=False)
assert status_code < 400
# Sweeping assertion that we do not have any snake_case in that config.
# Might need refining.
assert not {x for x in _get_all_keys(result) if "-" in x or "_" in x}
cfg = safe.get_path(result, "configs", str(default_projectkey.public_key))
assert safe.get_path(cfg, "config", "filterSettings") is None
assert safe.get_path(cfg, "config", "groupingConfig") is None
@django_db_all
def test_internal_relays_should_receive_full_configs(
call_endpoint, default_project, default_projectkey
):
result, status_code = call_endpoint(full_config=True)
assert status_code < 400
# Sweeping assertion that we do not have any snake_case in that config.
# Might need refining.
assert not {x for x in _get_all_keys(result) if "-" in x or "_" in x}
cfg = safe.get_path(result, "configs", default_projectkey.public_key)
assert safe.get_path(cfg, "disabled") is False
(public_key,) = cfg["publicKeys"]
assert public_key["publicKey"] == default_projectkey.public_key
assert public_key["numericId"] == default_projectkey.id
assert public_key["isEnabled"]
assert safe.get_path(cfg, "slug") == default_project.slug
last_change = safe.get_path(cfg, "lastChange")
assert _date_regex.match(last_change) is not None
last_fetch = safe.get_path(cfg, "lastFetch")
assert _date_regex.match(last_fetch) is not None
assert safe.get_path(cfg, "organizationId") == default_project.organization.id
assert safe.get_path(cfg, "projectId") == default_project.id
assert safe.get_path(cfg, "slug") == default_project.slug
assert safe.get_path(cfg, "rev") is not None
assert safe.get_path(cfg, "config", "trustedRelays") == []
assert safe.get_path(cfg, "config", "filterSettings") is not None
assert safe.get_path(cfg, "config", "groupingConfig", "enhancements") is not None
assert safe.get_path(cfg, "config", "groupingConfig", "id") is not None
assert safe.get_path(cfg, "config", "piiConfig", "applications") is None
assert safe.get_path(cfg, "config", "piiConfig", "rules") is None
assert safe.get_path(cfg, "config", "datascrubbingSettings", "scrubData") is True
assert safe.get_path(cfg, "config", "datascrubbingSettings", "scrubDefaults") is True
assert safe.get_path(cfg, "config", "datascrubbingSettings", "scrubIpAddresses") is True
assert safe.get_path(cfg, "config", "datascrubbingSettings", "sensitiveFields") == []
assert safe.get_path(cfg, "config", "quotas") is None
# Event retention depends on settings, so assert the actual value.
assert safe.get_path(cfg, "config", "eventRetention") == quotas.backend.get_event_retention(
default_project.organization
)
@django_db_all
def test_relays_dyamic_sampling(
client, call_endpoint, default_project, default_projectkey, dyn_sampling_data
):
"""
Tests that dynamic sampling configuration set in project details are retrieved in relay configs
"""
with Feature(
{
"organizations:dynamic-sampling": True,
}
):
result, status_code = call_endpoint(full_config=False)
assert status_code < 400
dynamic_sampling = safe.get_path(
result,
"configs",
str(default_projectkey.public_key),
"config",
"sampling",
)
assert dynamic_sampling == {"version": 2, "rules": []}
@django_db_all
def test_trusted_external_relays_should_not_be_able_to_request_full_configs(
add_org_key, call_endpoint, no_internal_networks
):
result, status_code = call_endpoint(full_config=True)
assert status_code == 403
@django_db_all
def test_when_not_sending_full_config_info_into_a_internal_relay_a_restricted_config_is_returned(
call_endpoint, default_project, default_projectkey
):
result, status_code = call_endpoint(full_config=None)
assert status_code < 400
cfg = safe.get_path(result, "configs", str(default_projectkey.public_key))
assert safe.get_path(cfg, "config", "filterSettings") is None
assert safe.get_path(cfg, "config", "groupingConfig") is None
@django_db_all
def test_when_not_sending_full_config_info_into_an_external_relay_a_restricted_config_is_returned(
call_endpoint, add_org_key, relay, default_project, default_projectkey
):
relay.is_internal = False
relay.save()
result, status_code = call_endpoint(full_config=None)
assert status_code < 400
cfg = safe.get_path(result, "configs", str(default_projectkey.public_key))
assert safe.get_path(cfg, "config", "filterSettings") is None
assert safe.get_path(cfg, "config", "groupingConfig") is None
@django_db_all
def test_trusted_external_relays_should_receive_minimal_configs(
relay, add_org_key, call_endpoint, default_project, default_projectkey
):
relay.is_internal = False
relay.save()
result, status_code = call_endpoint(full_config=False)
assert status_code < 400
cfg = safe.get_path(result, "configs", default_projectkey.public_key)
assert safe.get_path(cfg, "disabled") is False
(public_key,) = cfg["publicKeys"]
assert public_key["publicKey"] == default_projectkey.public_key
assert public_key["numericId"] == default_projectkey.id
assert public_key["isEnabled"]
assert "quotas" not in public_key
assert safe.get_path(cfg, "slug") == default_project.slug
last_change = safe.get_path(cfg, "lastChange")
assert _date_regex.match(last_change) is not None
last_fetch = safe.get_path(cfg, "lastFetch")
assert _date_regex.match(last_fetch) is not None
assert safe.get_path(cfg, "organizationId") == default_project.organization.id
assert safe.get_path(cfg, "projectId") == default_project.id
assert safe.get_path(cfg, "slug") == default_project.slug
assert safe.get_path(cfg, "rev") is not None
assert safe.get_path(cfg, "config", "trustedRelays") == [relay.public_key]
assert safe.get_path(cfg, "config", "filterSettings") is None
assert safe.get_path(cfg, "config", "groupingConfig") is None
assert safe.get_path(cfg, "config", "datascrubbingSettings", "scrubData") is not None
assert safe.get_path(cfg, "config", "datascrubbingSettings", "scrubIpAddresses") is not None
assert safe.get_path(cfg, "config", "piiConfig", "rules") is None
assert safe.get_path(cfg, "config", "piiConfig", "applications") is None
assert safe.get_path(cfg, "config", "quotas") is None
@django_db_all
def test_untrusted_external_relays_should_not_receive_configs(
call_endpoint, default_project, default_projectkey, no_internal_networks
):
result, status_code = call_endpoint(full_config=False)
assert status_code < 400
cfg = result["configs"][default_projectkey.public_key]
assert cfg["disabled"]
@pytest.fixture
def projectconfig_cache_set(monkeypatch):
calls: list[dict[str, Any]] = []
monkeypatch.setattr("sentry.relay.projectconfig_cache.backend.set_many", calls.append)
return calls
@django_db_all
def test_relay_projectconfig_cache_minimal_config(
call_endpoint, default_project, projectconfig_cache_set, task_runner
):
"""
When a relay fetches a minimal config, that config should not end up in Redis.
"""
with task_runner():
result, status_code = call_endpoint(full_config=False)
assert status_code < 400
assert not projectconfig_cache_set
@django_db_all
def test_relay_projectconfig_cache_full_config(
call_endpoint, default_projectkey, projectconfig_cache_set, task_runner
):
"""
When a relay fetches a full config, that config should end up in Redis.
"""
with task_runner():
result, status_code = call_endpoint(full_config=True)
assert status_code < 400
http_cfg = result["configs"][default_projectkey.public_key]
(call,) = projectconfig_cache_set
assert len(call) == 1
redis_cfg = call[str(default_projectkey.public_key)]
del http_cfg["lastFetch"]
del http_cfg["lastChange"]
del redis_cfg["lastFetch"]
del redis_cfg["lastChange"]
assert redis_cfg == http_cfg
@django_db_all
def test_relay_nonexistent_project(call_endpoint, projectconfig_cache_set, task_runner):
wrong_public_key = ProjectKey.generate_api_key()
with task_runner():
result, status_code = call_endpoint(full_config=True, public_keys=[wrong_public_key])
assert status_code < 400
assert result == {"configs": {wrong_public_key: {"disabled": True}}}
assert projectconfig_cache_set == [{str(wrong_public_key): result["configs"][wrong_public_key]}]
@django_db_all
def test_relay_disabled_project(
call_endpoint, default_project, projectconfig_cache_set, task_runner
):
default_project.update(status=ObjectStatus.PENDING_DELETION)
wrong_public_key = ProjectKey.generate_api_key()
with task_runner():
result, status_code = call_endpoint(full_config=True, public_keys=[wrong_public_key])
assert status_code < 400
assert result == {"configs": {wrong_public_key: {"disabled": True}}}
assert projectconfig_cache_set == [{str(wrong_public_key): result["configs"][wrong_public_key]}]
@django_db_all
def test_relay_disabled_key(
call_endpoint, default_project, projectconfig_cache_set, task_runner, default_projectkey
):
default_projectkey.update(status=ProjectKeyStatus.INACTIVE)
with task_runner():
result, status_code = call_endpoint(full_config=True)
assert status_code < 400
(http_cfg,) = result["configs"].values()
assert http_cfg == {"disabled": True}
assert projectconfig_cache_set == [{str(default_projectkey.public_key): http_cfg}]
@django_db_all
@pytest.mark.parametrize("drop_sessions", [False, True])
def test_session_metrics_extraction(call_endpoint, task_runner, drop_sessions):
with Feature({"organizations:metrics-extraction": True}), Feature(
{"organizations:release-health-drop-sessions": drop_sessions}
):
with task_runner():
result, status_code = call_endpoint(full_config=True)
assert status_code < 400
for config in result["configs"].values():
config = config["config"]
assert config["sessionMetrics"] == {"version": 1}
@django_db_all
@pytest.mark.parametrize("abnormal_mechanism_rollout", [0, 1])
def test_session_metrics_abnormal_mechanism_tag_extraction(
call_endpoint, task_runner, set_sentry_option, abnormal_mechanism_rollout
):
with set_sentry_option(
"sentry-metrics.releasehealth.abnormal-mechanism-extraction-rate",
abnormal_mechanism_rollout,
):
with Feature({"organizations:metrics-extraction": True}):
with task_runner():
result, status_code = call_endpoint(full_config=True)
assert status_code < 400
for config in result["configs"].values():
config = config["config"]
assert config["sessionMetrics"] == {
"version": 2 if abnormal_mechanism_rollout else 1,
}