|
8 | 8 |
|
9 | 9 | import org.elasticsearch.action.ActionRequest; |
10 | 10 | import org.elasticsearch.action.ActionRequestValidationException; |
| 11 | +import org.elasticsearch.action.support.IndexComponentSelector; |
| 12 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
11 | 13 | import org.elasticsearch.common.io.stream.StreamInput; |
12 | 14 | import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.core.Tuple; |
13 | 16 | import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; |
14 | 17 | import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges; |
15 | 18 | import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges; |
16 | 19 |
|
17 | 20 | import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
18 | 23 |
|
19 | 24 | /** |
20 | 25 | * A request for checking a user's privileges |
@@ -84,7 +89,50 @@ public ApplicationResourcePrivileges[] applicationPrivileges() { |
84 | 89 | } |
85 | 90 |
|
86 | 91 | 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; |
88 | 136 | } |
89 | 137 |
|
90 | 138 | public void clusterPrivileges(String... privileges) { |
|
0 commit comments