Skip to content

Commit

Permalink
IndexPattern Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Bandener <[email protected]>
  • Loading branch information
nibix committed Oct 8, 2024
1 parent 301f48e commit d3764ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void dateMathIndex() throws Exception {
IndexPattern indexPattern = IndexPattern.from("<index_year_{now/y{yyyy}}>");
assertFalse(indexPattern.hasStaticPattern());
assertTrue(indexPattern.hasDynamicPattern());
assertEquals("index_a11", indexPattern.toString());
assertEquals("<index_year_{now/y{yyyy}}>", indexPattern.toString());

assertTrue(indexPattern.matches("index_year_" + CURRENT_YEAR, ctx(), INDEX_METADATA.getIndicesLookup()));
assertFalse(indexPattern.matches("index_year_" + NEXT_YEAR, ctx(), INDEX_METADATA.getIndicesLookup()));
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/org/opensearch/security/privileges/IndexPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public class IndexPattern {
/**
* An IndexPattern which does not match any index.
*/
public static final IndexPattern EMPTY = new IndexPattern(null, ImmutableList.of(), ImmutableList.of());
public static final IndexPattern EMPTY = new IndexPattern(WildcardMatcher.NONE, ImmutableList.of(), ImmutableList.of());

private final WildcardMatcher staticPattern;
private final ImmutableList<String> patternTemplates;
private final ImmutableList<String> dateMathExpressions;
private final int hashCode;

public IndexPattern(WildcardMatcher staticPattern, ImmutableList<String> patternTemplates, ImmutableList<String> dateMathExpressions) {
private IndexPattern(WildcardMatcher staticPattern, ImmutableList<String> patternTemplates, ImmutableList<String> dateMathExpressions) {
this.staticPattern = staticPattern;
this.patternTemplates = patternTemplates;
this.dateMathExpressions = dateMathExpressions;
Expand All @@ -52,7 +52,7 @@ public IndexPattern(WildcardMatcher staticPattern, ImmutableList<String> pattern

public boolean matches(String index, PrivilegesEvaluationContext context, Map<String, IndexAbstraction> indexMetadata)
throws PrivilegesEvaluationException {
if (staticPattern != null && staticPattern.test(index)) {
if (staticPattern != WildcardMatcher.NONE && staticPattern.test(index)) {
return true;
}

Expand Down Expand Up @@ -116,14 +116,32 @@ public boolean matches(String index, PrivilegesEvaluationContext context, Map<St

@Override
public String toString() {
if (staticPattern != null && patternTemplates != null && patternTemplates.size() != 0) {
return staticPattern + " " + patternTemplates;
} else if (staticPattern != null) {
if (patternTemplates.size() == 0 && dateMathExpressions.size() == 0) {
return staticPattern.toString();
} else if (patternTemplates != null) {
return patternTemplates.toString();
} else {
return "-/-";
StringBuilder result = new StringBuilder();

if (staticPattern != WildcardMatcher.NONE) {
result.append(staticPattern);
}

if (patternTemplates.size() != 0) {
if (result.length() != 0) {
result.append(" ");
}

result.append(String.join(",", patternTemplates));
}

if (dateMathExpressions.size() != 0) {
if (result.length() != 0) {
result.append(" ");
}

result.append(String.join(",", dateMathExpressions));
}

return result.toString();
}
}

Expand All @@ -135,7 +153,7 @@ public WildcardMatcher getStaticPattern() {
* Returns true if this object contains patterns which can be matched against indices upfront.
*/
public boolean hasStaticPattern() {
return staticPattern != null && staticPattern != WildcardMatcher.NONE;
return staticPattern != WildcardMatcher.NONE;
}

/**
Expand All @@ -154,7 +172,7 @@ public IndexPattern dynamicOnly() {
if (patternTemplates.isEmpty() && dateMathExpressions.isEmpty()) {
return EMPTY;
} else {
return new IndexPattern(null, this.patternTemplates, this.dateMathExpressions);
return new IndexPattern(WildcardMatcher.NONE, this.patternTemplates, this.dateMathExpressions);
}
}

Expand Down Expand Up @@ -207,7 +225,7 @@ void add(List<String> source) {

IndexPattern build() {
return new IndexPattern(
constantPatterns.size() != 0 ? WildcardMatcher.from(constantPatterns) : null,
constantPatterns.size() != 0 ? WildcardMatcher.from(constantPatterns) : WildcardMatcher.NONE,
ImmutableList.copyOf(patternTemplates),
ImmutableList.copyOf(dateMathExpressions)
);
Expand Down

0 comments on commit d3764ed

Please sign in to comment.