|
| 1 | +package com.github.functionalone.xray.handlers; |
| 2 | + |
| 3 | +import java.io.BufferedOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.lang.reflect.Modifier; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 14 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 15 | + |
| 16 | +public class GenerateParameterWhitelist { |
| 17 | + |
| 18 | + public static final String USAGE = "This program will generate a parameter whitelise based upon reflection. Paramaters are read from the System properties:\n" |
| 19 | + + "gen.file: [output file name, default to whitelist.json]\n" |
| 20 | + + "gen.class: <class to generate whitelist for, example: 'com.amazonaws.services.s3.AmazonS3Client'>\n" |
| 21 | + + "gen.service: <service name, example: 'Amazon S3'>\n" |
| 22 | + + "gen.params: <parameter names comma separated, example: BucketName,Key,VersionId,Prefix>\n" |
| 23 | + + "gen.verbose: [true|false, default:false, will print more info on stdout regarding what is being done]"; |
| 24 | + |
| 25 | + private static String capitalizeFirst(String input) { |
| 26 | + return input.substring(0, 1).toUpperCase() + input.substring(1); |
| 27 | + } |
| 28 | + |
| 29 | + private static boolean VERBOSE = false; |
| 30 | + |
| 31 | + private static void debug(String msg) { |
| 32 | + if(VERBOSE) { |
| 33 | + System.out.println(msg); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Uses system properties as parameters. See usage string. |
| 39 | + * |
| 40 | + * @param args |
| 41 | + */ |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + String file = System.getProperty("gen.file", "whitelist.json"), serviceClass = System.getProperty("gen.class"), |
| 44 | + serviceName = System.getProperty("gen.service"), paramsStr = System.getProperty("gen.params"); |
| 45 | + VERBOSE = Boolean.parseBoolean(System.getProperty("gen.verbose", "false")); |
| 46 | + if (null == serviceClass || null == serviceName || null == paramsStr) { |
| 47 | + System.err.println("\n\nMissing system parameters in order to run!!!\n\n" + USAGE); |
| 48 | + System.exit(1); |
| 49 | + return; |
| 50 | + } |
| 51 | + List<String> paramsList = Arrays.asList(paramsStr.split(",")); |
| 52 | + File f = new File(file); |
| 53 | + ObjectMapper mapper = new ObjectMapper(); |
| 54 | + ObjectNode root = (ObjectNode) mapper.createObjectNode(); |
| 55 | + ObjectNode serviceNode = (ObjectNode) mapper.createObjectNode(); |
| 56 | + root.put(serviceName, serviceNode); |
| 57 | + ObjectNode operations = (ObjectNode) mapper.createObjectNode(); |
| 58 | + serviceNode.put("operations", operations); |
| 59 | + List<String> noParamMethods = new ArrayList<String>(); |
| 60 | + Class cls = Class.forName(serviceClass); |
| 61 | + Method[] methods = cls.getDeclaredMethods(); |
| 62 | + for (Method method : methods) { |
| 63 | + if (Modifier.isPublic(method.getModifiers())) { |
| 64 | + Class[] params = method.getParameterTypes(); |
| 65 | + if (params.length == 1 && params[0].getName().endsWith("Request")) { |
| 66 | + Class p = params[0]; |
| 67 | + debug("Processing Method: " + method.getName() + " with param: " + p.getSimpleName()); |
| 68 | + Method[] paramMethods = p.getMethods(); |
| 69 | + ObjectNode paramsNode = (ObjectNode) mapper.createObjectNode(); |
| 70 | + ArrayNode request_parameters = (ArrayNode) mapper.createArrayNode(); |
| 71 | + paramsNode.put("request_parameters", request_parameters); |
| 72 | + boolean hasParam = false; |
| 73 | + for (Method paramMethod : paramMethods) { |
| 74 | + String pName = paramMethod.getName(); |
| 75 | + if (pName.startsWith("get")) { |
| 76 | + pName = pName.substring("get".length()); |
| 77 | + if (paramsList.contains(pName)) { |
| 78 | + debug("\t param name: " + pName); |
| 79 | + hasParam = true; |
| 80 | + request_parameters.add(pName); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + if (!hasParam) { |
| 85 | + noParamMethods.add(method.getName()); |
| 86 | + } else { |
| 87 | + operations.put(capitalizeFirst(method.getName()), paramsNode); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + System.out.println("Methods without params: " + noParamMethods); |
| 93 | + BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(f)); |
| 94 | + bout.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(root)); |
| 95 | + bout.close(); |
| 96 | + System.out.println("Json paramter whitelist written to file: " + f.getAbsolutePath()); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments