Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions faststream/nats/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,18 @@ def __init__(
nkeys_seed_str=nkeys_seed_str,
)

parsed_servers = [
urlparse(server if "://" in server else f"//{server}") for server in servers
]

if (
security is not None
and type(security)
not in {
BaseSecurity,
NatsSecurity,
}
and any(
urlparse(url if "://" in url else f"//{url}").username is not None
for url in servers
)
and any(parsed.username is not None for parsed in parsed_servers)
):
msg = "URL credentials conflict with `security`."
raise SetupError(msg)
Expand All @@ -590,7 +591,10 @@ def __init__(
else:
specification_url = list(specification_url)
else:
specification_url = servers
specification_url = [
server.replace(parsed.netloc, parsed.netloc.rpartition("@")[-1], 1)
for server, parsed in zip(servers, parsed_servers, strict=True)
]

js_producer = NatsJSFastProducer(
parser=parser,
Expand Down
2 changes: 1 addition & 1 deletion faststream/rabbit/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(
)

if specification_url is None:
specification_url = str(amqp_url)
specification_url = str(amqp_url.with_user(None))

# respect ascynapi_url argument scheme
built_asyncapi_url = urlparse(specification_url)
Expand Down
3 changes: 2 additions & 1 deletion faststream/redis/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def _init_broker(self, url: str, kwargs: dict[str, Any]) -> None:
self.message_format = message_format

if specification_url is None:
specification_url = url
netloc = urlparse(url).netloc
specification_url = url.replace(netloc, netloc.rpartition("@")[-1], 1)
if protocol is None:
protocol = urlparse(specification_url).scheme
if retry_on_error is None:
Expand Down
30 changes: 30 additions & 0 deletions tests/asyncapi/nats/v3_0_0/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ def test_base() -> None:
}, schema


@pytest.mark.nats()
@pytest.mark.parametrize(
("servers", "expected"),
(
pytest.param(
["nats://user:password@host1:4222", "admin:secret@host2:4222"],
["nats://host1:4222", "host2:4222"],
id="schemed and scheme-less",
),
pytest.param(
["nats://user:password@[::1]:4222"],
["nats://[::1]:4222"],
id="ipv6 keeps its brackets",
),
pytest.param(
["nats://mytoken@localhost:4222"],
["nats://localhost:4222"],
id="token without a password",
),
),
)
def test_credentials_stripped(
servers: list[str],
expected: list[str],
) -> None:
broker = NatsBroker(servers)

assert broker.specification.url == expected


@pytest.mark.nats()
def test_multi() -> None:
broker = NatsBroker(["nats:9092", "nats:9093"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"name": "some-tag"
}
],
"url": "amqps://guest:guest@localhost:5673/"
"url": "amqps://localhost:5673/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"user-password": []
}
],
"url": "amqp://user:pass@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/vhost"
"url": "amqp://localhost:5672/vhost"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/vhost"
"url": "amqp://localhost:5672/vhost"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"development": {
"protocol": "amqp",
"protocolVersion": "0.9.1",
"url": "amqp://guest:guest@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"protocol": "amqps",
"protocolVersion": "0.9.1",
"security": [],
"url": "amqps://guest:guest@localhost:5672/"
"url": "amqps://localhost:5672/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"user-password": []
}
],
"url": "amqps://admin:password@localhost:5671/"
"url": "amqps://localhost:5671/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"user-password": []
}
],
"url": "amqp://admin:password@localhost:5672/"
"url": "amqp://localhost:5672/"
}
}
}
2 changes: 1 addition & 1 deletion tests/asyncapi/rabbit/v2_6_0/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_kwargs() -> None:
)

assert broker.specification.url == [
"amqp://guest:guest@127.0.0.1:5672/?heartbeat=300",
"amqp://127.0.0.1:5672/?heartbeat=300",
]


Expand Down
6 changes: 3 additions & 3 deletions tests/asyncapi/rabbit/v2_6_0/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_base_security_schema(snapshot_json: SnapshotAssertion) -> None:

broker = RabbitBroker("amqp://guest:guest@localhost:5672/", security=security)

assert broker.specification.url == ["amqps://guest:guest@localhost:5672/"]
assert broker.specification.url == ["amqps://localhost:5672/"]
assert broker._connection_kwargs.get("ssl_context") is ssl_context

schema = get_2_6_0_schema(broker)
Expand All @@ -38,7 +38,7 @@ def test_plaintext_security_schema(snapshot_json: SnapshotAssertion) -> None:

broker = RabbitBroker("amqp://guest:guest@localhost/", security=security)

assert broker.specification.url == ["amqps://admin:password@localhost:5671/"]
assert broker.specification.url == ["amqps://localhost:5671/"]
assert broker._connection_kwargs.get("ssl_context") is ssl_context

schema = get_2_6_0_schema(broker)
Expand All @@ -55,7 +55,7 @@ def test_plaintext_security_schema_without_ssl(snapshot_json: SnapshotAssertion)

broker = RabbitBroker("amqp://guest:guest@localhost:5672/", security=security)

assert broker.specification.url == ["amqp://admin:password@localhost:5672/"]
assert broker.specification.url == ["amqp://localhost:5672/"]

schema = get_2_6_0_schema(broker)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"servers": {
"development": {
"description": "Test description",
"host": "guest:guest@localhost:5673",
"host": "localhost:5673",
"pathname": "/",
"protocol": "amqps",
"protocolVersion": "0.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"operations": {},
"servers": {
"development": {
"host": "user:pass@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/vhost",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/vhost",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"servers": {
"development": {
"host": "guest:guest@localhost:5672",
"host": "localhost:5672",
"pathname": "/",
"protocol": "amqp",
"protocolVersion": "0.9.1"
Expand Down
Loading
Loading