Skip to content

Commit

Permalink
Added kvm and apiName flag to use a kvm as source to retrive target e…
Browse files Browse the repository at this point in the history
…ndpoint url with apiName as key
  • Loading branch information
nico-iaco authored and nico-iaco committed Nov 4, 2023
1 parent ebe1898 commit 4e99a12
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 111 additions & 3 deletions src/main/java/com/apigee/proxywriter/GenerateProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/templates/soap2api/get-kvm-entry.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="getKvmEntry" mapIdentifier="kvm">
<DisplayName>Get Target Endpoint</DisplayName>
<ExclusiveCache>false</ExclusiveCache>
<ExpiryTimeInSecs>300</ExpiryTimeInSecs>
<Get assignTo="key" index="1">
<Key>
<Parameter>key</Parameter>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>

0 comments on commit 4e99a12

Please sign in to comment.