mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-23 23:10:50 +00:00
mqtt: keep commas that are part of a single broker URI
Splitting the MQTT URI on every comma broke previously valid single-broker
URIs with commas in the query string, credentials or path (for example
wss://broker/mqtt?token=a,b). A comma now only separates brokers when the
text after it starts with a URI scheme ("scheme://"); other commas stay in
the current broker URI. Adds regression cases for these URIs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7d70361870
commit
33cc8a46ed
@@ -14,6 +14,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -67,6 +68,11 @@ func init() {
|
||||
// and a plain comma-separated list of broker URIs. Transport options after
|
||||
// the closing parenthesis (e.g. "?randomize=false") are ignored: paho tries
|
||||
// the brokers in the order they are listed, on connect and on reconnect.
|
||||
//
|
||||
// A comma only separates brokers when the text after it starts with a URI
|
||||
// scheme ("scheme://"). Any other comma belongs to the current broker URI,
|
||||
// so existing single-broker URIs with commas in their credentials, path or
|
||||
// query string (e.g. "wss://broker/mqtt?token=a,b") keep working.
|
||||
func parseMQTTBrokers(raw string) []string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if len(raw) >= len("failover:") && strings.EqualFold(raw[:len("failover:")], "failover:") {
|
||||
@@ -80,8 +86,17 @@ func parseMQTTBrokers(raw string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
segments := []string{}
|
||||
for _, segment := range strings.Split(raw, ",") {
|
||||
if len(segments) > 0 && !mqttBrokerSchemePattern.MatchString(strings.TrimSpace(segment)) {
|
||||
segments[len(segments)-1] += "," + segment
|
||||
continue
|
||||
}
|
||||
segments = append(segments, segment)
|
||||
}
|
||||
|
||||
brokers := []string{}
|
||||
for _, broker := range strings.Split(raw, ",") {
|
||||
for _, broker := range segments {
|
||||
if broker = strings.TrimSpace(broker); broker != "" {
|
||||
brokers = append(brokers, broker)
|
||||
}
|
||||
@@ -89,6 +104,9 @@ func parseMQTTBrokers(raw string) []string {
|
||||
return brokers
|
||||
}
|
||||
|
||||
// mqttBrokerSchemePattern matches the start of a URI with a scheme (RFC 3986).
|
||||
var mqttBrokerSchemePattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.-]*://`)
|
||||
|
||||
// enableMQTTConnectionDiagnostics installs our instrumented TCP/TLS dialer.
|
||||
// The custom dialer is used for every broker, so it is only installed when
|
||||
// all brokers use a TCP-based scheme (WebSocket brokers keep paho's dialer).
|
||||
|
||||
@@ -230,6 +230,15 @@ func TestParseMQTTBrokers(t *testing.T) {
|
||||
},
|
||||
{name: "failover without parentheses", raw: "failover:tcp://b-1.example:1883", want: []string{"tcp://b-1.example:1883"}},
|
||||
{name: "comma separated", raw: "tcp://b-1.example:1883,tcp://b-2.example:1883", want: []string{"tcp://b-1.example:1883", "tcp://b-2.example:1883"}},
|
||||
{name: "comma in query string", raw: "wss://broker.example/mqtt?token=a,b", want: []string{"wss://broker.example/mqtt?token=a,b"}},
|
||||
{name: "comma in password", raw: "mqtt+ssl://user:p,ss@broker.example:8883", want: []string{"mqtt+ssl://user:p,ss@broker.example:8883"}},
|
||||
{name: "comma in path", raw: "wss://broker.example/a,b/mqtt", want: []string{"wss://broker.example/a,b/mqtt"}},
|
||||
{name: "comma followed by text that looks like a scheme without slashes", raw: "wss://broker.example/mqtt?list=a,b:c", want: []string{"wss://broker.example/mqtt?list=a,b:c"}},
|
||||
{
|
||||
name: "failover with commas inside a broker URI",
|
||||
raw: "failover:(wss://b-1.example/mqtt?token=a,b,wss://b-2.example/mqtt?token=c,d)",
|
||||
want: []string{"wss://b-1.example/mqtt?token=a,b", "wss://b-2.example/mqtt?token=c,d"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user