Search in sources :

Example 1 with AuditAccessException

use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.

the class EventRepositoryHBase method getAuditTrail.

/**
 * {@inheritDoc}
 */
@Override
public EventSeries getAuditTrail(EventQueryDefinition query) {
    EventSeries es = new EventSeries();
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
            query.getActionFilters().add(ACTION_CHECK_OK);
            Scan scanQuery = buildQuery(query, COLS_EVENT, null);
            Set<String> candidates = Util.set(ACTION_DISCONNECT, ACTION_TOGGLE_ON, ACTION_TOGGLE_OFF, ACTION_CREATE, ACTION_DELETE, ACTION_UPDATE, ACTION_CLEAR);
            try (ResultScanner scanner = table.getScanner(scanQuery)) {
                for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                    String action = Bytes.toString(rr.getValue(B_AUDIT_CF, B_EVENT_ACTION));
                    if (candidates.contains(action)) {
                        es.add(MAPPER.fromStore(rr));
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new AuditAccessException("Cannot search audit trail ", e);
    }
    return es;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) EventSeries(org.ff4j.audit.EventSeries) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) AuditAccessException(org.ff4j.exception.AuditAccessException)

Example 2 with AuditAccessException

use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.

the class EventRepositoryHBase method computeHitCount.

/**
 * {@inheritDoc}
 */
private Map<String, MutableHitCount> computeHitCount(EventQueryDefinition query, String pColName) {
    Map<String, MutableHitCount> hitCount = new HashMap<String, MutableHitCount>();
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
            query.getActionFilters().add(ACTION_CHECK_OK);
            Scan scanQuery = buildQuery(query, Util.set(pColName), EventConstants.TARGET_FEATURE);
            try (ResultScanner scanner = table.getScanner(scanQuery)) {
                for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                    String colValue = Bytes.toString(rr.getValue(B_AUDIT_CF, Bytes.toBytes(pColName)));
                    if (hitCount.containsKey(colValue)) {
                        hitCount.get(colValue).inc();
                    } else {
                        hitCount.put(colValue, new MutableHitCount(1));
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new AuditAccessException("Compute hitcount based on " + pColName, e);
    }
    return hitCount;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) HashMap(java.util.HashMap) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) MutableHitCount(org.ff4j.audit.MutableHitCount) Result(org.apache.hadoop.hbase.client.Result) AuditAccessException(org.ff4j.exception.AuditAccessException)

Example 3 with AuditAccessException

use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.

the class EventRepositoryHBase method buildQuery.

private Scan buildQuery(EventQueryDefinition qDef, Set<String> columnNames, String type) {
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
            // Columns to retrieve dynamically
            Scan s = new Scan();
            for (String col : columnNames) {
                s.addColumn(B_AUDIT_CF, Bytes.toBytes(col));
            }
            /*
                s.addColumn(B_AUDIT_CF, B_EVENT_UID);
                s.addColumn(B_AUDIT_CF, B_EVENT_SOURCE);
                s.addColumn(B_AUDIT_CF, B_EVENT_NAME);
                s.addColumn(B_AUDIT_CF, B_EVENT_ACTION);
                s.addColumn(B_AUDIT_CF, B_EVENT_TYPE);
                s.addColumn(B_AUDIT_CF, B_EVENT_DURATION);
                s.addColumn(B_AUDIT_CF, B_EVENT_HOSTNAME);
                s.addColumn(B_AUDIT_CF, B_EVENT_USER);
                s.addColumn(B_AUDIT_CF, B_EVENT_VALUE);
                s.addColumn(B_AUDIT_CF, B_EVENT_DATE);
                s.addColumn(B_AUDIT_CF, B_EVENT_TIME);
                s.addColumn(B_AUDIT_CF, B_EVENT_KEYS);
                */
            // Filters
            FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            // from
            SingleColumnValueFilter filterFrom = new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TIME, CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(qDef.getFrom()));
            filterFrom.setFilterIfMissing(true);
            filterList.addFilter(filterFrom);
            // To
            SingleColumnValueFilter filterTo = new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TIME, CompareOp.LESS_OR_EQUAL, Bytes.toBytes(qDef.getTo()));
            filterTo.setFilterIfMissing(true);
            filterList.addFilter(filterTo);
            // Type
            if (null != type) {
                filterList.addFilter(new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TYPE, CompareOp.EQUAL, Bytes.toBytes(type)));
            }
            // Actions
            if (!qDef.getActionFilters().isEmpty()) {
                filterList.addFilter(createFilterListOR(B_EVENT_ACTION, qDef.getActionFilters()));
            }
            // Host
            if (!qDef.getHostFilters().isEmpty()) {
                filterList.addFilter(createFilterListOR(B_EVENT_HOSTNAME, qDef.getHostFilters()));
            }
            // Names
            if (!qDef.getNamesFilter().isEmpty()) {
                filterList.addFilter(createFilterListOR(B_EVENT_NAME, qDef.getNamesFilter()));
            }
            // Sources
            if (!qDef.getSourceFilters().isEmpty()) {
                filterList.addFilter(createFilterListOR(B_EVENT_SOURCE, qDef.getSourceFilters()));
            }
            s.setFilter(filterList);
            return s;
        }
    } catch (IOException e) {
        throw new AuditAccessException("Cannot execute command", e);
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) FilterList(org.apache.hadoop.hbase.filter.FilterList) IOException(java.io.IOException) AuditAccessException(org.ff4j.exception.AuditAccessException)

Example 4 with AuditAccessException

use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.

the class EventRepositoryHBase method searchFeatureUsageEvents.

/**
 * {@inheritDoc}
 */
@Override
public EventSeries searchFeatureUsageEvents(EventQueryDefinition query) {
    EventSeries es = new EventSeries();
    try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
        try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
            query.getActionFilters().add(ACTION_CHECK_OK);
            Scan scanQuery = buildQuery(query, COLS_EVENT, EventConstants.TARGET_FEATURE);
            try (ResultScanner scanner = table.getScanner(scanQuery)) {
                for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
                    es.add(MAPPER.fromStore(rr));
                }
            }
        }
    } catch (IOException e) {
        throw new AuditAccessException("Cannot search feature usage ", e);
    }
    return es;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) EventSeries(org.ff4j.audit.EventSeries) HBaseConnection(org.ff4j.hbase.HBaseConnection) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) AuditAccessException(org.ff4j.exception.AuditAccessException)

Example 5 with AuditAccessException

use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.

the class JdbcEventRepository method saveEvent.

/**
 * {@inheritDoc}
 */
// FIXME Stop dynamic query !
@Override
public boolean saveEvent(Event evt) {
    Util.assertEvent(evt);
    Connection sqlConn = null;
    PreparedStatement stmt = null;
    try {
        // Get collection from Pool
        sqlConn = dataSource.getConnection();
        // Open TX Bloc
        sqlConn.setAutoCommit(false);
        int idx = 9;
        Map<Integer, String> statementParams = new HashMap<Integer, String>();
        StringBuilder sb = new StringBuilder("INSERT INTO " + getQueryBuilder().getTableNameAudit() + "(EVT_UUID,EVT_TIME,EVT_TYPE,EVT_NAME,EVT_ACTION,EVT_HOSTNAME,EVT_SOURCE,EVT_DURATION");
        if (Util.hasLength(evt.getUser())) {
            sb.append(", EVT_USER");
            statementParams.put(idx, evt.getUser());
            idx++;
        }
        if (Util.hasLength(evt.getValue())) {
            sb.append(", EVT_VALUE");
            statementParams.put(idx, evt.getValue());
            idx++;
        }
        if (!evt.getCustomKeys().isEmpty()) {
            sb.append(", EVT_KEYS");
            statementParams.put(idx, MappingUtil.fromMap(evt.getCustomKeys()));
            idx++;
        }
        sb.append(") VALUES (?");
        for (int offset = 1; offset < idx - 1; offset++) {
            sb.append(",?");
        }
        sb.append(")");
        stmt = sqlConn.prepareStatement(sb.toString());
        stmt.setString(1, evt.getUuid());
        stmt.setTimestamp(2, new java.sql.Timestamp(evt.getTimestamp()));
        stmt.setString(3, evt.getType());
        stmt.setString(4, evt.getName());
        stmt.setString(5, evt.getAction());
        stmt.setString(6, evt.getHostName());
        stmt.setString(7, evt.getSource());
        stmt.setLong(8, evt.getDuration());
        for (int id = 9; id < idx; id++) {
            stmt.setString(id, statementParams.get(id));
        }
        // Execute Query
        stmt.executeUpdate();
        // Commit TX
        sqlConn.commit();
    } catch (Exception exc) {
        rollback(sqlConn);
        throw new AuditAccessException("Cannot insert event into DB (" + exc.getClass() + ") " + exc.getCause(), exc);
    } finally {
        closeStatement(stmt);
        closeConnection(sqlConn);
    }
    return true;
}
Also used : HashMap(java.util.HashMap) Timestamp(java.sql.Timestamp) Connection(java.sql.Connection) JdbcUtils.closeConnection(org.ff4j.utils.JdbcUtils.closeConnection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) FeatureAccessException(org.ff4j.exception.FeatureAccessException) AuditAccessException(org.ff4j.exception.AuditAccessException) AuditAccessException(org.ff4j.exception.AuditAccessException)

Aggregations

AuditAccessException (org.ff4j.exception.AuditAccessException)6 IOException (java.io.IOException)5 Connection (org.apache.hadoop.hbase.client.Connection)5 Scan (org.apache.hadoop.hbase.client.Scan)5 Table (org.apache.hadoop.hbase.client.Table)5 HBaseConnection (org.ff4j.hbase.HBaseConnection)5 Result (org.apache.hadoop.hbase.client.Result)4 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)4 HashMap (java.util.HashMap)2 EventSeries (org.ff4j.audit.EventSeries)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Delete (org.apache.hadoop.hbase.client.Delete)1 FilterList (org.apache.hadoop.hbase.filter.FilterList)1 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)1 MutableHitCount (org.ff4j.audit.MutableHitCount)1 FeatureAccessException (org.ff4j.exception.FeatureAccessException)1