Skip to content

Commit

Permalink
Preserve brackets around literal IPv6 hosts (#2552)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hutchinson authored May 24, 2024
1 parent e640956 commit 65b4f85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2420))
- `opentelemetry-instrumentation-asyncio` Check for __name__ attribute in the coroutine
([#2521](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2521))
- `opentelemetry-util-http` Preserve brackets around literal IPv6 hosts ([#2552](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2552))

## Version 1.24.0/0.45b0 (2024-03-28)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ def remove_url_credentials(url: str) -> str:
parsed = urlparse(url)
if all([parsed.scheme, parsed.netloc]): # checks for valid url
parsed_url = urlparse(url)
netloc = (
(":".join(((parsed_url.hostname or ""), str(parsed_url.port))))
if parsed_url.port
else (parsed_url.hostname or "")
)
_, _, netloc = parsed.netloc.rpartition("@")
return urlunparse(
(
parsed_url.scheme,
Expand Down
27 changes: 27 additions & 0 deletions util/opentelemetry-util-http/tests/test_remove_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest

from opentelemetry.util.http import remove_url_credentials


class TestRemoveUrlCredentials(unittest.TestCase):
def test_remove_no_credentials(self):
url = "http://opentelemetry.io:8080/test/path?query=value"
cleaned_url = remove_url_credentials(url)
self.assertEqual(cleaned_url, url)

def test_remove_credentials(self):
url = "http://someuser:[email protected]:8080/test/path?query=value"
cleaned_url = remove_url_credentials(url)
self.assertEqual(
cleaned_url, "http://opentelemetry.io:8080/test/path?query=value"
)

def test_remove_credentials_ipv4_literal(self):
url = "http://someuser:[email protected]:8080/test/path?query=value"
cleaned_url = remove_url_credentials(url)
self.assertEqual(cleaned_url, "http://127.0.0.1:8080/test/path?query=value")

def test_remove_credentials_ipv6_literal(self):
url = "http://someuser:somepass@[::1]:8080/test/path?query=value"
cleaned_url = remove_url_credentials(url)
self.assertEqual(cleaned_url, "http://[::1]:8080/test/path?query=value")

0 comments on commit 65b4f85

Please sign in to comment.