use brics automaton instead of lucene, so we don't depend on lucene

Closes elastic/elasticsearch#710

Original commit: elastic/x-pack-elasticsearch@71ca4e6241
This commit is contained in:
Robert Muir 2015-02-12 15:24:42 -05:00
parent 0aeeb8c26f
commit e7f141bd5c
7 changed files with 50 additions and 43 deletions

View File

@ -11,11 +11,6 @@ java.util.concurrent.Executors#privilegedThreadFactory()
java.lang.Character#codePointBefore(char[],int) @ Implicit start offset is error-prone when the char[] is a buffer and the first chars are random chars java.lang.Character#codePointBefore(char[],int) @ Implicit start offset is error-prone when the char[] is a buffer and the first chars are random chars
java.lang.Character#codePointAt(char[],int) @ Implicit end offset is error-prone when the char[] is a buffer and the last chars are random chars java.lang.Character#codePointAt(char[],int) @ Implicit end offset is error-prone when the char[] is a buffer and the last chars are random chars
@defaultMessage Collections.sort dumps data into an array, sorts the array and reinserts data into the list, one should rather use Lucene's CollectionUtil sort methods which sort in place
java.util.Collections#sort(java.util.List)
java.util.Collections#sort(java.util.List,java.util.Comparator)
java.io.StringReader#<init>(java.lang.String) @ Use FastStringReader instead java.io.StringReader#<init>(java.lang.String) @ Use FastStringReader instead
@defaultMessage Reference management is tricky, leave it to SearcherManager @defaultMessage Reference management is tricky, leave it to SearcherManager

View File

@ -177,6 +177,13 @@
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
<version>1.10</version> <version>1.10</version>
</dependency> </dependency>
<dependency>
<groupId>dk.brics.automaton</groupId>
<artifactId>automaton</artifactId>
<version>1.11-8</version>
</dependency>
</dependencies> </dependencies>

View File

@ -5,7 +5,6 @@
*/ */
package org.elasticsearch.shield.authc; package org.elasticsearch.shield.authc;
import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.collect.Lists; import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.Sets;
@ -100,7 +99,7 @@ public class Realms extends AbstractLifecycleComponent<Realms> implements Iterab
} }
if (!realms.isEmpty()) { if (!realms.isEmpty()) {
CollectionUtil.introSort(realms); Collections.sort(realms);
return realms; return realms;
} }

View File

@ -5,9 +5,10 @@
*/ */
package org.elasticsearch.shield.authz; package org.elasticsearch.shield.authz;
import org.apache.lucene.util.automaton.Automata; import dk.brics.automaton.Automaton;
import org.apache.lucene.util.automaton.Automaton; import dk.brics.automaton.BasicAutomata;
import org.apache.lucene.util.automaton.Operations; import dk.brics.automaton.BasicOperations;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.admin.indices.create.CreateIndexAction; import org.elasticsearch.action.admin.indices.create.CreateIndexAction;
@ -115,7 +116,7 @@ public abstract class Privilege<P extends Privilege<P>> {
public static class General extends AutomatonPrivilege<General> { public static class General extends AutomatonPrivilege<General> {
private static final General NONE = new General(Name.NONE, Automata.makeEmpty()); private static final General NONE = new General(Name.NONE, BasicAutomata.makeEmpty());
public General(String name, String... patterns) { public General(String name, String... patterns) {
super(name, patterns); super(name, patterns);
@ -143,7 +144,7 @@ public abstract class Privilege<P extends Privilege<P>> {
public static class Index extends AutomatonPrivilege<Index> { public static class Index extends AutomatonPrivilege<Index> {
public static final Index NONE = new Index(Name.NONE, Automata.makeEmpty()); public static final Index NONE = new Index(Name.NONE, BasicAutomata.makeEmpty());
public static final Index ALL = new Index(Name.ALL, "indices:*"); public static final Index ALL = new Index(Name.ALL, "indices:*");
public static final Index MANAGE = new Index("manage", "indices:monitor/*", "indices:admin/*"); public static final Index MANAGE = new Index("manage", "indices:monitor/*", "indices:admin/*");
public static final Index CREATE_INDEX = new Index("create_index", CreateIndexAction.NAME); public static final Index CREATE_INDEX = new Index("create_index", CreateIndexAction.NAME);
@ -246,7 +247,7 @@ public abstract class Privilege<P extends Privilege<P>> {
public static class Cluster extends AutomatonPrivilege<Cluster> { public static class Cluster extends AutomatonPrivilege<Cluster> {
public static final Cluster NONE = new Cluster(Name.NONE, Automata.makeEmpty()); public static final Cluster NONE = new Cluster(Name.NONE, BasicAutomata.makeEmpty());
public static final Cluster ALL = new Cluster(Name.ALL, "cluster:*", "indices:admin/template/*"); public static final Cluster ALL = new Cluster(Name.ALL, "cluster:*", "indices:admin/template/*");
public static final Cluster MONITOR = new Cluster("monitor", "cluster:monitor/*"); public static final Cluster MONITOR = new Cluster("monitor", "cluster:monitor/*");
public static final Cluster MANAGE_SHIELD = new Cluster("manage_shield", "cluster:admin/shield/*"); public static final Cluster MANAGE_SHIELD = new Cluster("manage_shield", "cluster:admin/shield/*");
@ -374,7 +375,7 @@ public abstract class Privilege<P extends Privilege<P>> {
@Override @Override
public boolean implies(P other) { public boolean implies(P other) {
return Operations.subsetOf(other.automaton, automaton); return BasicOperations.subsetOf(other.automaton, automaton);
} }
public String toString() { public String toString() {

View File

@ -5,8 +5,8 @@
*/ */
package org.elasticsearch.shield.support; package org.elasticsearch.shield.support;
import org.apache.lucene.util.automaton.Automaton; import dk.brics.automaton.RunAutomaton;
import org.apache.lucene.util.automaton.CharacterRunAutomaton; import dk.brics.automaton.Automaton;
import org.elasticsearch.common.base.Predicate; import org.elasticsearch.common.base.Predicate;
/** /**
@ -14,13 +14,13 @@ import org.elasticsearch.common.base.Predicate;
*/ */
public class AutomatonPredicate implements Predicate<String> { public class AutomatonPredicate implements Predicate<String> {
private final CharacterRunAutomaton automaton; private final RunAutomaton automaton;
public AutomatonPredicate(Automaton automaton) { public AutomatonPredicate(Automaton automaton) {
this(new CharacterRunAutomaton(automaton)); this(new RunAutomaton(automaton));
} }
public AutomatonPredicate(CharacterRunAutomaton automaton) { public AutomatonPredicate(RunAutomaton automaton) {
this.automaton = automaton; this.automaton = automaton;
} }

View File

@ -5,18 +5,18 @@
*/ */
package org.elasticsearch.shield.support; package org.elasticsearch.shield.support;
import org.apache.lucene.util.automaton.Automata; import dk.brics.automaton.BasicAutomata;
import org.apache.lucene.util.automaton.Automaton; import dk.brics.automaton.Automaton;
import org.apache.lucene.util.automaton.Operations; import dk.brics.automaton.BasicOperations;
import org.apache.lucene.util.automaton.RegExp; import dk.brics.automaton.RegExp;
import org.elasticsearch.common.collect.ImmutableList; import org.elasticsearch.common.collect.ImmutableList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static org.apache.lucene.util.automaton.MinimizationOperations.minimize; import static dk.brics.automaton.MinimizationOperations.minimize;
import static org.apache.lucene.util.automaton.Operations.*; import static dk.brics.automaton.BasicOperations.*;
/** /**
* *
@ -42,7 +42,7 @@ public final class Automatons {
*/ */
public static Automaton patterns(Collection<String> patterns) { public static Automaton patterns(Collection<String> patterns) {
if (patterns.isEmpty()) { if (patterns.isEmpty()) {
return Automata.makeEmpty(); return BasicAutomata.makeEmpty();
} }
Automaton automaton = null; Automaton automaton = null;
for (String pattern : patterns) { for (String pattern : patterns) {
@ -52,7 +52,8 @@ public final class Automatons {
automaton = union(automaton, pattern(pattern)); automaton = union(automaton, pattern(pattern));
} }
} }
return determinize(minimize(automaton)); minimize(automaton); // minimal is also deterministic
return automaton;
} }
/** /**
@ -77,36 +78,40 @@ public final class Automatons {
static Automaton wildcard(String text) { static Automaton wildcard(String text) {
List<Automaton> automata = new ArrayList<>(); List<Automaton> automata = new ArrayList<>();
for (int i = 0; i < text.length();) { for (int i = 0; i < text.length();) {
final int c = text.codePointAt(i); final char c = text.charAt(i);
int length = Character.charCount(c); int length = 1;
switch(c) { switch(c) {
case WILDCARD_STRING: case WILDCARD_STRING:
automata.add(Automata.makeAnyString()); automata.add(BasicAutomata.makeAnyString());
break; break;
case WILDCARD_CHAR: case WILDCARD_CHAR:
automata.add(Automata.makeAnyChar()); automata.add(BasicAutomata.makeAnyChar());
break; break;
case WILDCARD_ESCAPE: case WILDCARD_ESCAPE:
// add the next codepoint instead, if it exists // add the next codepoint instead, if it exists
if (i + length < text.length()) { if (i + length < text.length()) {
final int nextChar = text.codePointAt(i + length); final char nextChar = text.charAt(i + length);
length += Character.charCount(nextChar); length += 1;
automata.add(Automata.makeChar(nextChar)); automata.add(BasicAutomata.makeChar(nextChar));
break; break;
} // else fallthru, lenient parsing with a trailing \ } // else fallthru, lenient parsing with a trailing \
default: default:
automata.add(Automata.makeChar(c)); automata.add(BasicAutomata.makeChar(c));
} }
i += length; i += length;
} }
return Operations.concatenate(automata); return BasicOperations.concatenate(automata);
} }
public static Automaton unionAndDeterminize(Automaton a1, Automaton a2) { public static Automaton unionAndDeterminize(Automaton a1, Automaton a2) {
return determinize(union(a1, a2)); Automaton res = union(a1, a2);
res.determinize();
return res;
} }
public static Automaton minusAndDeterminize(Automaton a1, Automaton a2) { public static Automaton minusAndDeterminize(Automaton a1, Automaton a2) {
return determinize(minus(a1, a2)); Automaton res = minus(a1, a2);
res.determinize();
return res;
} }
} }

View File

@ -5,8 +5,8 @@
*/ */
package org.elasticsearch.shield.support; package org.elasticsearch.shield.support;
import org.apache.lucene.util.automaton.Automaton; import dk.brics.automaton.Automaton;
import org.apache.lucene.util.automaton.CharacterRunAutomaton; import dk.brics.automaton.RunAutomaton;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test; import org.junit.Test;
@ -60,12 +60,12 @@ public class AutomatonsTests extends ElasticsearchTestCase {
} }
private void assertMatch(Automaton automaton, String text) { private void assertMatch(Automaton automaton, String text) {
CharacterRunAutomaton runAutomaton = new CharacterRunAutomaton(automaton); RunAutomaton runAutomaton = new RunAutomaton(automaton);
assertThat(runAutomaton.run(text), is(true)); assertThat(runAutomaton.run(text), is(true));
} }
private void assertMismatch(Automaton automaton, String text) { private void assertMismatch(Automaton automaton, String text) {
CharacterRunAutomaton runAutomaton = new CharacterRunAutomaton(automaton); RunAutomaton runAutomaton = new RunAutomaton(automaton);
assertThat(runAutomaton.run(text), is(false)); assertThat(runAutomaton.run(text), is(false));
} }