Skip to content

Commit 7077de9

Browse files
committed
support has privs
1 parent b34965d commit 7077de9

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88

99
import org.elasticsearch.action.ActionRequest;
1010
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.action.support.IndexComponentSelector;
12+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
1113
import org.elasticsearch.common.io.stream.StreamInput;
1214
import org.elasticsearch.common.io.stream.StreamOutput;
15+
import org.elasticsearch.core.Tuple;
1316
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine;
1417
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
1518
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges;
1619

1720
import java.io.IOException;
21+
import java.util.ArrayList;
22+
import java.util.List;
1823

1924
/**
2025
* A request for checking a user's privileges
@@ -84,7 +89,50 @@ public ApplicationResourcePrivileges[] applicationPrivileges() {
8489
}
8590

8691
public void indexPrivileges(IndicesPrivileges... privileges) {
87-
this.indexPrivileges = privileges;
92+
IndicesPrivileges[] newPrivileges = new IndicesPrivileges[privileges.length];
93+
for (int i = 0; i < privileges.length; i++) {
94+
IndicesPrivileges currentPriv = privileges[i];
95+
IndicesPrivileges.Builder builder = IndicesPrivileges.builder(privileges[i]);
96+
builder.indices((String[]) null);
97+
List<String> updatedIndexPatterns = new ArrayList<>();
98+
for (String indexPatternRequested : currentPriv.getIndices()) {
99+
Tuple<String, String> split = IndexNameExpressionResolver.splitSelectorExpression(indexPatternRequested);
100+
String indexNameNoSelector = split.v1();
101+
String selectorAsString = split.v2();
102+
if (selectorAsString == null) {
103+
assert indexPatternRequested.equals(indexNameNoSelector);
104+
updatedIndexPatterns.add(indexNameNoSelector); // add as-is, no selector
105+
} else {
106+
IndexComponentSelector selector = IndexComponentSelector.getByKey(selectorAsString);
107+
switch (selector) {
108+
case DATA:
109+
updatedIndexPatterns.add(indexNameNoSelector); // strip the selector
110+
break;
111+
case FAILURES:
112+
updatedIndexPatterns.add(indexPatternRequested); // add as-is, keep selector in name
113+
break;
114+
case ALL_APPLICABLE:
115+
updatedIndexPatterns.add(indexNameNoSelector); // add with no selector for data
116+
updatedIndexPatterns.add(
117+
IndexNameExpressionResolver.combineSelector(indexNameNoSelector, IndexComponentSelector.FAILURES)
118+
); // add with failure selector
119+
break;
120+
default:
121+
throw new IllegalArgumentException(
122+
"Unknown index component selector ["
123+
+ selectorAsString
124+
+ "], available options are: "
125+
+ IndexComponentSelector.values()
126+
);
127+
128+
}
129+
}
130+
builder.indices(updatedIndexPatterns);
131+
newPrivileges[i] = builder.build();
132+
}
133+
}
134+
135+
this.indexPrivileges = newPrivileges;
88136
}
89137

90138
public void clusterPrivileges(String... privileges) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,10 @@ public static Builder builder() {
13711371
return new Builder();
13721372
}
13731373

1374+
public static Builder builder(IndicesPrivileges copyFrom) {
1375+
return new Builder(copyFrom);
1376+
}
1377+
13741378
public String[] getIndices() {
13751379
return this.indices;
13761380
}
@@ -1553,6 +1557,15 @@ public static class Builder {
15531557

15541558
private Builder() {}
15551559

1560+
private Builder(IndicesPrivileges copyFrom) {
1561+
indicesPrivileges.indices = copyFrom.indices;
1562+
indicesPrivileges.privileges = copyFrom.privileges;
1563+
indicesPrivileges.grantedFields = copyFrom.grantedFields;
1564+
indicesPrivileges.deniedFields = copyFrom.deniedFields;
1565+
indicesPrivileges.query = copyFrom.query;
1566+
indicesPrivileges.allowRestrictedIndices = copyFrom.allowRestrictedIndices;
1567+
}
1568+
15561569
public Builder indices(String... indices) {
15571570
indicesPrivileges.indices = indices;
15581571
return this;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,17 +847,20 @@ public Group(
847847
// TODO: [Jake] how to support selectors for hasPrivileges ? (are reg-ex's just broken for hasPrivilege index checks ?)
848848
// TODO: [Jake] ensure that only ::failure selectors can be added the role (i.e. error on name::* or name::data)
849849
// TODO: [Jake] ensure that no selectors can be added to remote_indices (or gate usage with a feature flag, or just test)
850-
String[] indicesResolved = maybeAddFailureExclusions(indices);
850+
String[] patternsReWritten = maybeAddFailureExclusions(indices);
851851
this.allowRestrictedIndices = allowRestrictedIndices;
852852
ConcurrentHashMap<String[], Automaton> indexNameAutomatonMemo = new ConcurrentHashMap<>(1);
853853
if (allowRestrictedIndices) {
854-
this.indexNameMatcher = StringMatcher.of(indicesResolved);
855-
this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent(indices, k -> Automatons.patterns(indices));
854+
this.indexNameMatcher = StringMatcher.of(patternsReWritten);
855+
this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent(
856+
patternsReWritten,
857+
k -> Automatons.patterns(patternsReWritten)
858+
);
856859
} else {
857-
this.indexNameMatcher = StringMatcher.of(indicesResolved).and(name -> restrictedIndices.isRestricted(name) == false);
860+
this.indexNameMatcher = StringMatcher.of(patternsReWritten).and(name -> restrictedIndices.isRestricted(name) == false);
858861
this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent(
859-
indices,
860-
k -> Automatons.minusAndMinimize(Automatons.patterns(indices), restrictedIndices.getAutomaton())
862+
patternsReWritten,
863+
k -> Automatons.minusAndMinimize(Automatons.patterns(patternsReWritten), restrictedIndices.getAutomaton())
861864
);
862865
}
863866
this.fieldPermissions = Objects.requireNonNull(fieldPermissions);

0 commit comments

Comments
 (0)