Skip to content

Commit

Permalink
fix: addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anaik91 committed Sep 6, 2023
1 parent 7b9526f commit 2d9387a
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 54 deletions.
35 changes: 17 additions & 18 deletions tools/target-server-validator/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
Expand All @@ -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> # 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> # 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=<CSV Name>` 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
Expand All @@ -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 "<user>:<password>" | 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<Payload contentType="application/json">{
"host":"{request.header.host_name}",
"port": "{request.header.port_number}",
"status":"{REACHABLE_STATUS}"
"status":"{flow.reachableStatus}"
}
</Payload>
</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
limitations under the License.
-->
<JavaCallout continueOnError="false" enabled="true" name="JC-Port-Open-Check">
<DisplayName>JC-Port-Open-Check</DisplayName>
<Properties/>
<ClassName>com.apigeesample.PortOpenCheck</ClassName>
<ResourceURL>java://edge-custom-policy-java-hello.jar</ResourceURL>
<ClassName>com.apigee.devrel.apigee_target_server_validator.PortOpenCheck</ClassName>
<ResourceURL>java://jc-target-server-validator.jar</ResourceURL>
</JavaCallout>
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,4 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<APIProxy revision="1" name="target_server_validator">
<DisplayName></DisplayName>
<Description></Description>
<CreatedAt>1682421435781</CreatedAt>
<LastModifiedAt>1682421435781</LastModifiedAt>
<BasePaths>/validate_target_server</BasePaths>
<Policies>
<Policy>JC1</Policy>
<Policy>set-json-response</Policy>
</Policies>
<ProxyEndpoints>
<ProxyEndpoint>default</ProxyEndpoint>
</ProxyEndpoints>
<Resources>
<Resource>java://edge-custom-policy-java-hello.jar</Resource>
</Resources>
</APIProxy>
<APIProxy name="target_server_validator"/>
4 changes: 2 additions & 2 deletions tools/target-server-validator/callout/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.apigee.callout</groupId>
<artifactId>edge-custom-policy-java-hello</artifactId>
<artifactId>jc-target-server-validator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>EdgeCustomJavaHello</name>
<name>JavaTargetServerValidator</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
* @author anaik91
* @version .01
*/
package com.apigeesample;
package com.apigee.devrel.apigee_target_server_validator;
8 changes: 4 additions & 4 deletions tools/target-server-validator/input.properties
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
7 changes: 4 additions & 3 deletions tools/target-server-validator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit 2d9387a

Please sign in to comment.