Skip to content

Commit b014814

Browse files
committed
fix bugs
1 parent b0c2594 commit b014814

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
public class IndicesAccessControl {
3131

32-
public static final IndicesAccessControl ALLOW_ALL = new IndicesAccessControl(true, Collections.emptyMap());
3332
public static final IndicesAccessControl ALLOW_NO_INDICES = new IndicesAccessControl(true,
3433
Collections.singletonMap(IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER,
3534
new IndicesAccessControl.IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll())));
@@ -179,6 +178,12 @@ public int hashCode() {
179178
* @return {@link IndicesAccessControl}
180179
*/
181180
public IndicesAccessControl limitIndicesAccessControl(IndicesAccessControl limitedByIndicesAccessControl) {
181+
if (this instanceof AllowAllIndicesAccessControl) {
182+
return limitedByIndicesAccessControl;
183+
} else if (limitedByIndicesAccessControl instanceof AllowAllIndicesAccessControl) {
184+
return this;
185+
}
186+
182187
final boolean granted;
183188
if (this.granted == limitedByIndicesAccessControl.granted) {
184189
granted = this.granted;
@@ -205,4 +210,38 @@ public String toString() {
205210
", indexPermissions=" + indexPermissions +
206211
'}';
207212
}
213+
214+
public boolean isAllowAll() {
215+
return this == AllowAllIndicesAccessControl.ALLOW_ALL_INDICES_ACCESS_CONTROL;
216+
}
217+
218+
public static IndicesAccessControl allowAll() {
219+
return AllowAllIndicesAccessControl.ALLOW_ALL_INDICES_ACCESS_CONTROL;
220+
}
221+
222+
private static class AllowAllIndicesAccessControl extends IndicesAccessControl {
223+
224+
private static final IndicesAccessControl ALLOW_ALL_INDICES_ACCESS_CONTROL = new AllowAllIndicesAccessControl();
225+
private static final IndexAccessControl ALLOW_ALL_INDEX_ACCESS_CONTROL = new IndexAccessControl(true, null, null);
226+
227+
private AllowAllIndicesAccessControl() {
228+
super(true, null);
229+
}
230+
231+
@Override
232+
public IndexAccessControl getIndexPermissions(String index) {
233+
return ALLOW_ALL_INDEX_ACCESS_CONTROL;
234+
}
235+
236+
@Override
237+
public boolean isGranted() {
238+
return true;
239+
}
240+
241+
@Override
242+
public Collection<?> getDeniedIndices() {
243+
return Set.of();
244+
}
245+
}
246+
208247
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public static class Group {
507507
private final IndexPrivilege privilege;
508508
private final Predicate<String> actionMatcher;
509509
private final String[] indices;
510-
private final Predicate<String> indexNameMatcher;
510+
private final StringMatcher indexNameMatcher;
511511
private final Supplier<Automaton> indexNameAutomaton;
512512
private final FieldPermissions fieldPermissions;
513513
private final Set<BytesReference> query;
@@ -578,7 +578,7 @@ public Automaton getIndexMatcherAutomaton() {
578578

579579
public boolean isTotal() {
580580
return allowRestrictedIndices
581-
&& indexNameMatcher == StringMatcher.ALWAYS_TRUE_PREDICATE
581+
&& indexNameMatcher.isTotal()
582582
&& privilege == IndexPrivilege.ALL
583583
&& query == null
584584
&& false == fieldPermissions.hasFieldLevelSecurity();

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ public IndicesAccessControl authorize(String action, Set<String> requestedIndice
8484
super.authorize(action, requestedIndicesOrAliases, aliasAndIndexLookup, fieldPermissionsCache);
8585
IndicesAccessControl limitedByIndicesAccessControl = limitedBy.authorize(action, requestedIndicesOrAliases, aliasAndIndexLookup,
8686
fieldPermissionsCache);
87-
88-
if (indicesAccessControl == IndicesAccessControl.ALLOW_ALL && limitedByIndicesAccessControl == IndicesAccessControl.ALLOW_ALL) {
89-
return IndicesAccessControl.ALLOW_ALL;
90-
}
91-
9287
return indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl);
9388
}
9489

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public IndicesAccessControl authorize(String action, Set<String> requestedIndice
189189
);
190190
// Quick path for role that has access to all indices
191191
if (indexPermissions == null) {
192-
return IndicesAccessControl.ALLOW_ALL;
192+
return IndicesAccessControl.allowAll();
193193
}
194194

195195
// At least one role / indices permission set need to match with all the requested indices/aliases:

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/StringMatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class StringMatcher implements Predicate<String> {
3636

3737
private static final StringMatcher MATCH_NOTHING = new StringMatcher("(empty)", s -> false);
3838

39-
public static final Predicate<String> ALWAYS_TRUE_PREDICATE = s -> true;
39+
protected static final Predicate<String> ALWAYS_TRUE_PREDICATE = s -> true;
4040

4141
private final String description;
4242
private final Predicate<String> predicate;
@@ -69,6 +69,10 @@ public boolean test(String s) {
6969
return predicate.test(s);
7070
}
7171

72+
public boolean isTotal() {
73+
return predicate == ALWAYS_TRUE_PREDICATE;
74+
}
75+
7276
// For testing
7377
Predicate<String> getPredicate() {
7478
return predicate;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private void authorizeAction(final RequestInfo requestInfo, final String request
336336
if (ClusterPrivilegeResolver.isClusterAction(action)) {
337337
final ActionListener<AuthorizationResult> clusterAuthzListener =
338338
wrapPreservingContext(new AuthorizationResultListener<>(result -> {
339-
threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.ALLOW_ALL);
339+
threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.allowAll());
340340
listener.onResponse(null);
341341
}, listener::onFailure, requestInfo, requestId, authzInfo), threadContext);
342342
authzEngine.authorizeClusterAction(requestInfo, authzInfo, ActionListener.wrap(result -> {
@@ -508,7 +508,7 @@ private void authorizeSystemUser(final Authentication authentication, final Stri
508508
final TransportRequest request, final ActionListener<Void> listener) {
509509
final AuditTrail auditTrail = auditTrailService.get();
510510
if (SystemUser.isAuthorized(action)) {
511-
threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.ALLOW_ALL);
511+
threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.allowAll());
512512
threadContext.putTransient(AUTHORIZATION_INFO_KEY, SYSTEM_AUTHZ_INFO);
513513
auditTrail.accessGranted(requestId, authentication, action, request, SYSTEM_AUTHZ_INFO);
514514
listener.onResponse(null);

0 commit comments

Comments
 (0)