下面列出了org.mockito.Answers#org.elasticsearch.common.settings.Settings 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testIgnorePunctuation() throws Exception {
Settings settings = Settings.builder()
.put("index.analysis.analyzer.myAnalyzer.type", "icu_collation")
.put("index.analysis.analyzer.myAnalyzer.language", "en")
.put("index.analysis.analyzer.myAnalyzer.strength", "primary")
.put("index.analysis.analyzer.myAnalyzer.alternate", "shifted")
.build();
ESTestCase.TestAnalysis analysis = ESTestCase.createTestAnalysis(new Index("test", "_na_"),
settings,
new BundlePlugin(Settings.EMPTY));
Analyzer analyzer = analysis.indexAnalyzers.get("myAnalyzer");
TokenStream tsPunctuation = analyzer.tokenStream("content", "foo-bar");
BytesRef b1 = bytesFromTokenStream(tsPunctuation);
TokenStream tsWithoutPunctuation = analyzer.tokenStream("content", "foo bar");
BytesRef b2 = bytesFromTokenStream(tsWithoutPunctuation);
assertTrue(compare(b1.bytes, b2.bytes) == 0);
}
@Test
public void testLdapAuthenticationWrongBindDn() throws Exception {
try {
final Settings settings = createBaseSettings()
.putList(ConfigConstants.LDAP_HOSTS, "localhost:" + ldapPort)
.put("users.u1.search", "(uid={0})").put("users.u1.base", "ou=people,o=TEST")
.put(ConfigConstants.LDAP_BIND_DN, "cn=Captain Spock,ou=people,o=TEST")
.put(ConfigConstants.LDAP_PASSWORD, "wrong").build();
new LDAPAuthenticationBackend2(settings, null)
.authenticate(new AuthCredentials("jacksonm", "secret".getBytes(StandardCharsets.UTF_8)));
Assert.fail("Expected exception");
} catch (Exception e) {
Assert.assertTrue(ExceptionUtils.getStackTrace(e), ExceptionUtils.getStackTrace(e).contains("password was incorrect"));
}
}
/**
* Finds the next (closest) delay expiration of an delayed shard in nanoseconds based on current time.
* Returns 0 if delay is negative.
* Returns -1 if no delayed shard is found.
*/
public static long findNextDelayedAllocation(long currentNanoTime, ClusterState state) {
MetaData metaData = state.metaData();
RoutingTable routingTable = state.routingTable();
long nextDelayNanos = Long.MAX_VALUE;
for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.UNASSIGNED)) {
UnassignedInfo unassignedInfo = shard.unassignedInfo();
if (unassignedInfo.isDelayed()) {
Settings indexSettings = metaData.index(shard.index()).getSettings();
// calculate next time to schedule
final long newComputedLeftDelayNanos = unassignedInfo.getRemainingDelay(currentNanoTime, indexSettings);
if (newComputedLeftDelayNanos < nextDelayNanos) {
nextDelayNanos = newComputedLeftDelayNanos;
}
}
}
return nextDelayNanos == Long.MAX_VALUE ? -1L : nextDelayNanos;
}
@Inject
public RestSearchAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(GET, "/_search", this);
controller.registerHandler(POST, "/_search", this);
controller.registerHandler(GET, "/{index}/_search", this);
controller.registerHandler(POST, "/{index}/_search", this);
controller.registerHandler(GET, "/{index}/{type}/_search", this);
controller.registerHandler(POST, "/{index}/{type}/_search", this);
controller.registerHandler(GET, "/_search/template", this);
controller.registerHandler(POST, "/_search/template", this);
controller.registerHandler(GET, "/{index}/_search/template", this);
controller.registerHandler(POST, "/{index}/_search/template", this);
controller.registerHandler(GET, "/{index}/{type}/_search/template", this);
controller.registerHandler(POST, "/{index}/{type}/_search/template", this);
RestExistsAction restExistsAction = new RestExistsAction(settings, controller, client);
controller.registerHandler(GET, "/_search/exists", restExistsAction);
controller.registerHandler(POST, "/_search/exists", restExistsAction);
controller.registerHandler(GET, "/{index}/_search/exists", restExistsAction);
controller.registerHandler(POST, "/{index}/_search/exists", restExistsAction);
controller.registerHandler(GET, "/{index}/{type}/_search/exists", restExistsAction);
controller.registerHandler(POST, "/{index}/{type}/_search/exists", restExistsAction);
}
@Test
public void mismatchedAlgTest() throws Exception {
MockIpdServer mockIdpServer = new MockIpdServer(TestJwk.Jwks.RSA_1_WRONG_ALG);
try {
Settings settings = Settings.builder().put("openid_connect_url", mockIdpServer.getDiscoverUri()).build();
HTTPJwtKeyByOpenIdConnectAuthenticator jwtAuth = new HTTPJwtKeyByOpenIdConnectAuthenticator(settings, null);
AuthCredentials creds = jwtAuth.extractCredentials(
new FakeRestRequest(ImmutableMap.of("Authorization", TestJwts.NoKid.MC_COY_SIGNED_RSA_1),
new HashMap<String, String>()),
null);
Assert.assertNull(creds);
} finally {
try {
mockIdpServer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void testCustomConfigMustNotContainSettingsFromDefaultCrateYml() throws Exception {
HashMap<String, String> settings = new HashMap<>();
Path home = PathUtils.get(getClass().getResource(".").toURI());
settings.put("path.home", home.toString());
Path config = PathUtils.get(getClass().getResource("config_custom").toURI());
settings.put("path.conf", config.toString());
Settings finalSettings = InternalSettingsPreparer
.prepareEnvironment(Settings.EMPTY, settings, config, () -> "node1").settings();
// Values from crate.yml
assertThat(finalSettings.get("cluster.name"), is("custom"));
// path.logs is not set in config_custom/crate.yml
// so it needs to use default value and not the value set in config/crate.yml
assertThat(finalSettings.get("path.logs"), Matchers.anyOf(
endsWith("org/elasticsearch/node/logs"),
endsWith("org\\elasticsearch\\node\\logs")
));
}
@Test
public void testLdapAuthorizationNestedAttrFilter() throws Exception {
final Settings settings = createBaseSettings()
.putList(ConfigConstants.LDAP_HOSTS, "localhost:" + ldapPort)
.put(ConfigConstants.LDAP_AUTHC_USERSEARCH, "(uid={0})")
.put(ConfigConstants.LDAP_AUTHC_USERBASE, "ou=people,o=TEST")
.put(ConfigConstants.LDAP_AUTHZ_ROLEBASE, "ou=groups,o=TEST")
.put(ConfigConstants.LDAP_AUTHZ_ROLENAME, "cn")
.put(ConfigConstants.LDAP_AUTHZ_RESOLVE_NESTED_ROLES, true)
.put(ConfigConstants.LDAP_AUTHZ_ROLESEARCH, "(uniqueMember={0})")
.put(ConfigConstants.LDAP_AUTHZ_USERROLENAME, "description") // no memberOf OID
.put(ConfigConstants.LDAP_AUTHZ_ROLESEARCH_ENABLED, true)
.putList(ConfigConstants.LDAP_AUTHZ_NESTEDROLEFILTER, "cn=rolemo4*").build();
final User user = new User("spock");
new LDAPAuthorizationBackend(settings, null).fillRoles(user, null);
Assert.assertNotNull(user);
Assert.assertEquals("spock", user.getName());
Assert.assertEquals(6, user.getRoles().size());
Assert.assertEquals("role2", new ArrayList<>(new TreeSet<>(user.getRoles())).get(4));
Assert.assertEquals("nested1", new ArrayList<>(new TreeSet<>(user.getRoles())).get(2));
}
public CreateIndexResponse copyIndex(final String index, final String target, Settings settings, String... types)
throws InterruptedException, ExecutionException, IOException {
return execute(new Callable<CreateIndexResponse>() {
@Override
public CreateIndexResponse call() throws Exception {
LOGGER.trace("Copying {} index to {} for types {}", index, target, types);
GetIndexResponse response = getIndices(index);
CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(target);
if(settings != null) {
builder.setSettings(settings);
}
for (String type : types) {
builder.addMapping(type, response.mappings().get(index).get(type).getSourceAsMap());
}
return builder.get();
}
});
}
public BlobShardCollectorProvider(BlobShard blobShard,
ClusterService clusterService,
Schemas schemas,
NodeJobsCounter nodeJobsCounter,
Functions functions,
ThreadPool threadPool,
Settings settings,
TransportActionProvider transportActionProvider) {
super(
clusterService,
schemas,
nodeJobsCounter,
functions,
threadPool,
settings,
transportActionProvider,
blobShard.indexShard(),
new ShardRowContext(blobShard, clusterService)
);
inputFactory = new InputFactory(functions);
this.blobShard = blobShard;
}
@Test
public void invalidUrlTest() throws Exception {
String url = "faultyurl";
final Settings settings = Settings.builder()
.put("opendistro_security.audit.config.webhook.url", url)
.put("opendistro_security.audit.config.webhook.format", "slack")
.put("opendistro_security.ssl.transport.truststore_filepath",
FileHelper.getAbsoluteFilePathFromClassPath("auditlog/truststore.jks"))
.put("path.home", ".")
.build();
LoggingSink fallback = new LoggingSink("test", Settings.EMPTY, null, null);;
MockWebhookAuditLog auditlog = new MockWebhookAuditLog(settings, ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DEFAULT, fallback);
AuditMessage msg = MockAuditMessageFactory.validAuditMessage();
auditlog.store(msg);
Assert.assertEquals(null, auditlog.url);
Assert.assertEquals(null, auditlog.payload);
Assert.assertEquals(null, auditlog.webhookUrl);
// message must be stored in fallback
Assert.assertEquals(1, fallback.messages.size());
Assert.assertEquals(msg, fallback.messages.get(0));
}
@Test
public void testChainedLdapAuthorization() throws Exception {
final Settings settings = createBaseSettings()
.putList(ConfigConstants.LDAP_HOSTS, "127.0.0.1:4", "localhost:" + ldapPort)
.put("users.u1.search", "(uid={0})").put("users.u1.base", "ou=people,o=TEST")
.put(ConfigConstants.LDAP_AUTHZ_ROLENAME, "cn").put("roles.g1.base", "ou=groups,o=TEST")
.put("roles.g1.search", "(uniqueMember={0})").put("roles.g2.base", "ou=groups2,o=TEST")
.put("roles.g2.search", "(uniqueMember={0})").build();
final LdapUser user = (LdapUser) new LDAPAuthenticationBackend2(settings, null)
.authenticate(new AuthCredentials("jacksonm", "secret".getBytes(StandardCharsets.UTF_8)));
new LDAPAuthorizationBackend(settings, null).fillRoles(user, null);
Assert.assertNotNull(user);
Assert.assertEquals("cn=Michael Jackson,ou=people,o=TEST", user.getName());
Assert.assertEquals(3, user.getRoles().size());
Assert.assertTrue(user.getRoles().contains("ceo"));
Assert.assertTrue(user.getRoles().contains("king"));
Assert.assertTrue(user.getRoles().contains("role2"));
Assert.assertEquals(user.getName(), user.getUserEntry().getDn());
}
public void testTransformCyrillicLatin() throws Exception {
String source = "Российская Федерация";
String[] expected = new String[] { "Rossijskaâ", "Federaciâ" };
String resource = "icu_transform.json";
Settings settings = Settings.builder()
.loadFromStream(resource, getClass().getResourceAsStream(resource), true)
.build();
ESTestCase.TestAnalysis analysis = ESTestCase.createTestAnalysis(new Index("test", "_na_"),
settings,
new BundlePlugin(Settings.EMPTY));
Tokenizer tokenizer = analysis.tokenizer.get("my_icu_tokenizer_cyr").create();
tokenizer.setReader(new StringReader(source));
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_icu_transformer_cyr");
TokenStream tokenStream = tokenFilter.create(tokenizer);
assertTokenStreamContents(tokenStream, expected);
}
/**
* Finish preparing settings by replacing forced settings and any defaults that need to be added.
*/
private static void finalizeSettings(Settings.Builder output, Supplier<String> defaultNodeName) {
// allow to force set properties based on configuration of the settings provided
List<String> forcedSettings = new ArrayList<>();
for (String setting : output.keys()) {
if (setting.startsWith("force.")) {
forcedSettings.add(setting);
}
}
for (String forcedSetting : forcedSettings) {
String value = output.remove(forcedSetting);
output.put(forcedSetting.substring("force.".length()), value);
}
output.replacePropertyPlaceholders();
// put the cluster and node name if they aren't set
if (output.get(ClusterName.CLUSTER_NAME_SETTING.getKey()) == null) {
output.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY).value());
}
if (output.get(Node.NODE_NAME_SETTING.getKey()) == null) {
output.put(Node.NODE_NAME_SETTING.getKey(), defaultNodeName.get());
}
}
/**
* テスト用のElasticsearchノードを起動する.
*/
public static void startInternalNode() {
Settings settings = Settings.settingsBuilder()
.put("node.http.enabled", false)
.put("cluster.name", "testingCluster")
.put("node.name", "node1")
.put("gateway.type", "none")
.put("action.auto_create_index", "false")
.put("index.store.type", "memory")
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
.put("transport.tcp.port", "9399")
.build();
internalNode = NodeBuilder.nodeBuilder().settings(settings).node();
}
NGramTokenizerFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {
super(indexSettings, name, settings);
int maxAllowedNgramDiff = indexSettings.getMaxNgramDiff();
this.minGram = settings.getAsInt("min_gram", NGramTokenizer.DEFAULT_MIN_NGRAM_SIZE);
this.maxGram = settings.getAsInt("max_gram", NGramTokenizer.DEFAULT_MAX_NGRAM_SIZE);
int ngramDiff = maxGram - minGram;
if (ngramDiff > maxAllowedNgramDiff) {
deprecationLogger.deprecated("Deprecated big difference between max_gram and min_gram in NGram Tokenizer,"
+ "expected difference must be less than or equal to: [" + maxAllowedNgramDiff + "]");
}
this.matcher = parseTokenChars(settings.getAsList("token_chars"));
}
/**
* scans the node paths and loads existing metaData file. If not found a new meta data will be generated
* and persisted into the nodePaths
*/
private static NodeMetaData loadOrCreateNodeMetaData(Settings settings, Logger logger,
NodePath... nodePaths) throws IOException {
final Path[] paths = Arrays.stream(nodePaths).map(np -> np.path).toArray(Path[]::new);
NodeMetaData metaData = NodeMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, paths);
if (metaData == null) {
metaData = new NodeMetaData(generateNodeId(settings));
}
// we write again to make sure all paths have the latest state file
NodeMetaData.FORMAT.writeAndCleanup(metaData, paths);
return metaData;
}
@Inject
public IBSimilarityProvider(@Assisted String name, @Assisted Settings settings) {
super(name);
Distribution distribution = parseDistribution(settings);
Lambda lambda = parseLambda(settings);
Normalization normalization = parseNormalization(settings);
this.similarity = new IBSimilarity(distribution, lambda, normalization);
}
public NaturalSortKeyTokenizerFactory(IndexSettings indexSettings, Environment environment, String name,
Settings settings) {
super(indexSettings, name, settings);
Collator collator = NaturalSortKeyAnalyzerProvider.createCollator(settings);
int digits = settings.getAsInt("digits", 1);
int maxTokens = settings.getAsInt("maxTokens", 2);
this.factory = new NaturalSortKeyAttributeFactory(collator, digits, maxTokens);
this.bufferSize = settings.getAsInt("bufferSize", KeywordTokenizer.DEFAULT_BUFFER_SIZE);
}
public TransportBroadcastReplicationAction(String name, Class<Request> request, Settings settings, ThreadPool threadPool, ClusterService clusterService,
TransportService transportService,
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, TransportReplicationAction replicatedBroadcastShardAction) {
super(settings, name, threadPool, transportService, actionFilters, indexNameExpressionResolver, request);
this.replicatedBroadcastShardAction = replicatedBroadcastShardAction;
this.clusterService = clusterService;
}
static NamedWriteableRegistry getNamedWriteableRegistry()
{
IndicesModule indicesModule = new IndicesModule(Collections.emptyList());
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
entries.addAll(indicesModule.getNamedWriteables());
entries.addAll(searchModule.getNamedWriteables());
return new NamedWriteableRegistry(entries);
}
public void testDefaultSeedAddressesWithSmallProfilePortRange() {
testDefaultSeedAddresses(Settings.builder()
.put(TransportSettings.PORT_PROFILE.getConcreteSettingForNamespace(TransportSettings.DEFAULT_PROFILE).getKey(), "4300-4302")
.build(),
containsInAnyOrder(
"[::1]:4300", "[::1]:4301", "[::1]:4302",
"127.0.0.1:4300", "127.0.0.1:4301", "127.0.0.1:4302"));
}
@Override
public Settings indexSettings() {
Settings.Builder builder = Settings.builder();
builder.put(SETTING_NUMBER_OF_SHARDS, 1);
builder.put(SETTING_NUMBER_OF_REPLICAS, 0);
return builder.build();
}
public AbstractConfigurationValidator(final RestRequest request, final BytesReference ref, final Settings esSettings, Object... param) {
this.content = ref;
this.method = request.method();
this.esSettings = esSettings;
this.request = request;
this.param = param;
}
@Override
public Connection createPhysicalConnection(String url, Properties info) throws SQLException {
if (client == null) {
synchronized (this) {
if (client == null) {
Settings.Builder builder = Settings.builder();
info.forEach((k, v) -> builder.put(k.toString(), v.toString()));
String[] hostAndPortArray = url.split("/")[2].split(",");
int length = hostAndPortArray.length;
TransportAddress[] addresses = new TransportAddress[length];
try {
String[] hostAndPortArr;
for (int i = 0; i < length; ++i) {
hostAndPortArr = hostAndPortArray[i].split(":");
addresses[i] = new TransportAddress(InetAddress.getByName(hostAndPortArr[0]), Integer.parseInt(hostAndPortArr[1]));
}
} catch (UnknownHostException e) {
throw new SQLException(e);
}
client = new PreBuiltXPackTransportClient(builder.build()).addTransportAddresses(addresses);
}
}
}
Connection conn = new ElasticSearchConnection(client);
createCountUpdater.incrementAndGet(this);
return conn;
}
@Test
public void testLdapAuthenticationStartTLS() throws Exception {
final Settings settings = createBaseSettings()
.putList(ConfigConstants.LDAP_HOSTS, "localhost:" + ldapPort)
.put("users.u1.search", "(uid={0})").put(ConfigConstants.LDAPS_ENABLE_START_TLS, true)
.put("opendistro_security.ssl.transport.truststore_filepath",
FileHelper.getAbsoluteFilePathFromClassPath("ldap/truststore.jks"))
.put("verify_hostnames", false).put("path.home", ".").build();
final LdapUser user = (LdapUser) new LDAPAuthenticationBackend2(settings, null)
.authenticate(new AuthCredentials("jacksonm", "secret".getBytes(StandardCharsets.UTF_8)));
Assert.assertNotNull(user);
Assert.assertEquals("cn=Michael Jackson,ou=people,o=TEST", user.getName());
}
@Bean
public TransportClient transportClient(Settings settings) {
TransportClient client = TransportClient.builder().settings(settings).build();
for (String ip : this.esProperties.getIps().split(Constants.COMMA)) {
try {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), this.esProperties.getPort()));
} catch (UnknownHostException e) {
LOGGER.error("es集群主机名错误, ip: {}", ip);
}
}
return client;
}
static Netty4CorsConfig buildCorsConfig(Settings settings) {
if (SETTING_CORS_ENABLED.get(settings) == false) {
return Netty4CorsConfigBuilder.forOrigins().disable().build();
}
String origin = SETTING_CORS_ALLOW_ORIGIN.get(settings);
final Netty4CorsConfigBuilder builder;
if (Strings.isNullOrEmpty(origin)) {
builder = Netty4CorsConfigBuilder.forOrigins();
} else if (origin.equals(ANY_ORIGIN)) {
builder = Netty4CorsConfigBuilder.forAnyOrigin();
} else {
try {
Pattern p = RestUtils.checkCorsSettingForRegex(origin);
if (p == null) {
builder = Netty4CorsConfigBuilder.forOrigins(RestUtils.corsSettingAsArray(origin));
} else {
builder = Netty4CorsConfigBuilder.forPattern(p);
}
} catch (PatternSyntaxException e) {
throw new SettingsException("Bad regex in [" + SETTING_CORS_ALLOW_ORIGIN.getKey() + "]: [" + origin + "]", e);
}
}
if (SETTING_CORS_ALLOW_CREDENTIALS.get(settings)) {
builder.allowCredentials();
}
String[] strMethods = Strings.tokenizeToStringArray(SETTING_CORS_ALLOW_METHODS.get(settings), ",");
HttpMethod[] methods = Arrays.stream(strMethods)
.map(HttpMethod::valueOf)
.toArray(HttpMethod[]::new);
return builder.allowedRequestMethods(methods)
.maxAge(SETTING_CORS_MAX_AGE.get(settings))
.allowedRequestHeaders(Strings.tokenizeToStringArray(SETTING_CORS_ALLOW_HEADERS.get(settings), ","))
.shortCircuit()
.build();
}
@Override
public void afterIndexShardClosed(ShardId shardId, @Nullable IndexShard indexShard,
Settings indexSettings) {
for (IndexEventListener listener : listeners) {
try {
listener.afterIndexShardClosed(shardId, indexShard, indexSettings);
} catch (Exception e) {
logger.warn(() -> new ParameterizedMessage("[{}] failed to invoke after shard closed callback", shardId.getId()), e);
throw e;
}
}
}
@SuppressWarnings("unchecked")
public <Request extends ActionRequest, Response extends ActionResponse> void registerAction(GenericAction<Request, Response> action,
Class<? extends HttpAction<Request, Response>> httpAction) {
try {
HttpAction<Request, Response> instance = httpAction.getDeclaredConstructor(Settings.class).newInstance(settings);
actionMap.put(action.name(), new ActionEntry<>(action, instance));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e ) {
logger.error(e.getMessage(), e);
}
}
private CrateComponentLoader(Settings settings) {
ServiceLoader<CrateComponent> crateComponents = ServiceLoader.load(CrateComponent.class);
plugins = new ArrayList<>();
MapBuilder<Plugin, List<OnModuleReference>> onModuleReferences = MapBuilder.newMapBuilder();
for (CrateComponent crateComponent : crateComponents) {
logger.trace("Loading crateComponent: {}", crateComponent);
Plugin plugin = crateComponent.createPlugin(settings);
plugins.add(plugin);
List<OnModuleReference> list = Lists.newArrayList();
for (Method method : plugin.getClass().getDeclaredMethods()) {
if (!method.getName().equals("onModule")) {
continue;
}
if (method.getParameterTypes().length == 0 || method.getParameterTypes().length > 1) {
logger.warn("Plugin: {} implementing onModule with no parameters or more than one parameter", plugin.name());
continue;
}
Class moduleClass = method.getParameterTypes()[0];
if (!Module.class.isAssignableFrom(moduleClass)) {
logger.warn("Plugin: {} implementing onModule by the type is not of Module type {}", plugin.name(), moduleClass);
continue;
}
method.setAccessible(true);
//noinspection unchecked
list.add(new OnModuleReference(moduleClass, method));
}
if (!list.isEmpty()) {
onModuleReferences.put(plugin, list);
}
}
this.onModuleReferences = onModuleReferences.immutableMap();
}