Search in sources :

Example 16 with EntityAuditEvent

use of org.apache.atlas.EntityAuditEvent in project incubator-atlas by apache.

the class EntityV2JerseyResourceIT method testRequestUser.

@Test
public void testRequestUser() throws Exception {
    AtlasEntity hiveDBInstanceV2 = createHiveDB(randomString());
    List<EntityAuditEvent> events = atlasClientV1.getEntityAuditEvents(hiveDBInstanceV2.getGuid(), (short) 10);
    assertEquals(events.size(), 1);
    assertEquals(events.get(0).getUser(), "admin");
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) Test(org.testng.annotations.Test)

Example 17 with EntityAuditEvent

use of org.apache.atlas.EntityAuditEvent in project incubator-atlas by apache.

the class NotificationHookConsumerIT method testCreateEntity.

@Test
public void testCreateEntity() throws Exception {
    final Referenceable entity = new Referenceable(DATABASE_TYPE_BUILTIN);
    String dbName = "db" + randomString();
    entity.set(NAME, dbName);
    entity.set(DESCRIPTION, randomString());
    entity.set(QUALIFIED_NAME, dbName);
    entity.set(CLUSTER_NAME, randomString());
    sendHookMessage(new HookNotification.EntityCreateRequest(TEST_USER, entity));
    waitFor(MAX_WAIT_TIME, new Predicate() {

        @Override
        public boolean evaluate() throws Exception {
            JSONArray results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, entity.get(QUALIFIED_NAME)));
            return results.length() == 1;
        }
    });
    //Assert that user passed in hook message is used in audit
    Referenceable instance = atlasClientV1.getEntity(DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, (String) entity.get(QUALIFIED_NAME));
    List<EntityAuditEvent> events = atlasClientV1.getEntityAuditEvents(instance.getId()._getId(), (short) 1);
    assertEquals(events.size(), 1);
    assertEquals(events.get(0).getUser(), TEST_USER);
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent) HookNotification(org.apache.atlas.notification.hook.HookNotification) Referenceable(org.apache.atlas.typesystem.Referenceable) JSONArray(org.codehaus.jettison.json.JSONArray) Test(org.testng.annotations.Test)

Example 18 with EntityAuditEvent

use of org.apache.atlas.EntityAuditEvent in project incubator-atlas by apache.

the class HBaseBasedAuditRepository method listEvents.

/**
     * List events for the given entity id in decreasing order of timestamp, from the given startKey. Returns n results
     * @param entityId entity id
     * @param startKey key for the first event to be returned, used for pagination
     * @param n number of events to be returned
     * @return list of events
     * @throws AtlasException
     */
public List<EntityAuditEvent> listEvents(String entityId, String startKey, short n) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Listing events for entity id {}, starting timestamp {}, #records {}", entityId, startKey, n);
    }
    Table table = null;
    ResultScanner scanner = null;
    try {
        table = connection.getTable(tableName);
        /**
             * Scan Details:
             * In hbase, the events are stored in increasing order of timestamp. So, doing reverse scan to get the latest event first
             * Page filter is set to limit the number of results returned.
             * Stop row is set to the entity id to avoid going past the current entity while scanning
             * small is set to true to optimise RPC calls as the scanner is created per request
             */
        Scan scan = new Scan().setReversed(true).setFilter(new PageFilter(n)).setStopRow(Bytes.toBytes(entityId)).setCaching(n).setSmall(true);
        if (StringUtils.isEmpty(startKey)) {
            //Set start row to entity id + max long value
            byte[] entityBytes = getKey(entityId, Long.MAX_VALUE);
            scan = scan.setStartRow(entityBytes);
        } else {
            scan = scan.setStartRow(Bytes.toBytes(startKey));
        }
        scanner = table.getScanner(scan);
        Result result;
        List<EntityAuditEvent> events = new ArrayList<>();
        //So, adding extra check on n here
        while ((result = scanner.next()) != null && events.size() < n) {
            EntityAuditEvent event = fromKey(result.getRow());
            //In case the user sets random start key, guarding against random events
            if (!event.getEntityId().equals(entityId)) {
                continue;
            }
            event.setUser(getResultString(result, COLUMN_USER));
            event.setAction(EntityAuditEvent.EntityAuditAction.valueOf(getResultString(result, COLUMN_ACTION)));
            event.setDetails(getResultString(result, COLUMN_DETAIL));
            if (persistEntityDefinition) {
                String colDef = getResultString(result, COLUMN_DEFINITION);
                if (colDef != null) {
                    event.setEntityDefinition(colDef);
                }
            }
            events.add(event);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got events for entity id {}, starting timestamp {}, #records {}", entityId, startKey, events.size());
        }
        return events;
    } catch (IOException e) {
        throw new AtlasException(e);
    } finally {
        close(scanner);
        close(table);
    }
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent) Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan) PageFilter(org.apache.hadoop.hbase.filter.PageFilter) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) Result(org.apache.hadoop.hbase.client.Result)

Example 19 with EntityAuditEvent

use of org.apache.atlas.EntityAuditEvent in project incubator-atlas by apache.

the class EntityAuditListener method onTraitsUpdated.

@Override
public void onTraitsUpdated(ITypedReferenceableInstance entity, Collection<? extends IStruct> traits) throws AtlasException {
    if (traits != null) {
        for (IStruct trait : traits) {
            EntityAuditEvent event = createEvent(entity, EntityAuditAction.TAG_UPDATE, "Updated trait: " + InstanceSerialization.toJson(trait, true));
            auditRepository.putEvents(event);
        }
    }
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent) IStruct(org.apache.atlas.typesystem.IStruct)

Example 20 with EntityAuditEvent

use of org.apache.atlas.EntityAuditEvent in project incubator-atlas by apache.

the class EntityAuditListener method onTraitsDeleted.

@Override
public void onTraitsDeleted(ITypedReferenceableInstance entity, Collection<String> traitNames) throws AtlasException {
    if (traitNames != null) {
        for (String traitName : traitNames) {
            EntityAuditEvent event = createEvent(entity, EntityAuditAction.TAG_DELETE, "Deleted trait: " + traitName);
            auditRepository.putEvents(event);
        }
    }
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent)

Aggregations

EntityAuditEvent (org.apache.atlas.EntityAuditEvent)20 ArrayList (java.util.ArrayList)7 Test (org.testng.annotations.Test)5 Referenceable (org.apache.atlas.typesystem.Referenceable)4 AtlasException (org.apache.atlas.AtlasException)3 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)3 IOException (java.io.IOException)2 IStruct (org.apache.atlas.typesystem.IStruct)2 Table (org.apache.hadoop.hbase.client.Table)2 BeforeTest (org.testng.annotations.BeforeTest)2 AtlasClient (org.apache.atlas.AtlasClient)1 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)1 HookNotification (org.apache.atlas.notification.hook.HookNotification)1 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)1 Put (org.apache.hadoop.hbase.client.Put)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 Scan (org.apache.hadoop.hbase.client.Scan)1 PageFilter (org.apache.hadoop.hbase.filter.PageFilter)1 JSONArray (org.codehaus.jettison.json.JSONArray)1