Java Code Examples for org.elasticsearch.common.settings.Settings.Builder#build()

The following examples show how to use org.elasticsearch.common.settings.Settings.Builder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchConnection.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Settings settings() {
  Builder builder = Settings.builder()
      .put("cluster.name", getClusterName());
  if (getUser() == null && noPassword()) {
    return builder.build();
  }
  if (getUser() == null || getPassword() == null) {
    throw new IllegalArgumentException("Lacking user or password");
  }
  return builder
      .put("xpack.security.user", getUser() + COLON + getPassword())
      //        .put("client.transport.sniff", clientTransportSniff)
      //        .put("client.transport.ignore_cluster_name", clientIgnoreClusterName)
      //        .put("client.transport.ping_timeout", clientPingTimeout)
      //        .put("client.transport.nodes_sampler_interval", clientNodesSamplerInterval)
      .build();
}
 
Example 2
Source File: FollowersCheckerTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testFailsNodeThatDoesNotRespond() {
    final Builder settingsBuilder = Settings.builder();
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_RETRY_COUNT_SETTING.getKey(), randomIntBetween(1, 10));
    }
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_INTERVAL_SETTING.getKey(), randomIntBetween(100, 100000) + "ms");
    }
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_TIMEOUT_SETTING.getKey(), randomIntBetween(1, 100000) + "ms");
    }
    final Settings settings = settingsBuilder.build();

    testBehaviourOfFailingNode(settings, () -> null,
        "followers check retry count exceeded",
        (FOLLOWER_CHECK_RETRY_COUNT_SETTING.get(settings) - 1) * FOLLOWER_CHECK_INTERVAL_SETTING.get(settings).millis()
            + FOLLOWER_CHECK_RETRY_COUNT_SETTING.get(settings) * FOLLOWER_CHECK_TIMEOUT_SETTING.get(settings).millis());
}
 
Example 3
Source File: FollowersCheckerTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testFailsNodeThatRejectsCheck() {
    final Builder settingsBuilder = Settings.builder();
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_RETRY_COUNT_SETTING.getKey(), randomIntBetween(1, 10));
    }
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_INTERVAL_SETTING.getKey(), randomIntBetween(100, 100000) + "ms");
    }
    final Settings settings = settingsBuilder.build();

    testBehaviourOfFailingNode(settings, () -> {
            throw new ElasticsearchException("simulated exception");
        },
        "followers check retry count exceeded",
        (FOLLOWER_CHECK_RETRY_COUNT_SETTING.get(settings) - 1) * FOLLOWER_CHECK_INTERVAL_SETTING.get(settings).millis());
}
 
Example 4
Source File: InternalTestCluster.java    From crate with Apache License 2.0 6 votes vote down vote up
private Settings getSettings(int nodeOrdinal, long nodeSeed, Settings others) {
    Builder builder = Settings.builder().put(defaultSettings)
        .put(getRandomNodeSettings(nodeSeed));
    Settings settings = nodeConfigurationSource.nodeSettings(nodeOrdinal);
    if (settings != null) {
        if (settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) != null) {
            throw new IllegalStateException("Tests must not set a '" + ClusterName.CLUSTER_NAME_SETTING.getKey()
                    + "' as a node setting set '" + ClusterName.CLUSTER_NAME_SETTING.getKey() + "': ["
                    + settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) + "]");
        }
        builder.put(settings);
    }
    if (others != null) {
        builder.put(others);
    }
    builder.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), clusterName);
    return builder.build();
}
 
Example 5
Source File: DisabledCategoriesTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void completetlyInvalidConfigurationTest() throws Exception {
	Builder settingsBuilder = Settings.builder();
	settingsBuilder.put("opendistro_security.audit.type", TestAuditlogImpl.class.getName());
	settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "nonexistent");
       settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "nonexistent");
	AuditLogImpl auditLog = new AuditLogImpl(settingsBuilder.build(), null, null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);
	logAll(auditLog);

	auditLog.close();

	String result = TestAuditlogImpl.sb.toString();
	Assert.assertTrue(categoriesPresentInLog(result, filterComplianceCategories(Category.values())));

}
 
Example 6
Source File: DisabledCategoriesTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void invalidConfigurationTest() {
	Builder settingsBuilder  = Settings.builder();
	settingsBuilder.put("opendistro_security.audit.type", "debug");
	settingsBuilder.put("opendistro_security.audit.config.disabled_categories", "nonexistant, bad_headers");
	AuditLog auditLog = new AuditLogImpl(settingsBuilder.build(), null, null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);
	logAll(auditLog);
	String result = TestAuditlogImpl.sb.toString();
	Assert.assertFalse(categoriesPresentInLog(result, Category.BAD_HEADERS));
}
 
Example 7
Source File: DisabledCategoriesTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void enableAllCategoryTest() throws Exception {
	final Builder settingsBuilder  = Settings.builder();

	settingsBuilder.put("opendistro_security.audit.type", TestAuditlogImpl.class.getName());
	settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE");
       settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, "NONE");

	// we use the debug output, no ES client is needed. Also, we
	// do not need to close.
	AuditLogImpl auditLog = new AuditLogImpl(settingsBuilder.build(), null, null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);

	logAll(auditLog);

	// we're using the ExecutorService in AuditLogImpl, so we need to wait
	// until all tasks are finished before we can check the result
	auditLog.close();

	String result = TestAuditlogImpl.sb.toString();

	Assert.assertTrue(Category.values()+"#"+result, categoriesPresentInLog(result, filterComplianceCategories(Category.values())));

	Assert.assertThat(result, containsString("testuser.transport.succeededlogin"));
	Assert.assertThat(result, containsString("testuser.rest.succeededlogin"));
	Assert.assertThat(result, containsString("testuser.rest.failedlogin"));
	Assert.assertThat(result, containsString("testuser.transport.failedlogin"));
	Assert.assertThat(result, containsString("privilege.missing"));
	Assert.assertThat(result, containsString("action.indexattempt"));
	Assert.assertThat(result, containsString("action.transport.ssl"));
	Assert.assertThat(result, containsString("action.success"));
	Assert.assertThat(result, containsString("Empty"));
}
 
Example 8
Source File: DisabledCategoriesTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void checkCategoriesDisabled(Category ... disabledCategories) throws Exception {

		List<String> categoryNames = new LinkedList<>();
		for (Category category : disabledCategories) {
			categoryNames.add(category.name().toLowerCase());
		}
		String disabledCategoriesString = Joiner.on(",").join(categoryNames);

		Builder settingsBuilder  = Settings.builder();
		settingsBuilder.put("opendistro_security.audit.type", TestAuditlogImpl.class.getName());
		settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, disabledCategoriesString);
        settingsBuilder.put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES, disabledCategoriesString);


		// we use the debug output, no ES client is needed. Also, we
		// do not need to close.
		AuditLog auditLog = new AuditLogImpl(settingsBuilder.build(), null, null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);

		logAll(auditLog);

		auditLog.close();

		String result = TestAuditlogImpl.sb.toString();

		List<Category> allButDisablesCategories = new LinkedList<>(Arrays.asList(Category.values()));
		allButDisablesCategories.removeAll(Arrays.asList(disabledCategories));

		System.out.println(result+"###"+disabledCategoriesString);
		Assert.assertFalse(categoriesPresentInLog(result, disabledCategories));
		Assert.assertTrue(categoriesPresentInLog(result, filterComplianceCategories(allButDisablesCategories.toArray(new Category[] {}))));
	}
 
Example 9
Source File: DelegateTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private void testAuditType(String type, Class<? extends AuditLogSink> expectedClass) throws Exception {
		Builder settingsBuilder  = Settings.builder();
		settingsBuilder.put("opendistro_security.audit.type", type);
		settingsBuilder.put("path.home", ".");
		AuditLogImpl auditLog = new AuditLogImpl(settingsBuilder.build(), null, null, null, null, null);
		auditLog.close();
//		if (expectedClass != null) {
//		    Assert.assertNotNull("delegate is null for type: "+type,auditLog.delegate);
//			Assert.assertEquals(expectedClass, auditLog.delegate.getClass());
//		} else {
//			Assert.assertNull(auditLog.delegate);
//		}

	}
 
Example 10
Source File: FollowersCheckerTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testFailureCounterResetsOnSuccess() {
    final Builder settingsBuilder = Settings.builder();
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_RETRY_COUNT_SETTING.getKey(), randomIntBetween(2, 10));
    }
    if (randomBoolean()) {
        settingsBuilder.put(FOLLOWER_CHECK_INTERVAL_SETTING.getKey(), randomIntBetween(100, 100000) + "ms");
    }
    final Settings settings = settingsBuilder.build();

    final int retryCount = FOLLOWER_CHECK_RETRY_COUNT_SETTING.get(settings);
    final int maxRecoveries = randomIntBetween(3, 10);

    // passes just enough checks to keep it alive, up to maxRecoveries, and then fails completely
    testBehaviourOfFailingNode(settings, new Supplier<Empty>() {
            private int checkIndex;
            private int recoveries;

            @Override
            public Empty get() {
                checkIndex++;
                if (checkIndex % retryCount == 0 && recoveries < maxRecoveries) {
                    recoveries++;
                    return Empty.INSTANCE;
                }
                throw new ElasticsearchException("simulated exception");
            }
        },
        "followers check retry count exceeded",
        (FOLLOWER_CHECK_RETRY_COUNT_SETTING.get(settings) * (maxRecoveries + 1) - 1)
            * FOLLOWER_CHECK_INTERVAL_SETTING.get(settings).millis());
}
 
Example 11
Source File: SinkProviderTLSTest.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
@Test
public void testTlsConfigurationNoFallback() throws Exception {

	TestHttpHandler handler = new TestHttpHandler();

	server = ServerBootstrap.bootstrap().setListenerPort(8083).setServerInfo("Test/1.1").setSslContext(createSSLContext()).registerHandler("*", handler).create();

	server.start();

	Builder builder = Settings.builder().loadFromPath(FileHelper.getAbsoluteFilePathFromClassPath("auditlog/endpoints/sink/configuration_tls.yml"));
	builder.put("path.home", "/");

	// replace some values with absolute paths for unit tests
	builder.put("opendistro_security.audit.config.webhook.ssl.pemtrustedcas_filepath", FileHelper.getAbsoluteFilePathFromClassPath("auditlog/root-ca.pem"));
	builder.put("opendistro_security.audit.endpoints.endpoint1.config.webhook.ssl.pemtrustedcas_filepath", FileHelper.getAbsoluteFilePathFromClassPath("auditlog/root-ca.pem"));
	builder.put("opendistro_security.audit.endpoints.endpoint2.config.webhook.ssl.pemtrustedcas_content", FileHelper.loadFile("auditlog/root-ca.pem"));

	SinkProvider provider = new SinkProvider(builder.build(), null, null, null);
	WebhookSink defaultSink = (WebhookSink) provider.defaultSink;
	Assert.assertEquals(true, defaultSink.verifySSL);

	AuditMessage msg = MockAuditMessageFactory.validAuditMessage();
	provider.allSinks.get("endpoint1").store(msg);

	Assert.assertTrue(handler.method.equals("POST"));
       Assert.assertTrue(handler.body != null);
       Assert.assertTrue(handler.body.contains("{"));
       assertStringContainsAllKeysAndValues(handler.body);

	handler.reset();

	provider.allSinks.get("endpoint2").store(msg);

	Assert.assertTrue(handler.method.equals("POST"));
       Assert.assertTrue(handler.body != null);
       Assert.assertTrue(handler.body.contains("{"));
       assertStringContainsAllKeysAndValues(handler.body);

	handler.reset();

	provider.defaultSink.store(msg);

	Assert.assertTrue(handler.method.equals("POST"));
       Assert.assertTrue(handler.body != null);
       Assert.assertTrue(handler.body.contains("{"));
       assertStringContainsAllKeysAndValues(handler.body);

       server.stop();
}
 
Example 12
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
protected Client createHttpClient(final FessConfig fessConfig, final String host) {
    final Builder builder = Settings.builder().putList("http.hosts", host).put("processors", fessConfig.availableProcessors());
    return new HttpClient(builder.build(), null);
}
 
Example 13
Source File: InternalTestCluster.java    From crate with Apache License 2.0 4 votes vote down vote up
private static Settings getRandomNodeSettings(long seed) {
    Random random = new Random(seed);
    Builder builder = Settings.builder();
    builder.put(TransportSettings.TRANSPORT_COMPRESS.getKey(), rarely(random));
    if (random.nextBoolean()) {
        builder.put("cache.recycler.page.type", RandomPicks.randomFrom(random, PageCacheRecycler.Type.values()));
    }

    builder.put(EsExecutors.PROCESSORS_SETTING.getKey(), 1 + random.nextInt(3));

    // randomize tcp settings
    if (random.nextBoolean()) {
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_RECOVERY.getKey(), random.nextInt(2) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_BULK.getKey(), random.nextInt(3) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_REG.getKey(), random.nextInt(6) + 1);
    }

    if (random.nextBoolean()) {
        builder.put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(),
                timeValueSeconds(RandomNumbers.randomIntBetween(random, 10, 30)).getStringRep());
    }

    if (random.nextInt(10) == 0) {
        builder.put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
        builder.put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
    }

    if (random.nextBoolean()) {
        if (random.nextInt(10) == 0) { // do something crazy slow here
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
                    new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 10), ByteSizeUnit.MB));
        } else {
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(),
                    new ByteSizeValue(RandomNumbers.randomIntBetween(random, 10, 200), ByteSizeUnit.MB));
        }
    }

    if (random.nextBoolean()) {
        builder.put(TransportSettings.PING_SCHEDULE.getKey(), RandomNumbers.randomIntBetween(random, 100, 2000) + "ms");
    }

    return builder.build();
}