Search in sources :

Example 21 with SubjectIssuerDNPair

use of datawave.security.authorization.SubjectIssuerDNPair in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQueryAndNext_DoubleAuditValues.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateQueryAndNext_DoubleAuditValues() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String queryName = "queryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 999999);
    int pagesize = 10;
    int pageTimeout = -1;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    boolean trace = false;
    String userName = "userName";
    String userSid = "userSid";
    String userDN = "userDN";
    SubjectIssuerDNPair userDNpair = SubjectIssuerDNPair.of(userDN);
    List<String> dnList = Collections.singletonList(userDN);
    UUID queryId = UUID.randomUUID();
    long pageNumber = 0L;
    HashMap<String, Collection<String>> authsMap = new HashMap<>();
    authsMap.put("userdn", Arrays.asList(queryAuthorizations));
    MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
    queryParameters.putSingle(QueryParameters.QUERY_STRING, query);
    queryParameters.putSingle(QueryParameters.QUERY_NAME, queryName);
    queryParameters.putSingle(QueryParameters.QUERY_LOGIC_NAME, queryLogicName);
    queryParameters.putSingle(QueryParameters.QUERY_BEGIN, QueryParametersImpl.formatDate(beginDate));
    queryParameters.putSingle(QueryParameters.QUERY_END, QueryParametersImpl.formatDate(endDate));
    queryParameters.putSingle(QueryParameters.QUERY_EXPIRATION, QueryParametersImpl.formatDate(expirationDate));
    queryParameters.putSingle(QueryParameters.QUERY_AUTHORIZATIONS, queryAuthorizations);
    queryParameters.putSingle(QueryParameters.QUERY_PAGESIZE, String.valueOf(pagesize));
    queryParameters.putSingle(QueryParameters.QUERY_PAGETIMEOUT, String.valueOf(pageTimeout));
    queryParameters.putSingle(QueryParameters.QUERY_PERSISTENCE, persistenceMode.name());
    queryParameters.putSingle(QueryParameters.QUERY_TRACE, String.valueOf(trace));
    queryParameters.putSingle(ColumnVisibilitySecurityMarking.VISIBILITY_MARKING, queryVisibility);
    queryParameters.putSingle(QueryParameters.QUERY_PARAMS, "auditType:NONE;auditColumnVisibility:A&B&C&D;auditUserDN:" + userDN);
    queryParameters.putSingle("valid", "param");
    ColumnVisibilitySecurityMarking marking = new ColumnVisibilitySecurityMarking();
    marking.validate(queryParameters);
    QueryParameters qp = new QueryParametersImpl();
    qp.validate(queryParameters);
    MultivaluedMap<String, String> op = new MultivaluedMapImpl<>();
    op.putAll(qp.getUnknownParameters(queryParameters));
    // op.putSingle(PrivateAuditConstants.AUDIT_TYPE, AuditType.NONE.name());
    op.putSingle(PrivateAuditConstants.LOGIC_CLASS, queryLogicName);
    op.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, queryVisibility);
    op.putSingle(PrivateAuditConstants.USER_DN, userDNpair.subjectDN());
    // Set expectations of the create logic
    queryLogic1.validate(queryParameters);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, this.principal)).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getMaxPageSize()).andReturn(1000).times(2);
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(userSid);
    expect(this.principal.getUserDN()).andReturn(userDNpair);
    expect(this.principal.getDNs()).andReturn(new String[] { userDN });
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    expect(this.queryLogic1.containsDNWithAccess(Collections.singletonList(userDN))).andReturn(true);
    expect(this.queryLogic1.getAuditType(null)).andReturn(AuditType.NONE);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(queryAuthorizations)));
    expect(this.principal.getPrimaryUser()).andReturn(dwUser);
    expect(this.dwUser.getAuths()).andReturn(Collections.singleton(queryAuthorizations));
    expect(this.principal.getProxiedUsers()).andReturn(Collections.singletonList(dwUser));
    expect(persister.create(eq(userDNpair.subjectDN()), eq(dnList), eq(marking), eq(queryLogicName), eq(qp), eq(op))).andReturn(this.query);
    expect(this.queryLogic1.getAuditType(this.query)).andReturn(AuditType.NONE);
    expect(this.queryLogic1.getConnectionPriority()).andReturn(Priority.NORMAL);
    expect(this.queryLogic1.getConnPoolName()).andReturn("connPool1");
    expect(this.connectionFactory.getTrackingMap(isA(StackTraceElement[].class))).andReturn(null);
    expect(this.connectionFactory.getConnection("connPool1", Priority.NORMAL, null)).andReturn(this.connector);
    expect(this.traceInfos.get(userSid)).andReturn(new ArrayList<>(0));
    expect(this.traceInfos.get(null)).andReturn(Arrays.asList(PatternWrapper.wrap("NONMATCHING_REGEX")));
    expect(this.qlCache.add(queryId.toString(), userSid, this.queryLogic1, this.connector)).andReturn(true);
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(true);
    expect(this.query.getOwner()).andReturn(userSid).anyTimes();
    expect(this.query.getId()).andReturn(queryId).anyTimes();
    expect(this.query.getQuery()).andReturn(queryName).anyTimes();
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).anyTimes();
    expect(this.query.getBeginDate()).andReturn(null).anyTimes();
    expect(this.query.getEndDate()).andReturn(null).anyTimes();
    expect(this.query.getColumnVisibility()).andReturn(null).anyTimes();
    expect(this.query.getQueryAuthorizations()).andReturn(queryAuthorizations).anyTimes();
    expect(this.query.getQueryName()).andReturn(null).anyTimes();
    expect(this.query.getPagesize()).andReturn(0).anyTimes();
    expect(this.query.getPageTimeout()).andReturn(-1).anyTimes();
    expect(this.query.getExpirationDate()).andReturn(null).anyTimes();
    expect(this.query.getParameters()).andReturn((Set) Collections.emptySet()).anyTimes();
    expect(this.query.getUncaughtExceptionHandler()).andReturn(new QueryUncaughtExceptionHandler()).anyTimes();
    this.metrics.updateMetric(isA(QueryMetric.class));
    PowerMock.expectLastCall().times(2);
    expect(this.query.getUserDN()).andReturn(userDN).anyTimes();
    expect(this.query.getDnList()).andReturn(dnList).anyTimes();
    expect(this.queryLogic1.getResultLimit(dnList)).andReturn(-1L);
    expect(this.queryLogic1.getMaxResults()).andReturn(-1L);
    expect(this.queryLogic1.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic1.setupQuery(this.genericConfiguration);
    expect(this.queryLogic1.getTransformIterator(this.query)).andReturn(this.transformIterator);
    expect(this.genericConfiguration.getQueryString()).andReturn(queryName).once();
    cache.put(eq(queryId.toString()), isA(RunningQuery.class));
    expect(this.qlCache.poll(queryId.toString())).andReturn(null);
    // Set expectations of the next logic
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(userSid);
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(cache.lock(queryId.toString())).andReturn(true);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    this.connectionRequestBean.requestBegin(queryId.toString());
    expect(this.runningQuery.getConnection()).andReturn(this.connector);
    this.connectionRequestBean.requestEnd(queryId.toString());
    this.runningQuery.setActiveCall(true);
    expectLastCall();
    expect(this.runningQuery.getTraceInfo()).andReturn(this.traceInfo);
    expect(this.runningQuery.next()).andReturn(this.resultsPage);
    expect(this.runningQuery.getLastPageNumber()).andReturn(pageNumber);
    expect(this.runningQuery.getLogic()).andReturn((QueryLogic) this.queryLogic1).times(2);
    expect(this.runningQuery.getSettings()).andReturn(this.query).anyTimes();
    expect(this.queryLogic1.getTransformer(this.query)).andReturn(this.transformer);
    expect(this.transformer.createResponse(this.resultsPage)).andReturn(this.baseResponse);
    expect(this.resultsPage.getResults()).andReturn(Arrays.asList((Object) "result1")).times(2);
    this.baseResponse.setHasResults(true);
    this.baseResponse.setPageNumber(pageNumber);
    expect(this.queryLogic1.getLogicName()).andReturn(queryLogicName);
    this.baseResponse.setLogicName(queryLogicName);
    this.baseResponse.setQueryId(queryId.toString());
    expect(this.runningQuery.getMetric()).andReturn(this.queryMetric);
    this.runningQuery.setActiveCall(false);
    expectLastCall();
    this.queryMetric.setProxyServers(eq(new ArrayList<>(0)));
    expect(this.responseObjectFactory.getEventQueryResponse()).andReturn(new DefaultEventQueryResponse());
    cache.unlock(queryId.toString());
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_ACTIVE).anyTimes();
    this.transaction.commit();
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, CreatedQueryLogicCacheBean.class, qlCache);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactoryImpl.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, AuditBean.class, auditor);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, Multimap.class, traceInfos);
    setInternalState(subject, SecurityMarking.class, new ColumnVisibilitySecurityMarking());
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    setInternalState(connectionRequestBean, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionRequestBean.class, connectionRequestBean);
    BaseQueryResponse result1 = subject.createQueryAndNext(queryLogicName, queryParameters);
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null response", result1);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ArrayList(java.util.ArrayList) QueryUncaughtExceptionHandler(datawave.webservice.query.util.QueryUncaughtExceptionHandler) QueryMetric(datawave.microservice.querymetric.QueryMetric) QueryPersistence(datawave.webservice.query.QueryPersistence) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParameters(datawave.webservice.query.QueryParameters) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) ColumnVisibilitySecurityMarking(datawave.marking.ColumnVisibilitySecurityMarking) Collection(java.util.Collection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with SubjectIssuerDNPair

use of datawave.security.authorization.SubjectIssuerDNPair in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testReset_auditException.

@Test(expected = DatawaveWebApplicationException.class)
public void testReset_auditException() throws Exception {
    // Set local test input
    String authorization = "AUTH_1";
    String queryName = "queryName";
    String queryLogicName = "queryLogicName";
    String userName = "userName";
    String userDN = "userDN";
    SubjectIssuerDNPair userDNpair = SubjectIssuerDNPair.of(userDN);
    String sid = "sid";
    UUID queryId = UUID.randomUUID();
    HashMap<String, Collection<String>> authsMap = new HashMap<>();
    authsMap.put("USERDN", Arrays.asList(authorization));
    QueryImpl qp = new QueryImpl();
    qp.setQuery("someQuery");
    qp.setQueryAuthorizations(authorization);
    qp.setBeginDate(new Date());
    qp.setEndDate(new Date());
    qp.setExpirationDate(new Date());
    qp.setQueryName(queryName);
    qp.setUserDN(userDN);
    qp.setDnList(Collections.singletonList(userDN));
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.set(PrivateAuditConstants.AUDIT_TYPE, AuditType.PASSIVE.name());
    map.set(PrivateAuditConstants.LOGIC_CLASS, queryLogicName);
    map.set(PrivateAuditConstants.COLUMN_VISIBILITY, authorization);
    map.set(PrivateAuditConstants.USER_DN, userDN);
    map.set(AuditParameters.AUDIT_ID, queryName);
    MultivaluedMap auditMap = new MultivaluedMapImpl();
    auditMap.putAll(map);
    // Set expectations
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_ACTIVE).anyTimes();
    expect(this.context.getCallerPrincipal()).andReturn(this.principal);
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(sid);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(authorization)));
    expect(this.principal.getUserDN()).andReturn(userDNpair);
    expect(this.cache.get(queryName)).andReturn(null);
    expect(this.persister.findById(queryName)).andReturn(Arrays.asList((Query) this.query));
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).anyTimes();
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getConnectionPriority()).andReturn(Priority.NORMAL);
    expect(this.query.getQueryAuthorizations()).andReturn(authorization).anyTimes();
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(false);
    expect(this.query.getUncaughtExceptionHandler()).andReturn(exceptionHandler).anyTimes();
    expect(this.exceptionHandler.getThrowable()).andReturn(null).anyTimes();
    expect(this.query.getOwner()).andReturn(sid).anyTimes();
    expect(this.query.getId()).andReturn(queryId).anyTimes();
    expect(this.query.getQuery()).andReturn(queryName).anyTimes();
    this.cache.put(eq(queryId.toString()), isA(RunningQuery.class));
    expect(this.cache.lock(queryName)).andReturn(true);
    expect(this.queryLogic1.getAuditType(this.query)).andReturn(AuditType.PASSIVE);
    expect(this.query.getUserDN()).andReturn(userDN).anyTimes();
    expect(this.query.toMap()).andReturn(map);
    expect(this.query.getColumnVisibility()).andReturn(authorization);
    expect(this.queryLogic1.getSelectors(this.query)).andReturn(new ArrayList<>());
    expect(this.auditor.audit(auditMap)).andReturn(null);
    expectLastCall().andThrow(new Exception("EXPECTED EXCEPTION IN AUDIT"));
    cache.unlock(queryName);
    transaction.commit();
    expect(this.queryLogic1.getLogicName()).andReturn(queryLogicName);
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, AuditBean.class, auditor);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    setInternalState(connectionRequestBean, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionRequestBean.class, connectionRequestBean);
    subject.reset(queryName);
    PowerMock.verifyAll();
}
Also used : Query(datawave.webservice.query.Query) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Date(java.util.Date) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) JMSRuntimeException(javax.jms.JMSRuntimeException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QueryImpl(datawave.webservice.query.QueryImpl) Collection(java.util.Collection) UUID(java.util.UUID) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with SubjectIssuerDNPair

use of datawave.security.authorization.SubjectIssuerDNPair in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQueryAndNext_HappyPath.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateQueryAndNext_HappyPath() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String queryName = "queryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 999999);
    int pagesize = 10;
    int pageTimeout = -1;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    boolean trace = false;
    String userName = "userName";
    String userSid = "userSid";
    String userDN = "userdn";
    SubjectIssuerDNPair userDNpair = SubjectIssuerDNPair.of(userDN);
    List<String> dnList = Collections.singletonList(userDN);
    UUID queryId = UUID.randomUUID();
    long pageNumber = 0L;
    HashMap<String, Collection<String>> authsMap = new HashMap<>();
    authsMap.put("userdn", Arrays.asList(queryAuthorizations));
    MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
    queryParameters.putSingle(QueryParameters.QUERY_STRING, query);
    queryParameters.putSingle(QueryParameters.QUERY_NAME, queryName);
    queryParameters.putSingle(QueryParameters.QUERY_LOGIC_NAME, queryLogicName);
    queryParameters.putSingle(QueryParameters.QUERY_BEGIN, QueryParametersImpl.formatDate(beginDate));
    queryParameters.putSingle(QueryParameters.QUERY_END, QueryParametersImpl.formatDate(endDate));
    queryParameters.putSingle(QueryParameters.QUERY_EXPIRATION, QueryParametersImpl.formatDate(expirationDate));
    queryParameters.putSingle(QueryParameters.QUERY_AUTHORIZATIONS, queryAuthorizations);
    queryParameters.putSingle(QueryParameters.QUERY_PAGESIZE, String.valueOf(pagesize));
    queryParameters.putSingle(QueryParameters.QUERY_PAGETIMEOUT, String.valueOf(pageTimeout));
    queryParameters.putSingle(QueryParameters.QUERY_PERSISTENCE, persistenceMode.name());
    queryParameters.putSingle(QueryParameters.QUERY_TRACE, String.valueOf(trace));
    queryParameters.putSingle(ColumnVisibilitySecurityMarking.VISIBILITY_MARKING, queryVisibility);
    queryParameters.putSingle("valid", "param");
    ColumnVisibilitySecurityMarking marking = new ColumnVisibilitySecurityMarking();
    marking.validate(queryParameters);
    QueryParameters qp = new QueryParametersImpl();
    qp.validate(queryParameters);
    MultivaluedMap<String, String> op = new MultivaluedMapImpl<>();
    op.putAll(qp.getUnknownParameters(queryParameters));
    // op.putSingle(PrivateAuditConstants.AUDIT_TYPE, AuditType.NONE.name());
    op.putSingle(PrivateAuditConstants.LOGIC_CLASS, queryLogicName);
    op.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, queryVisibility);
    op.putSingle(PrivateAuditConstants.USER_DN, userDNpair.subjectDN());
    // Set expectations of the create logic
    queryLogic1.validate(queryParameters);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, this.principal)).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getMaxPageSize()).andReturn(1000).times(2);
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(userSid);
    expect(this.principal.getUserDN()).andReturn(userDNpair);
    expect(this.principal.getDNs()).andReturn(new String[] { userDN });
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    expect(this.queryLogic1.containsDNWithAccess(Collections.singletonList(userDN))).andReturn(true);
    expect(this.queryLogic1.getAuditType(null)).andReturn(AuditType.NONE);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(queryAuthorizations)));
    expect(persister.create(eq(userDNpair.subjectDN()), eq(dnList), eq(marking), eq(queryLogicName), eq(qp), eq(op))).andReturn(this.query);
    expect(this.queryLogic1.getAuditType(this.query)).andReturn(AuditType.NONE);
    expect(this.queryLogic1.getConnectionPriority()).andReturn(Priority.NORMAL);
    expect(this.queryLogic1.getConnPoolName()).andReturn("connPool1");
    expect(this.connectionFactory.getTrackingMap(isA(StackTraceElement[].class))).andReturn(null);
    this.connectionRequestBean.requestBegin(queryId.toString());
    expect(this.connectionFactory.getConnection("connPool1", Priority.NORMAL, null)).andReturn(this.connector);
    this.connectionRequestBean.requestEnd(queryId.toString());
    expect(this.traceInfos.get(userSid)).andReturn(new ArrayList<>(0));
    expect(this.traceInfos.get(null)).andReturn(Arrays.asList(PatternWrapper.wrap("NONMATCHING_REGEX")));
    expect(this.qlCache.add(queryId.toString(), userSid, this.queryLogic1, this.connector)).andReturn(true);
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(true);
    expect(this.principal.getPrimaryUser()).andReturn(dwUser);
    expect(this.dwUser.getAuths()).andReturn(Collections.singleton(queryAuthorizations));
    expect(this.principal.getProxiedUsers()).andReturn(Collections.singletonList(dwUser));
    expect(this.query.getOwner()).andReturn(userSid).anyTimes();
    expect(this.query.getId()).andReturn(queryId).anyTimes();
    expect(this.query.getQuery()).andReturn(queryName).anyTimes();
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).anyTimes();
    expect(this.query.getBeginDate()).andReturn(null).anyTimes();
    expect(this.query.getEndDate()).andReturn(null).anyTimes();
    expect(this.query.getColumnVisibility()).andReturn(null).anyTimes();
    expect(this.query.getQueryAuthorizations()).andReturn(queryAuthorizations).anyTimes();
    expect(this.query.getQueryName()).andReturn(null).anyTimes();
    expect(this.query.getPagesize()).andReturn(0).anyTimes();
    expect(this.query.getExpirationDate()).andReturn(null).anyTimes();
    expect(this.query.getParameters()).andReturn((Set) Collections.emptySet()).anyTimes();
    expect(this.query.getUncaughtExceptionHandler()).andReturn(new QueryUncaughtExceptionHandler()).anyTimes();
    this.metrics.updateMetric(isA(QueryMetric.class));
    PowerMock.expectLastCall().times(2);
    expect(this.query.getUserDN()).andReturn(userDN).anyTimes();
    expect(this.query.getDnList()).andReturn(dnList).anyTimes();
    expect(this.queryLogic1.getResultLimit(dnList)).andReturn(-1L);
    expect(this.queryLogic1.getMaxResults()).andReturn(-1L);
    expect(this.queryLogic1.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic1.setupQuery(this.genericConfiguration);
    expect(this.queryLogic1.getTransformIterator(this.query)).andReturn(this.transformIterator);
    cache.put(eq(queryId.toString()), isA(RunningQuery.class));
    expect(this.genericConfiguration.getQueryString()).andReturn(queryName).once();
    expect(this.qlCache.poll(queryId.toString())).andReturn(null);
    // Set expectations of the next logic
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(userSid);
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(cache.lock(queryId.toString())).andReturn(true);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.runningQuery.getConnection()).andReturn(this.connector);
    this.runningQuery.setActiveCall(true);
    expectLastCall();
    expect(this.runningQuery.getTraceInfo()).andReturn(this.traceInfo);
    expect(this.runningQuery.next()).andReturn(this.resultsPage);
    expect(this.runningQuery.getLastPageNumber()).andReturn(pageNumber);
    expect(this.runningQuery.getLogic()).andReturn((QueryLogic) this.queryLogic1).times(2);
    expect(this.runningQuery.getSettings()).andReturn(this.query).anyTimes();
    expect(this.queryLogic1.getTransformer(this.query)).andReturn(this.transformer);
    expect(this.transformer.createResponse(this.resultsPage)).andReturn(this.baseResponse);
    expect(this.resultsPage.getResults()).andReturn(Arrays.asList((Object) "result1")).times(2);
    this.baseResponse.setHasResults(true);
    this.baseResponse.setPageNumber(pageNumber);
    expect(this.queryLogic1.getLogicName()).andReturn(queryLogicName);
    this.baseResponse.setLogicName(queryLogicName);
    this.baseResponse.setQueryId(queryId.toString());
    expect(this.runningQuery.getMetric()).andReturn(this.queryMetric);
    this.runningQuery.setActiveCall(false);
    expectLastCall();
    this.queryMetric.setProxyServers(eq(new ArrayList<>(0)));
    expect(this.responseObjectFactory.getEventQueryResponse()).andReturn(new DefaultEventQueryResponse());
    cache.unlock(queryId.toString());
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_ACTIVE).anyTimes();
    this.transaction.commit();
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, CreatedQueryLogicCacheBean.class, qlCache);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactoryImpl.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, AuditBean.class, auditor);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, Multimap.class, traceInfos);
    setInternalState(subject, SecurityMarking.class, new ColumnVisibilitySecurityMarking());
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    setInternalState(connectionRequestBean, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionRequestBean.class, connectionRequestBean);
    BaseQueryResponse result1 = subject.createQueryAndNext(queryLogicName, queryParameters);
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null response", result1);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ArrayList(java.util.ArrayList) QueryUncaughtExceptionHandler(datawave.webservice.query.util.QueryUncaughtExceptionHandler) QueryMetric(datawave.microservice.querymetric.QueryMetric) QueryPersistence(datawave.webservice.query.QueryPersistence) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParameters(datawave.webservice.query.QueryParameters) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) ColumnVisibilitySecurityMarking(datawave.marking.ColumnVisibilitySecurityMarking) Collection(java.util.Collection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with SubjectIssuerDNPair

use of datawave.security.authorization.SubjectIssuerDNPair in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testDuplicateQuery_UncheckedExceptionThrownDuringCreateQuery.

@Ignore
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(expected = DatawaveWebApplicationException.class)
public void testDuplicateQuery_UncheckedExceptionThrownDuringCreateQuery() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String newQueryName = "newQueryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 9999);
    int pagesize = 10;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = "invalidparam; valid:param";
    boolean trace = true;
    String userName = "userName";
    String userSid = "userSid";
    String userDN = "userDN";
    SubjectIssuerDNPair userDNpair = SubjectIssuerDNPair.of(userDN);
    UUID queryId = UUID.randomUUID();
    // Set expectations of the create logic
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).times(4);
    expect(this.principal.getName()).andReturn(userName).times(2);
    expect(this.principal.getShortName()).andReturn(userSid).times(2);
    expect(this.principal.getUserDN()).andReturn(userDNpair);
    expect(this.principal.getDNs()).andReturn(new String[] { userDN });
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    expect(this.queryLogic1.getAuditType(null)).andReturn(AuditType.NONE);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(queryAuthorizations))).times(2);
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(this.runningQuery.getSettings()).andReturn(this.query).times(2);
    expect(this.query.getOwner()).andReturn(userSid);
    QueryImpl newQuery1 = new QueryImpl();
    newQuery1.setId(UUID.randomUUID());
    expect(this.query.duplicate(newQueryName)).andReturn(newQuery1);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn((QueryLogic) this.queryLogic1).times(2);
    expect(this.queryLogic1.getLogicName()).andReturn(queryLogicName);
    expect(this.queryLogic1.getMaxPageSize()).andReturn(100).times(2);
    QueryImpl newQuery2 = new TestQuery(newQuery1);
    expect(this.queryLogic1.getAuditType(newQuery2)).andReturn(AuditType.NONE);
    Exception uncheckedException = new IllegalStateException("INTENTIONALLY THROWN UNCHECKED TEST EXCEPTION");
    expect(this.queryLogic1.getConnectionPriority()).andThrow(uncheckedException);
    this.queryLogic1.close();
    this.persister.remove(newQuery2);
    expect(this.qlCache.poll(newQuery1.getId().toString())).andReturn(null);
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, CreatedQueryLogicCacheBean.class, qlCache);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, Multimap.class, traceInfos);
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    try {
        subject.duplicateQuery(queryId.toString(), newQueryName, queryLogicName, query, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace);
    } finally {
        PowerMock.verifyAll();
    }
}
Also used : SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) ArrayList(java.util.ArrayList) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) JMSRuntimeException(javax.jms.JMSRuntimeException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QueryImpl(datawave.webservice.query.QueryImpl) QueryPersistence(datawave.webservice.query.QueryPersistence) Collection(java.util.Collection) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) PowerMockIgnore(org.powermock.core.classloader.annotations.PowerMockIgnore) Ignore(org.junit.Ignore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with SubjectIssuerDNPair

use of datawave.security.authorization.SubjectIssuerDNPair in project datawave by NationalSecurityAgency.

the class IndexQueryLogicTest method querySetUp.

@Before
public void querySetUp() throws IOException {
    log.debug("---------  querySetUp  ---------");
    // Super call to pick up authSet initialization
    super.querySetUp();
    this.logic = new IndexQueryLogic();
    QueryTestTableHelper.configureLogicToScanTables(this.logic);
    this.logic.setFullTableScanEnabled(false);
    this.logic.setIncludeDataTypeAsField(true);
    this.logic.setIncludeGroupingContext(true);
    this.logic.setDateIndexHelperFactory(new DateIndexHelperFactory());
    this.logic.setMarkingFunctions(new MarkingFunctions.Default());
    this.logic.setMetadataHelperFactory(new MetadataHelperFactory());
    this.logic.setQueryPlanner(new DefaultQueryPlanner());
    this.logic.setResponseObjectFactory(new DefaultResponseObjectFactory());
    // init must set auths
    testInit();
    SubjectIssuerDNPair dn = SubjectIssuerDNPair.of("userDn", "issuerDn");
    DatawaveUser user = new DatawaveUser(dn, DatawaveUser.UserType.USER, Sets.newHashSet(this.auths.toString().split(",")), null, null, -1L);
    this.principal = new DatawavePrincipal(Collections.singleton(user));
    this.testHarness = new QueryLogicTestHarness(this);
}
Also used : DefaultResponseObjectFactory(datawave.webservice.query.result.event.DefaultResponseObjectFactory) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) DatawaveUser(datawave.security.authorization.DatawaveUser) QueryLogicTestHarness(datawave.query.testframework.QueryLogicTestHarness) DefaultQueryPlanner(datawave.query.planner.DefaultQueryPlanner) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DateIndexHelperFactory(datawave.query.util.DateIndexHelperFactory) MarkingFunctions(datawave.marking.MarkingFunctions) MetadataHelperFactory(datawave.query.util.MetadataHelperFactory) Before(org.junit.Before)

Aggregations

SubjectIssuerDNPair (datawave.security.authorization.SubjectIssuerDNPair)29 Test (org.junit.Test)18 QueryMetricFactoryImpl (datawave.microservice.querymetric.QueryMetricFactoryImpl)13 DatawaveUser (datawave.security.authorization.DatawaveUser)13 Date (java.util.Date)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)12 QueryParametersImpl (datawave.webservice.query.QueryParametersImpl)12 QueryPersistence (datawave.webservice.query.QueryPersistence)12 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)12 ColumnVisibilitySecurityMarking (datawave.marking.ColumnVisibilitySecurityMarking)11 UUID (java.util.UUID)11 QueryParameters (datawave.webservice.query.QueryParameters)10 ArrayList (java.util.ArrayList)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)7 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)7 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6