Skip to content

Commit

Permalink
Make setting filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Nov 3, 2023
1 parent ec08a46 commit dff2616
Showing 1 changed file with 7 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,24 @@ public class NativeRoleMappingStore implements UserRoleMapper {
public static final Setting<Boolean> FALLBACK_CACHE_ENABLED_SETTING = Setting.boolSetting(
"xpack.security.authc.role_mapping.fallback_cache.enabled",
false,
Setting.Property.NodeScope
Setting.Property.NodeScope,
Setting.Property.Filtered
);

private final Settings settings;
private final Client client;
private final SecurityIndexManager securityIndex;
private final ScriptService scriptService;
private final List<String> realmsToRefresh = new CopyOnWriteArrayList<>();
private final boolean shouldCacheSuccessfulLoad;
private final boolean fallbackCacheEnabled;
private final AtomicReference<List<ExpressionRoleMapping>> lastSuccessfulLoadRef = new AtomicReference<>(null);

public NativeRoleMappingStore(Settings settings, Client client, SecurityIndexManager securityIndex, ScriptService scriptService) {
this.settings = settings;
this.client = client;
this.securityIndex = securityIndex;
this.scriptService = scriptService;
this.shouldCacheSuccessfulLoad = FALLBACK_CACHE_ENABLED_SETTING.get(settings);
}

NativeRoleMappingStore(
Settings settings,
Client client,
SecurityIndexManager securityIndex,
ScriptService scriptService,
boolean shouldCacheSuccessfulLoad
) {
this.settings = settings;
this.client = client;
this.securityIndex = securityIndex;
this.scriptService = scriptService;
this.shouldCacheSuccessfulLoad = shouldCacheSuccessfulLoad;
this.fallbackCacheEnabled = FALLBACK_CACHE_ENABLED_SETTING.get(settings);
}

private static String getNameFromId(String id) {
Expand Down Expand Up @@ -165,7 +152,7 @@ protected void loadMappings(ActionListener<List<ExpressionRoleMapping>> listener
new ContextPreservingActionListener<>(supplier, ActionListener.wrap((Collection<ExpressionRoleMapping> mappings) -> {
final List<ExpressionRoleMapping> mappingList = mappings.stream().filter(Objects::nonNull).toList();
logger.debug("successfully loaded [{}] role-mapping(s) from [{}]", mappingList.size(), securityIndex.aliasName());
if (shouldCacheSuccessfulLoad) {
if (fallbackCacheEnabled) {
logger.debug("caching loaded role-mapping(s)");
lastSuccessfulLoadRef.set(mappingList);
}
Expand Down Expand Up @@ -331,7 +318,7 @@ private void getMappings(ActionListener<List<ExpressionRoleMapping>> listener) {
final List<ExpressionRoleMapping> lastSuccessfulLoad = lastSuccessfulLoadRef.get();
if (frozenSecurityIndex.indexIsClosed()) {
if (lastSuccessfulLoad != null) {
assert shouldCacheSuccessfulLoad;
assert fallbackCacheEnabled;
logger.debug("The security index exists but is closed - returning previously cached role mappings");
listener.onResponse(lastSuccessfulLoad);
} else {
Expand All @@ -341,7 +328,7 @@ private void getMappings(ActionListener<List<ExpressionRoleMapping>> listener) {
} else if (frozenSecurityIndex.isAvailable(SEARCH_SHARDS) == false) {
final ElasticsearchException unavailableReason = frozenSecurityIndex.getUnavailableReason(SEARCH_SHARDS);
if (lastSuccessfulLoad != null) {
assert shouldCacheSuccessfulLoad;
assert fallbackCacheEnabled;
logger.debug(
"The security index exists but is not available - returning previously cached role mappings",
unavailableReason
Expand Down

0 comments on commit dff2616

Please sign in to comment.