|
| 1 | +#! /usr/local/bin/node |
| 2 | +/*jslint node:true, esversion:6 */ |
| 3 | +// findJavaPolicies.js |
| 4 | +// ------------------------------------------------------------------ |
| 5 | +// In Apigee Edge, find all policies in all proxies that reference a Java callout. |
| 6 | +// Or, alternatively, find proxies in an org that include a specific JAR as a resource. |
| 7 | +// |
| 8 | +// This tool does not examine environment-wide or organization-wide resources. |
| 9 | +// |
| 10 | +// last saved: <2017-December-07 10:30:42> |
| 11 | + |
| 12 | +var fs = require('fs'), |
| 13 | + async = require('async'), |
| 14 | + edgejs = require('apigee-edge-js'), |
| 15 | + common = edgejs.utility, |
| 16 | + apigeeEdge = edgejs.edge, |
| 17 | + sprintf = require('sprintf-js').sprintf, |
| 18 | + Getopt = require('node-getopt'), |
| 19 | + merge = require('merge'), |
| 20 | + version = '20170727-1150', |
| 21 | + gRegexp, |
| 22 | + getopt = new Getopt(common.commonOptions.concat([ |
| 23 | + ['J' , 'jar=ARG', 'Optional. JAR name to find. Default: search for all JavaCallout policies.'], |
| 24 | + ['R' , 'regexp', 'Optional. Treat the -J option as a regexp. Default: perform string match.'] |
| 25 | + ])).bindHelp(); |
| 26 | + |
| 27 | +// ======================================================== |
| 28 | + |
| 29 | +console.log( |
| 30 | + 'Apigee Edge JavaCallout/JAR check tool, version: ' + version + '\n' + |
| 31 | + 'Node.js ' + process.version + '\n'); |
| 32 | + |
| 33 | +common.logWrite('start'); |
| 34 | + |
| 35 | +// process.argv array starts with 'node' and 'scriptname.js' |
| 36 | +var opt = getopt.parse(process.argv.slice(2)); |
| 37 | + |
| 38 | +function handleError(e) { |
| 39 | + if (e) { |
| 40 | + console.log(e); |
| 41 | + console.log(e.stack); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function examineOnePolicy(org, options) { |
| 47 | + return function(policyName, callback) { |
| 48 | + org.proxies.getPoliciesForRevision(merge(options, {policy:policyName}), function(e, result) { |
| 49 | + handleError(e); |
| 50 | + var boolResult = (result.policyType == 'JavaCallout'); |
| 51 | + callback(boolResult); |
| 52 | + }); |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function getOneRevision (org, proxyName) { |
| 57 | + return function (revision, callback) { |
| 58 | + var options = {name:proxyName, revision:revision}; |
| 59 | + if ( opt.options.jar ) { |
| 60 | + // url = sprintf('apis/%s/revisions/%s/resources', proxyName, revision); |
| 61 | + if (opt.options.regexp && !gRegexp) { |
| 62 | + gRegexp = new RegExp(opt.options.jar); |
| 63 | + } |
| 64 | + org.proxies.getResourcesForRevision.get(options, function(e, result){ |
| 65 | + if (e) { |
| 66 | + return callback(null, null); |
| 67 | + } |
| 68 | + var jars = result && result.filter(function(item){ |
| 69 | + var isJava = item.startsWith('java://'); |
| 70 | + if ( ! isJava ) return false; |
| 71 | + var jarName = item.substring(7); |
| 72 | + return (gRegexp)?gRegexp.test(jarName) : (jarName == opt.options.jar); |
| 73 | + }); |
| 74 | + callback(null, (jars && jars.length>0)?sprintf('apis/%s/revisions/%s', proxyName, revision):null); |
| 75 | + }); |
| 76 | + } |
| 77 | + else { |
| 78 | + //url = sprintf('apis/%s/revisions/%s/policies', proxyName, revision); |
| 79 | + org.proxies.getPoliciesForRevision(options, function(e, allPolicies){ |
| 80 | + if (e) { |
| 81 | + return callback(e, []); |
| 82 | + } |
| 83 | + async.filterSeries(allPolicies, examineOnePolicy(org, options), function(results) { |
| 84 | + var javaPolicies = results.map(function(elt){ return sprintf('apis/%s/revisions/%s/policies/%s', proxyName, revision, elt); }); |
| 85 | + callback(null, javaPolicies); |
| 86 | + }); |
| 87 | + }); |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +function doneAllRevisions(proxyName, callback) { |
| 93 | + return function(e, results) { |
| 94 | + handleError(e); |
| 95 | + if (opt.options.jar) { |
| 96 | + results = results.filter(function(r) {return r;}); |
| 97 | + if (results && results.length > 0) { |
| 98 | + //results = results.map(function(r) {return parseInt(r, 10);}); |
| 99 | + common.logWrite('proxy: '+ proxyName + ' ' + JSON.stringify(results)); |
| 100 | + } |
| 101 | + callback(null, results); |
| 102 | + } |
| 103 | + else { |
| 104 | + // results is an array of arrays |
| 105 | + var flattened = [].concat.apply([], results); |
| 106 | + common.logWrite('proxy: '+ proxyName + ' ' + JSON.stringify(flattened)); |
| 107 | + callback(null, flattened); |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function doneAllProxies(e, results) { |
| 113 | + handleError(e); |
| 114 | + var flattened = [].concat.apply([], results); |
| 115 | + common.logWrite('matching Java %s: %s', (opt.options.jar)?"proxies":"policies", JSON.stringify(flattened)); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +function analyzeOneProxy(org) { |
| 120 | + return function(proxyName, callback) { |
| 121 | + org.proxies.get({ name: proxyName }, function(e, result) { |
| 122 | + handleError(e); |
| 123 | + async.mapSeries(result.revision, getOneRevision(org, proxyName), doneAllRevisions(proxyName, callback)); |
| 124 | + }); |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +common.verifyCommonRequiredParameters(opt.options, getopt); |
| 130 | + |
| 131 | +var options = { |
| 132 | + mgmtServer: opt.options.mgmtserver, |
| 133 | + org : opt.options.org, |
| 134 | + user: opt.options.username, |
| 135 | + password: opt.options.password, |
| 136 | + verbosity: opt.options.verbose || 0 |
| 137 | + }; |
| 138 | + |
| 139 | +apigeeEdge.connect(options, function(e, org){ |
| 140 | + if (e) { |
| 141 | + common.logWrite(JSON.stringify(e, null, 2)); |
| 142 | + //console.log(e.stack); |
| 143 | + process.exit(1); |
| 144 | + } |
| 145 | + |
| 146 | + org.proxies.get({}, function(e, proxies) { |
| 147 | + async.mapSeries(proxies, analyzeOneProxy(org), doneAllProxies); |
| 148 | + }); |
| 149 | + |
| 150 | +}); |
0 commit comments