Skip to content

Commit

Permalink
Getting nested objects recursively + interactive links in objects view
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneg committed Jun 21, 2018
1 parent 1fae96f commit ffa4d53
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum ShowPackageConfiguration {
private static Boolean dereferenceGroupMembers = null;
private List<String> installedPackages = new ArrayList<>();
private static Map<String, String> uidToName = new HashMap<>();
private static Queue<String> nestedObjectsToRetrieve = new LinkedList<>();
List<GatewayAndServer> gatewaysWithPolicy = new ArrayList<>();
private static Set<String> knownInlineLayers = new HashSet<>();

Expand Down Expand Up @@ -409,6 +410,11 @@ Map<String, String> getUidToName()
return uidToName;
}

Queue<String> getNestedObjectsToRetrieve()
{
return nestedObjectsToRetrieve;
}

String getTarGzPath()
{
return tarGzPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,52 @@ private static PolicyPackage buildPackagePolicy(String packageName, JSONArray ob
for (Layer threatLayer : threatLayers) {
showThreatRulebase(packageName, threatLayer);
}

final Queue<String> objectsQueue = configuration.getNestedObjectsToRetrieve();
configuration.getLogger().info("There are " + objectsQueue.size() + " nested object(s) to retrieve (with limit " + LIMIT + ")");
while (!objectsQueue.isEmpty()) {

final Set<String> objectsToRetrieveChunk = new LinkedHashSet<>();
String uidFromQueue;
while (objectsToRetrieveChunk.size() < LIMIT && (uidFromQueue = objectsQueue.poll()) != null) {
objectsToRetrieveChunk.add(uidFromQueue);
}

JSONObject payload = new JSONObject();

payload.put("limit", LIMIT);
payload.put("details-level", "full");

addNewFlagsToControlDetailsLevel(payload);

final JSONArray objectsFilter = new JSONArray();
objectsFilter.add("objId");
objectsFilter.addAll(objectsToRetrieveChunk);

payload.put("in", objectsFilter);

try {
ApiResponse res = client.apiCall(loginResponse, "show-objects", payload);
addObjectsInfoIntoCollections((JSONArray) res.getPayload().get("objects"));

List<String> missingUids = new ArrayList<>();
for (String uid : objectsToRetrieveChunk) {
if (!configuration.getUidToName().containsKey(uid)) {
missingUids.add(uid);
}
}

configuration.getLogger().info(res.getPayload().get("total") + " objects were retrieved. New size of nested objects queue is " + objectsQueue.size());
if (!missingUids.isEmpty()) {
configuration.getLogger().info("There are " + missingUids.size() + " failed / non-object uid(s) " + missingUids.toString());
}
}
catch (ApiClientException e) {
handleException(e, "Failed to run show-objects");
}
}


//Crete a Html page that contains the objects of the package
writeDictionary(packageName);

Expand Down Expand Up @@ -1246,6 +1292,22 @@ private static void addObjectInformationIntoCollections(JSONObject object){
}
uidToName.put(uid, name);

Object membersField = object.get("members");
if (membersField instanceof JSONArray) {
JSONArray members = (JSONArray) membersField;
for (Object member : members) {
if (member instanceof JSONObject) {
member = ((JSONObject) member).get("uid");
}

if (!(member instanceof String) || uidToName.containsKey(member)) {
continue;
}

configuration.getNestedObjectsToRetrieve().offer((String) member);
}
}

writeJSonObjectToFile(object, configuration.getObjectsWriter(), false);
}
}
Expand Down
136 changes: 93 additions & 43 deletions src/main/resources/com/checkpoint/mgmt_api/templates/objects.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
width : auto;
}

.object_link {
text-decoration: none;
color: blue;
}

.object_link:hover {
text-decoration: underline;
}

</style>
<script>
// this part of the page is generated dynamically in the Java code by using the Check Point management APIs
Expand All @@ -55,22 +64,30 @@
// end of dynamic content
</script>
<script>
var keysOrderArray = ["uid", "name", "type", "domain", "ipv4-address", "ipv6-address", "subnet4", "subnet-mask", "mask-length4", "subnet6", "mask-length6", "port"];
var objectsMap = {};

var objects = {};

function filterObjects(arr, queryString) {
var keysOrderArray = ["uid", "name", "type", "domain", "ipv4-address", "ipv6-address", "subnet4", "subnet-mask", "mask-length4", "subnet6", "mask-length6", "port"];

function filterObjects(arr) {
var queryParams = getQueryParams();
return arr.filter(function(obj) {
var keep = true;
if ( ("type" in obj) && ("type" in queryString) ) keep = (queryString["type"] == obj["type"]);
if ( (keep) && ("uid" in obj) && ("uid" in queryString) ) keep = (queryString["uid"] == obj["uid"]);
if ( ("type" in obj) && ("type" in queryParams) ) keep = (queryParams["type"] === obj["type"]);
if ( (keep) && ("uid" in obj) && ("uid" in queryParams) ) keep = (queryParams["uid"] === obj["uid"]);
return keep;
});
}

function sortAndFilterObjects() {
var queryString = getQueryParams(document.location.search);
var filteredObjects = filterObjects(data,queryString);
data.forEach(function (o) {
if (o.uid) {
objectsMap[o.uid] = o;
}
});

var filteredObjects = filterObjects(data);
objects = filteredObjects.sort(function(a,b) {
if(a.type == null && b.type == null){ return 0}
if(a.type == null){ return 1}
Expand All @@ -93,12 +110,12 @@
arr.forEach(function(elem, index) {
var rowElement = document.createElement("tr");
var indexElement = document.createElement("td");
var valueElement = document.createElement("td");
indexElement.className = "array_item";
indexElement.appendChild(document.createTextNode("#"+index.toString()));
valueElement.className = "object_value";
printObject(elem, valueElement, level+1);
rowElement.appendChild(indexElement);

var valueElement = document.createElement("td");
printChildElement(elem, valueElement, level);
rowElement.appendChild(valueElement);
tableElement.appendChild(rowElement);
});
Expand All @@ -108,7 +125,7 @@
function printObject(obj, parentObject, level) {

if (typeof(obj) !== "object") {
parentObject.appendChild(document.createTextNode(obj.toString()));
parentObject.appendChild(generateValueNode(obj.toString()));
return;
}

Expand All @@ -122,32 +139,35 @@
var bb = keysOrderArray.indexOf(b);
return aa !== -1 && bb !== -1 && aa - bb || aa !== -1 && -1 || bb !== -1 && 1 || a.localeCompare(b);
}).forEach(function (key) {
var rowElement = document.createElement("tr");
var rowElement = document.createElement("tr");
var keyElement = document.createElement("td");
var valueElement = document.createElement("td");
var valueOfKey = obj[key];
var typeOfValue = Object.prototype.toString.call((valueOfKey));
keyElement.className = "key";
keyElement.appendChild(document.createTextNode(key + ":"));

switch (typeOfValue) {
case "[object Object]":
valueElement.className = "object_value";
printObject(valueOfKey, valueElement, level+1);
break;
case "[object Array]":
valueElement.className = "object_value";
printArray(valueOfKey, valueElement, level+1);
break;
default:
valueElement.appendChild(document.createTextNode(valueOfKey));
}
rowElement.appendChild(keyElement);

var valueElement = document.createElement("td");
printChildElement(obj[key], valueElement, level);
rowElement.appendChild(valueElement);

tableElement.appendChild(rowElement);
});
});

parentObject.appendChild(tableElement);
}

parentObject.appendChild(tableElement);
function printChildElement (elem, parent, level) {
switch (Object.prototype.toString.call((elem))){
case "[object Object]":
parent.className = "object_value";
printObject(elem, parent, level + 1);
break;
case "[object Array]":
parent.className = "object_value";
printArray(elem, parent, level + 1);
break;
default:
parent.appendChild(generateValueNode(elem));
}
}

function printObjectTitle(obj) {
Expand All @@ -165,20 +185,6 @@

}

function getQueryParams(qs) {
qs = qs.split('+').join(' ');

var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;

while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}

return params;
}

function printAllObjects() {
sortAndFilterObjects();
document.title = "Package Objects";
Expand All @@ -197,6 +203,50 @@
});
}

var isUUID = function () {
var uuidRegEx = RegExp("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", "i");
return function(str) {
return uuidRegEx.test(str);
}
}();

var getLinkBase = function () {
var linkBase = "./" + document.location.pathname.split("/").splice(-1,1) + "?uid=";
return function () {
return linkBase;
}
}();

var getQueryParams = function () {
var qs = document.location.search.split('+').join(' ');

var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;

while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}

return function () {
return params;
};
}();

function generateValueNode(value) {
var valueNode;
if (isUUID(value) && objectsMap[value] && getQueryParams()["uid"] !== value) {
valueNode = document.createElement("a");
valueNode.className = "object_link";
valueNode.setAttribute("href", getLinkBase() + value);
valueNode.setAttribute("target", "_blank");
valueNode.appendChild(document.createTextNode(value));
} else {
valueNode = document.createTextNode(value)
}
return valueNode;
}

</script>
</head>

Expand Down

0 comments on commit ffa4d53

Please sign in to comment.