Skip to content

Commit b23f04c

Browse files
committed
Tests and comments
1 parent 4ca9978 commit b23f04c

File tree

3 files changed

+238
-31
lines changed

3 files changed

+238
-31
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ public boolean hasFieldOrDocumentLevelSecurity() {
143143

144144
private IsResourceAuthorizedPredicate buildIndexMatcherPredicateForAction(String action) {
145145
final Set<String> dataAccessOrdinaryIndices = new HashSet<>();
146-
final Set<String> failureAccessOrdinaryIndices = new HashSet<>();
146+
final Set<String> failuresAccessOrdinaryIndices = new HashSet<>();
147147
final Set<String> dataAccessRestrictedIndices = new HashSet<>();
148-
final Set<String> failureAccessRestrictedIndices = new HashSet<>();
148+
final Set<String> failuresAccessRestrictedIndices = new HashSet<>();
149149
final Set<String> grantMappingUpdatesOnIndices = new HashSet<>();
150150
final Set<String> grantMappingUpdatesOnRestrictedIndices = new HashSet<>();
151151
final boolean isMappingUpdateAction = isMappingUpdateAction(action);
@@ -157,15 +157,15 @@ private IsResourceAuthorizedPredicate buildIndexMatcherPredicateForAction(String
157157
dataAccessRestrictedIndices.addAll(indexList);
158158
}
159159
if (group.checkSelector(IndexComponentSelector.FAILURES)) {
160-
failureAccessRestrictedIndices.addAll(indexList);
160+
failuresAccessRestrictedIndices.addAll(indexList);
161161
}
162162
} else {
163163
List<String> indexList = Arrays.asList(group.indices());
164164
if (group.checkSelector(IndexComponentSelector.DATA)) {
165165
dataAccessOrdinaryIndices.addAll(indexList);
166166
}
167167
if (group.checkSelector(IndexComponentSelector.FAILURES)) {
168-
failureAccessOrdinaryIndices.addAll(indexList);
168+
failuresAccessOrdinaryIndices.addAll(indexList);
169169
}
170170
}
171171
} else if (isMappingUpdateAction && containsPrivilegeThatGrantsMappingUpdatesForBwc(group)) {
@@ -179,9 +179,9 @@ private IsResourceAuthorizedPredicate buildIndexMatcherPredicateForAction(String
179179
}
180180
}
181181
final StringMatcher dataAccessNameMatcher = indexMatcher(dataAccessOrdinaryIndices, dataAccessRestrictedIndices);
182-
final StringMatcher failureAccessNameMatcher = indexMatcher(failureAccessOrdinaryIndices, failureAccessRestrictedIndices);
182+
final StringMatcher failuresAccessNameMatcher = indexMatcher(failuresAccessOrdinaryIndices, failuresAccessRestrictedIndices);
183183
final StringMatcher bwcSpecialCaseMatcher = indexMatcher(grantMappingUpdatesOnIndices, grantMappingUpdatesOnRestrictedIndices);
184-
return new IsResourceAuthorizedPredicate(dataAccessNameMatcher, failureAccessNameMatcher, bwcSpecialCaseMatcher);
184+
return new IsResourceAuthorizedPredicate(dataAccessNameMatcher, failuresAccessNameMatcher, bwcSpecialCaseMatcher);
185185
}
186186

187187
/**
@@ -190,32 +190,32 @@ private IsResourceAuthorizedPredicate buildIndexMatcherPredicateForAction(String
190190
*/
191191
public static class IsResourceAuthorizedPredicate {
192192

193-
private final BiPredicate<String, IndexAbstraction> isAuthorizedForData;
194-
private final BiPredicate<String, IndexAbstraction> isAuthorizedForFailureStore;
193+
private final BiPredicate<String, IndexAbstraction> isAuthorizedForDataAccess;
194+
private final BiPredicate<String, IndexAbstraction> isAuthorizedForFailuresAccess;
195195

196196
// public for tests
197197
public IsResourceAuthorizedPredicate(
198-
StringMatcher resourceNameMatcher,
199-
StringMatcher failureStoreNameMatcher,
198+
StringMatcher dataResourceNameMatcher,
199+
StringMatcher failuresResourceNameMatcher,
200200
StringMatcher additionalNonDatastreamNameMatcher
201201
) {
202202
this((String name, @Nullable IndexAbstraction indexAbstraction) -> {
203203
assert indexAbstraction == null || name.equals(indexAbstraction.getName());
204-
return resourceNameMatcher.test(name)
204+
return dataResourceNameMatcher.test(name)
205205
|| (isPartOfDatastream(indexAbstraction) == false && additionalNonDatastreamNameMatcher.test(name));
206206
}, (String name, @Nullable IndexAbstraction indexAbstraction) -> {
207207
assert indexAbstraction == null || name.equals(indexAbstraction.getName());
208208
// we can't enforce that the abstraction is part of a data stream since we need to account for non-existent resources
209-
return failureStoreNameMatcher.test(name);
209+
return failuresResourceNameMatcher.test(name);
210210
});
211211
}
212212

213213
private IsResourceAuthorizedPredicate(
214-
BiPredicate<String, IndexAbstraction> isAuthorizedForData,
215-
BiPredicate<String, IndexAbstraction> isAuthorizedForFailureStore
214+
BiPredicate<String, IndexAbstraction> isAuthorizedForDataAccess,
215+
BiPredicate<String, IndexAbstraction> isAuthorizedForFailuresAccess
216216
) {
217-
this.isAuthorizedForData = isAuthorizedForData;
218-
this.isAuthorizedForFailureStore = isAuthorizedForFailureStore;
217+
this.isAuthorizedForDataAccess = isAuthorizedForDataAccess;
218+
this.isAuthorizedForFailuresAccess = isAuthorizedForFailuresAccess;
219219
}
220220

221221
/**
@@ -225,14 +225,14 @@ private IsResourceAuthorizedPredicate(
225225
*/
226226
public final IsResourceAuthorizedPredicate and(IsResourceAuthorizedPredicate other) {
227227
return new IsResourceAuthorizedPredicate(
228-
this.isAuthorizedForData.and(other.isAuthorizedForData),
229-
this.isAuthorizedForFailureStore.and(other.isAuthorizedForFailureStore)
228+
this.isAuthorizedForDataAccess.and(other.isAuthorizedForDataAccess),
229+
this.isAuthorizedForFailuresAccess.and(other.isAuthorizedForFailuresAccess)
230230
);
231231
}
232232

233233
// TODO remove me (this has >700 usages in tests which would make for a horrible diff; will remove this once the main PR is merged)
234234
public boolean test(IndexAbstraction indexAbstraction) {
235-
return test(indexAbstraction.getName(), indexAbstraction, null);
235+
return test(indexAbstraction.getName(), indexAbstraction, IndexComponentSelector.DATA);
236236
}
237237

238238
/**
@@ -252,8 +252,8 @@ public boolean test(IndexAbstraction indexAbstraction, @Nullable IndexComponentS
252252
*/
253253
public boolean test(String name, @Nullable IndexAbstraction indexAbstraction, @Nullable IndexComponentSelector selector) {
254254
return IndexComponentSelector.FAILURES.equals(selector)
255-
? isAuthorizedForFailureStore.test(name, indexAbstraction)
256-
: isAuthorizedForData.test(name, indexAbstraction);
255+
? isAuthorizedForFailuresAccess.test(name, indexAbstraction)
256+
: isAuthorizedForDataAccess.test(name, indexAbstraction);
257257
}
258258

259259
private static boolean isPartOfDatastream(IndexAbstraction indexAbstraction) {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ static AuthorizedIndices resolveAuthorizedIndicesFromRole(
905905
} else {
906906
for (IndexAbstraction indexAbstraction : lookup.values()) {
907907
if (indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM
908+
// data check is correct, even for failure indices -- in this context, we treat them as regular indices, and
909+
// only include them if direct data access to them is granted (e.g., if a role has `read` over "*")
908910
&& predicate.test(indexAbstraction, IndexComponentSelector.DATA)) {
909911
indicesAndAliases.add(indexAbstraction.getName());
910912
}
@@ -1072,31 +1074,31 @@ private static boolean isAsyncRelatedAction(String action) {
10721074
}
10731075

10741076
static final class AuthorizedIndices implements AuthorizationEngine.AuthorizedIndices {
1075-
private final CachedSupplier<Set<String>> authorizedAndAvailableSupplier;
1076-
private final CachedSupplier<Set<String>> failureStoreAuthorizedAndAvailableSupplier;
1077+
private final CachedSupplier<Set<String>> authorizedAndAvailableDataResources;
1078+
private final CachedSupplier<Set<String>> authorizedAndAvailableFailuresResources;
10771079
private final BiPredicate<String, IndexComponentSelector> isAuthorizedPredicate;
10781080

10791081
AuthorizedIndices(
1080-
Supplier<Set<String>> authorizedAndAvailableSupplier,
1081-
Supplier<Set<String>> failureStoreAuthorizedAndAvailableSupplier,
1082+
Supplier<Set<String>> authorizedAndAvailableDataResources,
1083+
Supplier<Set<String>> authorizedAndAvailableFailuresResources,
10821084
BiPredicate<String, IndexComponentSelector> isAuthorizedPredicate
10831085
) {
1084-
this.authorizedAndAvailableSupplier = CachedSupplier.wrap(authorizedAndAvailableSupplier);
1085-
this.failureStoreAuthorizedAndAvailableSupplier = CachedSupplier.wrap(failureStoreAuthorizedAndAvailableSupplier);
1086+
this.authorizedAndAvailableDataResources = CachedSupplier.wrap(authorizedAndAvailableDataResources);
1087+
this.authorizedAndAvailableFailuresResources = CachedSupplier.wrap(authorizedAndAvailableFailuresResources);
10861088
this.isAuthorizedPredicate = Objects.requireNonNull(isAuthorizedPredicate);
10871089
}
10881090

10891091
@Override
10901092
public Set<String> all(IndexComponentSelector selector) {
1091-
Objects.requireNonNull(selector);
1093+
Objects.requireNonNull(selector, "must specify a selector to get authorized indices");
10921094
return IndexComponentSelector.FAILURES.equals(selector)
1093-
? failureStoreAuthorizedAndAvailableSupplier.get()
1094-
: authorizedAndAvailableSupplier.get();
1095+
? authorizedAndAvailableFailuresResources.get()
1096+
: authorizedAndAvailableDataResources.get();
10951097
}
10961098

10971099
@Override
10981100
public boolean check(String name, IndexComponentSelector selector) {
1099-
Objects.requireNonNull(selector);
1101+
Objects.requireNonNull(selector, "must specify a selector for authorization check");
11001102
return isAuthorizedPredicate.test(name, selector);
11011103
}
11021104
}

0 commit comments

Comments
 (0)