Exemplo n.º 1
0
        public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            // The authentication options below are set if you have non-null/empty
            // settings in the configuration.
            var apiKeyId = configuration["ElasticsearchSettings:apiKeyId"];
            var apiKey   = configuration["ElasticsearchSettings:apiKey"];

            if (!string.IsNullOrEmpty(apiKeyId) && !string.IsNullOrEmpty(apiKey))
            {
                settings = settings.ApiKeyAuthentication(apiKeyId, apiKey);
            }
            else
            {
                var basicAuthUser     = configuration["ElasticsearchSettings:basicAuthUser"];
                var basicAuthPassword = configuration["ElasticsearchSettings:basicAuthPassword"];

                if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
                {
                    settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
                }
            }

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client); // It's Thread safe
        }
Exemplo n.º 2
0
        private async Task <bool> SwitchToNextNodeAsync(CancellationToken cancellationToken = default)
        {
            if (_currentNode == null)
            {
                _currentNode = _nodes[0];
            }
            else
            {
                var currentIndex = _nodes.IndexOf(_currentNode);

                _currentNode = currentIndex == _nodes.Count - 1 ? _nodes[0] : _nodes[currentIndex + 1];
            }

            var uri = new Uri(_currentNode.Host);

            var settings = new ConnectionSettings(uri);

            settings.EnableHttpCompression();
            settings.MaximumRetries(_maximumRetries);
            settings.MaxRetryTimeout(_maxRetryTimeout);

            switch (_currentNode.AuthenticationType)
            {
            case AuthenticationType.Basic:
                settings.BasicAuthentication(_currentNode.UserName, _currentNode.Password);
                break;

            case AuthenticationType.ApiKey:
                settings.ApiKeyAuthentication(_currentNode.Id, _currentNode.ApiKey);
                break;

            default:
                break;
            }

            _client = new ElasticClient(settings);

            _logger?.LogInformation($"Trying to connect to {uri} ({_eventLogItemsIndex})");

            var response = await _client.PingAsync(pd => pd, cancellationToken);

            if (!(response.OriginalException is TaskCanceledException))
            {
                if (!response.IsValid)
                {
                    _logger?.LogWarning($"Failed to connect to {uri} ({_eventLogItemsIndex}): {response.OriginalException.Message}");
                }
                else
                {
                    _logger?.LogInformation($"Successfully connected to {uri} ({_eventLogItemsIndex})");
                }
            }

            return(response.IsValid);
        }
Exemplo n.º 3
0
        public static ElasticClient CreateClient(ElasticSearchConnectionString connectionString, TimeSpan?requestTimeout = null, TimeSpan?pingTimeout = null)
        {
            Uri[] nodesUrls = connectionString.Nodes.Select(x => new Uri(x)).ToArray();

            StaticConnectionPool pool     = new StaticConnectionPool(nodesUrls);
            ConnectionSettings   settings = new ConnectionSettings(pool);

            if (requestTimeout != null)
            {
                settings.RequestTimeout(requestTimeout.Value);
            }

            if (pingTimeout != null)
            {
                settings.PingTimeout(pingTimeout.Value);
            }

            if (connectionString.Authentication != null)
            {
                if (connectionString.Authentication.Basic != null)
                {
                    settings.BasicAuthentication(connectionString.Authentication.Basic.Username, connectionString.Authentication.Basic.Password);
                }
                else if (connectionString.Authentication.ApiKey != null)
                {
                    settings.ApiKeyAuthentication(connectionString.Authentication.ApiKey.ApiKeyId, connectionString.Authentication.ApiKey.ApiKey);
                }
                else if (connectionString.Authentication.Certificate != null)
                {
                    if (connectionString.Authentication.Certificate.CertificatesBase64.Length == 1)
                    {
                        var cert = new X509Certificate2(Convert.FromBase64String(connectionString.Authentication.Certificate.CertificatesBase64.First()));
                        settings.ClientCertificate(cert);
                    }
                    else
                    {
                        var certificates = new X509CertificateCollection();

                        foreach (var certificateBase64 in connectionString.Authentication.Certificate.CertificatesBase64)
                        {
                            certificates.Add(new X509Certificate2(Convert.FromBase64String(certificateBase64)));
                        }

                        settings.ClientCertificates(certificates);
                    }
                }
            }

            ElasticClient client = new(settings);

            return(client);
        }
Exemplo n.º 4
0
        public ElasticsearchContext(IOptions <ElsaElasticsearchOptions> options)
        {
            var pool     = new SniffingConnectionPool(options.Value.Uri);
            var settings = new ConnectionSettings(pool);

            if (options.Value.BasicAuthentication != null)
            {
                settings.BasicAuthentication(options.Value.BasicAuthentication.Username, options.Value.BasicAuthentication.Password);
            }
            else if (options.Value.ApiKeyAuthentication != null)
            {
                settings.ApiKeyAuthentication(options.Value.ApiKeyAuthentication.Id, options.Value.ApiKeyAuthentication.ApiKey);
            }

            Client = new ElasticClient(settings);
        }
        public ElasticSearchClientWrapper(IConfig config, IErrorLog errorLog, ITenantService tenantService)
        {
            _errorLog      = errorLog;
            _tenantService = tenantService;
            var node     = new Uri(config.SearchUrl);
            var settings = new ConnectionSettings(node)
                           .DefaultIndex(IndexName).DisableDirectStreaming();

            if (!string.IsNullOrEmpty(config.SearchKey))
            {
                var pair = config.SearchKey.Split("|");
                if (pair.Length == 2)
                {
                    settings.ApiKeyAuthentication(pair[0], pair[1]);
                }
            }
            _client = new ElasticClient(settings);
        }
Exemplo n.º 6
0
        public ElasticsearchReporter(string[] elasticsearchUris, string indexName
                                     , string apiId       = null, string apiKey   = null
                                     , string username    = null, string password = null
                                     , int?timeoutSeconds = null, bool debug      = false)
        {
            _uris      = elasticsearchUris;
            _indexName = indexName;
            _debug     = debug;
            //https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/connection-pooling.html
            if (null == elasticsearchUris || 0 == elasticsearchUris.Length)
            {
                throw new ArgumentException("Uri list should not be empty", nameof(elasticsearchUris));
            }
            IConnectionPool pool = elasticsearchUris.Length == 1 ? new SingleNodeConnectionPool(new Uri(elasticsearchUris.First())) as IConnectionPool :
                                   new StaticConnectionPool(_uris.Select(u => new Uri(u)));
            var _settings = new ConnectionSettings(pool)
                            .DefaultIndex(indexName)
                            //.DefaultMappingFor<ElasticsearchErrorDocument>(m => m)
                            //.IndexName())
            ;

            if (!string.IsNullOrEmpty(apiId))
            {
                _settings.ApiKeyAuthentication(apiId, apiKey);
            }
            if (!string.IsNullOrEmpty(username))
            {
                _settings.BasicAuthentication(username, password);
                _settings.ServerCertificateValidationCallback((o, certificate, chain, errors) => true);
            }
            if (timeoutSeconds.HasValue)
            {
                _settings = _settings.RequestTimeout(TimeSpan.FromSeconds(timeoutSeconds.Value))
                            .MaxDeadTimeout(TimeSpan.FromSeconds(timeoutSeconds.Value));
            }
            if (_debug)
            {
                _settings = _settings.DisableDirectStreaming();
            }
            _client = new ElasticsearchImpl.ElasticClient(new ElasticClient(_settings));
        }
        public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));

            var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];

            if (!string.IsNullOrEmpty(defaultIndex))
            {
                settings = settings.DefaultIndex(defaultIndex);
            }

            // The authentication options below are set if you have non-null/empty
            // settings in the configuration.  These are just samples -- there are
            // other authentication methods available.
            var apiKeyId = configuration["ElasticsearchSettings:apiKeyId"];
            var apiKey   = configuration["ElasticsearchSettings:apiKey"];

            if (!string.IsNullOrEmpty(apiKeyId) && !string.IsNullOrEmpty(apiKey))
            {
                settings = settings.ApiKeyAuthentication(apiKeyId, apiKey);
            }
            else
            {
                var basicAuthUser     = configuration["ElasticsearchSettings:basicAuthUser"];
                var basicAuthPassword = configuration["ElasticsearchSettings:basicAuthPassword"];

                if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
                {
                    settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
                }
            }

            var client = new ElasticClient(settings);

            // ElasticClient is thread-safe
            // See https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/lifetimes.html
            services.AddSingleton <IElasticClient>(client);
        }
Exemplo n.º 8
0
        public static async System.Threading.Tasks.Task Log(IfyContext context, Event log)
        {
            if (EventLogConfig == null || EventLogConfig.Settings == null || EventLogConfig.Settings.Count == 0 ||
                EventLogConfig.Settings["baseurl"] == null ||
                EventLogConfig.Settings["index"] == null ||
                EventLogConfig.Settings["pipeline"] == null ||
                string.IsNullOrEmpty(EventLogConfig.Settings["baseurl"].Value))
            {
                throw new Exception("Missing event log configuration in web.config");
            }

            var json = JsonConvert.SerializeObject(log);

            context.LogDebug(context, "Event log : " + json);

            var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri(EventLogConfig.Settings["baseurl"].Value)), sourceSerializer: JsonNetSerializer.Default);

            if (EventLogConfig.Settings["auth_apikey"] != null && !string.IsNullOrEmpty(EventLogConfig.Settings["auth_apikey"].Value))
            {
                settings.ApiKeyAuthentication(new Elasticsearch.Net.ApiKeyAuthenticationCredentials(EventLogConfig.Settings["auth_apikey"].Value));
            }
            else if (EventLogConfig.Settings["auth_username"] != null && !string.IsNullOrEmpty(EventLogConfig.Settings["auth_username"].Value) && EventLogConfig.Settings["auth_password"] != null)
            {
                settings.BasicAuthentication(EventLogConfig.Settings["auth_username"].Value, EventLogConfig.Settings["auth_password"].Value);
            }

            var client = new ElasticClient(settings);

            try{
                var response = await client.IndexAsync(log, e => e.Index(EventLogConfig.Settings["index"].Value).Pipeline(EventLogConfig.Settings["pipeline"].Value));

                context.LogDebug(context, string.Format("Log event response: (ID={0}) {1}", response.Id, response.DebugInformation));
            }catch (Exception e) {
                context.LogError(context, "Log event error  (POST): " + e.Message);
                context.WriteError("Log event error  (POST): " + e.Message);
            }
        }