From ffa4d5390758a3880d45d2539023146f1e794a7b Mon Sep 17 00:00:00 2001 From: eugeneg Date: Thu, 21 Jun 2018 13:17:17 +0300 Subject: [PATCH] Getting nested objects recursively + interactive links in objects view --- .../examples/ShowPackageConfiguration.java | 6 + .../mgmt_api/examples/ShowPackageTool.java | 62 ++++++++ .../mgmt_api/templates/objects.tpl.html | 136 ++++++++++++------ 3 files changed, 161 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java index cfff2d7..4fbcbe6 100644 --- a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java +++ b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java @@ -67,6 +67,7 @@ enum ShowPackageConfiguration { private static Boolean dereferenceGroupMembers = null; private List installedPackages = new ArrayList<>(); private static Map uidToName = new HashMap<>(); + private static Queue nestedObjectsToRetrieve = new LinkedList<>(); List gatewaysWithPolicy = new ArrayList<>(); private static Set knownInlineLayers = new HashSet<>(); @@ -409,6 +410,11 @@ Map getUidToName() return uidToName; } + Queue getNestedObjectsToRetrieve() + { + return nestedObjectsToRetrieve; + } + String getTarGzPath() { return tarGzPath; diff --git a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java index e3cea41..7b64e13 100644 --- a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java +++ b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java @@ -484,6 +484,52 @@ private static PolicyPackage buildPackagePolicy(String packageName, JSONArray ob for (Layer threatLayer : threatLayers) { showThreatRulebase(packageName, threatLayer); } + + final Queue objectsQueue = configuration.getNestedObjectsToRetrieve(); + configuration.getLogger().info("There are " + objectsQueue.size() + " nested object(s) to retrieve (with limit " + LIMIT + ")"); + while (!objectsQueue.isEmpty()) { + + final Set 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 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); @@ -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); } } diff --git a/src/main/resources/com/checkpoint/mgmt_api/templates/objects.tpl.html b/src/main/resources/com/checkpoint/mgmt_api/templates/objects.tpl.html index 8914c9d..deac856 100644 --- a/src/main/resources/com/checkpoint/mgmt_api/templates/objects.tpl.html +++ b/src/main/resources/com/checkpoint/mgmt_api/templates/objects.tpl.html @@ -46,6 +46,15 @@ width : auto; } + .object_link { + text-decoration: none; + color: blue; + } + + .object_link:hover { + text-decoration: underline; + } +