use of org.apache.shiro.subject.ExecutionException in project ddf by codice.
the class SecurityTest method testRunWithSubjectOrElevateWhenUserSubjectExistsAndCallableThrowsException.
@Test
public void testRunWithSubjectOrElevateWhenUserSubjectExistsAndCallableThrowsException() throws Exception {
when(SecurityUtils.getSubject()).thenReturn(shiroSubject);
when(shiroSubject.execute(callable)).thenThrow(new ExecutionException(new UnsupportedOperationException()));
try {
security.runWithSubjectOrElevate(callable);
fail("InvocationTargetException expected");
} catch (SecurityServiceException e) {
throw e;
} catch (InvocationTargetException e) {
assertThat(e.getCause(), is(instanceOf(UnsupportedOperationException.class)));
}
}
use of org.apache.shiro.subject.ExecutionException in project ddf by codice.
the class TestWorkspaceQueryService method testRun.
@SuppressWarnings("unchecked")
@Test
public void testRun() throws SchedulerException, UnsupportedQueryException, SourceUnavailableException, FederationException {
String workspaceId = "3";
QueryUpdateSubscriber queryUpdateSubscriber = mock(QueryUpdateSubscriber.class);
WorkspaceService workspaceService = mock(WorkspaceService.class);
CatalogFramework catalogFramework = mock(CatalogFramework.class);
FilterBuilder filterBuilder = mock(FilterBuilder.class);
Scheduler scheduler = mock(Scheduler.class);
when(scheduler.getContext()).thenReturn(mock(SchedulerContext.class));
Supplier<Optional<Scheduler>> schedulerSupplier = () -> Optional.of(scheduler);
SecurityService securityService = new SecurityService() {
@Override
public Subject getSystemSubject() {
return mock(Subject.class);
}
@Override
public Map<String, Serializable> addSystemSubject(Map<String, Serializable> properties) {
return properties;
}
};
FilterService filterService = mock(FilterService.class);
when(filterService.getModifiedDateFilter(any())).thenReturn(mock(Filter.class));
when(filterBuilder.anyOf(Mockito.any(Filter.class))).thenReturn(mock(Or.class));
when(filterBuilder.allOf(Mockito.<Filter>anyVararg())).thenReturn(mock(And.class));
WorkspaceQueryServiceImpl workspaceQueryServiceImpl = new WorkspaceQueryServiceImpl(queryUpdateSubscriber, workspaceService, catalogFramework, filterBuilder, schedulerSupplier, securityService, filterService);
workspaceQueryServiceImpl.setQueryTimeInterval(60);
String ecql = "area( Polygon((10 10, 20 10, 20 20, 10 10)) ) BETWEEN 10000 AND 30000";
WorkspaceMetacardImpl workspaceMetacard = mock(WorkspaceMetacardImpl.class);
when(workspaceMetacard.getId()).thenReturn(workspaceId);
QueryMetacardImpl queryMetacardWithSource = mock(QueryMetacardImpl.class);
when(queryMetacardWithSource.getSources()).thenReturn(Collections.singletonList("SomeSource"));
when(queryMetacardWithSource.getCql()).thenReturn(ecql);
Attribute id1 = mock(Attribute.class);
when(id1.getValue()).thenReturn("1");
when(queryMetacardWithSource.getAttribute(Metacard.ID)).thenReturn(id1);
QueryMetacardImpl queryMetacardWithoutSource = mock(QueryMetacardImpl.class);
when(queryMetacardWithoutSource.getSources()).thenReturn(Collections.emptyList());
when(queryMetacardWithoutSource.getCql()).thenReturn(ecql);
Attribute id2 = mock(Attribute.class);
when(id2.getValue()).thenReturn("2");
when(queryMetacardWithoutSource.getAttribute(Metacard.ID)).thenReturn(id2);
Map<String, Pair<WorkspaceMetacardImpl, List<QueryMetacardImpl>>> queryMetacards = Collections.singletonMap(id2.getValue().toString(), new ImmutablePair<>(workspaceMetacard, Arrays.asList(queryMetacardWithSource, queryMetacardWithoutSource)));
when(workspaceService.getQueryMetacards()).thenReturn(queryMetacards);
long hitCount1 = 10;
long hitCount2 = 20;
QueryResponse queryResponse = mock(QueryResponse.class);
when(queryResponse.getHits()).thenReturn(hitCount1).thenReturn(hitCount2);
when(catalogFramework.query(any())).thenReturn(queryResponse);
workspaceQueryServiceImpl.setSubject(new Subject() {
@Override
public boolean isGuest() {
return false;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public PrincipalCollection getPrincipals() {
return null;
}
@Override
public boolean isPermitted(String s) {
return false;
}
@Override
public boolean isPermitted(Permission permission) {
return false;
}
@Override
public boolean[] isPermitted(String... strings) {
return new boolean[0];
}
@Override
public boolean[] isPermitted(List<Permission> list) {
return new boolean[0];
}
@Override
public boolean isPermittedAll(String... strings) {
return false;
}
@Override
public boolean isPermittedAll(Collection<Permission> collection) {
return false;
}
@Override
public void checkPermission(String s) throws AuthorizationException {
}
@Override
public void checkPermission(Permission permission) throws AuthorizationException {
}
@Override
public void checkPermissions(String... strings) throws AuthorizationException {
}
@Override
public void checkPermissions(Collection<Permission> collection) throws AuthorizationException {
}
@Override
public boolean hasRole(String s) {
return false;
}
@Override
public boolean[] hasRoles(List<String> list) {
return new boolean[0];
}
@Override
public boolean hasAllRoles(Collection<String> collection) {
return false;
}
@Override
public void checkRole(String s) throws AuthorizationException {
}
@Override
public void checkRoles(Collection<String> collection) throws AuthorizationException {
}
@Override
public void checkRoles(String... strings) throws AuthorizationException {
}
@Override
public void login(AuthenticationToken authenticationToken) throws AuthenticationException {
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public boolean isRemembered() {
return false;
}
@Override
public Session getSession() {
return null;
}
@Override
public Session getSession(boolean b) {
return null;
}
@Override
public void logout() {
}
@Override
public <V> V execute(Callable<V> callable) throws ExecutionException {
try {
return callable.call();
} catch (Exception e) {
throw new ExecutionException(e);
}
}
@Override
public void execute(Runnable runnable) {
}
@Override
public <V> Callable<V> associateWith(Callable<V> callable) {
return null;
}
@Override
public Runnable associateWith(Runnable runnable) {
return null;
}
@Override
public void runAs(PrincipalCollection principalCollection) throws NullPointerException, IllegalStateException {
}
@Override
public boolean isRunAs() {
return false;
}
@Override
public PrincipalCollection getPreviousPrincipals() {
return null;
}
@Override
public PrincipalCollection releaseRunAs() {
return null;
}
});
workspaceQueryServiceImpl.setCronString("0 0 0 * * ?");
workspaceQueryServiceImpl.setQueryTimeoutMinutes(5L);
workspaceQueryServiceImpl.run();
ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);
verify(queryUpdateSubscriber).notify(argumentCaptor.capture());
Map queryUpdateSubscriberArgumentRaw = argumentCaptor.getValue();
Map<String, Pair<WorkspaceMetacardImpl, Long>> queryUpdateSubscriberArgument = (Map<String, Pair<WorkspaceMetacardImpl, Long>>) queryUpdateSubscriberArgumentRaw;
assertThat(queryUpdateSubscriberArgument.get(workspaceId).getRight(), is(hitCount1 + hitCount2));
}
use of org.apache.shiro.subject.ExecutionException in project ddf by codice.
the class SubjectCommands method runWithUserName.
private Object runWithUserName() throws InvocationTargetException {
try {
String password = getLine("Password for " + user + ": ", false);
Subject subject = security.getSubject(user, password);
if (subject == null) {
printErrorMessage("Invalid username/password");
return null;
}
return subject.execute(this::executeWithSubject);
} catch (ExecutionException e) {
LOGGER.info("Failed to run command: {}", e.getCause().getMessage(), e.getCause());
throw new InvocationTargetException(e.getCause());
} catch (IOException e) {
LOGGER.info("Failed to run command", e);
printErrorMessage("Failed to read password");
}
return null;
}
use of org.apache.shiro.subject.ExecutionException in project ddf by codice.
the class SubjectCommandsTest method doExecuteWhenSubjectExecuteThrowsExecutionException.
@Test
public void doExecuteWhenSubjectExecuteThrowsExecutionException() throws Exception {
subjectCommands.user = USERNAME;
when(session.getKeyboard()).thenReturn(new ByteArrayInputStream(PASSWORD.getBytes()));
when(security.getSubject(USERNAME, PASSWORD)).thenReturn(subject);
when(subject.execute(any(Callable.class))).thenThrow(new ExecutionException(new IllegalStateException(ERROR)));
subjectCommands.execute();
verify(security).getSubject(USERNAME, PASSWORD);
assertThat(consoleOutput.getOutput(), containsString(ERROR));
}
use of org.apache.shiro.subject.ExecutionException in project ddf by codice.
the class MetacardApplication method attemptDeleteDeletedMetacard.
private void attemptDeleteDeletedMetacard(String id) throws UnsupportedQueryException, SourceUnavailableException, FederationException {
LOGGER.trace("Attemping to delete metacard [{}]", id);
Filter tags = filterBuilder.attribute(Metacard.TAGS).is().like().text(DeletedMetacard.DELETED_TAG);
Filter deletion = filterBuilder.attribute(DeletedMetacard.DELETION_OF_ID).is().like().text(id);
Filter filter = filterBuilder.allOf(tags, deletion);
QueryResponse response = null;
try {
response = catalogFramework.query(new QueryRequestImpl(new QueryImpl(filter), false));
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.debug("Could not find the deleted metacard marker to delete", e);
}
if (response == null || response.getResults() == null || response.getResults().size() != 1) {
LOGGER.debug("There should have been one deleted metacard marker");
return;
}
final QueryResponse _response = response;
try {
executeAsSystem(() -> catalogFramework.delete(new DeleteRequestImpl(_response.getResults().get(0).getMetacard().getId())));
} catch (ExecutionException e) {
LOGGER.debug("Could not delete the deleted metacard marker", e);
}
LOGGER.trace("Deleted delete marker metacard successfully");
}
Aggregations