Skip to content

Commit 7e79602

Browse files
albertzaharovitsweizijun
authored andcommitted
Do not create the missing index when invoking getRole (elastic#39039)
In most of the places we avoid creating the `.security` index (or updating the mapping) for read/search operations. This is more of a nit for the case of the getRole call, that fixes a possible mapping update during a get role, and removes a dead if branch about creating the `.security` index.
1 parent 5231b83 commit 7e79602

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ public void accept(Set<String> names, ActionListener<RoleRetrievalResult> listen
114114
* Retrieve a list of roles, if rolesToGet is null or empty, fetch all roles
115115
*/
116116
public void getRoleDescriptors(Set<String> names, final ActionListener<RoleRetrievalResult> listener) {
117-
if (securityIndex.indexExists() == false) {
117+
final SecurityIndexManager frozenSecurityIndex = this.securityIndex.freeze();
118+
if (frozenSecurityIndex.indexExists() == false) {
118119
// TODO remove this short circuiting and fix tests that fail without this!
119120
listener.onResponse(RoleRetrievalResult.success(Collections.emptySet()));
121+
} else if (frozenSecurityIndex.isAvailable() == false) {
122+
listener.onResponse(RoleRetrievalResult.failure(frozenSecurityIndex.getUnavailableReason()));
120123
} else if (names == null || names.isEmpty()) {
121124
securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> {
122125
QueryBuilder query = QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE);
@@ -311,17 +314,20 @@ public String toString() {
311314
}
312315

313316
private void getRoleDescriptor(final String roleId, ActionListener<RoleRetrievalResult> resultListener) {
314-
if (securityIndex.indexExists() == false) {
317+
final SecurityIndexManager frozenSecurityIndex = this.securityIndex.freeze();
318+
if (frozenSecurityIndex.indexExists() == false) {
315319
// TODO remove this short circuiting and fix tests that fail without this!
316320
resultListener.onResponse(RoleRetrievalResult.success(Collections.emptySet()));
321+
} else if (frozenSecurityIndex.isAvailable() == false) {
322+
resultListener.onResponse(RoleRetrievalResult.failure(frozenSecurityIndex.getUnavailableReason()));
317323
} else {
318-
securityIndex.prepareIndexIfNeededThenExecute(e -> resultListener.onResponse(RoleRetrievalResult.failure(e)), () ->
319-
executeGetRoleRequest(roleId, new ActionListener<GetResponse>() {
324+
securityIndex.checkIndexVersionThenExecute(e -> resultListener.onResponse(RoleRetrievalResult.failure(e)),
325+
() -> executeGetRoleRequest(roleId, new ActionListener<GetResponse>() {
320326
@Override
321327
public void onResponse(GetResponse response) {
322328
final RoleDescriptor descriptor = transformRole(response);
323-
resultListener.onResponse(RoleRetrievalResult.success(
324-
descriptor == null ? Collections.emptySet() : Collections.singleton(descriptor)));
329+
resultListener.onResponse(RoleRetrievalResult
330+
.success(descriptor == null ? Collections.emptySet() : Collections.singleton(descriptor)));
325331
}
326332

327333
@Override

0 commit comments

Comments
 (0)