diff --git a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java index b133a5e9125..67196f549f5 100644 --- a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java +++ b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java @@ -6,7 +6,7 @@ package org.elasticsearch.shield.audit.index; import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; @@ -66,7 +66,11 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.UnknownHostException; -import java.util.*; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Lock; @@ -74,7 +78,17 @@ import java.util.concurrent.locks.ReentrantLock; import static org.elasticsearch.shield.audit.AuditUtil.indices; import static org.elasticsearch.shield.audit.AuditUtil.restRequestContent; -import static org.elasticsearch.shield.audit.index.IndexAuditLevel.*; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ACCESS_DENIED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ACCESS_GRANTED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ANONYMOUS_ACCESS_DENIED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.AUTHENTICATION_FAILED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.CONNECTION_DENIED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.CONNECTION_GRANTED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.RUN_AS_DENIED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.RUN_AS_GRANTED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.SYSTEM_ACCESS_GRANTED; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.TAMPERED_REQUEST; +import static org.elasticsearch.shield.audit.index.IndexAuditLevel.parse; import static org.elasticsearch.shield.audit.index.IndexNameResolver.resolve; /** @@ -107,7 +121,7 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl RUN_AS_GRANTED.toString() }; - private static final ImmutableSet forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic"); + private static final String FORBIDDEN_INDEX_SETTING = "index.mapper.dynamic"; private final AtomicReference state = new AtomicReference<>(State.INITIALIZED); private final String nodeName; @@ -725,7 +739,7 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl Settings.Builder builder = Settings.builder(); for (Map.Entry entry : newSettings.getAsMap().entrySet()) { String name = "index." + entry.getKey(); - if (forbiddenIndexSettings.contains(name)) { + if (FORBIDDEN_INDEX_SETTING.equals(name)) { logger.warn("overriding the default [{}} setting is forbidden. ignoring...", name); continue; } diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersTool.java b/shield/src/main/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersTool.java index 903f1e72d68..f1dd95f9afb 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersTool.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/esusers/tool/ESUsersTool.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.shield.authc.esusers.tool; -import com.google.common.collect.ImmutableSet; import org.apache.commons.cli.CommandLine; import org.elasticsearch.common.Strings; import org.elasticsearch.common.cli.CheckFileCommand; @@ -27,7 +26,14 @@ import org.elasticsearch.shield.support.Validation; import java.nio.file.Files; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -431,7 +437,7 @@ public class ESUsersTool extends CliTool { @Override public ExitStatus execute(Settings settings, Environment env) throws Exception { Settings esusersSettings = Realms.internalRealmSettings(settings, ESUsersRealm.TYPE); - ImmutableSet knownRoles = loadRoleNames(terminal, settings, env); + Set knownRoles = loadRoleNames(terminal, settings, env); Path userRolesFilePath = FileUserRolesStore.resolveFile(esusersSettings, env); Map userRoles = FileUserRolesStore.parseFile(userRolesFilePath, null); Path userFilePath = FileUserPasswdStore.resolveFile(esusersSettings, env); @@ -493,7 +499,7 @@ public class ESUsersTool extends CliTool { } } - private static ImmutableSet loadRoleNames(Terminal terminal, Settings settings, Environment env) { + private static Set loadRoleNames(Terminal terminal, Settings settings, Environment env) { Path rolesFile = FileRolesStore.resolveFile(settings, env); try { return FileRolesStore.parseFileForRoleNames(rolesFile, null); @@ -520,7 +526,7 @@ public class ESUsersTool extends CliTool { } private static void verifyRoles(Terminal terminal, Settings settings, Environment env, String[] roles) { - ImmutableSet knownRoles = loadRoleNames(terminal, settings, env); + Set knownRoles = loadRoleNames(terminal, settings, env); Set unknownRoles = Sets.difference(Sets.newHashSet(roles), knownRoles); if (!unknownRoles.isEmpty()) { Path rolesFile = FileRolesStore.resolveFile(settings, env); diff --git a/shield/src/main/java/org/elasticsearch/shield/authz/Permission.java b/shield/src/main/java/org/elasticsearch/shield/authz/Permission.java index c48788e139e..277b3962a5a 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authz/Permission.java +++ b/shield/src/main/java/org/elasticsearch/shield/authz/Permission.java @@ -6,7 +6,7 @@ package org.elasticsearch.shield.authz; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.cluster.metadata.AliasOrIndex; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; @@ -30,6 +30,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; +import static java.util.Collections.unmodifiableSet; + /** * Represents a permission in the system. There are 3 types of permissions: * @@ -235,6 +237,7 @@ public interface Permission { return privilege; } + @Override public boolean check(String action) { return predicate.test(action); } @@ -346,8 +349,8 @@ public interface Permission { // by at least one indices permission group SortedMap allAliasesAndIndices = metaData.getAliasAndIndexLookup(); - Map> fieldsBuilder = new HashMap<>(); - Map> queryBuilder = new HashMap<>(); + Map> rolesFieldsByIndex = new HashMap<>(); + Map> roleQueriesByIndex = new HashMap<>(); Map grantedBuilder = new HashMap<>(); for (String indexOrAlias : requestedIndicesOrAliases) { @@ -365,20 +368,20 @@ public interface Permission { granted = true; for (String index : concreteIndices) { if (group.getFields() != null) { - ImmutableSet.Builder roleFieldsBuilder = fieldsBuilder.get(index); - if (roleFieldsBuilder == null) { - roleFieldsBuilder = ImmutableSet.builder(); - fieldsBuilder.put(index, roleFieldsBuilder); + Set roleFields = rolesFieldsByIndex.get(index); + if (roleFields == null) { + roleFields = new HashSet<>(); + rolesFieldsByIndex.put(index, roleFields); } - roleFieldsBuilder.addAll(group.getFields()); + roleFields.addAll(group.getFields()); } if (group.getQuery() != null) { - ImmutableSet.Builder roleQueriesBuilder = queryBuilder.get(index); - if (roleQueriesBuilder == null) { - roleQueriesBuilder = ImmutableSet.builder(); - queryBuilder.put(index, roleQueriesBuilder); + Set roleQueries = roleQueriesByIndex.get(index); + if (roleQueries == null) { + roleQueries = new HashSet<>(); + roleQueriesByIndex.put(index, roleQueries); } - roleQueriesBuilder.add(group.getQuery()); + roleQueries.add(group.getQuery()); } } } @@ -396,19 +399,13 @@ public interface Permission { ImmutableMap.Builder indexPermissions = ImmutableMap.builder(); for (Map.Entry entry : grantedBuilder.entrySet()) { String index = entry.getKey(); - ImmutableSet.Builder roleQueriesBuilder = queryBuilder.get(index); - ImmutableSet.Builder roleFieldsBuilder = fieldsBuilder.get(index); - final ImmutableSet roleFields; - if (roleFieldsBuilder != null) { - roleFields = roleFieldsBuilder.build(); - } else { - roleFields = null; + Set roleQueries = roleQueriesByIndex.get(index); + if (roleQueries != null) { + roleQueries = unmodifiableSet(roleQueries); } - final ImmutableSet roleQueries; - if (roleQueriesBuilder != null) { - roleQueries = roleQueriesBuilder.build(); - } else { - roleQueries = null; + Set roleFields = rolesFieldsByIndex.get(index); + if (roleFields != null) { + roleFields = unmodifiableSet(roleFields); } indexPermissions.put(index, new IndicesAccessControl.IndexAccessControl(entry.getValue(), roleFields, roleQueries)); } diff --git a/shield/src/main/java/org/elasticsearch/shield/authz/Privilege.java b/shield/src/main/java/org/elasticsearch/shield/authz/Privilege.java index 99bb76f6c94..e863b9de1b7 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authz/Privilege.java +++ b/shield/src/main/java/org/elasticsearch/shield/authz/Privilege.java @@ -5,11 +5,10 @@ */ package org.elasticsearch.shield.authz; -import com.google.common.collect.ImmutableSet; -import com.google.common.util.concurrent.UncheckedExecutionException; import dk.brics.automaton.Automaton; import dk.brics.automaton.BasicAutomata; import dk.brics.automaton.BasicOperations; + import org.elasticsearch.action.admin.indices.create.CreateIndexAction; import org.elasticsearch.action.get.GetAction; import org.elasticsearch.action.get.MultiGetAction; @@ -21,12 +20,16 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.shield.support.AutomatonPredicate; import org.elasticsearch.shield.support.Automatons; +import java.util.HashSet; import java.util.Locale; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.Predicate; +import static java.util.Collections.singleton; +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.shield.support.Automatons.patterns; /** @@ -401,6 +404,7 @@ public abstract class Privilege

> { return BasicOperations.subsetOf(other.automaton, automaton); } + @Override public String toString() { return name.toString(); } @@ -417,20 +421,20 @@ public abstract class Privilege

> { public static final Name NONE = new Name("none"); public static final Name ALL = new Name("all"); - private final ImmutableSet parts; + private final Set parts; public Name(String name) { assert name != null && !name.contains(","); - parts = ImmutableSet.of(name); + parts = singleton(name); } public Name(Set parts) { assert !parts.isEmpty(); - this.parts = ImmutableSet.copyOf(parts); + this.parts = unmodifiableSet(new HashSet<>(parts)); } public Name(String... parts) { - this(ImmutableSet.copyOf(parts)); + this(unmodifiableSet(newHashSet(parts))); } @Override diff --git a/shield/src/main/java/org/elasticsearch/shield/authz/accesscontrol/IndicesAccessControl.java b/shield/src/main/java/org/elasticsearch/shield/authz/accesscontrol/IndicesAccessControl.java index 14adc0612a5..1bec72f80af 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authz/accesscontrol/IndicesAccessControl.java +++ b/shield/src/main/java/org/elasticsearch/shield/authz/accesscontrol/IndicesAccessControl.java @@ -5,8 +5,6 @@ */ package org.elasticsearch.shield.authz.accesscontrol; -import com.google.common.collect.ImmutableSet; - import org.elasticsearch.common.Nullable; import org.elasticsearch.common.bytes.BytesReference; @@ -15,6 +13,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + /** * Encapsulates the field and document permissions per concrete index based on the current request. */ @@ -52,10 +52,10 @@ public class IndicesAccessControl { public static class IndexAccessControl { private final boolean granted; - private final ImmutableSet fields; - private final ImmutableSet queries; + private final Set fields; + private final Set queries; - public IndexAccessControl(boolean granted, ImmutableSet fields, ImmutableSet queries) { + public IndexAccessControl(boolean granted, Set fields, Set queries) { this.granted = granted; this.fields = fields; this.queries = queries; @@ -94,27 +94,27 @@ public class IndicesAccessControl { // this code is a bit of a pita, but right now we can't just initialize an empty set, // because an empty Set means no permissions on fields and // null means no field level security - ImmutableSet fields = null; + Set fields = null; if (this.fields != null || other.getFields() != null) { - Set _fields = new HashSet<>(); + fields = new HashSet<>(); if (this.fields != null) { - _fields.addAll(this.fields); + fields.addAll(this.fields); } if (other.getFields() != null) { - _fields.addAll(other.getFields()); + fields.addAll(other.getFields()); } - fields = ImmutableSet.copyOf(_fields); + fields = unmodifiableSet(fields); } - ImmutableSet queries = null; + Set queries = null; if (this.queries != null || other.getQueries() != null) { - Set _queries = new HashSet<>(); + queries = new HashSet<>(); if (this.queries != null) { - _queries.addAll(this.queries); + queries.addAll(this.queries); } if (other.getQueries() != null) { - _queries.addAll(other.getQueries()); + queries.addAll(other.getQueries()); } - queries = ImmutableSet.copyOf(_queries); + queries = unmodifiableSet(queries); } return new IndexAccessControl(granted, fields, queries); } diff --git a/shield/src/main/java/org/elasticsearch/shield/authz/store/FileRolesStore.java b/shield/src/main/java/org/elasticsearch/shield/authz/store/FileRolesStore.java index 9333af4be60..e95aa782437 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authz/store/FileRolesStore.java +++ b/shield/src/main/java/org/elasticsearch/shield/authz/store/FileRolesStore.java @@ -6,8 +6,7 @@ package org.elasticsearch.shield.authz.store; import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; @@ -37,9 +36,21 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableMap; +import static java.util.Collections.unmodifiableSet; + /** * */ @@ -51,10 +62,10 @@ public class FileRolesStore extends AbstractLifecycleComponent imple private final Path file; private final RefreshListener listener; - private final ImmutableSet reservedRoles; + private final Set reservedRoles; private final ResourceWatcherService watcherService; - private volatile ImmutableMap permissions; + private volatile Map permissions; @Inject public FileRolesStore(Settings settings, Environment env, ResourceWatcherService watcherService, Set reservedRoles) { @@ -66,8 +77,8 @@ public class FileRolesStore extends AbstractLifecycleComponent imple this.file = resolveFile(settings, env); this.listener = listener; this.watcherService = watcherService; - this.reservedRoles = ImmutableSet.copyOf(reservedRoles); - permissions = ImmutableMap.of(); + this.reservedRoles = unmodifiableSet(new HashSet<>(reservedRoles)); + permissions = emptyMap(); } @Override @@ -104,19 +115,19 @@ public class FileRolesStore extends AbstractLifecycleComponent imple return env.binFile().getParent().resolve(location); } - public static ImmutableSet parseFileForRoleNames(Path path, ESLogger logger) { - ImmutableMap roleMap = parseFile(path, Collections.emptySet(), logger, false); + public static Set parseFileForRoleNames(Path path, ESLogger logger) { + Map roleMap = parseFile(path, Collections.emptySet(), logger, false); if (roleMap == null) { - return ImmutableSet.builder().build(); + return emptySet(); } return roleMap.keySet(); } - public static ImmutableMap parseFile(Path path, Set reservedRoles, ESLogger logger) { + public static Map parseFile(Path path, Set reservedRoles, ESLogger logger) { return parseFile(path, reservedRoles, logger, true); } - public static ImmutableMap parseFile(Path path, Set reservedRoles, ESLogger logger, boolean resolvePermission) { + public static Map parseFile(Path path, Set reservedRoles, ESLogger logger, boolean resolvePermission) { if (logger == null) { logger = NoOpLogger.INSTANCE; } @@ -150,7 +161,7 @@ public class FileRolesStore extends AbstractLifecycleComponent imple roles.put(reservedRole.name(), reservedRole); } - return ImmutableMap.copyOf(roles); + return unmodifiableMap(roles); } private static Permission.Global.Role parseRole(String segment, Path path, ESLogger logger, boolean resolvePermissions) { diff --git a/shield/src/test/java/org/elasticsearch/shield/audit/AuditTrailServiceTests.java b/shield/src/test/java/org/elasticsearch/shield/audit/AuditTrailServiceTests.java index 4c67b313b0d..2231184ef70 100644 --- a/shield/src/test/java/org/elasticsearch/shield/audit/AuditTrailServiceTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/audit/AuditTrailServiceTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.shield.audit; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.shield.User; @@ -18,8 +17,10 @@ import org.junit.Before; import org.junit.Test; import java.net.InetAddress; +import java.util.HashSet; import java.util.Set; +import static java.util.Collections.unmodifiableSet; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -37,11 +38,11 @@ public class AuditTrailServiceTests extends ESTestCase { @Before public void init() throws Exception { - ImmutableSet.Builder builder = ImmutableSet.builder(); + Set auditTrailsBuilder = new HashSet<>(); for (int i = 0; i < randomIntBetween(1, 4); i++) { - builder.add(mock(AuditTrail.class)); + auditTrailsBuilder.add(mock(AuditTrail.class)); } - auditTrails = builder.build(); + auditTrails = unmodifiableSet(auditTrailsBuilder); service = new AuditTrailService(Settings.EMPTY, auditTrails); token = mock(AuthenticationToken.class); message = mock(TransportMessage.class); diff --git a/shield/src/test/java/org/elasticsearch/shield/authc/esusers/ESUsersRealmTests.java b/shield/src/test/java/org/elasticsearch/shield/authc/esusers/ESUsersRealmTests.java index 2910f0f6d88..f5fc1a216c8 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authc/esusers/ESUsersRealmTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authc/esusers/ESUsersRealmTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.shield.authc.esusers; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.action.Action; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; @@ -16,7 +15,6 @@ import org.elasticsearch.client.ClusterAdminClient; import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.client.support.Headers; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.env.Environment; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestController; @@ -29,14 +27,24 @@ import org.elasticsearch.shield.authc.support.UsernamePasswordToken; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.watcher.ResourceWatcherService; -import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import java.util.Locale; -import static org.hamcrest.Matchers.*; -import static org.mockito.Mockito.*; +import static java.util.Collections.emptySet; +import static org.hamcrest.Matchers.arrayContaining; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.sameInstance; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * @@ -184,7 +192,7 @@ public class ESUsersRealmTests extends ESTestCase { RestController restController = mock(RestController.class); RealmConfig config = new RealmConfig("esusers-test", Settings.EMPTY, globalSettings); new ESUsersRealm(config, new UserPasswdStore(config), new UserRolesStore(config)); - when(restController.relevantHeaders()).thenReturn(ImmutableSet.of()); + when(restController.relevantHeaders()).thenReturn(emptySet()); when(client.admin()).thenReturn(adminClient); when(client.settings()).thenReturn(Settings.EMPTY); when(client.headers()).thenReturn(Headers.EMPTY); diff --git a/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperIntegrationTests.java b/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperIntegrationTests.java index 09c955699c8..87d7c5d19f5 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperIntegrationTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperIntegrationTests.java @@ -6,13 +6,24 @@ package org.elasticsearch.shield.authz.accesscontrol; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.search.*; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TotalHitCountCollector; +import org.apache.lucene.search.Weight; import org.apache.lucene.search.join.BitSetProducer; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BitSet; @@ -22,16 +33,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.IndexService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.EngineConfig; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.IndexQueryParserService; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesLifecycle; -import org.elasticsearch.indices.IndicesWarmer; import org.elasticsearch.shield.authz.InternalAuthorizationService; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportRequest; @@ -39,12 +47,14 @@ import org.mockito.Matchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import java.io.IOException; import java.util.Collection; import java.util.Collections; +import static java.util.Collections.singleton; import static org.hamcrest.Matchers.equalTo; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -65,7 +75,7 @@ public class ShieldIndexSearcherWrapperIntegrationTests extends ESTestCase { TransportRequest request = new TransportRequest.Empty(); RequestContext.setCurrent(new RequestContext(request)); - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, null, ImmutableSet.of(new BytesArray("{}"))); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, null, singleton(new BytesArray("{}"))); request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl))); IndexQueryParserService parserService = mock(IndexQueryParserService.class); diff --git a/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperUnitTests.java b/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperUnitTests.java index cf4125b8c39..62a4158e854 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperUnitTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authz/accesscontrol/ShieldIndexSearcherWrapperUnitTests.java @@ -6,13 +6,26 @@ package org.elasticsearch.shield.authz.accesscontrol; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.*; -import org.apache.lucene.search.*; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.TermsEnum; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.LeafCollector; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.QueryCachingPolicy; +import org.apache.lucene.search.Scorer; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.Weight; import org.apache.lucene.search.similarities.BM25Similarity; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; @@ -41,14 +54,17 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportRequest; import org.junit.After; import org.junit.Before; -import org.junit.Test; import java.io.IOException; +import static java.util.Collections.emptySet; +import static java.util.Collections.singleton; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.shield.authz.accesscontrol.ShieldIndexSearcherWrapper.intersectScorerAndRoleBits; -import static org.hamcrest.Matchers.*; -import static org.mockito.Matchers.eq; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.sameInstance; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -99,7 +115,7 @@ public class ShieldIndexSearcherWrapperUnitTests extends ESTestCase { .endObject().endObject(); mapperService.merge("type", new CompressedXContent(mappingSource.string()), false, false); - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, ImmutableSet.of(), null); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, emptySet(), null); request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl))); FieldSubsetReader.FieldSubsetDirectoryReader result = (FieldSubsetReader.FieldSubsetDirectoryReader) shieldIndexSearcherWrapper.wrap(esIn); @@ -309,7 +325,7 @@ public class ShieldIndexSearcherWrapperUnitTests extends ESTestCase { } private void assertResolvedFields(String expression, String... expectedFields) { - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, ImmutableSet.of(expression), null); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, singleton(expression), null); request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl))); FieldSubsetReader.FieldSubsetDirectoryReader result = (FieldSubsetReader.FieldSubsetDirectoryReader) shieldIndexSearcherWrapper.wrap(esIn); assertThat(result.getFieldNames().size() - shieldIndexSearcherWrapper.getAllowedMetaFields().size(), equalTo(expectedFields.length)); diff --git a/shield/src/test/java/org/elasticsearch/shield/authz/store/FileRolesStoreTests.java b/shield/src/test/java/org/elasticsearch/shield/authz/store/FileRolesStoreTests.java index dab08360dad..7abcac78f03 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authz/store/FileRolesStoreTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authz/store/FileRolesStoreTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.shield.authz.store; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.shield.audit.logfile.CapturingLogger; @@ -30,7 +29,17 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.hamcrest.Matchers.*; +import static java.util.Collections.singleton; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.startsWith; /** * @@ -250,7 +259,7 @@ public class FileRolesStoreTests extends ESTestCase { public void testThatRoleNamesDoesNotResolvePermissions() throws Exception { Path path = getDataPath("invalid_roles.yml"); CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.ERROR); - ImmutableSet roleNames = FileRolesStore.parseFileForRoleNames(path, logger); + Set roleNames = FileRolesStore.parseFileForRoleNames(path, logger); assertThat(roleNames.size(), is(5)); assertThat(roleNames, containsInAnyOrder("valid_role", "role1", "role2", "role3", "role4")); @@ -261,11 +270,9 @@ public class FileRolesStoreTests extends ESTestCase { @Test public void testReservedRoles() throws Exception { - Set reservedRoles = ImmutableSet.builder() - .add(Permission.Global.Role.builder("reserved") + Set reservedRoles = singleton(Permission.Global.Role.builder("reserved") .cluster(Privilege.Cluster.ALL) - .build()) - .build(); + .build()); CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.INFO); @@ -294,11 +301,9 @@ public class FileRolesStoreTests extends ESTestCase { @Test public void testReservedRolesNonExistentRolesFile() throws Exception { - Set reservedRoles = ImmutableSet.builder() - .add(Permission.Global.Role.builder("reserved") + Set reservedRoles = singleton(Permission.Global.Role.builder("reserved") .cluster(Privilege.Cluster.ALL) - .build()) - .build(); + .build()); CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.INFO); diff --git a/shield/src/test/java/org/elasticsearch/transport/KnownActionsTests.java b/shield/src/test/java/org/elasticsearch/transport/KnownActionsTests.java index 52a5be3e77a..97b2204aac6 100644 --- a/shield/src/test/java/org/elasticsearch/transport/KnownActionsTests.java +++ b/shield/src/test/java/org/elasticsearch/transport/KnownActionsTests.java @@ -5,12 +5,10 @@ */ package org.elasticsearch.transport; -import com.google.common.collect.ImmutableSet; import org.apache.lucene.util.IOUtils; import org.elasticsearch.action.Action; import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.Streams; -import org.elasticsearch.common.util.Callback; import org.elasticsearch.license.plugin.LicensePlugin; import org.elasticsearch.shield.action.ShieldActionModule; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; @@ -25,22 +23,29 @@ import java.lang.reflect.Modifier; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.*; +import java.nio.file.DirectoryStream; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import static java.util.Collections.unmodifiableSet; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; @ClusterScope(numClientNodes = 0, numDataNodes = 1) public class KnownActionsTests extends ShieldIntegTestCase { - - private static ImmutableSet knownActions; - private static ImmutableSet knownHandlers; - private static ImmutableSet codeActions; + private static Set knownActions; + private static Set knownHandlers; + private static Set codeActions; @BeforeClass public static void init() throws Exception { @@ -81,38 +86,26 @@ public class KnownActionsTests extends ShieldIntegTestCase { } } - public static ImmutableSet loadKnownActions() { - final ImmutableSet.Builder knownActionsBuilder = ImmutableSet.builder(); - try (InputStream input = KnownActionsTests.class.getResourceAsStream("actions")) { - Streams.readAllLines(input, new Callback() { - @Override - public void handle(String action) { - knownActionsBuilder.add(action); - } - }); - } catch (IOException ioe) { - throw new IllegalStateException("could not load known actions", ioe); - } - return knownActionsBuilder.build(); + public static Set loadKnownActions() { + return readSetFromResource("actions"); } - public static ImmutableSet loadKnownHandlers() { - final ImmutableSet.Builder knownHandlersBuilder = ImmutableSet.builder(); - try (InputStream input = KnownActionsTests.class.getResourceAsStream("handlers")) { - Streams.readAllLines(input, new Callback() { - @Override - public void handle(String action) { - knownHandlersBuilder.add(action); - } - }); - } catch (IOException ioe) { - throw new IllegalStateException("could not load known handlers", ioe); - } - return knownHandlersBuilder.build(); + public static Set loadKnownHandlers() { + return readSetFromResource("handlers"); } - private static ImmutableSet loadCodeActions() throws IOException, ReflectiveOperationException, URISyntaxException { - ImmutableSet.Builder actions = ImmutableSet.builder(); + private static Set readSetFromResource(String resource) { + Set knownActions = new HashSet<>(); + try (InputStream input = KnownActionsTests.class.getResourceAsStream(resource)) { + Streams.readAllLines(input, action -> knownActions.add(action)); + } catch (IOException ioe) { + throw new IllegalStateException("could not load known " + resource, ioe); + } + return unmodifiableSet(knownActions); + } + + private static Set loadCodeActions() throws IOException, ReflectiveOperationException, URISyntaxException { + Set actions = new HashSet<>(); // loading es core actions loadActions(collectSubClasses(Action.class, Action.class), actions); @@ -123,10 +116,10 @@ public class KnownActionsTests extends ShieldIntegTestCase { // also loading all actions from the licensing plugin loadActions(collectSubClasses(Action.class, LicensePlugin.class), actions); - return actions.build(); + return unmodifiableSet(actions); } - private static void loadActions(Collection> clazzes, ImmutableSet.Builder actions) throws ReflectiveOperationException { + private static void loadActions(Collection> clazzes, Set actions) throws ReflectiveOperationException { for (Class clazz : clazzes) { if (!Modifier.isAbstract(clazz.getModifiers())) { Field field = null; @@ -153,7 +146,7 @@ public class KnownActionsTests extends ShieldIntegTestCase { if (codeLocation.toURI().toString().endsWith(".jar")) { try { // hack around a bug in the zipfilesystem implementation before java 9, - // its checkWritable was incorrect and it won't work without write permissions. + // its checkWritable was incorrect and it won't work without write permissions. // if we add the permission, it will open jars r/w, which is too scary! so copy to a safe r-w location. Path tmp = createTempFile(null, ".jar"); try (InputStream in = codeLocation.openStream()) { diff --git a/watcher/src/main/java/org/elasticsearch/watcher/history/HistoryStore.java b/watcher/src/main/java/org/elasticsearch/watcher/history/HistoryStore.java index ac37f09128c..16e57e96d39 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/history/HistoryStore.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/history/HistoryStore.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.history; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; @@ -33,7 +32,6 @@ public class HistoryStore extends AbstractComponent { public static final String DOC_TYPE = "watch_record"; static final DateTimeFormatter indexTimeFormat = DateTimeFormat.forPattern("YYYY.MM.dd"); - private static final ImmutableSet forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic"); private final ClientProxy client; diff --git a/watcher/src/main/java/org/elasticsearch/watcher/input/http/HttpInput.java b/watcher/src/main/java/org/elasticsearch/watcher/input/http/HttpInput.java index 425e2014136..25262274eec 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/input/http/HttpInput.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/input/http/HttpInput.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.input.http; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; @@ -20,9 +19,12 @@ import org.elasticsearch.watcher.watch.Payload; import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + /** * */ @@ -171,7 +173,7 @@ public class HttpInput implements Input { public static class Builder implements Input.Builder { private final HttpRequestTemplate request; - private final ImmutableSet.Builder extractKeys = ImmutableSet.builder(); + private final Set extractKeys = new HashSet<>(); private HttpContentType expectedResponseXContentType = null; private Builder(HttpRequestTemplate request) { @@ -184,7 +186,7 @@ public class HttpInput implements Input { } public Builder extractKeys(String... keys) { - extractKeys.add(keys); + Collections.addAll(extractKeys, keys); return this; } @@ -195,8 +197,7 @@ public class HttpInput implements Input { @Override public HttpInput build() { - ImmutableSet keys = extractKeys.build(); - return new HttpInput(request, expectedResponseXContentType, keys.isEmpty() ? null : keys); + return new HttpInput(request, expectedResponseXContentType, extractKeys.isEmpty() ? null : unmodifiableSet(extractKeys)); } } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/input/search/SearchInput.java b/watcher/src/main/java/org/elasticsearch/watcher/input/search/SearchInput.java index 200343bf9b1..f1519146eb9 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/input/search/SearchInput.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/input/search/SearchInput.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.input.search; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.Nullable; @@ -23,9 +22,12 @@ import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.Set; +import static java.util.Collections.unmodifiableSet; + /** * */ @@ -193,7 +195,7 @@ public class SearchInput implements Input { public static class Builder implements Input.Builder { private final SearchRequest request; - private final ImmutableSet.Builder extractKeys = ImmutableSet.builder(); + private final Set extractKeys = new HashSet<>(); private TimeValue timeout; private DateTimeZone dynamicNameTimeZone; @@ -207,7 +209,7 @@ public class SearchInput implements Input { } public Builder extractKeys(String... keys) { - extractKeys.add(keys); + Collections.addAll(extractKeys, keys); return this; } @@ -223,8 +225,7 @@ public class SearchInput implements Input { @Override public SearchInput build() { - Set keys = extractKeys.build(); - return new SearchInput(request, keys.isEmpty() ? null : keys, timeout, dynamicNameTimeZone); + return new SearchInput(request, extractKeys.isEmpty() ? null : unmodifiableSet(extractKeys), timeout, dynamicNameTimeZone); } } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherIndexTemplateRegistry.java b/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherIndexTemplateRegistry.java index fc4612b352a..559dc70879d 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherIndexTemplateRegistry.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherIndexTemplateRegistry.java @@ -6,8 +6,7 @@ package org.elasticsearch.watcher.support; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.util.concurrent.MoreExecutors; + import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; import org.elasticsearch.cluster.ClusterChangedEvent; @@ -27,20 +26,22 @@ import org.elasticsearch.watcher.support.init.proxy.ClientProxy; import org.elasticsearch.watcher.watch.WatchStore; import java.io.InputStream; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.Executor; +import static java.util.Collections.unmodifiableSet; + /** */ public class WatcherIndexTemplateRegistry extends AbstractComponent implements ClusterStateListener, NodeSettingsService.Listener { - - private static final ImmutableSet forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic"); + private static final String FORBIDDEN_INDEX_SETTING = "index.mapper.dynamic"; private final ClientProxy client; private final ThreadPool threadPool; private final ClusterService clusterService; - private final ImmutableSet indexTemplates; + private final Set indexTemplates; private volatile ImmutableMap customIndexSettings; @@ -51,7 +52,7 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C this.client = client; this.threadPool = threadPool; this.clusterService = clusterService; - this.indexTemplates = ImmutableSet.copyOf(configs); + this.indexTemplates = unmodifiableSet(new HashSet<>(configs)); clusterService.add(this); nodeSettingsService.addListener(this); @@ -126,7 +127,7 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C Settings.Builder builder = Settings.builder().put(existingSettings); for (Map.Entry newSettingsEntry : newSettings.getAsMap().entrySet()) { String name = "index." + newSettingsEntry.getKey(); - if (forbiddenIndexSettings.contains(name)) { + if (FORBIDDEN_INDEX_SETTING.equals(name)) { logger.warn("overriding the default [{}} setting is forbidden. ignoring...", name); continue; } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java index 1c0852d4e5f..725a25a9a94 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule.support; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.Strings; @@ -19,6 +18,7 @@ import java.util.HashSet; import java.util.Locale; import java.util.Set; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; import static org.elasticsearch.watcher.support.Strings.join; @@ -87,7 +87,7 @@ public class MonthTimes implements Times { if (!Arrays.equals(days, that.days)) return false; // order doesn't matter - if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false; + if (!newHashSet(times).equals(newHashSet(that.times))) return false; return true; } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java index 5e9bf53ce99..404c102f49a 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java @@ -5,15 +5,19 @@ */ package org.elasticsearch.watcher.trigger.schedule.support; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.watcher.support.Strings.join; /** @@ -73,7 +77,7 @@ public class WeekTimes implements Times { if (!days.equals(that.days)) return false; // we don't care about order - if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false; + if (!newHashSet(times).equals(newHashSet(that.times))) return false; return true; } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java index ca96b30d449..956fd02749b 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule.support; -import com.google.common.collect.ImmutableSet; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.Strings; @@ -14,8 +13,14 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; import static org.elasticsearch.watcher.support.Strings.join; @@ -91,7 +96,7 @@ public class YearTimes implements Times { if (!Arrays.equals(days, that.days)) return false; if (!months.equals(that.months)) return false; // order doesn't matter - if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false; + if (!newHashSet(times).equals(newHashSet(that.times))) return false; return true; } diff --git a/watcher/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java b/watcher/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java index e88b0067114..a11abe43d92 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.watcher.actions.index; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.Settings; @@ -32,11 +32,18 @@ import org.junit.Test; import java.util.Arrays; import java.util.Map; +import static java.util.Collections.unmodifiableSet; +import static org.elasticsearch.common.util.set.Sets.newHashSet; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.joda.time.DateTimeZone.UTC; /** @@ -129,7 +136,7 @@ public class IndexActionTests extends ESIntegTestCase { Object list = randomFrom( new Map[] { ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1") }, Arrays.asList(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1")), - ImmutableSet.of(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1")) + unmodifiableSet(newHashSet(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1"))) ); IndexAction action = new IndexAction("test-index", "test-type", timestampField, null, null); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java b/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java index 505cedcb8d5..a24f1cb1342 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.watcher.support; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -14,9 +13,15 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; -import static java.util.concurrent.TimeUnit.*; +import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentParser; import static org.hamcrest.CoreMatchers.nullValue; @@ -63,11 +68,10 @@ public class WatcherDateTimeUtilsTests extends ESTestCase { @Test public void testParseTimeValue_String() throws Exception { int value = randomIntBetween(2, 200); - ImmutableMap values = ImmutableMap.builder() - .put(value + "s", TimeValue.timeValueSeconds(value)) - .put(value + "m", TimeValue.timeValueMinutes(value)) - .put(value + "h", TimeValue.timeValueHours(value)) - .build(); + Map values = new HashMap<>(); + values.put(value + "s", TimeValue.timeValueSeconds(value)); + values.put(value + "m", TimeValue.timeValueMinutes(value)); + values.put(value + "h", TimeValue.timeValueHours(value)); String key = randomFrom(values.keySet().toArray(new String[values.size()])); @@ -84,11 +88,10 @@ public class WatcherDateTimeUtilsTests extends ESTestCase { @Test(expected = ElasticsearchParseException.class) public void testParseTimeValue_String_Negative() throws Exception { int value = -1 * randomIntBetween(2, 200); - ImmutableMap values = ImmutableMap.builder() - .put(value + "s", TimeValue.timeValueSeconds(value)) - .put(value + "m", TimeValue.timeValueMinutes(value)) - .put(value + "h", TimeValue.timeValueHours(value)) - .build(); + Map values = new HashMap<>(); + values.put(value + "s", TimeValue.timeValueSeconds(value)); + values.put(value + "m", TimeValue.timeValueMinutes(value)); + values.put(value + "h", TimeValue.timeValueHours(value)); String key = randomFrom(values.keySet().toArray(new String[values.size()])); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheTests.java b/watcher/src/test/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheTests.java index 32677562e85..2188b9e06b6 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/support/text/xmustache/XMustacheTests.java @@ -6,9 +6,9 @@ package org.elasticsearch.watcher.support.text.xmustache; import com.fasterxml.jackson.core.io.JsonStringEncoder; -import org.elasticsearch.common.bytes.BytesReference; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.script.CompiledScript; @@ -25,7 +25,13 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import static org.hamcrest.Matchers.*; +import static java.util.Collections.singleton; +import static org.elasticsearch.common.util.set.Sets.newHashSet; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; /** * @@ -46,14 +52,21 @@ public class XMustacheTests extends ESTestCase { Map vars = new HashMap<>(); Object data = randomFrom( new String[] { "foo", "bar" }, - Arrays.asList("foo", "bar"), - ImmutableSet.of("foo", "bar")); + Arrays.asList("foo", "bar")); vars.put("data", data); Object output = engine.execute(mustache, vars); assertThat(output, notNullValue()); assertThat(output, instanceOf(BytesReference.class)); BytesReference bytes = (BytesReference) output; assertThat(bytes.toUtf8(), equalTo("foo bar")); + + // Sets can come out in any order + vars.put("data", newHashSet("foo", "bar")); + output = engine.execute(mustache, vars); + assertThat(output, notNullValue()); + assertThat(output, instanceOf(BytesReference.class)); + bytes = (BytesReference) output; + assertThat(bytes.toUtf8(), both(containsString("foo")).and(containsString("bar"))); } @Test @@ -64,7 +77,7 @@ public class XMustacheTests extends ESTestCase { Object data = randomFrom( new String[][] { new String[] { "foo", "bar" }}, Collections.singletonList(new String[] { "foo", "bar" }), - ImmutableSet.of(new String[] { "foo", "bar" }) + singleton(new String[] { "foo", "bar" }) ); vars.put("data", data); Object output = engine.execute(mustache, vars); @@ -81,14 +94,22 @@ public class XMustacheTests extends ESTestCase { Map vars = new HashMap<>(); Object data = randomFrom( new Map[] { ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar") }, - Arrays.asList(ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar")), - ImmutableSet.of(ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar"))); + Arrays.asList(ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar"))); vars.put("data", data); Object output = engine.execute(mustache, vars); assertThat(output, notNullValue()); assertThat(output, instanceOf(BytesReference.class)); BytesReference bytes = (BytesReference) output; assertThat(bytes.toUtf8(), equalTo("foo bar")); + + // HashSet iteration order isn't fixed + vars.put("data", newHashSet(ImmutableMap.of("key", "foo"), ImmutableMap.of("key", "bar"))); + output = engine.execute(mustache, vars); + assertThat(output, notNullValue()); + assertThat(output, instanceOf(BytesReference.class)); + bytes = (BytesReference) output; + assertThat(bytes.toUtf8(), both(containsString("foo")).and(containsString("bar"))); + } @Test diff --git a/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java b/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java index 4dbc11a3114..36ecba54b6d 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/transform/script/ScriptTransformTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.watcher.transform.script; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -32,10 +32,18 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; +import static java.util.Collections.singleton; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; -import static org.elasticsearch.watcher.test.WatcherTestUtils.*; -import static org.hamcrest.Matchers.*; +import static org.elasticsearch.watcher.test.WatcherTestUtils.EMPTY_PAYLOAD; +import static org.elasticsearch.watcher.test.WatcherTestUtils.getScriptServiceProxy; +import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext; +import static org.elasticsearch.watcher.test.WatcherTestUtils.simplePayload; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -133,7 +141,7 @@ public class ScriptTransformTests extends ESTestCase { Map model = Variables.createCtxModel(ctx, payload); ExecutableScript executable = mock(ExecutableScript.class); - Object value = randomFrom("value", 1, new String[] { "value" }, Arrays.asList("value"), ImmutableSet.of("value")); + Object value = randomFrom("value", 1, new String[] { "value" }, Arrays.asList("value"), singleton("value")); when(executable.run()).thenReturn(value); when(service.executable(compiledScript, model)).thenReturn(executable); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java b/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java index ae29c9a4218..43a53812966 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java @@ -6,7 +6,7 @@ package org.elasticsearch.watcher.watch; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.logging.ESLogger; @@ -16,7 +16,11 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.watcher.actions.*; +import org.elasticsearch.watcher.actions.ActionFactory; +import org.elasticsearch.watcher.actions.ActionRegistry; +import org.elasticsearch.watcher.actions.ActionStatus; +import org.elasticsearch.watcher.actions.ActionWrapper; +import org.elasticsearch.watcher.actions.ExecutableActions; import org.elasticsearch.watcher.actions.email.DataAttachment; import org.elasticsearch.watcher.actions.email.EmailAction; import org.elasticsearch.watcher.actions.email.EmailActionFactory; @@ -92,19 +96,43 @@ import org.elasticsearch.watcher.transform.search.SearchTransformFactory; import org.elasticsearch.watcher.trigger.Trigger; import org.elasticsearch.watcher.trigger.TriggerEngine; import org.elasticsearch.watcher.trigger.TriggerService; -import org.elasticsearch.watcher.trigger.schedule.*; -import org.elasticsearch.watcher.trigger.schedule.support.*; +import org.elasticsearch.watcher.trigger.schedule.CronSchedule; +import org.elasticsearch.watcher.trigger.schedule.DailySchedule; +import org.elasticsearch.watcher.trigger.schedule.HourlySchedule; +import org.elasticsearch.watcher.trigger.schedule.IntervalSchedule; +import org.elasticsearch.watcher.trigger.schedule.MonthlySchedule; +import org.elasticsearch.watcher.trigger.schedule.Schedule; +import org.elasticsearch.watcher.trigger.schedule.ScheduleRegistry; +import org.elasticsearch.watcher.trigger.schedule.ScheduleTrigger; +import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerEngine; +import org.elasticsearch.watcher.trigger.schedule.WeeklySchedule; +import org.elasticsearch.watcher.trigger.schedule.YearlySchedule; +import org.elasticsearch.watcher.trigger.schedule.support.DayOfWeek; +import org.elasticsearch.watcher.trigger.schedule.support.Month; +import org.elasticsearch.watcher.trigger.schedule.support.MonthTimes; +import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes; +import org.elasticsearch.watcher.trigger.schedule.support.YearTimes; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.Before; import org.junit.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import static java.util.Collections.singleton; import static org.elasticsearch.watcher.input.InputBuilders.searchInput; import static org.elasticsearch.watcher.test.WatcherTestUtils.matchAllRequest; import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; import static org.joda.time.DateTimeZone.UTC; import static org.mockito.Mockito.mock; @@ -147,7 +175,7 @@ public class WatchTests extends ESTestCase { Trigger trigger = new ScheduleTrigger(schedule); ScheduleRegistry scheduleRegistry = registry(schedule); TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, clock); - TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine)); + TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine)); SecretService secretService = new SecretService.PlainText(); ExecutableInput input = randomInput(); @@ -197,7 +225,7 @@ public class WatchTests extends ESTestCase { ClockMock clock = new ClockMock(); ScheduleRegistry scheduleRegistry = registry(randomSchedule()); TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, clock); - TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine)); + TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine)); SecretService secretService = new SecretService.PlainText(); ExecutableCondition condition = randomCondition(); ConditionRegistry conditionRegistry = registry(condition); @@ -228,7 +256,7 @@ public class WatchTests extends ESTestCase { Schedule schedule = randomSchedule(); ScheduleRegistry scheduleRegistry = registry(schedule); TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, SystemClock.INSTANCE); - TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine)); + TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine)); SecretService secretService = new SecretService.PlainText(); ConditionRegistry conditionRegistry = registry(new ExecutableAlwaysCondition(logger));