Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Sep 13, 2024
1 parent fb285d0 commit a2b3862
Showing 1 changed file with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ public void testGetPrivilegesByStarApplicationName() throws Exception {
verify(securityIndex, never()).whenIndexAvailableForSearch(anyActionListener(), any());
}

@SuppressWarnings("unchecked")
public void testGetPrivilegesSucceedsWithWaitOnAvailableSecurityIndex() throws Exception {
final List<ApplicationPrivilegeDescriptor> sourcePrivileges = Arrays.asList(
new ApplicationPrivilegeDescriptor("myapp", "admin", newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap()),
Expand All @@ -326,6 +325,7 @@ public void testGetPrivilegesSucceedsWithWaitOnAvailableSecurityIndex() throws E
when(securityIndex.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(false).thenReturn(true);
when(securityIndex.getUnavailableReason(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(unavailableShardsException());
doAnswer(invocation -> {
@SuppressWarnings("unchecked")
final var listener = (ActionListener<Void>) invocation.getArguments()[0];
listener.onResponse(null);
return null;
Expand All @@ -340,41 +340,14 @@ public void testGetPrivilegesSucceedsWithWaitOnAvailableSecurityIndex() throws E
verify(securityIndex, times(1)).whenIndexAvailableForSearch(anyActionListener(), any());
}

@SuppressWarnings("unchecked")
public void testGetPrivilegesSucceedsWithWaitOnAvailableSecurityIndexAfterWaitFailure() throws Exception {
final List<ApplicationPrivilegeDescriptor> sourcePrivileges = Arrays.asList(
new ApplicationPrivilegeDescriptor("myapp", "admin", newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap()),
new ApplicationPrivilegeDescriptor("myapp", "user", newHashSet("action:login", "data:read/*"), emptyMap()),
new ApplicationPrivilegeDescriptor("myapp", "author", newHashSet("action:login", "data:read/*", "data:write/*"), emptyMap())
);

// first call fails, second (after "wait" fail) succeeds
when(securityIndex.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(false).thenReturn(true);
when(securityIndex.getUnavailableReason(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(unavailableShardsException());
doAnswer(invocation -> {
final var listener = (ActionListener<Void>) invocation.getArguments()[0];
// we fail here, but since the final check for availability succeeds, the overall request will succeed
listener.onFailure(new ElasticsearchTimeoutException("slow potato"));
return null;
}).when(securityIndex).whenIndexAvailableForSearch(anyActionListener(), any());

final PlainActionFuture<Collection<ApplicationPrivilegeDescriptor>> future = new PlainActionFuture<>();
store.innerGetPrivileges(Arrays.asList("myapp", "yourapp"), true, future);

ActionListener.respondAndRelease(listener.get(), buildSearchResponse(buildHits(sourcePrivileges)));

assertResult(sourcePrivileges, future);
verify(securityIndex, times(1)).whenIndexAvailableForSearch(anyActionListener(), any());
}

@SuppressWarnings("unchecked")
public void testGetPrivilegesSucceedsWillOnlyWaitOnUnavailableShardException() {
public void testGetPrivilegesWillOnlyWaitOnUnavailableShardException() {
when(securityIndex.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(false).thenReturn(true);

when(securityIndex.getUnavailableReason(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(
new IndexClosedException(new Index("potato", randomUUID()))
);
doAnswer(invocation -> {
@SuppressWarnings("unchecked")
final var listener = (ActionListener<Void>) invocation.getArguments()[0];
listener.onResponse(null);
return null;
Expand All @@ -387,14 +360,14 @@ public void testGetPrivilegesSucceedsWillOnlyWaitOnUnavailableShardException() {
verify(securityIndex, never()).whenIndexAvailableForSearch(anyActionListener(), any());
}

@SuppressWarnings("unchecked")
public void testGetPrivilegesFailsAfterWaitOnUnavailableShardException() {
when(securityIndex.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(false).thenReturn(false);

when(securityIndex.getUnavailableReason(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(
unavailableShardsException()
);
doAnswer(invocation -> {
@SuppressWarnings("unchecked")
final var listener = (ActionListener<Void>) invocation.getArguments()[0];
listener.onFailure(new ElasticsearchTimeoutException("slow potato"));
return null;
Expand All @@ -407,6 +380,34 @@ public void testGetPrivilegesFailsAfterWaitOnUnavailableShardException() {
verify(securityIndex, times(1)).whenIndexAvailableForSearch(anyActionListener(), any());
}

public void testGetPrivilegesSucceedsWithWaitOnAvailableSecurityIndexAfterWaitFailure() throws Exception {
final List<ApplicationPrivilegeDescriptor> sourcePrivileges = Arrays.asList(
new ApplicationPrivilegeDescriptor("myapp", "admin", newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap()),
new ApplicationPrivilegeDescriptor("myapp", "user", newHashSet("action:login", "data:read/*"), emptyMap()),
new ApplicationPrivilegeDescriptor("myapp", "author", newHashSet("action:login", "data:read/*", "data:write/*"), emptyMap())
);

// first call fails, second (after "wait" fail) succeeds
when(securityIndex.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(false).thenReturn(true);
when(securityIndex.getUnavailableReason(SecurityIndexManager.Availability.SEARCH_SHARDS)).thenReturn(unavailableShardsException());

doAnswer(invocation -> {
@SuppressWarnings("unchecked")
final var listener = (ActionListener<Void>) invocation.getArguments()[0];
// we fail here, but since the final check for availability succeeds, the overall request will succeed
listener.onFailure(new ElasticsearchTimeoutException("slow potato"));
return null;
}).when(securityIndex).whenIndexAvailableForSearch(anyActionListener(), any());

final PlainActionFuture<Collection<ApplicationPrivilegeDescriptor>> future = new PlainActionFuture<>();
store.innerGetPrivileges(Arrays.asList("myapp", "yourapp"), true, future);

ActionListener.respondAndRelease(listener.get(), buildSearchResponse(buildHits(sourcePrivileges)));

assertResult(sourcePrivileges, future);
verify(securityIndex, times(1)).whenIndexAvailableForSearch(anyActionListener(), any());
}

public void testGetAllPrivileges() throws Exception {
final List<ApplicationPrivilegeDescriptor> sourcePrivileges = Arrays.asList(
new ApplicationPrivilegeDescriptor("app1", "admin", newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap()),
Expand Down

0 comments on commit a2b3862

Please sign in to comment.