diff --git a/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiClient.java b/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiClient.java index 1c339c0..7d45939 100644 --- a/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiClient.java +++ b/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiClient.java @@ -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); } @@ -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); } /** @@ -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) { @@ -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); diff --git a/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiLoginResponse.java b/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiLoginResponse.java index 5b8058f..a59c372 100644 --- a/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiLoginResponse.java +++ b/mgmt_api_lib/src/main/java/com/checkpoint/mgmt_api/client/ApiLoginResponse.java @@ -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; @@ -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")) { @@ -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; } } diff --git a/samples/clone_host/src/main/java/com/checkpoint/mgmt_api/examples/CloneHostExample.java b/samples/clone_host/src/main/java/com/checkpoint/mgmt_api/examples/CloneHostExample.java index 8f6ee22..71ef6e6 100644 --- a/samples/clone_host/src/main/java/com/checkpoint/mgmt_api/examples/CloneHostExample.java +++ b/samples/clone_host/src/main/java/com/checkpoint/mgmt_api/examples/CloneHostExample.java @@ -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"; @@ -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);