Skip to content

Commit

Permalink
RANGER-4440: when compression is enabled for x_security_zone.jsonData…
Browse files Browse the repository at this point in the history
…, store summary (not complete resource details) in trx log - #2
  • Loading branch information
mneethiraj committed Oct 5, 2023
1 parent ef19f0a commit 5acbc65
Showing 1 changed file with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -42,6 +43,7 @@
import org.apache.ranger.entity.XXUser;
import org.apache.ranger.plugin.model.RangerPolicyDelta;
import org.apache.ranger.plugin.model.RangerSecurityZone;
import org.apache.ranger.plugin.model.RangerSecurityZone.RangerSecurityZoneService;
import org.apache.ranger.util.RangerEnumUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -306,8 +308,7 @@ public List<XXTrxLog> getTransactionLog(RangerSecurityZone vSecurityZone, Ranger
}
}
if("services".equalsIgnoreCase(fieldName)) {
Gson gson = new Gson();
value = gson.toJson(vSecurityZone.getServices(), HashMap.class);
value = toTrxLog(vSecurityZone.getServices());
}
if ("create".equalsIgnoreCase(action)) {
xTrxLog.setNewValue(value);
Expand All @@ -325,8 +326,7 @@ else if ("update".equalsIgnoreCase(action)) {
String mFieldName = mField.getName();
if (fieldName.equalsIgnoreCase(mFieldName)) {
if("services".equalsIgnoreCase(mFieldName)) {
Gson gson = new Gson();
oldValue = gson.toJson(securityZoneDB.getServices(), HashMap.class);
oldValue = toTrxLog(securityZoneDB.getServices());
}
else {
oldValue = mField.get(securityZoneDB) + "";
Expand Down Expand Up @@ -357,4 +357,33 @@ else if ("update".equalsIgnoreCase(action)) {
}
return trxLogList;
}

private String toTrxLog(Map<String, RangerSecurityZoneService> services) {
String ret;

if (services == null) {
services = Collections.emptyMap();
}

if (compressJsonData) { // when compression is enabled, summarize services info for trx log
Map<String, RangerSecurityZoneService> servicesSummary = new HashMap<>(services.size());

for (Map.Entry<String, RangerSecurityZoneService> entry : services.entrySet()) {
String serviceName = entry.getKey();
RangerSecurityZoneService zoneService = entry.getValue();
Integer resourceCount = (zoneService != null && zoneService.getResources() != null) ? zoneService.getResources().size() : 0;
RangerSecurityZoneService zoneServiceSummary = new RangerSecurityZoneService();

zoneServiceSummary.getResources().add(new HashMap<String, List<String>>() {{ put("resourceCount", Collections.singletonList(resourceCount.toString())); }});

servicesSummary.put(serviceName, zoneServiceSummary);
}

ret = new Gson().toJson(servicesSummary, Map.class);
} else {
ret = new Gson().toJson(services, Map.class);
}

return ret;
}
}

0 comments on commit 5acbc65

Please sign in to comment.