Search in sources :

Example 1 with MetadataPayload

use of io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload in project cdap by caskdata.

the class SystemMetadataAuditPublishTest method addAllSystemMetadata.

private int addAllSystemMetadata(Set<String> allMetadata) {
    for (AuditMessage auditMessage : getMetadataUpdateMessages()) {
        AuditPayload payload = auditMessage.getPayload();
        Assert.assertTrue(payload instanceof MetadataPayload);
        MetadataPayload metadataPayload = (MetadataPayload) payload;
        Map<MetadataScope, Metadata> additions = metadataPayload.getAdditions();
        if (additions.containsKey(MetadataScope.SYSTEM)) {
            allMetadata.addAll(additions.get(MetadataScope.SYSTEM).getProperties().keySet());
            allMetadata.addAll(additions.get(MetadataScope.SYSTEM).getTags());
        }
        Map<MetadataScope, Metadata> deletions = metadataPayload.getDeletions();
        if (deletions.containsKey(MetadataScope.SYSTEM)) {
            allMetadata.addAll(deletions.get(MetadataScope.SYSTEM).getProperties().keySet());
            allMetadata.addAll(deletions.get(MetadataScope.SYSTEM).getTags());
        }
    }
    return allMetadata.size();
}
Also used : AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) Metadata(io.cdap.cdap.api.metadata.Metadata) AuditPayload(io.cdap.cdap.proto.audit.AuditPayload) MetadataPayload(io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope)

Example 2 with MetadataPayload

use of io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload in project cdap by caskdata.

the class AuditMessageTest method testMetadataChange.

@Test
public void testMetadataChange() throws Exception {
    String metadataJson = "{\"version\":2,\"time\":3000,\"metadataEntity\": { \"details\": { \"namespace\": \"ns1\", \"application\": " + "\"app1\", \"version\": \"v1\" }, \"type\": \"application\" },\"user\":\"user1\",\"type\":" + "\"METADATA_CHANGE\",\"payload\":{" + "\"previous\":{\"USER\":{\"properties\":{\"uk\":\"uv\",\"uk1\":\"uv2\"},\"tags\":[\"ut1\",\"ut2\"]}," + "\"SYSTEM\":{\"properties\":{\"sk\":\"sv\"},\"tags\":[]}}," + "\"additions\":{\"SYSTEM\":{\"properties\":{\"sk\":\"sv\"},\"tags\":[\"t1\",\"t2\"]}}," + "\"deletions\":{\"USER\":{\"properties\":{\"uk\":\"uv\"},\"tags\":[\"ut1\"]}}}}";
    Map<String, String> userProperties = new HashMap<>();
    userProperties.put("uk", "uv");
    userProperties.put("uk1", "uv2");
    Map<String, String> systemProperties = new HashMap<>();
    systemProperties.put("sk", "sv");
    Set<String> userTags = new LinkedHashSet<>();
    userTags.add("ut1");
    userTags.add("ut2");
    Map<MetadataScope, Metadata> previous = new LinkedHashMap<>();
    previous.put(MetadataScope.USER, new Metadata(Collections.unmodifiableMap(userProperties), Collections.unmodifiableSet(userTags)));
    previous.put(MetadataScope.SYSTEM, new Metadata(Collections.unmodifiableMap(systemProperties), Collections.unmodifiableSet(new LinkedHashSet<String>())));
    Map<String, String> sysPropertiesAdded = new HashMap<>();
    sysPropertiesAdded.put("sk", "sv");
    Set<String> systemTagsAdded = new LinkedHashSet<>();
    systemTagsAdded.add("t1");
    systemTagsAdded.add("t2");
    Map<MetadataScope, Metadata> additions = new HashMap<>();
    additions.put(MetadataScope.SYSTEM, new Metadata(Collections.unmodifiableMap(sysPropertiesAdded), Collections.unmodifiableSet(systemTagsAdded)));
    Map<String, String> userPropertiesDeleted = new HashMap<>();
    userPropertiesDeleted.put("uk", "uv");
    Set<String> userTagsDeleted = new LinkedHashSet<>();
    userTagsDeleted.add("ut1");
    Map<MetadataScope, Metadata> deletions = new HashMap<>();
    deletions.put(MetadataScope.USER, new Metadata(Collections.unmodifiableMap(userPropertiesDeleted), Collections.unmodifiableSet(userTagsDeleted)));
    AuditMessage metadataChange = new AuditMessage(3000L, new NamespaceId("ns1").app("app1", "v1"), "user1", AuditType.METADATA_CHANGE, new MetadataPayload(previous, additions, deletions));
    Assert.assertEquals(jsonToMap(metadataJson), jsonToMap(GSON.toJson(metadataChange)));
    Assert.assertEquals(metadataChange, GSON.fromJson(metadataJson, AuditMessage.class));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Metadata(io.cdap.cdap.api.metadata.Metadata) LinkedHashMap(java.util.LinkedHashMap) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) MetadataPayload(io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload) Test(org.junit.Test)

Example 3 with MetadataPayload

use of io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload in project cdap by caskdata.

the class AuditMetadataStorage method publishAudit.

private void publishAudit(MetadataChange change, MetadataScope scope) {
    Map<String, String> propsBefore = change.getBefore().getProperties(scope);
    Map<String, String> propsAfter = change.getAfter().getProperties(scope);
    Set<String> tagsBefore = change.getBefore().getTags(scope);
    Set<String> tagsAfter = change.getAfter().getTags(scope);
    boolean propsChanged = !propsBefore.equals(propsAfter);
    boolean tagsChanged = !tagsBefore.equals(tagsAfter);
    if (!propsChanged && !tagsChanged) {
        // no change to log
        return;
    }
    // previous state is already given
    MetadataRecord previous = new MetadataRecord(change.getEntity(), scope, propsBefore, tagsBefore);
    // compute what was added
    @SuppressWarnings("ConstantConditions") Map<String, String> propsAdded = Maps.filterEntries(propsAfter, entry -> !entry.getValue().equals(propsBefore.get(entry.getKey())));
    Set<String> tagsAdded = Sets.difference(tagsAfter, tagsBefore);
    MetadataRecord additions = new MetadataRecord(change.getEntity(), scope, propsAdded, tagsAdded);
    // compute what was deleted
    @SuppressWarnings("ConstantConditions") Map<String, String> propsDeleted = Maps.filterEntries(propsBefore, entry -> !entry.getValue().equals(propsAfter.get(entry.getKey())));
    Set<String> tagsDeleted = Sets.difference(tagsBefore, tagsAfter);
    MetadataRecord deletions = new MetadataRecord(change.getEntity(), scope, propsDeleted, tagsDeleted);
    // and publish
    MetadataPayload payload = new MetadataPayloadBuilder().addPrevious(previous).addAdditions(additions).addDeletions(deletions).build();
    AuditPublishers.publishAudit(auditPublisher, previous.getMetadataEntity(), AuditType.METADATA_CHANGE, payload);
}
Also used : MetadataPayloadBuilder(io.cdap.cdap.data2.audit.payload.builder.MetadataPayloadBuilder) MetadataRecord(io.cdap.cdap.common.metadata.MetadataRecord) MetadataPayload(io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload)

Aggregations

MetadataPayload (io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload)3 Metadata (io.cdap.cdap.api.metadata.Metadata)2 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)2 MetadataRecord (io.cdap.cdap.common.metadata.MetadataRecord)1 MetadataPayloadBuilder (io.cdap.cdap.data2.audit.payload.builder.MetadataPayloadBuilder)1 AuditMessage (io.cdap.cdap.proto.audit.AuditMessage)1 AuditPayload (io.cdap.cdap.proto.audit.AuditPayload)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1