From cb1cda98b02fe2195af50168f4bcb22b80880521 Mon Sep 17 00:00:00 2001 From: Brian Crawford Date: Tue, 5 Mar 2024 14:41:14 -0800 Subject: [PATCH] Fix CreateFromEnvVars to allow '=' in env var value. --- .../MqttConnectionSettingsFixture.cs | 14 ++++++++++---- .../all_settings.txt | 2 +- .../MqttConnectionSettings.cs | 13 ++++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/MqttConnectionSettingsFixture.cs b/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/MqttConnectionSettingsFixture.cs index 320df78d..46e84a39 100644 --- a/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/MqttConnectionSettingsFixture.cs +++ b/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/MqttConnectionSettingsFixture.cs @@ -94,7 +94,7 @@ public void CreateFromEnvFile_WithAllSettings() Assert.False(cs.CleanSession); Assert.Equal(32, cs.KeepAliveInSeconds); Assert.Equal("sample_client", cs.ClientId); - Assert.Equal("sample_user", cs.Username); + Assert.Equal("sample_user/api-version=2024-01-01", cs.Username); Assert.Equal("foo", cs.Password); Assert.Equal("ca.pem", cs.CaFile); Assert.Equal("cert.pem", cs.CertFile); @@ -132,13 +132,19 @@ private void RemoveTestEnvVars(string envFile) { foreach (var line in File.ReadAllLines(envFile)) { - var parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries); - if (parts.Length != 2) + int index = line.IndexOf('='); + if (index < 0) { continue; } - Environment.SetEnvironmentVariable(parts[0], null); + string key = line[..index].Trim(); + if (string.IsNullOrEmpty(key)) + { + continue; + } + + Environment.SetEnvironmentVariable(key, null); } } } diff --git a/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/all_settings.txt b/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/all_settings.txt index 0973eb41..b2f6fe42 100644 --- a/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/all_settings.txt +++ b/mqttclients/dotnet/MQTTnet.Client.Extensions.Tests/all_settings.txt @@ -4,7 +4,7 @@ MQTT_USE_TLS=false MQTT_CLEAN_SESSION=false MQTT_KEEP_ALIVE_IN_SECONDS=32 MQTT_CLIENT_ID=sample_client -MQTT_USERNAME=sample_user +MQTT_USERNAME=sample_user/api-version=2024-01-01 MQTT_PASSWORD=foo MQTT_CA_FILE=ca.pem MQTT_CERT_FILE=cert.pem diff --git a/mqttclients/dotnet/MQTTnet.Client.Extensions/MqttConnectionSettings.cs b/mqttclients/dotnet/MQTTnet.Client.Extensions/MqttConnectionSettings.cs index 8a731010..2e44da21 100644 --- a/mqttclients/dotnet/MQTTnet.Client.Extensions/MqttConnectionSettings.cs +++ b/mqttclients/dotnet/MQTTnet.Client.Extensions/MqttConnectionSettings.cs @@ -61,13 +61,20 @@ public static MqttConnectionSettings CreateFromEnvVars(string? envFile = "") Trace.TraceInformation("Loading environment variables from {envFile}" + new FileInfo(envFile).FullName); foreach (string line in File.ReadAllLines(envFile)) { - string[] parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries); - if (parts.Length != 2) + int index = line.IndexOf('='); + if (index < 0) { continue; } - Environment.SetEnvironmentVariable(parts[0], parts[1]); + string key = line[..index].Trim(); + string value = line[(index + 1)..].Trim(); + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(key)) + { + continue; + } + + Environment.SetEnvironmentVariable(key, value); } } else