use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class MapReduceStatePersisterTest method setup.
@Before
public void setup() throws Exception {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
connection = instance.getConnector("root", new PasswordToken(""));
if (connection.tableOperations().exists(TABLE_NAME))
connection.tableOperations().delete(TABLE_NAME);
if (connection.tableOperations().exists(INDEX_TABLE_NAME))
connection.tableOperations().delete(INDEX_TABLE_NAME);
DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of(userDN, "CN=ca, OU=acme"), UserType.USER, Arrays.asList(auths), null, null, 0L);
principal = new DatawavePrincipal(Collections.singletonList(user));
connectionFactory = createMock(AccumuloConnectionFactory.class);
ctx = createStrictMock(EJBContext.class);
bean = new MapReduceStatePersisterBean();
field(MapReduceStatePersisterBean.class, "connectionFactory").set(bean, connectionFactory);
field(MapReduceStatePersisterBean.class, "ctx").set(bean, ctx);
Logger.getLogger(MapReduceStatePersisterBean.class).setLevel(Level.OFF);
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class MapReduceStatePersisterTest method testDontFindSomeoneElsesResults.
@Test
public void testDontFindSomeoneElsesResults() throws Exception {
// create some entries
testPersistentCreate();
PowerMock.resetAll();
id = UUID.randomUUID().toString();
testPersistentCreate();
PowerMock.resetAll();
id = UUID.randomUUID().toString();
testPersistentCreate();
PowerMock.resetAll();
DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("CN=Gal Some Other sogal, OU=acme", "CN=ca, OU=acme"), UserType.USER, Arrays.asList(auths), null, null, 0L);
principal = new DatawavePrincipal(Collections.singletonList(user));
EasyMock.expect(ctx.getCallerPrincipal()).andReturn(principal);
HashMap<String, String> trackingMap = new HashMap<>();
expect(connectionFactory.getTrackingMap(EasyMock.anyObject())).andReturn(trackingMap);
expect(connectionFactory.getConnection(EasyMock.eq(AccumuloConnectionFactory.Priority.ADMIN), EasyMock.eq(trackingMap))).andReturn(connection);
connectionFactory.returnConnection(connection);
replayAll();
MapReduceInfoResponseList result = bean.find();
verifyAll();
assertEquals(0, result.getResults().size());
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class ModelBean method listModelNames.
/**
* Get the names of the models
*
* @param modelTableName
* name of the table that contains the model
* @return datawave.webservice.model.ModelList
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
*
* @HTTP 200 success
* @HTTP 500 internal server error
*/
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/list")
@GZIP
@Interceptors(ResponseInterceptor.class)
public ModelList listModelNames(@QueryParam("modelTableName") String modelTableName) {
if (modelTableName == null) {
modelTableName = defaultModelTableName;
}
ModelList response = new ModelList(jqueryUri, dataTablesUri, modelTableName);
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String user = p.getName();
Set<Authorizations> cbAuths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal cp = (DatawavePrincipal) p;
user = cp.getShortName();
for (Collection<String> auths : cp.getAuthorizations()) {
cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
}
}
log.trace(user + " has authorizations " + cbAuths);
Connector connector = null;
HashSet<String> modelNames = new HashSet<>();
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
for (Entry<Key, Value> entry : scanner) {
String colf = entry.getKey().getColumnFamily().toString();
if (!RESERVED_COLF_VALUES.contains(colf) && !modelNames.contains(colf)) {
String[] parts = colf.split(ModelKeyParser.NULL_BYTE);
if (parts.length == 1)
modelNames.add(colf);
else if (parts.length == 2)
modelNames.add(parts[0]);
}
}
}
} catch (Exception e) {
QueryException qe = new QueryException(DatawaveErrorCode.MODEL_NAME_LIST_ERROR, e);
log.error(qe);
response.addException(qe.getBottomQueryException());
throw new DatawaveWebApplicationException(qe, response);
} finally {
if (null != connector) {
try {
connectionFactory.returnConnection(connector);
} catch (Exception e) {
log.error("Error returning connection to factory", e);
}
}
}
response.setNames(modelNames);
return response;
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class ModelBean method getModel.
/**
* Retrieve the model and all of its mappings
*
* @param name
* model name
* @param modelTableName
* name of the table that contains the model
* @return datawave.webservice.model.Model
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
*
* @HTTP 200 success
* @HTTP 404 model not found
* @HTTP 500 internal server error
*/
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/{name}")
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
public datawave.webservice.model.Model getModel(@Required("name") @PathParam("name") String name, @QueryParam("modelTableName") String modelTableName) {
if (modelTableName == null) {
modelTableName = defaultModelTableName;
}
datawave.webservice.model.Model response = new datawave.webservice.model.Model(jqueryUri, dataTablesUri);
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String user = p.getName();
Set<Authorizations> cbAuths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal cp = (DatawavePrincipal) p;
user = cp.getShortName();
for (Collection<String> auths : cp.getAuthorizations()) {
cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
}
}
log.trace(user + " has authorizations " + cbAuths);
Connector connector = null;
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
IteratorSetting cfg = new IteratorSetting(21, "colfRegex", RegExFilter.class.getName());
cfg.addOption(RegExFilter.COLF_REGEX, "^" + name + "(\\x00.*)?");
scanner.addScanIterator(cfg);
for (Entry<Key, Value> entry : scanner) {
FieldMapping mapping = ModelKeyParser.parseKey(entry.getKey(), cbAuths);
response.getFields().add(mapping);
}
}
} catch (Exception e) {
QueryException qe = new QueryException(DatawaveErrorCode.MODEL_FETCH_ERROR, e);
log.error(qe);
response.addException(qe.getBottomQueryException());
throw new DatawaveWebApplicationException(qe, response);
} finally {
if (null != connector) {
try {
connectionFactory.returnConnection(connector);
} catch (Exception e) {
log.error("Error returning connection to factory", e);
}
}
}
// return 404 if model not found
if (response.getFields().isEmpty()) {
throw new NotFoundException(null, response);
}
response.setName(name);
return response;
}
use of datawave.security.authorization.DatawavePrincipal in project datawave by NationalSecurityAgency.
the class Persister method findById.
/**
* Finds Query objects by the query id
*
* @param id
* @return null if no results or list of query objects
*/
@SuppressWarnings("unchecked")
public List<Query> findById(String id) {
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String sid = p.getName();
Set<Authorizations> auths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal dp = (DatawavePrincipal) p;
sid = dp.getShortName();
for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
}
log.trace(sid + " has authorizations " + auths);
Connector conn = null;
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
conn = connectionFactory.getConnection(Priority.ADMIN, trackingMap);
tableCheck(conn);
IteratorSetting regex = new IteratorSetting(21, RegExFilter.class);
regex.addOption(RegExFilter.COLQ_REGEX, id + "\0.*");
try (Scanner scanner = ScannerHelper.createScanner(conn, TABLE_NAME, auths)) {
scanner.setRange(new Range(sid, sid));
scanner.addScanIterator(regex);
return Lists.newArrayList(Iterables.transform(scanner, resultsTransform));
}
} catch (Exception e) {
log.error("Error creating query", e);
throw new EJBException("Error creating query", e);
} finally {
try {
connectionFactory.returnConnection(conn);
} catch (Exception e) {
log.error("Error creating query", e);
}
}
}
Aggregations