Skip to content

Commit b5b5272

Browse files
committed
Improved formatting
1 parent 146947d commit b5b5272

File tree

11 files changed

+141
-65
lines changed

11 files changed

+141
-65
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,9 @@ async def do_request(url):
793793
(StatusCode.UNSET, None),
794794
{
795795
HTTP_METHOD: "GET",
796-
HTTP_URL: ("http://REDACTED:REDACTED@localhost:5000/status/200?Signature=REDACTED"),
796+
HTTP_URL: (
797+
"http://REDACTED:REDACTED@localhost:5000/status/200?Signature=REDACTED"
798+
),
797799
HTTP_STATUS_CODE: int(HTTPStatus.OK),
798800
},
799801
)

instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,45 @@ async def test_suppress_instrumentation(
153153

154154
assert len(memory_exporter.get_finished_spans()) == 0
155155

156+
156157
@pytest.mark.asyncio
157158
async def test_remove_sensitive_params(tracer, aiohttp_server):
158159
"""Test that sensitive information in URLs is properly redacted."""
159160
_, memory_exporter = tracer
160-
161+
161162
# Set up instrumentation
162163
AioHttpServerInstrumentor().instrument()
163-
164+
164165
# Create app with test route
165166
app = aiohttp.web.Application()
167+
166168
async def handler(request):
167169
return aiohttp.web.Response(text="hello")
168-
169-
app.router.add_get('/status/200', handler)
170-
170+
171+
app.router.add_get("/status/200", handler)
172+
171173
# Start the server
172174
server = await aiohttp_server(app)
173-
175+
174176
# Make request with sensitive data in URL
175177
url = f"http://username:password@{server.host}:{server.port}/status/200?Signature=secret"
176178
async with aiohttp.ClientSession() as session:
177179
async with session.get(url) as response:
178180
assert response.status == 200
179181
assert await response.text() == "hello"
180-
182+
181183
# Verify redaction in span attributes
182184
spans = memory_exporter.get_finished_spans()
183185
assert len(spans) == 1
184-
186+
185187
span = spans[0]
186188
assert span.attributes[HTTP_METHOD] == "GET"
187189
assert span.attributes[HTTP_STATUS_CODE] == 200
188-
assert span.attributes[HTTP_URL] == f"http://{server.host}:{server.port}/status/200?Signature=REDACTED"
189-
190+
assert (
191+
span.attributes[HTTP_URL]
192+
== f"http://{server.host}:{server.port}/status/200?Signature=REDACTED"
193+
)
194+
190195
# Clean up
191196
AioHttpServerInstrumentor().uninstrument()
192-
memory_exporter.clear()
197+
memory_exporter.clear()

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,8 @@ def test_remove_sensitive_params(self):
18151815
self.scope["query_string"] = b"X-Goog-Signature=1234567890"
18161816
attrs = otel_asgi.collect_request_attributes(self.scope)
18171817
self.assertEqual(
1818-
attrs[SpanAttributes.HTTP_URL], "http://REDACTED:REDACTED@mock/status/200?X-Goog-Signature=REDACTED"
1818+
attrs[SpanAttributes.HTTP_URL],
1819+
"http://REDACTED:REDACTED@mock/status/200?X-Goog-Signature=REDACTED",
18191820
)
18201821

18211822
def test_collect_target_attribute_missing(self):

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,10 @@ def test_remove_sensitive_params(self):
13061306
self.perform_request(new_url)
13071307
span = self.assert_span()
13081308

1309-
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], "http://REDACTED:REDACTED@mock/status/200?sig=REDACTED")
1309+
self.assertEqual(
1310+
span.attributes[SpanAttributes.HTTP_URL],
1311+
"http://REDACTED:REDACTED@mock/status/200?sig=REDACTED",
1312+
)
13101313

13111314

13121315
class TestAsyncIntegration(BaseTestCases.BaseManualTest):
@@ -1378,7 +1381,10 @@ def test_remove_sensitive_params(self):
13781381
self.perform_request(new_url)
13791382
span = self.assert_span()
13801383

1381-
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], "http://REDACTED:REDACTED@mock/status/200?Signature=REDACTED")
1384+
self.assertEqual(
1385+
span.attributes[SpanAttributes.HTTP_URL],
1386+
"http://REDACTED:REDACTED@mock/status/200?Signature=REDACTED",
1387+
)
13821388

13831389

13841390
class TestSyncInstrumentationIntegration(BaseTestCases.BaseInstrumentorTest):

instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,16 @@ def perform_request(url: str, session: requests.Session = None):
687687
return session.get(url)
688688

689689
def test_remove_sensitive_params(self):
690-
new_url = "http://username:password@mock/status/200?AWSAccessKeyId=secret"
690+
new_url = (
691+
"http://username:password@mock/status/200?AWSAccessKeyId=secret"
692+
)
691693
self.perform_request(new_url)
692694
span = self.assert_span()
693695

694-
self.assertEqual(span.attributes[HTTP_URL], "http://REDACTED:REDACTED@mock/status/200?AWSAccessKeyId=REDACTED")
696+
self.assertEqual(
697+
span.attributes[HTTP_URL],
698+
"http://REDACTED:REDACTED@mock/status/200?AWSAccessKeyId=REDACTED",
699+
)
695700

696701
def test_if_headers_equals_none(self):
697702
result = requests.get(self.URL, headers=None, timeout=5)

instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,10 @@ def test_remove_sensitive_params(self):
519519
self.perform_request(url)
520520

521521
span = self.assert_span()
522-
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], "http://REDACTED:REDACTED@mock/status/200")
522+
self.assertEqual(
523+
span.attributes[SpanAttributes.HTTP_URL],
524+
"http://REDACTED:REDACTED@mock/status/200",
525+
)
523526

524527
def test_hooks(self):
525528
def request_hook(span, request_obj):

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ def collect_request_attributes(
371371
else:
372372
# old semconv v1.20.0
373373
if _report_old(sem_conv_opt_in_mode):
374-
result[HTTP_URL] = redact_url(
375-
wsgiref_util.request_uri(environ)
376-
)
374+
result[HTTP_URL] = redact_url(wsgiref_util.request_uri(environ))
377375

378376
remote_addr = environ.get("REMOTE_ADDR")
379377
if remote_addr:

util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from re import compile as re_compile
2121
from re import search
2222
from typing import Callable, Iterable, overload
23-
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
23+
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
2424

2525
from opentelemetry.semconv.trace import SpanAttributes
2626

@@ -60,6 +60,7 @@
6060

6161
PARAMS_TO_REDACT = ["AWSAccessKeyId", "Signature", "sig", "X-Goog-Signature"]
6262

63+
6364
class ExcludeList:
6465
"""Class to exclude certain paths (given as a list of regexes) from tracing requests"""
6566

@@ -160,11 +161,11 @@ def parse_excluded_urls(excluded_urls: str) -> ExcludeList:
160161

161162

162163
def remove_url_credentials(url: str) -> str:
163-
""" Given a string url, replace the username and password with the keyword "REDACTED "only if it is a valid url"""
164+
"""Given a string url, replace the username and password with the keyword "REDACTED "only if it is a valid url"""
164165
try:
165166
parsed = urlparse(url)
166167
if all([parsed.scheme, parsed.netloc]): # checks for valid url
167-
if '@' in parsed.netloc:
168+
if "@" in parsed.netloc:
168169
_, _, host = parsed.netloc.rpartition("@")
169170
new_netloc = "REDACTED:REDACTED@" + host
170171
return urlunparse(
@@ -257,6 +258,7 @@ def _parse_url_query(url: str):
257258
query_params = parsed_url.query
258259
return path, query_params
259260

261+
260262
def redact_query_parameters(url: str) -> str:
261263
"""Given a string url, redact sensitive query parameter values"""
262264
try:
@@ -282,8 +284,9 @@ def redact_query_parameters(url: str) -> str:
282284
except ValueError: # an unparsable url was passed
283285
return url
284286

287+
285288
def redact_url(url: str) -> str:
286289
"""Redact sensitive data from the URL, including credentials and query parameters."""
287290
url = remove_url_credentials(url)
288291
url = redact_query_parameters(url)
289-
return url
292+
return url

util/opentelemetry-util-http/tests/test_redact_query_parameters.py

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,86 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
1617
from opentelemetry.util.http import redact_query_parameters
1718

19+
1820
class TestRedactSensitiveInfo(unittest.TestCase):
1921
def test_redact_goog_signature(self):
2022
url = "https://www.example.com/path?color=blue&X-Goog-Signature=secret"
21-
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&X-Goog-Signature=REDACTED")
22-
23+
self.assertEqual(
24+
redact_query_parameters(url),
25+
"https://www.example.com/path?color=blue&X-Goog-Signature=REDACTED",
26+
)
27+
2328
def test_no_redaction_needed(self):
2429
url = "https://www.example.com/path?color=blue&query=secret"
25-
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&query=secret")
26-
30+
self.assertEqual(
31+
redact_query_parameters(url),
32+
"https://www.example.com/path?color=blue&query=secret",
33+
)
34+
2735
def test_no_query_parameters(self):
2836
url = "https://www.example.com/path"
29-
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path")
30-
37+
self.assertEqual(
38+
redact_query_parameters(url), "https://www.example.com/path"
39+
)
40+
3141
def test_empty_query_string(self):
3242
url = "https://www.example.com/path?"
33-
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?")
34-
43+
self.assertEqual(
44+
redact_query_parameters(url), "https://www.example.com/path?"
45+
)
46+
3547
def test_empty_url(self):
3648
url = ""
3749
self.assertEqual(redact_query_parameters(url), "")
38-
50+
3951
def test_redact_aws_access_key_id(self):
40-
url = "https://www.example.com/path?color=blue&AWSAccessKeyId=secrets"
41-
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&AWSAccessKeyId=REDACTED")
42-
52+
url = "https://www.example.com/path?color=blue&AWSAccessKeyId=secrets"
53+
self.assertEqual(
54+
redact_query_parameters(url),
55+
"https://www.example.com/path?color=blue&AWSAccessKeyId=REDACTED",
56+
)
57+
4358
def test_api_key_not_in_redact_list(self):
4459
url = "https://www.example.com/path?api_key=secret%20key&user=john"
45-
self.assertNotEqual(redact_query_parameters(url), "https://www.example.com/path?api_key=REDACTED&user=john")
46-
60+
self.assertNotEqual(
61+
redact_query_parameters(url),
62+
"https://www.example.com/path?api_key=REDACTED&user=john",
63+
)
64+
4765
def test_password_key_not_in_redact_list(self):
4866
url = "https://api.example.com?key=abc&password=123&user=admin"
49-
self.assertNotEqual(redact_query_parameters(url), "https://api.example.com?key=REDACTED&password=REDACTED&user=admin")
50-
67+
self.assertNotEqual(
68+
redact_query_parameters(url),
69+
"https://api.example.com?key=REDACTED&password=REDACTED&user=admin",
70+
)
71+
5172
def test_url_with_at_symbol_in_path_and_query(self):
5273
url = "https://github.com/p@th?foo=b@r"
53-
self.assertEqual(redact_query_parameters(url), "https://github.com/p@th?foo=b@r")
54-
74+
self.assertEqual(
75+
redact_query_parameters(url), "https://github.com/p@th?foo=b@r"
76+
)
77+
5578
def test_aws_access_key_with_real_format(self):
56-
url = "https://microsoft.com?AWSAccessKeyId=AKIAIOSFODNN7"
57-
self.assertEqual(redact_query_parameters(url), "https://microsoft.com?AWSAccessKeyId=REDACTED")
58-
79+
url = "https://microsoft.com?AWSAccessKeyId=AKIAIOSFODNN7"
80+
self.assertEqual(
81+
redact_query_parameters(url),
82+
"https://microsoft.com?AWSAccessKeyId=REDACTED",
83+
)
84+
5985
def test_signature_parameter(self):
60-
url = "https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0"
61-
self.assertEqual(redact_query_parameters(url), "https://service.com?sig=REDACTED")
62-
86+
url = (
87+
"https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0"
88+
)
89+
self.assertEqual(
90+
redact_query_parameters(url), "https://service.com?sig=REDACTED"
91+
)
92+
6393
def test_signature_with_url_encoding(self):
64-
url = "https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377"
65-
self.assertEqual(redact_query_parameters(url), "https://service.com?Signature=REDACTED")
94+
url = "https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377"
95+
self.assertEqual(
96+
redact_query_parameters(url),
97+
"https://service.com?Signature=REDACTED",
98+
)

util/opentelemetry-util-http/tests/test_redact_url.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,52 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
1617
from opentelemetry.util.http import redact_url
1718

19+
1820
class TestRedactUrl(unittest.TestCase):
1921
def test_redact_both_credentials_and_query_params(self):
2022
"""Test URL with both credentials and sensitive query parameters."""
2123
url = "https://user:[email protected]/data?AWSAccessKeyId=AKIAIOSFODNN7&color=blue"
2224
expected = "https://REDACTED:[email protected]/data?AWSAccessKeyId=REDACTED&color=blue"
2325
self.assertEqual(redact_url(url), expected)
24-
26+
2527
def test_multiple_sensitive_query_params(self):
2628
"""Test URL with multiple sensitive query parameters."""
2729
url = "https://admin:[email protected]/secure?Signature=abc123&X-Goog-Signature=xyz789&sig=def456"
2830
expected = "https://REDACTED:[email protected]/secure?Signature=REDACTED&X-Goog-Signature=REDACTED&sig=REDACTED"
2931
self.assertEqual(redact_url(url), expected)
30-
32+
3133
def test_url_with_special_characters(self):
3234
"""Test URL with special characters in both credentials and query parameters."""
3335
url = "https://user@domain:p@[email protected]/path?Signature=s%40me+special%20chars&normal=fine"
3436
expected = "https://REDACTED:[email protected]/path?Signature=REDACTED&normal=fine"
3537
self.assertEqual(redact_url(url), expected)
36-
38+
3739
def test_edge_cases(self):
3840
"""Test unusual URL formats and corner cases."""
3941
# URL with fragment
40-
url1 = "https://user:[email protected]/data?Signature=secret#section"
41-
self.assertEqual(redact_url(url1), "https://REDACTED:[email protected]/data?Signature=REDACTED#section")
42-
42+
url1 = (
43+
"https://user:[email protected]/data?Signature=secret#section"
44+
)
45+
self.assertEqual(
46+
redact_url(url1),
47+
"https://REDACTED:[email protected]/data?Signature=REDACTED#section",
48+
)
49+
4350
# URL with port number
44-
url2 = "https://user:[email protected]:8443/data?AWSAccessKeyId=secret"
45-
self.assertEqual(redact_url(url2), "https://REDACTED:[email protected]:8443/data?AWSAccessKeyId=REDACTED")
46-
51+
url2 = (
52+
"https://user:[email protected]:8443/data?AWSAccessKeyId=secret"
53+
)
54+
self.assertEqual(
55+
redact_url(url2),
56+
"https://REDACTED:[email protected]:8443/data?AWSAccessKeyId=REDACTED",
57+
)
58+
4759
# URL with IP address instead of domain
4860
url3 = "https://user:[email protected]/path?X-Goog-Signature=xyz"
49-
self.assertEqual(redact_url(url3), "https://REDACTED:[email protected]/path?X-Goog-Signature=REDACTED")
61+
self.assertEqual(
62+
redact_url(url3),
63+
"https://REDACTED:[email protected]/path?X-Goog-Signature=REDACTED",
64+
)

0 commit comments

Comments
 (0)