@Inject
  public TribeService(
      Settings settings, ClusterService clusterService, DiscoveryService discoveryService) {
    super(settings);
    this.clusterService = clusterService;
    Map<String, Settings> nodesSettings = new HashMap<>(settings.getGroups("tribe", true));
    nodesSettings.remove("blocks"); // remove prefix settings that don't indicate a client
    nodesSettings.remove("on_conflict"); // remove prefix settings that don't indicate a client
    for (Map.Entry<String, Settings> entry : nodesSettings.entrySet()) {
      Settings.Builder sb = Settings.builder().put(entry.getValue());
      sb.put("name", settings.get("name") + "/" + entry.getKey());
      sb.put(
          Environment.PATH_HOME_SETTING.getKey(),
          Environment.PATH_HOME_SETTING.get(settings)); // pass through ES home dir
      sb.put(TRIBE_NAME, entry.getKey());
      if (sb.get("http.enabled") == null) {
        sb.put("http.enabled", false);
      }
      sb.put(Node.NODE_CLIENT_SETTING.getKey(), true);
      nodes.add(new TribeClientNode(sb.build()));
    }

    String[] blockIndicesWrite = Strings.EMPTY_ARRAY;
    String[] blockIndicesRead = Strings.EMPTY_ARRAY;
    String[] blockIndicesMetadata = Strings.EMPTY_ARRAY;
    if (!nodes.isEmpty()) {
      // remove the initial election / recovery blocks since we are not going to have a
      // master elected in this single tribe  node local "cluster"
      clusterService.removeInitialStateBlock(discoveryService.getNoMasterBlock());
      clusterService.removeInitialStateBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK);
      if (settings.getAsBoolean("tribe.blocks.write", false)) {
        clusterService.addInitialStateBlock(TRIBE_WRITE_BLOCK);
      }
      blockIndicesWrite = settings.getAsArray("tribe.blocks.write.indices", Strings.EMPTY_ARRAY);
      if (settings.getAsBoolean("tribe.blocks.metadata", false)) {
        clusterService.addInitialStateBlock(TRIBE_METADATA_BLOCK);
      }
      blockIndicesMetadata =
          settings.getAsArray("tribe.blocks.metadata.indices", Strings.EMPTY_ARRAY);
      blockIndicesRead = settings.getAsArray("tribe.blocks.read.indices", Strings.EMPTY_ARRAY);
      for (Node node : nodes) {
        node.injector().getInstance(ClusterService.class).add(new TribeClusterStateListener(node));
      }
    }
    this.blockIndicesMetadata = blockIndicesMetadata;
    this.blockIndicesRead = blockIndicesRead;
    this.blockIndicesWrite = blockIndicesWrite;

    this.onConflict = settings.get("tribe.on_conflict", ON_CONFLICT_ANY);
  }
    public IndexMetaData build() {
      MapBuilder<String, AliasMetaData> tmpAliases = aliases;
      Settings tmpSettings = settings;

      // For backward compatibility
      String[] legacyAliases = settings.getAsArray("index.aliases");
      if (legacyAliases.length > 0) {
        tmpAliases = MapBuilder.newMapBuilder();
        for (String alias : legacyAliases) {
          AliasMetaData aliasMd = AliasMetaData.newAliasMetaDataBuilder(alias).build();
          tmpAliases.put(alias, aliasMd);
        }
        tmpAliases.putAll(aliases.immutableMap());
        // Remove index.aliases from settings once they are migrated to the new data structure
        tmpSettings =
            ImmutableSettings.settingsBuilder().put(settings).putArray("index.aliases").build();
      }

      // update default mapping on the MappingMetaData
      if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
        MappingMetaData defaultMapping = mappings.get(MapperService.DEFAULT_MAPPING);
        for (MappingMetaData mappingMetaData : mappings.map().values()) {
          mappingMetaData.updateDefaultMapping(defaultMapping);
        }
      }

      return new IndexMetaData(
          index,
          version,
          state,
          tmpSettings,
          mappings.immutableMap(),
          tmpAliases.immutableMap(),
          customs.immutableMap());
    }
  /**
   * Fetches a list of words from the specified settings file. The list should either be available
   * at the key specified by settingsPrefix or in a file specified by settingsPrefix + _path.
   *
   * @throws IllegalArgumentException If the word list cannot be found at either key.
   */
  public static List<String> getWordList(Environment env, Settings settings, String settingPrefix) {
    String wordListPath = settings.get(settingPrefix + "_path", null);

    if (wordListPath == null) {
      String[] explicitWordList = settings.getAsArray(settingPrefix, null);
      if (explicitWordList == null) {
        return null;
      } else {
        return Arrays.asList(explicitWordList);
      }
    }

    final Path wordListFile = env.configFile().resolve(wordListPath);

    try (BufferedReader reader =
        FileSystemUtils.newBufferedReader(wordListFile.toUri().toURL(), StandardCharsets.UTF_8)) {
      return loadWordList(reader, "#");
    } catch (IOException ioe) {
      String message =
          String.format(
              Locale.ROOT,
              "IOException while reading %s_path: %s",
              settingPrefix,
              ioe.getMessage());
      throw new IllegalArgumentException(message);
    }
  }
  @Inject
  public AwsEc2UnicastHostsProvider(
      Settings settings,
      TransportService transportService,
      AwsEc2Service awsEc2Service,
      Version version) {
    super(settings);
    this.transportService = transportService;
    this.client = awsEc2Service.client();
    this.version = version;

    this.hostType =
        HostType.valueOf(
            settings.get("discovery.ec2.host_type", "private_ip").toUpperCase(Locale.ROOT));

    this.bindAnyGroup = settings.getAsBoolean("discovery.ec2.any_group", true);
    this.groups = new HashSet<>();
    groups.addAll(Arrays.asList(settings.getAsArray("discovery.ec2.groups")));

    this.tags = settings.getByPrefix("discovery.ec2.tag.").getAsMap();

    Set<String> availabilityZones = new HashSet();
    availabilityZones.addAll(
        Arrays.asList(settings.getAsArray("discovery.ec2.availability_zones")));
    if (settings.get("discovery.ec2.availability_zones") != null) {
      availabilityZones.addAll(
          Strings.commaDelimitedListToSet(settings.get("discovery.ec2.availability_zones")));
    }
    this.availabilityZones = availabilityZones;

    if (logger.isDebugEnabled()) {
      logger.debug(
          "using host_type [{}], tags [{}], groups [{}] with any_group [{}], availability_zones [{}]",
          hostType,
          tags,
          groups,
          bindAnyGroup,
          availabilityZones);
    }
  }
 @Inject
 public HtmlStripCharFilterFactory(
     Index index,
     @IndexSettings Settings indexSettings,
     @Assisted String name,
     @Assisted Settings settings) {
   super(index, indexSettings, name);
   String[] escapedTags = settings.getAsArray("escaped_tags");
   if (escapedTags.length > 0) {
     this.escapedTags = unmodifiableSet(newHashSet(escapedTags));
   } else {
     this.escapedTags = null;
   }
 }
 @Inject
 public EdgeNGramTokenizerFactory(
     Index index,
     @IndexSettings Settings indexSettings,
     @Assisted String name,
     @Assisted Settings settings) {
   super(index, indexSettings, name, settings);
   this.minGram = settings.getAsInt("min_gram", NGramTokenizer.DEFAULT_MIN_NGRAM_SIZE);
   this.maxGram = settings.getAsInt("max_gram", NGramTokenizer.DEFAULT_MAX_NGRAM_SIZE);
   this.side =
       Lucene43EdgeNGramTokenizer.Side.getSide(
           settings.get("side", Lucene43EdgeNGramTokenizer.DEFAULT_SIDE.getLabel()));
   this.matcher = parseTokenChars(settings.getAsArray("token_chars"));
   this.esVersion =
       indexSettings.getAsVersion(
           IndexMetaData.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT);
 }
 public static CharArraySet parseStemExclusion(
     Settings settings, CharArraySet defaultStemExclusion) {
   String value = settings.get("stem_exclusion");
   if (value != null) {
     if ("_none_".equals(value)) {
       return CharArraySet.EMPTY_SET;
     } else {
       // LUCENE 4 UPGRADE: Should be settings.getAsBoolean("stem_exclusion_case", false)?
       return new CharArraySet(Strings.commaDelimitedListToSet(value), false);
     }
   }
   String[] stemExclusion = settings.getAsArray("stem_exclusion", null);
   if (stemExclusion != null) {
     // LUCENE 4 UPGRADE: Should be settings.getAsBoolean("stem_exclusion_case", false)?
     return new CharArraySet(Arrays.asList(stemExclusion), false);
   } else {
     return defaultStemExclusion;
   }
 }
Exemple #8
0
  public static LdapConnection getConnection(final Settings settings)
      throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
          FileNotFoundException, IOException, LdapException {
    final boolean useSSL =
        settings.getAsBoolean(ConfigConstants.ARMOR_AUTHENTICATION_LDAP_LDAPS_SSL_ENABLED, false);
    final boolean useStartSSL =
        settings.getAsBoolean(
            ConfigConstants.ARMOR_AUTHENTICATION_LDAP_LDAPS_STARTTLS_ENABLED, false);
    final LdapConnectionConfig config = new LdapConnectionConfig();

    if (useSSL || useStartSSL) {
      // ## Truststore ##
      final KeyStore ts =
          KeyStore.getInstance(
              settings.get(ConfigConstants.ARMOR_AUTHENTICATION_LDAP_LDAPS_TRUSTSTORE_TYPE, "JKS"));
      ts.load(
          new FileInputStream(
              new File(
                  settings.get(
                      ConfigConstants.ARMOR_AUTHENTICATION_LDAP_LDAPS_TRUSTSTORE_FILEPATH, null))),
          settings
              .get(ConfigConstants.ARMOR_AUTHENTICATION_LDAP_LDAPS_TRUSTSTORE_PASSWORD, "changeit")
              .toCharArray());

      final TrustManagerFactory tmf =
          TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      tmf.init(ts);

      config.setSslProtocol("TLS");
      config.setEnabledCipherSuites(SecurityUtil.ENABLED_SSL_CIPHERS);
      config.setTrustManagers(tmf.getTrustManagers());
    }

    config.setUseSsl(useSSL);
    config.setUseTls(useStartSSL);
    config.setTimeout(5000L); // 5 sec

    final String[] ldapHosts =
        settings.getAsArray(
            ConfigConstants.ARMOR_AUTHENTICATION_LDAP_HOST, new String[] {"localhost"});

    LdapConnection ldapConnection = null;

    for (int i = 0; i < ldapHosts.length; i++) {
      log.trace("Connect to {}", ldapHosts[i]);

      try {

        final String[] split = ldapHosts[i].split(":");

        config.setLdapHost(split[0]);

        if (split.length > 1) {
          config.setLdapPort(Integer.parseInt(split[1]));
        } else {
          config.setLdapPort(useSSL ? 636 : 389);
        }

        ldapConnection = new LdapNetworkConnection(config);
        ldapConnection.connect();
        if (!ldapConnection.isConnected()) {
          continue;
        } else {
          break;
        }

      } catch (final Exception e) {
        continue;
      }
    }

    if (ldapConnection == null || !ldapConnection.isConnected()) {
      throw new LdapException(
          "Unable to connect to any of those ldap servers " + Arrays.toString(ldapHosts));
    }

    return ldapConnection;
  }