/// <summary>
        /// The settings provider used to bridge Service Fabric configuration information and DotNetty
        /// </summary>
        /// <param name="traceId">The unique identifier used to correlate debugging and diagnostics messages</param>
        /// <param name="componentName">The component name used for debug and diagnostics messages</param>
        /// <param name="logger">The service fabric logger to be used when writing debug and diagnostics information</param>
        /// <param name="gatewayConfiguration">The gateway configuration used to get current configuration information for DotNetty</param>
        /// <param name="iotHubConfiguration">The IotHub Client configuration used to get current configuration information for DotNetty</param>
        /// <param name="mqttConfiguration">The MQTT configuration used to get current configuration information for DotNetty</param>
        public ServiceFabricConfigurationProvider(Guid traceId, string componentName, IServiceLogger logger, GatewayConfiguration gatewayConfiguration, IoTHubConfiguration iotHubConfiguration, MqttServiceConfiguration mqttConfiguration)
        {
            this.componentName = componentName;
            this.logger = logger;

            this.logger.Verbose(traceId, this.componentName, "Initializing configuration provider.");

            var baseProperties = new Dictionary<string, string>();

            foreach (PropertyInfo element in typeof(GatewayConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(gatewayConfiguration, null);
                    baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString());
                }
            }

            foreach (PropertyInfo element in typeof(MqttServiceConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(mqttConfiguration, null);
                    baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString());
                }
            }

            foreach (PropertyInfo element in typeof(IoTHubConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(iotHubConfiguration, null);
                    baseProperties.Add($"IotHubClient.{element.Name}", value?.ToString());
                }
            }



            this.configurationValues = baseProperties.ToImmutableDictionary();
            this.logger.Informational(traceId, this.componentName, "Initializing configuration provider complete.");
        }
Esempio n. 2
0
        /// <summary>
        /// The settings provider used to bridge Service Fabric configuration information and DotNetty
        /// </summary>
        /// <param name="traceId">The unique identifier used to correlate debugging and diagnostics messages</param>
        /// <param name="componentName">The component name used for debug and diagnostics messages</param>
        /// <param name="logger">The service fabric logger to be used when writing debug and diagnostics information</param>
        /// <param name="gatewayConfiguration">The gateway configuration used to get current configuration information for DotNetty</param>
        /// <param name="iotHubConfiguration">The IotHub Client configuration used to get current configuration information for DotNetty</param>
        /// <param name="mqttConfiguration">The MQTT configuration used to get current configuration information for DotNetty</param>
        public ServiceFabricConfigurationProvider(Guid traceId, string componentName, IServiceLogger logger, GatewayConfiguration gatewayConfiguration, IoTHubConfiguration iotHubConfiguration, MqttServiceConfiguration mqttConfiguration)
        {
            this.componentName = componentName;
            this.logger        = logger;

            this.logger.Verbose(traceId, this.componentName, "Initializing configuration provider.");

            var baseProperties = new Dictionary <string, string>();

            foreach (PropertyInfo element in typeof(GatewayConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(gatewayConfiguration, null);
                    baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString());
                }
            }

            foreach (PropertyInfo element in typeof(MqttServiceConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(mqttConfiguration, null);
                    baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString());
                }
            }

            foreach (PropertyInfo element in typeof(IoTHubConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!element.PropertyType.IsArray)
                {
                    object value = element.GetValue(iotHubConfiguration, null);
                    baseProperties.Add($"IotHubClient.{element.Name}", value?.ToString());
                }
            }



            this.configurationValues = baseProperties.ToImmutableDictionary();
            this.logger.Informational(traceId, this.componentName, "Initializing configuration provider complete.");
        }
        /// <summary>
        /// A common MQTT address building method
        /// </summary>
        /// <param name="gatewayConfiguration">The Gateway configuration to use when building the address</param>
        /// <param name="hostName">The name of the host to use</param>
        /// <returns>A MQTT URI for the requested host and configuration</returns>
        string BuildAddress(GatewayConfiguration gatewayConfiguration, string hostName)
        {
            EndpointResourceDescription description = this.serviceContext.CodePackageActivationContext.GetEndpoint(gatewayConfiguration.EndPointName);

            return $"mqtts://{ hostName }:{ description.Port }";
        }
 /// <summary>
 /// Retrieves the address that the MQTT listener should publish to service fabric
 /// </summary>
 /// <param name="gatewayConfiguration">The Gateway configuration to use when building the address</param>
 /// <returns>A service fabric publication MQTT URI for the requested host and configuration</returns>
 string BuildPublishAddress(GatewayConfiguration gatewayConfiguration)
 {
     return this.BuildAddress(gatewayConfiguration, FabricRuntime.GetNodeContext().IPAddressOrFQDN);
 }
 /// <summary>
 /// Retrieves the address that the MQTT listener should listen on
 /// </summary>
 /// <param name="gatewayConfiguration">The Gateway configuration to use when building the address</param>
 /// <returns>A service fabric listening MQTT URI for the requested host and configuration</returns>
 string BuildListeningAddress(GatewayConfiguration gatewayConfiguration)
 {
     return this.BuildAddress(gatewayConfiguration, "+");
 }
        /// <summary>
        /// Retrieves the X509 Certificate used for the server side of TLS
        /// </summary>
        /// <param name="configuration">The gateway configuration</param>
        /// <returns>An X509 Certificate if available</returns>
        X509Certificate2 GetServerCertificate(GatewayConfiguration configuration)
        {
            X509Certificate2 certificate = null;

            switch (configuration.X509Location)
            {
                case GatewayConfiguration.CertificateLocation.Data:
                    string certificateFile = Path.Combine(this.serviceContext.CodePackageActivationContext.GetDataPackageObject("Data").Path, configuration.X509Identifier);
                    certificate = CertificateUtilities.GetCertificateFromFile(certificateFile, configuration.X509Credential);
                    break;

                case GatewayConfiguration.CertificateLocation.KeyVault:
                    //    certificate = CertificateUtilities.GetCertificateFromKeyVault(certificateFile, this.configuration.X509Credential);
                    throw new NotImplementedException();


                case GatewayConfiguration.CertificateLocation.LocalStore:
                    certificate = CertificateUtilities.GetCertificate(configuration.X509Identifier, StoreName.My, StoreLocation.LocalMachine);
                    break;
            }

            return certificate;
        }