From 31f61a7a643e61e5369dd8e0b731235cd2135c07 Mon Sep 17 00:00:00 2001 From: eugeneg Date: Sun, 1 Jul 2018 11:53:20 +0300 Subject: [PATCH] New flag for query limit configuration --- .../examples/ShowPackageConfiguration.java | 57 +++++++++++++++++-- .../mgmt_api/examples/ShowPackageTool.java | 17 +++--- 2 files changed, 58 insertions(+), 16 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 4fbcbe6..8832f2e 100644 --- a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java +++ b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageConfiguration.java @@ -40,7 +40,6 @@ enum ShowPackageConfiguration { /*Set the names of the tar and log files*/ private static final String tarName; private static final String logFileName; - static { SimpleDateFormat formatDate = new SimpleDateFormat("YYYY-MM-dd_HH-mm-ss"); String date = formatDate.format(new Date()); @@ -48,6 +47,7 @@ enum ShowPackageConfiguration { logFileName = PREFIX + date + LOG_SUFFIX; } + /*Login credentials*/ private static String username; private static String password; @@ -63,30 +63,34 @@ enum ShowPackageConfiguration { private static String userRequestGateway; private static String userRequestPackage; private static boolean showRulesHitCounts = false; + + private static Integer queryLimit = null; + + private static final int DEFAULT_QUERY_LIMIT = 10; private static Boolean showMembership = null; + 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<>(); - /*Logger settings*/ + private static final MyLogger logger = new MyLogger("MyLog", null); private FileHandler fileHandler; - /*Paths settings*/ + private static String tarGzPath = tarName; private static String resultFolderPath; - //Define if the function needs only to show the existing packages + private static boolean showPackagesList = false; private static String proxy = ""; private HtmlUtils htmlUtil = HtmlUtils.INSTANCE; - private static RandomAccessFile objectsWriter; - private static RandomAccessFile rulbaseWriter; + private static RandomAccessFile rulbaseWriter; void initializeParameters(String[] args) throws Exception{ //Default debug level @@ -507,6 +511,11 @@ public String getResultFolderPath() return resultFolderPath; } + public Integer getQueryLimit() + { + return queryLimit == null ? DEFAULT_QUERY_LIMIT : queryLimit; + } + public boolean showRulesHitCounts() { return showRulesHitCounts; } public Boolean getShowMembership() @@ -763,6 +772,42 @@ String debugString() return "showRulesHitCounts:(-c)=" + showRulesHitCounts; } }, + queryLimit("--query-limit") { + void runCommand(String limitString) + { + final Integer limit; + + try { + limit = Integer.valueOf(limitString); + if (limit < 1 || limit > 500) { + throw new IllegalArgumentException(); + } + } + catch (IllegalArgumentException e) { + final String errorMessage = "The value of --query-limit must be an integer in range from 1 to 500"; + System.out.println(errorMessage); + throw new IllegalArgumentException(errorMessage); + } + + ShowPackageConfiguration.queryLimit = limit; + } + + void flagToString() + { + System.out.println("\tNo more than that many results will be returned on each call." + + "\n\tIncrease the limit to get a faster execution (because of less number of calls in total)" + + "\n\tDecrease the limit when your calls are hitting the timeout (e.g. when your rulebase is huge and complex)" + + "\n\tThe limit must be in range from 1 to 500" + + "\n\tDefault {" + DEFAULT_QUERY_LIMIT + "}"); + } + String debugString() + { + return "queryLimit:(--query-limit)=" + ShowPackageConfiguration.queryLimit; + } + String value(){ + return " limit"; + } + }, showMembershipOption("--show-membership") { void runCommand(String value) { 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 7b64e13..a339a05 100644 --- a/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java +++ b/src/main/java/com/checkpoint/mgmt_api/examples/ShowPackageTool.java @@ -51,9 +51,6 @@ public class ShowPackageTool { } } - //Constants - private static final int LIMIT = 10; - private static ShowPackageConfiguration configuration = ShowPackageConfiguration.INSTANCE; private static ApiClient client; private static ApiLoginResponse loginResponse; @@ -122,9 +119,9 @@ public static void main(String[] args) { /*Create an Api client and verify the server's fingerprint*/ client = new ApiClient(apiClientArgs); - client.setLimitQuery(LIMIT); + client.setLimitQuery(configuration.getQueryLimit()); - configuration.getLogger().debug("Limit number of object per page: " + LIMIT); + configuration.getLogger().debug("Limit number of object per page: " + configuration.getQueryLimit()); /*Check if the connection is to the local server*/ boolean loginAsRoot = isLoginAsRoot(); @@ -486,18 +483,18 @@ private static PolicyPackage buildPackagePolicy(String packageName, JSONArray ob } final Queue objectsQueue = configuration.getNestedObjectsToRetrieve(); - configuration.getLogger().info("There are " + objectsQueue.size() + " nested object(s) to retrieve (with limit " + LIMIT + ")"); + configuration.getLogger().info("There are " + objectsQueue.size() + " nested object(s) to retrieve (with limit " + configuration.getQueryLimit() + ")"); while (!objectsQueue.isEmpty()) { final Set objectsToRetrieveChunk = new LinkedHashSet<>(); String uidFromQueue; - while (objectsToRetrieveChunk.size() < LIMIT && (uidFromQueue = objectsQueue.poll()) != null) { + while (objectsToRetrieveChunk.size() < configuration.getQueryLimit() && (uidFromQueue = objectsQueue.poll()) != null) { objectsToRetrieveChunk.add(uidFromQueue); } JSONObject payload = new JSONObject(); - payload.put("limit", LIMIT); + payload.put("limit", configuration.getQueryLimit()); payload.put("details-level", "full"); addNewFlagsToControlDetailsLevel(payload); @@ -716,7 +713,7 @@ private static boolean showRulebase(Layer layer, String packageName, String comm ApiResponse res; int totalObjects = 0; - int limit = LIMIT; + int limit = configuration.getQueryLimit(); Set inlineLayers = new HashSet<>(); @@ -863,7 +860,7 @@ private static boolean showThreatRulebase(String packageName, Layer threatLayer) int iterations = 0; int receivedObjects; int totalObjects = 0; - int limit = LIMIT; + int limit = configuration.getQueryLimit(); configuration.getLogger().info("Starting handling threat layer: '" + threatLayer.getName() + "'"); configuration.getLogger().debug("Run command: 'show-threat-rulebase' for rulebase: '" + threatLayer.getUid()