<commit_before>/**
 * Returns the Elasticsearch client used to create indices, execute watches and store alarms.
 *
 * By default both watches and alerts history are stored in the data cluster.
 */
import elasticsearch from 'elasticsearch';
import fs from 'fs';
import Crypto from './crypto';
import Log from './log';

/**
* Get client
*
*/
function esClient(config, username, password) {
  const options = {
    host: [
      {
        host: config.es.host,
        protocol: config.es.protocol,
        port: config.es.port
      }
    ]
  };

  if (username && password) {
    options.host[0].auth = username + ':' + password;
  }

  if (!config.settings.authentication.cert.selfsigned) {
    options.ssl = {
      ca: fs.readFileSync(config.settings.authentication.cert.pem),
      rejectUnauthorized: true,
    };
  }

  return new elasticsearch.Client(options);
};


/**
* Get Elasticsearch client
*
*/
export default function getElasticsearchClient(server, config, type = 'data', impersonate = null) {
  const log = new Log(config.app_name, server, 'get_elasticsearch_client');

  const auth = config.settings.authentication;
  // Basic authentication
  if (auth.enabled) {
    const crypto = new Crypto(auth.encryption);

    if (impersonate) {
      log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.sha}`);
      return esClient(config, impersonate.username, crypto.decrypt(impersonate.sha));
    }

    if (auth.sha) {
      log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.sha}`);
      return esClient(config, auth.username, crypto.decrypt(auth.sha));
    }

    log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.password}`);
    return esClient(config, auth.username, auth.password);
  }

  // Authentication via Kibi Access Control app
  if (server.plugins.kibi_access_control && server.plugins.kibi_access_control.getSentinlClient) {
    log.debug('auth via Kibi Access Control');
    return server.plugins.kibi_access_control.getSentinlClient();
  }

  // Authentication via Investigate Access Control app pre Sentinl rename
  if (server.plugins.investigate_access_control && server.plugins.investigate_access_control.getSentinlClient) {
    log.debug('auth via Investigate Access Control');
    return server.plugins.investigate_access_control.getSentinlClient();
  }

  log.debug('auth via Kibana server elasticseaarch plugin');
  if (type === 'data') {
    return server.plugins.elasticsearch.getCluster('data').getClient();
  }

  return server.plugins.elasticsearch.getCluster('admin').getClient();
}<commit_msg>Siren only. Support storing alarm history in a cluster other than data.<commit_after>/**
 * Returns the Elasticsearch client used to create indices, execute watches and store alarms.
 *
 * By default both watches and alerts history are stored in the data cluster.
 */
import elasticsearch from 'elasticsearch';
import fs from 'fs';
import Crypto from './crypto';
import Log from './log';

/**
* Get client
*
*/
function esClient(config, username, password) {
  const options = {
    host: [
      {
        host: config.es.host,
        protocol: config.es.protocol,
        port: config.es.port
      }
    ]
  };

  if (username && password) {
    options.host[0].auth = username + ':' + password;
  }

  if (!config.settings.authentication.cert.selfsigned) {
    options.ssl = {
      ca: fs.readFileSync(config.settings.authentication.cert.pem),
      rejectUnauthorized: true,
    };
  }

  return new elasticsearch.Client(options);
};


/**
* Get Elasticsearch client
*
*/
export default function getElasticsearchClient(server, config, type = 'data', impersonate = null) {
  const log = new Log(config.app_name, server, 'get_elasticsearch_client');

  const auth = config.settings.authentication;
  // Basic authentication
  if (auth.enabled) {
    const crypto = new Crypto(auth.encryption);

    if (impersonate) {
      log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.sha}`);
      return esClient(config, impersonate.username, crypto.decrypt(impersonate.sha));
    }

    if (auth.sha) {
      log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.sha}`);
      return esClient(config, auth.username, crypto.decrypt(auth.sha));
    }

    log.debug(`impersonating Elasticsearch client by ${auth.username}:${auth.password}`);
    return esClient(config, auth.username, auth.password);
  }

  // Authentication via Kibi Access Control app
  if (server.plugins.kibi_access_control && server.plugins.kibi_access_control.getSentinlClient) {
    log.debug('auth via Kibi Access Control');
    return server.plugins.kibi_access_control.getSentinlClient();
  }

  // Authentication via Investigate Access Control app pre Sentinl rename
  if (server.plugins.investigate_access_control && server.plugins.investigate_access_control.getSentinlClient) {
    let cluster = server.config().get('elasticsearch.siren.alert.admin.cluster');
    if (!cluster) {
      cluster = 'data';
    }
    log.debug('auth via Investigate Access Control, cluster name: ' + cluster);
    return server.plugins.elasticsearch.getCluster(cluster).createClient({
      username: server.config().get('investigate_access_control.sirenalert.elasticsearch.username'),
      password: server.config().get('investigate_access_control.sirenalert.elasticsearch.password'),
    });
  }

  log.debug('auth via Kibana server elasticseaarch plugin');
  if (type === 'data') {
    return server.plugins.elasticsearch.getCluster('data').getClient();
  }

  return server.plugins.elasticsearch.getCluster('admin').getClient();
}
{'fraction_non_alphanumeric': 0.12437899219304471, 'fraction_numerical': 0.00035486160397445, 'mean_word_length': 3.936077057793345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4654376', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1632, 'n_words': 389}