Skip to content

Commit

Permalink
New flag for query limit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneg committed Jul 1, 2018
1 parent 471139f commit 31f61a7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ 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());
tarName = PREFIX + date + TAR_SUFFIX;
logFileName = PREFIX + date + LOG_SUFFIX;
}


/*Login credentials*/
private static String username;
private static String password;
Expand All @@ -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<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<>();

/*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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -486,18 +483,18 @@ private static PolicyPackage buildPackagePolicy(String packageName, JSONArray ob
}

final Queue<String> 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<String> 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);
Expand Down Expand Up @@ -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<Layer> inlineLayers = new HashSet<>();

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 31f61a7

Please sign in to comment.