Skip to content

Commit

Permalink
Add support to smart-1 cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-royl committed Mar 15, 2023
1 parent 18c57bc commit d9f4b97
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,27 @@ public ApiClient(ApiClientArgs args) {
* @throws ApiClientException if error occurs while preforming an API call
*/
public ApiLoginResponse login(String serverIpAddress, String payload) throws ApiClientException {
return login(serverIpAddress, payload, null);
}

/**
* This function uses the login command to login into the management server.
*
* @param serverIpAddress The IP address or name of the Check Point Management Server.
* @param payload String representing a JSON object containing the login command's arguments.
* @param cloudMgmtId The Smart-1 Cloud management UID.
*
* @return {@link ApiResponse} object
*
* @throws ApiClientException if error occurs while preforming an API call
*/
public ApiLoginResponse login(String serverIpAddress, String payload, String cloudMgmtId) throws ApiClientException {
if(serverIpAddress == null || serverIpAddress.isEmpty()){
throw new ApiClientException("Error: server IP address is invalid");
}

int port = portResolver.getPort(false);
ApiLoginResponse loginResponse = new ApiLoginResponse(serverIpAddress , OK_RESPONSE_CODE, port, new JSONObject());
ApiLoginResponse loginResponse = new ApiLoginResponse(serverIpAddress , OK_RESPONSE_CODE, port, new JSONObject(), cloudMgmtId);
return (ApiLoginResponse) apiCall(loginResponse,"login", payload);
}

Expand All @@ -135,18 +149,32 @@ public ApiLoginResponse login(String serverIpAddress, String payload) throws Api
*
* @param serverIpAddress The IP address or name of the Check Point Management Server.
* @param payload JSON object containing the login command's arguments.
* @param cloudMgmtId The Smart-1 Cloud management UID.
*
* @return {@link ApiResponse} object
*
* @throws ApiClientException if error occurs while preforming an API call
*/
public ApiLoginResponse login(String serverIpAddress, JSONObject payload) throws ApiClientException {

public ApiLoginResponse login(String serverIpAddress, JSONObject payload, String cloudMgmtId) throws ApiClientException {
if(payload == null){
throw new ApiClientException("Error: payload is invalid");
}
String pay = payload.toString();
return login(serverIpAddress,pay);
return login(serverIpAddress, pay, cloudMgmtId);
}

/**
* This function uses login command to login into the management server.
*
* @param serverIpAddress The IP address or name of the Check Point Management Server.
* @param payload JSON object containing the login command's arguments.
*
* @return {@link ApiResponse} object
*
* @throws ApiClientException if error occurs while preforming an API call
*/
public ApiLoginResponse login(String serverIpAddress, JSONObject payload) throws ApiClientException {
return login(serverIpAddress,payload,null);
}

/**
Expand Down Expand Up @@ -242,7 +270,12 @@ public ApiResponse apiCall(ApiLoginResponse loginResponse, String command, Strin
try {
try {
// 1) Establish Connection
url = new URL(URL_PROTOCOL, loginResponse.getServerIP(), loginResponse.getPort(), CONTEXT + command);
String urlSuffix = "";
if(loginResponse.getCloudMgmtId() != null && !loginResponse.getCloudMgmtId().isEmpty()) {
urlSuffix = "/" + loginResponse.getCloudMgmtId() + "/";
}
urlSuffix += CONTEXT + command;
url = new URL(URL_PROTOCOL, loginResponse.getServerIP(), loginResponse.getPort(), urlSuffix);
connection = establishConnection(loginResponse, command, url);
}
catch (Exception e) {
Expand All @@ -269,7 +302,7 @@ public ApiResponse apiCall(ApiLoginResponse loginResponse, String command, Strin
StringBuilder response = readResponse(input);
if (LOGIN_CMD.equals(command)) {
res = new ApiLoginResponse(loginResponse.getServerIP(), connection.getResponseCode(),
loginResponse.getPort(), (JSONObject)UtilClass.convertToJson(response.toString()));
loginResponse.getPort(), (JSONObject)UtilClass.convertToJson(response.toString()), loginResponse.getCloudMgmtId());

// 4) When the command is 'login', hiding the password so that it would not appear in the debug file.
data = changePasswordInData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class ApiLoginResponse extends ApiResponse{

//Management server name or IP-address
final private String serverIpAddress;
final private String cloudMgmtId;

//Port of connection
final private int port;
Expand All @@ -38,10 +39,15 @@ public final class ApiLoginResponse extends ApiResponse{
final private String apiVersion;

public ApiLoginResponse(String serverIP, int statusCode, int port, JSONObject responseBody) {
this(serverIP, statusCode, port, responseBody, null);
}

public ApiLoginResponse(String serverIP, int statusCode, int port, JSONObject responseBody, String cloudMgmtId) {

super(statusCode,responseBody);

this.serverIpAddress = serverIP;
this.cloudMgmtId = cloudMgmtId;
this.port = port;

if (getPayload().containsKey("sid")) {
Expand Down Expand Up @@ -92,4 +98,11 @@ public int getPort()
* @return The version
*/
public String getApiVersion(){return apiVersion;}

/**
* Gets the Smart-1 Cloud management UID
*
* @return The Smart-1 Cloud management UID
*/
public String getCloudMgmtId() { return cloudMgmtId; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public static void main(String[] args) {

//Management server IP address
String server = "127.0.0.1";
String cloudMgmtId = "aa3ce335-801a-4fc4-ba78-56cd20300d50";

//Login credentials
String username = "username";
String password = "password";
String apiKey = "api-key";

//The name of the original Host object to be cloned
String origHost = "originalHost";
Expand Down Expand Up @@ -69,9 +71,10 @@ public static void main(String[] args) {
JSONObject loginPayload = new JSONObject();
loginPayload.put("user", username);
loginPayload.put("password",password);
loginPayload.put("api-key", apiKey);

try {
loginResponse = client.login(server,loginPayload);
loginResponse = client.login(server, loginPayload, cloudMgmtId);
} catch (ApiClientException e) {
System.out.println(e.getMessage());
System.exit(1);
Expand Down

0 comments on commit d9f4b97

Please sign in to comment.