From 4e99a12ff987c596721dcb15c0bea468b2dc3102 Mon Sep 17 00:00:00 2001 From: nico-iaco Date: Fri, 3 Nov 2023 16:04:26 +0100 Subject: [PATCH 1/2] Added kvm and apiName flag to use a kvm as source to retrive target endpoint url with apiName as key --- README.MD | 1 + .../com/apigee/proxywriter/GenerateProxy.java | 114 +++++++++++++++++- .../templates/soap2api/get-kvm-entry.xml | 12 ++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/templates/soap2api/get-kvm-entry.xml diff --git a/README.MD b/README.MD index be8ef2d..87799a9 100644 --- a/README.MD +++ b/README.MD @@ -37,6 +37,7 @@ build=specify build folder default is temp folder; ensure user has access to r opsmap=specify operations map backendurl = Specify the target backend url backendurlvalidation=false Specify to disable the backendurl validation +kvm = Specify a KVM from where get the target endpoint url as entry ``` ## Output You should see an API Proxy bundle generated in the same folder you ran the command. The bundle follows the convention of diff --git a/src/main/java/com/apigee/proxywriter/GenerateProxy.java b/src/main/java/com/apigee/proxywriter/GenerateProxy.java index c890139..744362e 100644 --- a/src/main/java/com/apigee/proxywriter/GenerateProxy.java +++ b/src/main/java/com/apigee/proxywriter/GenerateProxy.java @@ -109,6 +109,7 @@ public class GenerateProxy { private static final String SOAP2API_XSLT12_TEMPLATE = "/templates/soap2api/add-namespace12.xslt"; private static final String SOAP2API_JSON_TO_XML_TEMPLATE = "/templates/soap2api/json-to-xml.xml"; private static final String SOAP2API_ADD_SOAPACTION_TEMPLATE = "/templates/soap2api/add-soapaction.xml"; + private static final String SOAP2API_GET_KVM_ENTRY_TEMPLATE = "/templates/soap2api/get-kvm-entry.xml"; // open-api feature private static final String SOAP2API_RETURN_OPENAPI_TEMPLATE = "/templates/soap2api/return-open-api.xml"; // private static final String SOAP2API_JSPOLICY_TEMPLATE = @@ -164,6 +165,11 @@ public class GenerateProxy { private String basePath; private String backendUrl; + + private String kvm; + + private String apiName; + private String proxyName; private String opsMap; @@ -464,6 +470,23 @@ private void writeSOAP2APIProxyEndpoint(String proxyDescription) throws Exceptio } } + if (org.apache.commons.lang3.StringUtils.isNotBlank(kvm)) { + String kvmEntryPolicyName = "getKvmEntry"; + + // Add policy to proxy.xml + Node policy1 = apiTemplateDocument.createElement("Policy"); + policy1.setTextContent(kvmEntryPolicyName); + + Node preFlowRequest = proxyDefault.getElementsByTagName("PreFlow").item(0).getChildNodes().item(1); + + step1 = proxyDefault.createElement("Step"); + name1 = proxyDefault.createElement("Name"); + name1.setTextContent(kvmEntryPolicyName); + step1.appendChild(name1); + + preFlowRequest.appendChild(step1); + } + if (APIKEY) { String apiKeyPolicy = "verify-api-key"; String remoAPIKeyPolicy = "remove-query-param-apikey"; @@ -941,6 +964,39 @@ private void writeAddNamespace(Document namespaceTemplate, String operationName, }.getClass().getEnclosingMethod().getName()); } + private void writeGetKvmEntryPolicy() throws Exception { + LOGGER.entering(GenerateProxy.class.getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + XMLUtils xmlUtils = new XMLUtils(); + + Document getKvmEntryDocument = xmlUtils.readXML(SOAP2API_GET_KVM_ENTRY_TEMPLATE); + + Document getKvmEntryXML = xmlUtils.cloneDocument(getKvmEntryDocument); + + Node rootElement = getKvmEntryXML.getFirstChild(); + NamedNodeMap attr = rootElement.getAttributes(); + Node nodeAttrName = attr.getNamedItem("name"); + nodeAttrName.setNodeValue("getKvmEntry"); + Node nodeAttrMapIdentifier = attr.getNamedItem("mapIdentifier"); + nodeAttrMapIdentifier.setNodeValue(kvm); + + String apigeeVarName = org.apache.commons.lang3.StringUtils.isNotBlank(apiName) ? apiName : serviceName; + + + Node get = getKvmEntryXML.getElementsByTagName("Get").item(0); + attr = get.getAttributes(); + Node nodeAttrAssignedTo = attr.getNamedItem("assignTo"); + nodeAttrAssignedTo.setNodeValue(apigeeVarName); + + Node parameter = getKvmEntryXML.getElementsByTagName("Parameter").item(0); + parameter.setTextContent(apigeeVarName); + + xmlUtils.writeXML(getKvmEntryXML, buildFolder + File.separator + "apiproxy" + File.separator + "policies" + + File.separator + "getKvmEntry" + ".xml"); + LOGGER.exiting(GenerateProxy.class.getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + private void writeSOAP2APIExtractPolicy(Document extractTemplate, String operationName, String policyName) throws Exception { @@ -1109,8 +1165,14 @@ private void writeTargetEndpoint() throws Exception { Node urlNode = targetDefault.getElementsByTagName("URL").item(0); - if (targetEndpoint != null && targetEndpoint.equalsIgnoreCase("") != true) { - urlNode.setTextContent(targetEndpoint); + + if (targetEndpoint != null && !targetEndpoint.equalsIgnoreCase("")) { + if (org.apache.commons.lang3.StringUtils.isNotBlank(kvm)) { + String url = org.apache.commons.lang3.StringUtils.isNotBlank(apiName) ? apiName : serviceName; + urlNode.setTextContent("{" + url + "}"); + } else { + urlNode.setTextContent(targetEndpoint); + } } else { LOGGER.warning("No target URL set"); } @@ -1311,6 +1373,18 @@ private void writeSOAPPassThruProxyEndpointConditions(String proxyDescription) t basePathNode.setTextContent(basePath); } + if (org.apache.commons.lang3.StringUtils.isNotBlank(kvm)) { + String kvmEntryPolicyName = "getKvmEntry"; + + Node preFlowRequest = proxyDefault.getElementsByTagName("PreFlow").item(0).getChildNodes().item(1); + + Node step1 = proxyDefault.createElement("Step"); + Node name1 = proxyDefault.createElement("Name"); + name1.setTextContent(kvmEntryPolicyName); + step1.appendChild(name1); + + preFlowRequest.appendChild(step1); + } // add oauth policies if set if (OAUTH) { @@ -2440,7 +2514,10 @@ private void getWSDLDetails(String wsdlPath) throws Exception { String[] schemes = {"http", "https"}; UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS); - if (backendUrl != null && !backendUrl.equalsIgnoreCase("")) { + if (org.apache.commons.lang3.StringUtils.isNotBlank(kvm)) { + String url = org.apache.commons.lang3.StringUtils.isNotBlank(apiName) ? apiName : serviceName; + targetEndpoint = "{" + url + "}"; + } else if (backendUrl != null && !backendUrl.equalsIgnoreCase("")) { targetEndpoint = backendUrl; } @@ -2906,6 +2983,9 @@ public InputStream begin(String proxyDescription, String wsdlPath) throws Except writeAPIProxy(proxyDescription); LOGGER.info("Generated Apigee proxy file."); + if (org.apache.commons.lang3.StringUtils.isNotBlank(kvm)) { + writeGetKvmEntryPolicy(); + } if (!PASSTHRU) { LOGGER.info("Generated SOAP Message Templates."); writeSOAP2APIProxyEndpoint(proxyDescription); @@ -2922,6 +3002,7 @@ public InputStream begin(String proxyDescription, String wsdlPath) throws Except writeSOAPPassThruProxyEndpointConditions(proxyDescription); } + File file = generateBundle.build(zipFolder, proxyName); LOGGER.info("Generated Apigee Edge API Bundle file: " + proxyName + ".zip"); LOGGER.exiting(GenerateProxy.class.getName(), new Object() { @@ -3093,6 +3174,10 @@ public static void main(String[] args) throws Exception { opt.getSet().addOption("backendurl", Separator.EQUALS, Multiplicity.ZERO_OR_ONE); // set backend url validation (default true) opt.getSet().addOption("backendurlvalidation", Separator.EQUALS, Multiplicity.ZERO_OR_ONE); + // set kvm name to use to retrive target backend url + opt.getSet().addOption("kvm", Separator.EQUALS, Multiplicity.ZERO_OR_ONE); + // set api name + opt.getSet().addOption("apiname", Separator.EQUALS, Multiplicity.ZERO_OR_ONE); // add enable cors conditions opt.getSet().addOption("cors", Separator.EQUALS, Multiplicity.ZERO_OR_ONE); // generate OAS spec @@ -3183,6 +3268,12 @@ public static void main(String[] args) throws Exception { if (opt.getSet().isSet("backendurlvalidation")) { genProxy.setBackendUrlValidation(new Boolean(opt.getSet().getOption("backendurlvalidation").getResultValue(0))); } + if (opt.getSet().isSet("kvm")) { + genProxy.setKvm(opt.getSet().getOption("kvm").getResultValue(0)); + } + if (opt.getSet().isSet("apiname")) { + genProxy.setApiName(opt.getSet().getOption("apiname").getResultValue(0)); + } if (opt.getSet().isSet("apikey")) { genProxy.setAPIKey(new Boolean(opt.getSet().getOption("apikey").getResultValue(0))); if (opt.getSet().isSet("quota")) { @@ -3282,6 +3373,23 @@ public GenerateProxy setBackendUrlValidation(Boolean backendUrlValidation) { return this; } + public String getKvm() { + return kvm; + } + + public GenerateProxy setKvm(String kvm) { + this.kvm = kvm; + return this; + } + + public String getApiName() { + return apiName; + } + + public void setApiName(String apiName) { + this.apiName = apiName; + } + /** * Copy and modify for Java com.predic8.wsdl.WSDLParser.groovy. Then update it to protect against XSS attacks * in getToken diff --git a/src/main/resources/templates/soap2api/get-kvm-entry.xml b/src/main/resources/templates/soap2api/get-kvm-entry.xml new file mode 100644 index 0000000..faae157 --- /dev/null +++ b/src/main/resources/templates/soap2api/get-kvm-entry.xml @@ -0,0 +1,12 @@ + + + Get Target Endpoint + false + 300 + + + key + + + environment + \ No newline at end of file From 67ae83fc9b7a44a71d9a2661e7531cb3080edf1f Mon Sep 17 00:00:00 2001 From: nico-iaco Date: Sat, 4 Nov 2023 10:07:03 +0100 Subject: [PATCH 2/2] updated README.MD --- README.MD | 1 + 1 file changed, 1 insertion(+) diff --git a/README.MD b/README.MD index 87799a9..62e569b 100644 --- a/README.MD +++ b/README.MD @@ -38,6 +38,7 @@ opsmap=specify operations map backendurl = Specify the target backend url backendurlvalidation=false Specify to disable the backendurl validation kvm = Specify a KVM from where get the target endpoint url as entry +apiname = Override service name to use it as entry key to retrieve target endpoint url from kvm ``` ## Output You should see an API Proxy bundle generated in the same folder you ran the command. The bundle follows the convention of