/// <summary>
        /// Creates an appropriatly configured HttpClient that will be used in communicating with the
        /// Spring Cloud Configuration Server
        /// </summary>
        /// <param name="settings">the settings used in configuring the HttpClient</param>
        /// <returns>The HttpClient used by the provider</returns>
        protected static HttpClient GetHttpClient(ConfigServerClientSettings settings)
        {
            var clientHandler = new HttpClientHandler();

            if (settings.ClientCertificate != null)
            {
                clientHandler.ClientCertificates.Add(settings.ClientCertificate);
            }

            return(HttpClientHelper.GetHttpClient(settings.ValidateCertificates, clientHandler, settings.Timeout));
        }
        public static void Initialize(string configPrefix, ConfigServerClientSettings settings, IConfiguration config)
        {
            if (configPrefix == null)
            {
                throw new ArgumentNullException(nameof(configPrefix));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var clientConfigsection = config.GetSection(configPrefix);

            settings.Name                 = GetApplicationName(configPrefix, config, settings.Name);
            settings.Environment          = GetEnvironment(clientConfigsection, settings.Environment);
            settings.Label                = GetLabel(clientConfigsection, settings.Label);
            settings.Username             = GetUsername(clientConfigsection, settings.Username);
            settings.Password             = GetPassword(clientConfigsection, settings.Password);
            settings.Uri                  = GetUri(clientConfigsection, settings.Uri);
            settings.Enabled              = GetEnabled(clientConfigsection, settings.Enabled);
            settings.FailFast             = GetFailFast(clientConfigsection, settings.FailFast);
            settings.ValidateCertificates = GetCertificateValidation(clientConfigsection, settings.ValidateCertificates);
            settings.RetryEnabled         = GetRetryEnabled(clientConfigsection, settings.RetryEnabled);
            settings.RetryInitialInterval = GetRetryInitialInterval(clientConfigsection, settings.RetryInitialInterval);
            settings.RetryMaxInterval     = GetRetryMaxInterval(clientConfigsection, settings.RetryMaxInterval);
            settings.RetryMultiplier      = GetRetryMultiplier(clientConfigsection, settings.RetryMultiplier);
            settings.RetryAttempts        = GetRetryMaxAttempts(clientConfigsection, settings.RetryAttempts);
            settings.Token                = GetToken(clientConfigsection);
            settings.Timeout              = GetTimeout(clientConfigsection, settings.Timeout);
            settings.AccessTokenUri       = GetAccessTokenUri(configPrefix, config);
            settings.ClientId             = GetClientId(configPrefix, config);
            settings.ClientSecret         = GetClientSecret(configPrefix, config);
            settings.TokenRenewRate       = GetTokenRenewRate(clientConfigsection);
            settings.DisableTokenRenewal  = GetDisableTokenRenewal(clientConfigsection);
            settings.TokenTtl             = GetTokenTtl(clientConfigsection);
            settings.DiscoveryEnabled     = GetDiscoveryEnabled(clientConfigsection, settings.DiscoveryEnabled);
            settings.DiscoveryServiceId   = GetDiscoveryServiceId(clientConfigsection, settings.DiscoveryServiceId);
            settings.HealthEnabled        = GetHealthEnabled(clientConfigsection, settings.HealthEnabled);
            settings.HealthTimeToLive     = GetHealthTimeToLive(clientConfigsection, settings.HealthTimeToLive);

            // Override Config server URI
            settings.Uri = GetCloudFoundryUri(configPrefix, config, settings.Uri);
        }
示例#3
0
        public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, string environment, string applicationName, ILoggerFactory logFactory = null)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            var settings = new ConfigServerClientSettings()
            {
                Name        = applicationName ?? Assembly.GetEntryAssembly()?.GetName().Name,
                Environment = environment ?? DEFAULT_ENVIRONMENT
            };

            return(configurationBuilder.AddConfigServer(settings, logFactory));
        }
        private static IConfigurationBuilder DoAddConfigServer(IConfigurationBuilder configurationBuilder, string applicationName, string environmentName, ILoggerFactory logFactory)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            var settings = new ConfigServerClientSettings()
            {
                Name        = applicationName,
                Environment = environmentName
            };

            return(configurationBuilder.AddConfigServer(settings, logFactory));
        }
示例#5
0
        /// <summary>
        /// Initializes a new instance of <see cref="ConfigServerConfigurationProvider"/>.
        /// </summary>
        /// <param name="settings">the configuration settings the provider uses when accessing the server.</param>
        /// <param name="environment">required Hosting environment, used in establishing config server profile</param>
        /// <param name="logFactory">optional logging factory</param>
        /// </summary>
        public ConfigServerConfigurationProvider(ConfigServerClientSettings settings, IHostingEnvironment environment, ILoggerFactory logFactory = null)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            _logger      = logFactory?.CreateLogger <ConfigServerConfigurationProvider>();
            _settings    = settings;
            _client      = null;
            _environment = environment;
        }
示例#6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigServerConfigurationSource"/> class.
        /// </summary>
        /// <param name="defaultSettings">the default settings used by the Config Server client</param>
        /// <param name="sources">configuration sources used by the Config Server client. The <see cref="Configuration"/> will be built from these sources and the
        /// values will override those found in <see cref="DefaultSettings"/></param>
        /// <param name="properties">properties to be used when sources are built</param>
        /// <param name="logFactory">optional logger factory used by the client</param>
        public ConfigServerConfigurationSource(ConfigServerClientSettings defaultSettings, IList <IConfigurationSource> sources, IDictionary <string, object> properties = null, ILoggerFactory logFactory = null)
        {
            if (sources == null)
            {
                throw new ArgumentNullException(nameof(sources));
            }

            _sources = new List <IConfigurationSource>(sources);

            if (properties != null)
            {
                _properties = new Dictionary <string, object>(properties);
            }

            DefaultSettings = defaultSettings ?? throw new ArgumentNullException(nameof(defaultSettings));
            LogFactory      = logFactory;
        }
示例#7
0
        public static void Initialize(string configPrefix, ConfigServerClientSettings settings, IHostingEnvironment environment, IConfigurationRoot root)
        {
            if (configPrefix == null)
            {
                throw new ArgumentNullException(nameof(configPrefix));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }


            var clientConfigsection = root.GetSection(configPrefix);

            settings.Name                 = ResovlePlaceholders(GetApplicationName(clientConfigsection, root), root);
            settings.Environment          = ResovlePlaceholders(GetEnvironment(clientConfigsection, environment), root);
            settings.Label                = ResovlePlaceholders(GetLabel(clientConfigsection), root);
            settings.Username             = ResovlePlaceholders(GetUsername(clientConfigsection), root);
            settings.Password             = ResovlePlaceholders(GetPassword(clientConfigsection), root);
            settings.Uri                  = ResovlePlaceholders(GetUri(clientConfigsection, root, settings.Uri), root);
            settings.Enabled              = GetEnabled(clientConfigsection, root, settings.Enabled);
            settings.FailFast             = GetFailFast(clientConfigsection, root, settings.FailFast);
            settings.ValidateCertificates = GetCertificateValidation(clientConfigsection, root, settings.ValidateCertificates);
            settings.RetryEnabled         = GetRetryEnabled(clientConfigsection, root, settings.RetryEnabled);
            settings.RetryInitialInterval = GetRetryInitialInterval(clientConfigsection, root, settings.RetryInitialInterval);
            settings.RetryMaxInterval     = GetRetryMaxInterval(clientConfigsection, root, settings.RetryMaxInterval);
            settings.RetryMultiplier      = GetRetryMultiplier(clientConfigsection, root, settings.RetryMultiplier);
            settings.RetryAttempts        = GetRetryMaxAttempts(clientConfigsection, root, settings.RetryAttempts);
            settings.Token                = GetToken(clientConfigsection);
            settings.Timeout              = GetTimeout(clientConfigsection, settings.Timeout);
        }
        internal void Initialize(ConfigServerClientSettings settings, IConfiguration configuration = null, HttpClient httpClient = null, ILoggerFactory logFactory = null)
        {
            _loggerFactory = logFactory ?? new NullLoggerFactory();
            _logger        = _loggerFactory.CreateLogger <ConfigServerConfigurationProvider>();
            if (configuration != null)
            {
                _configuration    = configuration;
                _hasConfiguration = true;
            }
            else
            {
                _configuration    = new ConfigurationBuilder().Build();
                _hasConfiguration = false;
            }

            _settings   = settings;
            _httpClient = httpClient ?? GetHttpClient(_settings);

            OnSettingsChanged();
        }
        public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, IHostingEnvironment environment, ILoggerFactory logFactory = null)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            var settings = new ConfigServerClientSettings()
            {
                Name        = environment.ApplicationName,
                Environment = environment.EnvironmentName
            };

            return(configurationBuilder.AddConfigServer(settings, logFactory));
        }
        /// <summary>
        /// Creates an appropriatly configured HttpClient that will be used in communicating with the
        /// Spring Cloud Configuration Server
        /// </summary>
        /// <param name="settings">the settings used in configuring the HttpClient</param>
        /// <returns>The HttpClient used by the provider</returns>
        protected static HttpClient GetHttpClient(ConfigServerClientSettings settings)
        {
            HttpClient client = null;

#if NET452
            client = new HttpClient();
#else
            if (settings != null && !settings.ValidateCertificates)
            {
                var handler = new HttpClientHandler();
                handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
                client = new HttpClient(handler);
            }
            else
            {
                client = new HttpClient();
            }
#endif
            client.Timeout = TimeSpan.FromMilliseconds(settings.Timeout);
            return(client);
        }
示例#11
0
        /// <summary>
        /// Create the HttpRequestMessage that will be used in accessing the Spring Cloud Configuration server
        /// </summary>
        /// <param name="requestUri">the Uri used when accessing the server</param>
        /// <param name="username">username to use if required</param>
        /// <param name="password">password to use if required</param>
        /// <returns>The HttpRequestMessage built from the path</returns>
        protected internal virtual HttpRequestMessage GetRequestMessage(string requestUri, string username, string password)
        {
            HttpRequestMessage request = null;

            if (string.IsNullOrEmpty(_settings.AccessTokenUri))
            {
                request = HttpClientHelper.GetRequestMessage(HttpMethod.Get, requestUri, username, password);
            }
            else
            {
                request = HttpClientHelper.GetRequestMessage(HttpMethod.Get, requestUri, FetchAccessToken);
            }

            if (!string.IsNullOrEmpty(_settings.Token) && !ConfigServerClientSettings.IsMultiServerConfig(_settings.Uri))
            {
                RenewToken(_settings.Token);
                request.Headers.Add(TOKEN_HEADER, _settings.Token);
            }

            return(request);
        }
示例#12
0
        public static void Initialize(string configPrefix, ConfigServerClientSettings settings, IConfiguration config)
        {
            if (configPrefix == null)
            {
                throw new ArgumentNullException(nameof(configPrefix));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var clientConfigsection = config.GetSection(configPrefix);

            settings.Name                 = GetApplicationName(clientConfigsection, config, settings.Name);
            settings.Environment          = GetEnvironment(clientConfigsection, config, settings.Environment);
            settings.Label                = GetLabel(clientConfigsection, config);
            settings.Username             = GetUsername(clientConfigsection, config);
            settings.Password             = GetPassword(clientConfigsection, config);
            settings.Uri                  = GetUri(clientConfigsection, config, settings.Uri);
            settings.Enabled              = GetEnabled(clientConfigsection, config, settings.Enabled);
            settings.FailFast             = GetFailFast(clientConfigsection, config, settings.FailFast);
            settings.ValidateCertificates = GetCertificateValidation(clientConfigsection, config, settings.ValidateCertificates);
            settings.RetryEnabled         = GetRetryEnabled(clientConfigsection, config, settings.RetryEnabled);
            settings.RetryInitialInterval = GetRetryInitialInterval(clientConfigsection, config, settings.RetryInitialInterval);
            settings.RetryMaxInterval     = GetRetryMaxInterval(clientConfigsection, config, settings.RetryMaxInterval);
            settings.RetryMultiplier      = GetRetryMultiplier(clientConfigsection, config, settings.RetryMultiplier);
            settings.RetryAttempts        = GetRetryMaxAttempts(clientConfigsection, config, settings.RetryAttempts);
            settings.Token                = GetToken(clientConfigsection, config);
            settings.Timeout              = GetTimeout(clientConfigsection, config, settings.Timeout);
        }
示例#13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigServerConfigurationSource"/> class.
 /// </summary>
 /// <param name="defaultSettings">the default settings used by the Config Server client</param>
 /// <param name="configuration">configuration used by the Config Server client. Values will override those found in default settings</param>
 /// <param name="logFactory">optional logger factory used by the client</param>
 public ConfigServerConfigurationSource(ConfigServerClientSettings defaultSettings, IConfiguration configuration, ILoggerFactory logFactory = null)
 {
     Configuration   = configuration ?? throw new ArgumentNullException(nameof(configuration));
     DefaultSettings = defaultSettings ?? throw new ArgumentNullException(nameof(defaultSettings));
     LogFactory      = logFactory;
 }
示例#14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigServerConfigurationProvider"/> class.
 /// </summary>
 /// <param name="settings">the configuration settings the provider uses when accessing the server.</param>
 /// <param name="httpClient">a HttpClient the provider uses to make requests of the server.</param>
 /// <param name="logFactory">optional logging factory</param>
 public ConfigServerConfigurationProvider(ConfigServerClientSettings settings, HttpClient httpClient, ILoggerFactory logFactory = null)
 {
     _settings = settings ?? throw new ArgumentNullException(nameof(settings));
     _client   = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
     _logger   = logFactory?.CreateLogger <ConfigServerConfigurationProvider>();
 }
示例#15
0
 /// <summary>
 /// Creates an appropriatly configured HttpClient that will be used in communicating with the
 /// Spring Cloud Configuration Server
 /// </summary>
 /// <param name="settings">the settings used in configuring the HttpClient</param>
 /// <returns>The HttpClient used by the provider</returns>
 protected static HttpClient GetHttpClient(ConfigServerClientSettings settings)
 {
     return(HttpClientHelper.GetHttpClient(settings.ValidateCertificates, settings.Timeout));
 }
        internal void UpdateSettingsFromDiscovery(IList <IServiceInstance> instances, ConfigServerClientSettings settings)
        {
            StringBuilder endpoints = new StringBuilder();

            foreach (var instance in instances)
            {
                var uri      = instance.Uri.ToString();
                var metaData = instance.Metadata;
                if (metaData != null)
                {
                    if (metaData.TryGetValue("password", out string password))
                    {
                        metaData.TryGetValue("user", out string username);
                        username          = username ?? "user";
                        settings.Username = username;
                        settings.Password = password;
                    }

                    if (metaData.TryGetValue("configPath", out string path))
                    {
                        if (uri.EndsWith("/") && path.StartsWith("/"))
                        {
                            uri = uri.Substring(0, uri.Length - 1);
                        }

                        uri += path;
                    }
                }

                endpoints.Append(uri);
                endpoints.Append(',');
            }

            if (endpoints.Length > 0)
            {
                var uris = endpoints.ToString(0, endpoints.Length - 1);
                settings.Uri = uris;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigServerConfigurationProvider"/> class.
 /// </summary>
 /// <param name="settings">the configuration settings the provider uses when accessing the server.</param>
 /// <param name="httpClient">a HttpClient the provider uses to make requests of the server.</param>
 /// <param name="logFactory">optional logging factory</param>
 public ConfigServerConfigurationProvider(ConfigServerClientSettings settings, HttpClient httpClient, ILoggerFactory logFactory = null)
 {
     _ = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
     Initialize(settings, httpClient: httpClient, logFactory: logFactory);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigServerConfigurationProvider"/> class.
        /// </summary>
        /// <param name="settings">the configuration settings the provider uses when accessing the server.</param>
        /// <param name="logFactory">optional logging factory</param>
        public ConfigServerConfigurationProvider(ConfigServerClientSettings settings, ILoggerFactory logFactory = null)
        {
            _ = settings ?? throw new ArgumentNullException(nameof(settings));

            Initialize(settings, logFactory: logFactory);
        }
示例#19
0
        public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, ConfigServerClientSettings defaultSettings, ILoggerFactory logFactory = null)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            if (defaultSettings == null)
            {
                throw new ArgumentNullException(nameof(defaultSettings));
            }

            configurationBuilder.Add(new ConfigServerConfigurationProvider(defaultSettings, logFactory));
            return(configurationBuilder);
        }
        public static IConfigurationBuilder AddConfigServer(this IConfigurationBuilder configurationBuilder, ConfigServerClientSettings defaultSettings, ILoggerFactory logFactory = null)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            if (defaultSettings == null)
            {
                throw new ArgumentNullException(nameof(defaultSettings));
            }

            if (!configurationBuilder.Sources.Any(c => c.GetType() == typeof(CloudFoundryConfigurationSource)))
            {
                configurationBuilder.Add(new CloudFoundryConfigurationSource());
            }

            configurationBuilder.Add(new ConfigServerConfigurationSource(defaultSettings, configurationBuilder.Sources, configurationBuilder.Properties, logFactory));
            return(configurationBuilder);
        }