public static MqttConnectionSettings FromFileMount()

in dotnet/src/Azure.Iot.Operations.Protocol/Connection/MqttConnectionSettings.cs [163:248]


        public static MqttConnectionSettings FromFileMount()
        {
            string configMapPath = Environment.GetEnvironmentVariable("AEP_CONFIGMAP_MOUNT_PATH")
                ?? throw new InvalidOperationException("AEP_CONFIGMAP_MOUNT_PATH is not set.");

            string? targetAddress;
            bool useTls;
            string? satMountPath = string.Empty;
            string? tlsCaCertMountPath = string.Empty;
            int port;

            try
            {
                string targetAddressAndPort = File.ReadAllText(configMapPath + "/BROKER_TARGET_ADDRESS");
                if (string.IsNullOrEmpty(targetAddressAndPort))
                {
                    throw new ArgumentException("BROKER_TARGET_ADDRESS is missing.");
                }

                try
                {
                    string[] targetAddressParts = targetAddressAndPort.Split(":");
                    targetAddress = targetAddressParts[0];
                    port = int.Parse(targetAddressParts[1], CultureInfo.InvariantCulture);
                }
                catch (Exception e)
                {
                    throw new ArgumentException($"BROKER_TARGET_ADDRESS is malformed. Cannot parse MQTT port from BROKER_TARGET_ADDRESS. Expected format <hostname>:<port>. Found: {targetAddressAndPort}", e);
                }
            }
            catch (Exception ex)
            {
                throw AkriMqttException.GetConfigurationInvalidException("BROKER_TARGET_ADDRESS", string.Empty, "Missing or malformed target address configuration file", ex);
            }

            string? useTlsString = File.ReadAllText(configMapPath + "/BROKER_USE_TLS");
            if (string.IsNullOrWhiteSpace(useTlsString) || !bool.TryParse(useTlsString, out useTls))
            {
                throw AkriMqttException.GetConfigurationInvalidException("BROKER_USE_TLS", string.Empty, "BROKER_USE_TLS not set or contains a value that could not be parsed as a boolean.");
            }

            // Optional field, so no need to validate that this file exists
            satMountPath = Environment.GetEnvironmentVariable("BROKER_SAT_MOUNT_PATH");

            X509Certificate2Collection chain = [];
            tlsCaCertMountPath = Environment.GetEnvironmentVariable("BROKER_TLS_TRUST_BUNDLE_CACERT_MOUNT_PATH");

            if (!string.IsNullOrWhiteSpace(tlsCaCertMountPath))
            {
                if (!Directory.Exists(tlsCaCertMountPath))
                {
                    throw AkriMqttException.GetConfigurationInvalidException("BROKER_TLS_TRUST_BUNDLE_CACERT_MOUNT_PATH", string.Empty, "A TLS cert mount path was provided, but the provided path does not exist. Path: " + tlsCaCertMountPath);
                }

                foreach (string caFilePath in Directory.EnumerateFiles(tlsCaCertMountPath))
                {
                    chain.ImportFromPemFile(caFilePath);
                }
            }

            string clientId = Guid.NewGuid().ToString();

            try
            {
                return new MqttConnectionSettings(targetAddress, clientId)
                {
                    UseTls = useTls,
                    SatAuthFile = satMountPath,
                    TrustChain = chain,
                    TcpPort = port
                };
            }
            catch (ArgumentException ex)
            {
                string? paramValue = ex.ParamName switch
                {
                    nameof(targetAddress) => targetAddress,
                    nameof(useTls) => useTls.ToString(),
                    nameof(satMountPath) => satMountPath,
                    nameof(tlsCaCertMountPath) => tlsCaCertMountPath,
                    _ => string.Empty
                };

                throw AkriMqttException.GetConfigurationInvalidException(ex.ParamName!, paramValue ?? string.Empty, "Invalid settings in provided configuration files: " + ex.Message, ex);
            }
        }