Search in sources :

Example 91 with DatawavePrincipal

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

the class AtomServiceBean method getFeed.

/**
 * @param category
 *            collection name
 * @param lastKey
 *            last key returned, page will begin with the next key
 * @param pagesize
 *            size of the page
 * @return Atom Feed document for a collection
 */
@GET
@GZIP
@Produces("application/atom+xml")
@Path("/{category}")
public Feed getFeed(@Required("category") @PathParam("category") String category, @QueryParam("l") String lastKey, @QueryParam("pagesize") @DefaultValue("30") int pagesize) {
    // Feed must contain
    // one atom:author
    // exactly one atom:id
    // SHOULD contain one atom:link element with a rel
    // attribute value of "self". This is the preferred URI for
    // retrieving Atom Feed Documents representing this Atom feed.
    // contain exactly one atom:title element.
    // contain exactly one atom:updated element.
    Principal p = ctx.getCallerPrincipal();
    Set<Authorizations> auths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
    }
    Feed result;
    Connector connection = null;
    Date maxDate = new Date(0);
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connection = connectionFactory.getConnection(poolName, Priority.NORMAL, trackingMap);
        result = abdera.newFeed();
        result.addAuthor(clustername);
        result.setTitle(category);
        Key nextLastKey = null;
        int count = 0;
        try (Scanner scanner = ScannerHelper.createScanner(connection, tableName, auths)) {
            if (null != lastKey) {
                Key lastSeenKey = deserializeKey(lastKey);
                scanner.setRange(new Range(lastSeenKey, false, new Key(category + "\1"), false));
            } else {
                scanner.setRange(new Range(category, true, category + "\1", false));
            }
            for (Map.Entry<Key, Value> entry : scanner) {
                AtomKeyValueParser atom = AtomKeyValueParser.parse(entry.getKey(), entry.getValue());
                if (atom.getUpdated().after(maxDate)) {
                    maxDate = atom.getUpdated();
                }
                nextLastKey = entry.getKey();
                Entry e = atom.toEntry(abdera, this.host, this.port);
                result.addEntry(e);
                count++;
                if (count >= pagesize)
                    break;
            }
        }
        String thisLastKey = "";
        if (null != nextLastKey)
            thisLastKey = serializeKey(nextLastKey);
        String id = MessageFormat.format(COLLECTION_LINK_FORMAT, this.host, this.port, category);
        result.setId(id);
        // need a link that contains the offset of null and current pagesize
        result.addLink(id + "?pagesize=" + pagesize, "first");
        // need a link that contains the next offset and current pagesize
        result.addLink(id + "?pagesize=" + pagesize + "&l=" + thisLastKey, "next");
        result.setUpdated(maxDate);
        if (count == 0)
            throw new NoResultsException(null);
        else
            return result;
    } catch (WebApplicationException web) {
        throw web;
    } catch (Exception e) {
        VoidResponse response = new VoidResponse();
        QueryException qe = new QueryException(DatawaveErrorCode.FEED_GET_ERROR, e, MessageFormat.format("collection: {0}", category));
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connection) {
            try {
                connectionFactory.returnConnection(connection);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Entry(org.apache.abdera.model.Entry) VoidResponse(datawave.webservice.result.VoidResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) HashSet(java.util.HashSet) Feed(org.apache.abdera.model.Feed) Authorizations(org.apache.accumulo.core.security.Authorizations) Range(org.apache.accumulo.core.data.Range) Date(java.util.Date) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) NoResultsException(datawave.webservice.common.exception.NoResultsException) QueryException(datawave.webservice.query.exception.QueryException) QueryException(datawave.webservice.query.exception.QueryException) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) Map(java.util.Map) HashMap(java.util.HashMap) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 92 with DatawavePrincipal

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

the class TestCardinalityWithQuery method setup.

@Before
public void setup() throws Exception {
    System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
    temporaryFolder = tempDir.newFolder().toPath();
    logic = new ShardQueryLogic();
    logic.setMarkingFunctions(new MarkingFunctions.Default());
    logic.setResponseObjectFactory(new DefaultResponseObjectFactory());
    logic.setMetadataHelperFactory(new MetadataHelperFactory());
    logic.setDateIndexHelperFactory(new DateIndexHelperFactory());
    QueryTestTableHelper.configureLogicToScanTables(logic);
    loadData();
    SubjectIssuerDNPair dn = SubjectIssuerDNPair.of("userDn", "issuerDn");
    DatawaveUser user = new DatawaveUser(dn, UserType.USER, Sets.newHashSet(auths.toString().split(",")), null, null, -1L);
    datawavePrincipal = new DatawavePrincipal(Collections.singleton(user));
}
Also used : DefaultResponseObjectFactory(datawave.webservice.query.result.event.DefaultResponseObjectFactory) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) ShardQueryLogic(datawave.query.tables.ShardQueryLogic) DatawaveUser(datawave.security.authorization.DatawaveUser) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) MarkingFunctions(datawave.marking.MarkingFunctions) MetadataHelperFactory(datawave.query.util.MetadataHelperFactory) DateIndexHelperFactory(datawave.query.util.DateIndexHelperFactory) Before(org.junit.Before)

Example 93 with DatawavePrincipal

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

the class AuthorizationsUtil method buildUserAuthorizationString.

public static String buildUserAuthorizationString(Principal principal) {
    String auths = "";
    if (principal != null && (principal instanceof DatawavePrincipal)) {
        DatawavePrincipal datawavePrincipal = (DatawavePrincipal) principal;
        auths = new Authorizations(datawavePrincipal.getPrimaryUser().getAuths().toArray(new String[0])).toString();
    }
    return auths;
}
Also used : Authorizations(org.apache.accumulo.core.security.Authorizations) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal)

Aggregations

DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)93 DatawaveUser (datawave.security.authorization.DatawaveUser)41 Principal (java.security.Principal)37 HashSet (java.util.HashSet)33 Test (org.junit.Test)29 QueryException (datawave.webservice.query.exception.QueryException)24 Connector (org.apache.accumulo.core.client.Connector)23 IOException (java.io.IOException)19 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)18 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)18 Authorizations (org.apache.accumulo.core.security.Authorizations)17 Query (datawave.webservice.query.Query)16 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)15 NoResultsException (datawave.webservice.common.exception.NoResultsException)13 ArrayList (java.util.ArrayList)13 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13 SubjectIssuerDNPair (datawave.security.authorization.SubjectIssuerDNPair)12 WebApplicationException (javax.ws.rs.WebApplicationException)12 BadRequestException (datawave.webservice.common.exception.BadRequestException)11