From 2d9387af1f71b67b16e3c3aa8cd9301c22510ec3 Mon Sep 17 00:00:00 2001 From: anaik91 Date: Wed, 6 Sep 2023 21:28:02 +0530 Subject: [PATCH] fix: addressed PR comments --- tools/target-server-validator/README.md | 35 +++++++++---------- .../policies/AM-Set-Json-Response.xml | 2 +- .../apiproxy/policies/JC-Port-Open-Check.xml | 6 ++-- .../apiproxy/target_server_validator.xml | 18 +--------- tools/target-server-validator/callout/pom.xml | 4 +-- .../callout/src/main/java/PortOpenCheck.java | 18 +++++++--- .../callout/src/main/java/package-info.java | 2 +- .../target-server-validator/input.properties | 8 ++--- tools/target-server-validator/main.py | 7 ++-- 9 files changed, 46 insertions(+), 54 deletions(-) diff --git a/tools/target-server-validator/README.md b/tools/target-server-validator/README.md index 0160adc3..8b7b24b0 100644 --- a/tools/target-server-validator/README.md +++ b/tools/target-server-validator/README.md @@ -1,20 +1,19 @@ # Apigee Target Server Validator The objective of this tool to validate targets in Target Servers & Apigee API Proxy Bundles exported from Apigee. -Validation is done by deploying a sample proxy which check if HOST & PORT is open from Apigee OPDK/X/Hybrid. +Validation is done by deploying a sample proxy which check if HOST & PORT is open from Apigee. > **NOTE**: Discovery of Targets in API Proxy & Sharedflows is limited to only parsing URL from `TargetEndpoint` & `ServiceCallout` Policy. > **NOTE**: Dynamic targets are **NOT** supported, Ex : `https://host.{request.formparam.region}.example.com}` ## Pre-Requisites -* python3.x +* Python3.x * Java -* mvn -* Please install required Python Libs - +* Maven +* Please install the required Python dependencies ``` -python3 -m pip install requirements.txt + python3 -m pip install -r requirements.txt ``` * Please build the java callout jar by running the below command @@ -26,13 +25,13 @@ bash callout/build_java_callout.sh ``` [source] -baseurl=http://34.131.144.184:8080/v1 # Apigee OPDK/Edge/X/Hybrid Base URL -org=xxx-xxxx-xxx-xxxxx # Apigee OPDK/Edge/X/Hybrid Org +baseurl=https://x.x.x.x/v1 # Apigee Base URL. e.g http://management-api.apigee-opdk.corp:8080 +org=xxx-xxxx-xxx-xxxxx # Apigee Org ID auth_type=basic # API Auth type basic | oauth [target] -baseurl=https://apigee.googleapis.com/v1 # Apigee OPDK/Edge/X/Hybrid Base URL -org=xxx-xxxx-xxx-xxxxx # Apigee OPDK/Edge/X/Hybrid Org Id +baseurl=https://apigee.googleapis.com/v1 # Apigee Base URL +org=xxx-xxxx-xxx-xxxxx # Apigee Org ID auth_type=oauth # API Auth type basic | oauth [csv] @@ -47,16 +46,16 @@ proxy_export_dir=export # Export directory needed when check api_env=dev # Target Environment to deploy Validation API Proxy api_name=target_server_validator # Target API Name of Validation API Proxy api_force_redeploy=false # set 'true' to Re-deploy Target API Proxy -vhost_domain_name=devgroup # Target VHost or EnvGroup -vhost_ip= # IP address corresponding to vhost_domain_name. Use if DNS record doesnt exist -report_format=csv # Report Format. Choose csv or md (Markdown) +api_hostname=example.apigee.com # Target VirtualHost or EnvGroup Domain Name +api_ip= # IP address corresponding to api_hostname. Use if DNS record doesnt exist +report_format=csv # Report Format. Choose csv or md (defaults to md) ``` * Sample input CSV with target servers > **NOTE:** You need to set `check_csv=true` in the `validation` section of `input.properties` > **NOTE:** You need to set `file=` in the `csv` section of `input.properties` - +> If PORT is omitted from the csv, the value of default_port will be used from `input.properties`. ``` HOST,PORT httpbin.org @@ -65,19 +64,19 @@ smtp.gmail.com,465 ``` -* Please run below command to authenticate against Apigee X/Hybrid APIs +* Please run below commands to authenticate, based on the Apigee flavours you are using. ``` export APIGEE_OPDK_ACCESS_TOKEN=$(echo -n ":" | base64) # Access token for Apigee OPDK -export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token) # Access token for Apigee X +export APIGEE_ACCESS_TOKEN=$(gcloud auth print-access-token) # Access token for Apigee X/Hybrid ``` ## Highlevel Working * Export Target Server Details * Export Proxy Bundle * Parse Each Proxy Bundle for Target -* Run Validate API against each Target -* Generate CSV Report +* Run Validate API against each Target (optional) +* Generate csv/md Report ## Usage diff --git a/tools/target-server-validator/apiproxy/policies/AM-Set-Json-Response.xml b/tools/target-server-validator/apiproxy/policies/AM-Set-Json-Response.xml index 29a8f0c5..df4a122b 100644 --- a/tools/target-server-validator/apiproxy/policies/AM-Set-Json-Response.xml +++ b/tools/target-server-validator/apiproxy/policies/AM-Set-Json-Response.xml @@ -16,7 +16,7 @@ { "host":"{request.header.host_name}", "port": "{request.header.port_number}", - "status":"{REACHABLE_STATUS}" + "status":"{flow.reachableStatus}" } diff --git a/tools/target-server-validator/apiproxy/policies/JC-Port-Open-Check.xml b/tools/target-server-validator/apiproxy/policies/JC-Port-Open-Check.xml index feb50800..cd206178 100644 --- a/tools/target-server-validator/apiproxy/policies/JC-Port-Open-Check.xml +++ b/tools/target-server-validator/apiproxy/policies/JC-Port-Open-Check.xml @@ -12,8 +12,6 @@ limitations under the License. --> - JC-Port-Open-Check - - com.apigeesample.PortOpenCheck - java://edge-custom-policy-java-hello.jar + com.apigee.devrel.apigee_target_server_validator.PortOpenCheck + java://jc-target-server-validator.jar \ No newline at end of file diff --git a/tools/target-server-validator/apiproxy/target_server_validator.xml b/tools/target-server-validator/apiproxy/target_server_validator.xml index ff411df9..a86e9f5a 100644 --- a/tools/target-server-validator/apiproxy/target_server_validator.xml +++ b/tools/target-server-validator/apiproxy/target_server_validator.xml @@ -11,20 +11,4 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - 1682421435781 - 1682421435781 - /validate_target_server - - JC1 - set-json-response - - - default - - - java://edge-custom-policy-java-hello.jar - - \ No newline at end of file + diff --git a/tools/target-server-validator/callout/pom.xml b/tools/target-server-validator/callout/pom.xml index 797024c9..90b66584 100644 --- a/tools/target-server-validator/callout/pom.xml +++ b/tools/target-server-validator/callout/pom.xml @@ -16,9 +16,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4.0.0 com.apigee.callout - edge-custom-policy-java-hello + jc-target-server-validator 1.0-SNAPSHOT - EdgeCustomJavaHello + JavaTargetServerValidator http://maven.apache.org jar diff --git a/tools/target-server-validator/callout/src/main/java/PortOpenCheck.java b/tools/target-server-validator/callout/src/main/java/PortOpenCheck.java index f8a564cb..c04630ba 100644 --- a/tools/target-server-validator/callout/src/main/java/PortOpenCheck.java +++ b/tools/target-server-validator/callout/src/main/java/PortOpenCheck.java @@ -13,7 +13,7 @@ // limitations under the License. -package com.apigeesample; +package com.apigee.devrel.apigee_target_server_validator; import com.apigee.flow.execution.ExecutionContext; import com.apigee.flow.execution.ExecutionResult; @@ -24,6 +24,8 @@ import java.net.Socket; import java.net.SocketTimeoutException; import java.net.UnknownHostException; +import com.apigee.flow.execution.Action; + /** * A callout that checks if a particular port is open on a specified host. @@ -54,7 +56,7 @@ private static String available(final String host, final int port) { try { socket.close(); } catch (IOException e) { - throw new RuntimeException("You should handle this error.", e); + throw new RuntimeException("Exception occured", e); } } } @@ -75,10 +77,18 @@ public ExecutionResult execute(final MessageContext messageContext, int portnumber = Integer.parseInt(port); String status = available(hostname, portnumber); // messageContext.getMessage().setContent(Status); - messageContext.setVariable("REACHABLE_STATUS", status); + messageContext.setVariable("flow.reachableStatus", status); return ExecutionResult.SUCCESS; } catch (Exception e) { - return ExecutionResult.ABORT; + ExecutionResult executionResult = new ExecutionResult(false, + Action.ABORT); + //--Returns custom error message and header + executionResult.setErrorResponse(e.getMessage()); + executionResult.addErrorResponseHeader("ExceptionClass", + e.getClass().getName()); + //--Set flow variables -- may be useful for debugging. + messageContext.setVariable("JAVA_ERROR", e.getMessage()); + return executionResult; } } } diff --git a/tools/target-server-validator/callout/src/main/java/package-info.java b/tools/target-server-validator/callout/src/main/java/package-info.java index 01910659..d1e2a182 100644 --- a/tools/target-server-validator/callout/src/main/java/package-info.java +++ b/tools/target-server-validator/callout/src/main/java/package-info.java @@ -19,4 +19,4 @@ * @author anaik91 * @version .01 */ -package com.apigeesample; +package com.apigee.devrel.apigee_target_server_validator; diff --git a/tools/target-server-validator/input.properties b/tools/target-server-validator/input.properties index 912e2197..093b4fab 100644 --- a/tools/target-server-validator/input.properties +++ b/tools/target-server-validator/input.properties @@ -1,11 +1,11 @@ [source] baseurl=https://apigee.googleapis.com/v1 -org=apigee-hybrid-378710 +org=xxx-xxx-xxx auth_type=oauth [target] baseurl=https://apigee.googleapis.com/v1 -org=apigee-hybrid-378710 +org=xxx-xxx-xxx auth_type=oauth [csv] @@ -20,6 +20,6 @@ skip_proxy_list=mock1,stream api_env=dev api_name=target_server_validator api_force_redeploy=true -vhost_domain_name=example.apigee.com -vhost_ip=34.134.171.41 +api_hostname=example.apigee.com +api_ip=x.x.x.x report_format=md diff --git a/tools/target-server-validator/main.py b/tools/target-server-validator/main.py index b954f24c..5d061a7a 100644 --- a/tools/target-server-validator/main.py +++ b/tools/target-server-validator/main.py @@ -157,10 +157,10 @@ def main(): # Fetch API Northbound Endpoint print( - f"INFO: Fetching VHost with name {cfg['validation']['vhost_domain_name']} !" # noqa + f"INFO: Fetching VHost with name {cfg['validation']['api_hostname']} !" # noqa ) - vhost_domain_name = cfg["validation"]["vhost_domain_name"] - vhost_ip = cfg["validation"].get("vhost_ip", "").strip() + vhost_domain_name = cfg["validation"]["api_hostname"] + vhost_ip = cfg["validation"].get("api_ip", "").strip() api_url = f"https://{vhost_domain_name}/validate_target_server" final_report = [] _cached_hosts = {} @@ -254,6 +254,7 @@ def main(): ) # Write CSV Report + # TODO: support relative report path if report_format == "csv": report_file = "report.csv" print(f"INFO: Dumping report to file {report_file}")