-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RoleRetrievalResult for better caching (#34197)
Security caches the result of role lookups and negative lookups are cached indefinitely. In the case of transient failures this leads to a bad experience as the roles could truly exist. The CompositeRolesStore needs to know if a failure occurred in one of the roles stores in order to make the appropriate decision as it relates to caching. In order to provide this information to the CompositeRolesStore, the return type of methods to retrieve roles has changed to a new class, RoleRetrievalResult. This class provides the ability to pass back an exception to the roles store. This exception does not mean that a request should be failed but instead serves as a signal to the roles store that missing roles should not be cached and neither should the combined role if there are missing roles. As part of this, the negative lookup cache was also changed from an unbounded cache to a cache with a configurable limit. Relates #33205
- Loading branch information
Showing
17 changed files
with
570 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
.../src/main/java/org/elasticsearch/xpack/core/security/authz/store/RoleRetrievalResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.authz.store; | ||
|
||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* The result of attempting to retrieve roles from a roles provider. The result can either be | ||
* successful or a failure. A successful result indicates that no errors occurred while retrieving | ||
* roles, even if none of the requested roles could be found. A failure indicates an error | ||
* occurred while retrieving the results but the error is not fatal and the request may be able | ||
* to continue. | ||
*/ | ||
public final class RoleRetrievalResult { | ||
|
||
private final Set<RoleDescriptor> descriptors; | ||
|
||
@Nullable | ||
private final Exception failure; | ||
|
||
private RoleRetrievalResult(Set<RoleDescriptor> descriptors, @Nullable Exception failure) { | ||
if (descriptors != null && failure != null) { | ||
throw new IllegalArgumentException("either descriptors or failure must be null"); | ||
} | ||
this.descriptors = descriptors; | ||
this.failure = failure; | ||
} | ||
|
||
/** | ||
* @return the resolved descriptors or {@code null} if there was a failure | ||
*/ | ||
public Set<RoleDescriptor> getDescriptors() { | ||
return descriptors; | ||
} | ||
|
||
/** | ||
* @return the failure or {@code null} if retrieval succeeded | ||
*/ | ||
@Nullable | ||
public Exception getFailure() { | ||
return failure; | ||
} | ||
|
||
/** | ||
* @return true if the retrieval succeeded | ||
*/ | ||
public boolean isSuccess() { | ||
return descriptors != null; | ||
} | ||
|
||
/** | ||
* Creates a successful result with the provided {@link RoleDescriptor} set, | ||
* which must be non-null | ||
*/ | ||
public static RoleRetrievalResult success(Set<RoleDescriptor> descriptors) { | ||
Objects.requireNonNull(descriptors, "descriptors must not be null if successful"); | ||
return new RoleRetrievalResult(descriptors, null); | ||
} | ||
|
||
/** | ||
* Creates a failed result with the provided non-null exception | ||
*/ | ||
public static RoleRetrievalResult failure(Exception e) { | ||
Objects.requireNonNull(e, "Exception must be provided"); | ||
return new RoleRetrievalResult(null, e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.