diff --git a/README.md b/README.md index 01cf9a0..9ddf4c0 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,23 @@ Note that SafeguardJava 6.8.0 is no longer compatible with versions of Safeguard One Identity open source projects are supported through [One Identity GitHub issues](https://github.com/OneIdentity/SafeguardJava/issues) and the [One Identity Community](https://www.oneidentity.com/community/). This includes all scripts, plugins, SDKs, modules, code snippets or other solutions. For assistance with any One Identity GitHub project, please raise a new Issue on the [One Identity GitHub project](https://github.com/OneIdentity/SafeguardJava/issues) page. You may also visit the [One Identity Community](https://www.oneidentity.com/community/) to ask questions. Requests for assistance made through official One Identity Support will be referred back to GitHub and the One Identity Community forums where those requests can benefit all users. +## Default API Update + +SafeguardDotNet will use v4 API by default starting with version 7.0. It is +possible to continue using the v3 API by passing in the apiVersion parameter +when creating a connection or A2A context. + +Safeguard for Privileged Passwords 7.X hosts both the v3 and v4 APIs. New coding +projects should target the v4 API, and existing projects can be migrated over time. +Notification will be given to customers many releases in advance of any plans to +remove the v3 API. There are currently no plans to remove the v3 API. + +```java +// Use v3 instead of v4 +var connection = Safeguard.Connect("safeguard.sample.corp", "local", "Admin", password, 3, true); +var a2aContext = Safeguard.A2A.GetContext("safeguard.sample.corp", thumbprint, 3, true); +``` + ## Introduction All functionality in Safeguard is available via the Safeguard API. There is diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 92289cd..07a99f1 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -12,7 +12,7 @@ pool: # Maven Build Variables: variables: - version: '6.12.0.$(Build.BuildId)-SNAPSHOT' + version: '7.0.0.$(Build.BuildId)-SNAPSHOT' targetDir: 'target' codeSigningCertFileName: 'OneIdentityCodeSigning.pfx' issuerKeyStorePath: 'settings/signingstore.jks' diff --git a/pom.xml b/pom.xml index c0e40ec..361cc45 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ UTF-8 - 6.12.0-SNAPSHOT + 7.0.0-SNAPSHOT ./signingcert.pfx 1 secret @@ -42,12 +42,12 @@ com.squareup.okhttp3 okhttp - 4.9.1 + 4.10.0 com.microsoft.signalr signalr - 5.0.10 + 5.0.17 org.apache.httpcomponents @@ -62,18 +62,18 @@ com.fasterxml.jackson.core jackson-databind - 2.12.5 + 2.12.7 jar org.slf4j slf4j-api - 1.7.32 + 1.7.36 com.google.code.gson gson - 2.8.8 + 2.8.9 diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/ISpsStreamingRequest.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/ISpsStreamingRequest.java index 0615ff4..0c04f97 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/ISpsStreamingRequest.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/ISpsStreamingRequest.java @@ -2,6 +2,8 @@ import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException; import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Map; /** @@ -19,10 +21,8 @@ public interface ISpsStreamingRequest { * @param parameters Additional parameters to add to the URL. * @param additionalHeaders Additional headers to add to the request. * @return Response body as a string. - * @throws - * com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException - * @throws - * com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException + * @throws SafeguardForJavaException General Safeguard for Java exception. + * @throws ArgumentException Invalid argument. */ String uploadStream(String relativeUrl, byte[] stream, IProgressCallback progressCallback, Map parameters, Map additionalHeaders) @@ -37,13 +37,42 @@ String uploadStream(String relativeUrl, byte[] stream, IProgressCallback progres * @param parameters Additional parameters to add to the URL. * @param additionalHeaders Additional headers to add to the request. * @return Response body as a string. - * @throws - * com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException - * @throws - * com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException + * @throws SafeguardForJavaException General Safeguard for Java exception. + * @throws ArgumentException Invalid argument. */ String uploadStream(String relativeUrl, String fileName, Map parameters, Map additionalHeaders) throws SafeguardForJavaException, ArgumentException; + + /** + * Call a Safeguard Sps GET API returning output as a stream. The caller takes ownership of the + * StreamResponse and should dispose it when finished. + * If there is a failure a SafeguardDotNetException will be thrown. + * + * @param relativeUrl Relative URL of the service to use. + * @param parameters Additional parameters to add to the URL. + * @param additionalHeaders Additional headers to add to the request. + * @return A StreamResponse. + * @throws SafeguardForJavaException General Safeguard for Java exception. + * @throws ArgumentException Invalid argument. + */ + StreamResponse downloadStream(String relativeUrl, Map parameters, Map additionalHeaders) + throws SafeguardForJavaException, ArgumentException; + + /** + * Call a Safeguard GET API providing an output file path to which streaming download data will + * be written. If there is a failure a SafeguardDotNetException will be thrown. + * + * @param relativeUrl Relative URL of the service to use. + * @param outputFilePath Full path to the file where download will be written. + * @param progressCallback Optionally report upload progress. + * @param parameters Additional parameters to add to the URL. + * @param additionalHeaders Additional headers to add to the request. + * @throws SafeguardForJavaException General Safeguard for Java exception. + * @throws ArgumentException Invalid argument. + */ + void downloadStream(String relativeUrl, String outputFilePath, IProgressCallback progressCallback, + Map parameters, Map additionalHeaders) + throws SafeguardForJavaException, ArgumentException; } diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/Safeguard.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/Safeguard.java index 287dbe8..34bae6f 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/Safeguard.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/Safeguard.java @@ -20,7 +20,7 @@ */ public final class Safeguard { - private static final int DEFAULTAPIVERSION = 3; + private static final int DEFAULTAPIVERSION = 4; private Safeguard() { diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardA2AContext.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardA2AContext.java index 3c9093b..9576c97 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardA2AContext.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardA2AContext.java @@ -131,8 +131,8 @@ public List getRetrievableAccounts() throws ObjectDispos account.setDisabled(registration.isDisabled() || retrieval.isAccountDisabled()); account.setAccountId(retrieval.getAccountId()); account.setApiKey(retrieval.getApiKey().toCharArray()); - account.setAssetId(retrieval.getSystemId()); - account.setAssetName(retrieval.getSystemName()); + account.setAssetId(retrieval.getAssetId()); + account.setAssetName(retrieval.getAssetName()); account.setAssetNetworkAddress(retrieval.getAssetNetworkAddress()); account.setAssetDescription(retrieval.getAssetDescription()); account.setAccountId(retrieval.getAccountId()); @@ -301,6 +301,7 @@ public String brokerAccessRequest(char[] apiKey, BrokeredAccessRequest accessReq if (accessRequest.getAssetId() == null && accessRequest.getAssetName() == null) { throw new SafeguardForJavaException("You must specify an asset to create an access request for"); } + accessRequest.setVersion(apiVersion); Map headers = new HashMap<>(); headers.put(HttpHeaders.ACCEPT, "application/json"); diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardSessionsConnection.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardSessionsConnection.java index cf3d751..47e0e89 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardSessionsConnection.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/SafeguardSessionsConnection.java @@ -25,7 +25,6 @@ class SafeguardSessionsConnection implements ISafeguardSessionsConnection { private boolean disposed; private RestClient client; - private final Header authCookie = null; public SafeguardSessionsConnection(String networkAddress, String username, char[] password, boolean ignoreSsl, HostnameVerifier validationCallback) diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/SpsStreamingRequest.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/SpsStreamingRequest.java index 88887ef..7bacf49 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/SpsStreamingRequest.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/SpsStreamingRequest.java @@ -4,12 +4,18 @@ import com.oneidentity.safeguard.safeguardjava.data.Method; import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException; import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException; +import com.oneidentity.safeguard.safeguardjava.restclient.OutputStreamProgress; import com.oneidentity.safeguard.safeguardjava.restclient.RestClient; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Map; import org.apache.http.client.methods.CloseableHttpResponse; class SpsStreamingRequest implements ISpsStreamingRequest { + private final Integer DefaultBufferSize = 81920; private RestClient client; SpsStreamingRequest(RestClient client) { @@ -88,4 +94,64 @@ public String uploadStream(String relativeUrl, String fileName, return fullResponse.getBody(); } + + @Override + public StreamResponse downloadStream(String relativeUrl, Map parameters, Map additionalHeaders) + throws SafeguardForJavaException, ArgumentException { + + if (Utils.isNullOrEmpty(relativeUrl)) { + throw new ArgumentException("Parameter relativeUrl cannot be null or empty"); + } + if (client == null) { + throw new ArgumentException("Invalid or unauthenticated SPS connection"); + } + + CloseableHttpResponse response = null; + + SafeguardConnection.logRequestDetails(Method.Get, client.getBaseURL() + "/" + relativeUrl, parameters, additionalHeaders); + + response = client.execGETBytes(relativeUrl, parameters, additionalHeaders, null, null); + + if (response == null) { + throw new SafeguardForJavaException(String.format("Unable to connect to SPS service %s", client.getBaseURL())); + } + + if (!Utils.isSuccessful(response.getStatusLine().getStatusCode())) { + String reply = Utils.getResponse(response); + throw new SafeguardForJavaException("Error returned from SPS API, Error: " + + String.format("%d %s", response.getStatusLine().getStatusCode(), reply)); + } + + FullResponse fullResponse = new FullResponse(response.getStatusLine().getStatusCode(), response.getAllHeaders(), null); + SafeguardConnection.logResponseDetails(fullResponse); + + return new StreamResponse(response); + } + + @Override + public void downloadStream(String relativeUrl, String outputFilePath, IProgressCallback progressCallback, + Map parameters, Map additionalHeaders) + throws SafeguardForJavaException, ArgumentException { + + StreamResponse streamResponse = null; + InputStream input = null; + OutputStream output = null; + byte[] buffer = new byte[DefaultBufferSize]; + + try { + streamResponse = downloadStream(relativeUrl, parameters, additionalHeaders); + input = streamResponse.getStream(); + output = new OutputStreamProgress(new FileOutputStream(outputFilePath), progressCallback, streamResponse.getContentLength()); + + for (int length; (length = input.read(buffer)) > 0;) { + output.write(buffer, 0, length); + } + } catch (Exception ex) { + throw new SafeguardForJavaException(String.format("Unable to download %s", outputFilePath), ex); + } finally { + if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} + if (streamResponse != null) streamResponse.dispose(); + } + } + } diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/StreamResponse.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/StreamResponse.java new file mode 100644 index 0000000..cfcc0c6 --- /dev/null +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/StreamResponse.java @@ -0,0 +1,61 @@ +package com.oneidentity.safeguard.safeguardjava; + +import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException; +import java.io.IOException; +import java.io.InputStream; +import org.apache.http.client.methods.CloseableHttpResponse; + +/** + * Represents a streamed response + */ +public class StreamResponse { + private boolean disposed; + + public StreamResponse(CloseableHttpResponse resp) { + response = resp; + } + + private final CloseableHttpResponse response; + private InputStream stream = null; + private Long contentLength = 0L; + + /** + * Get the response stream object + * + * @return The HTTP response body content as an inputstream + */ + public InputStream getStream() throws SafeguardForJavaException + { + if (stream == null) { + try { + stream = response.getEntity().getContent(); + } catch (Exception ex) { + throw new SafeguardForJavaException("Unable to read the download stream", ex); + } + } + return stream; + } + + /** + * Get the response content length + * + * @return The HTTP response body content length + */ + public Long getContentLength() { + if (contentLength == 0) { + contentLength = response.getEntity().getContentLength(); + } + return contentLength; + } + + public void dispose() { + if (!disposed) { + disposed = true; + if (stream != null) { + try { + stream.close(); + } catch (IOException logOrIgnore) {} + } + } + } +} diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/data/A2ARetrievableAccountInternal.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/data/A2ARetrievableAccountInternal.java index 76015eb..375e66d 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/data/A2ARetrievableAccountInternal.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/data/A2ARetrievableAccountInternal.java @@ -13,8 +13,12 @@ public class A2ARetrievableAccountInternal { private String apiKey; @JsonProperty("SystemId") private int systemId; + @JsonProperty("AssetId") + private int assetId; @JsonProperty("SystemName") private String systemName; + @JsonProperty("AssetName") + private String assetName; @JsonProperty("AccountId") private int accountId; @JsonProperty("AccountName") @@ -24,6 +28,8 @@ public class A2ARetrievableAccountInternal { @JsonProperty("AccountType") private String accountType; @JsonProperty("SystemDescription") + private String systemDescription; + @JsonProperty("AssetDescription") private String assetDescription; @JsonProperty("AccountDescription") private String accountDescription; @@ -49,20 +55,28 @@ public void setApiKey(String apiKey) { this.apiKey = apiKey; } - public int getSystemId() { - return systemId; + public int getAssetId() { + return assetId; } public void setSystemId(int systemId) { - this.systemId = systemId; + this.assetId = systemId; + } + + public void setAssetId(int assetId) { + this.assetId = assetId; } - public String getSystemName() { - return systemName; + public String getAssetName() { + return assetName; } public void setSystemName(String systemName) { - this.systemName = systemName; + this.assetName = systemName; + } + + public void setAssetName(String assetName) { + this.assetName = assetName; } public int getAccountId() { @@ -101,6 +115,10 @@ public String getAssetDescription() { return assetDescription; } + public void setSystemDescription(String systemDescription) { + this.assetDescription = systemDescription; + } + public void setAssetDescription(String assetDescription) { this.assetDescription = assetDescription; } diff --git a/src/main/java/com/oneidentity/safeguard/safeguardjava/data/BrokeredAccessRequest.java b/src/main/java/com/oneidentity/safeguard/safeguardjava/data/BrokeredAccessRequest.java index 9ed8acf..55dce91 100644 --- a/src/main/java/com/oneidentity/safeguard/safeguardjava/data/BrokeredAccessRequest.java +++ b/src/main/java/com/oneidentity/safeguard/safeguardjava/data/BrokeredAccessRequest.java @@ -9,7 +9,8 @@ */ public class BrokeredAccessRequest implements JsonObject { - + private int version; + private BrokeredAccessRequestType AccessType; // converted by AccessRequestTypeConverter private String ForUserName; private String ForUserIdentityProvider; // renamed from ForProvider @@ -30,6 +31,10 @@ public class BrokeredAccessRequest implements JsonObject private Long RequestedDurationHours; private Long RequestedDurationMinutes; + + public void setVersion(int apiVersion) { + version = apiVersion; + } /** * Get the type of access request to create. @@ -353,7 +358,7 @@ public void setRequestedDurationMinutes(Long RequestedDurationMinutes) { public String toJson() { return new StringBuffer("{") .append(Utils.toJsonString("AccountId", this.AccountId, false)) - .append(Utils.toJsonString("SystemId", this.AssetId, true)) + .append(version == 3 ? Utils.toJsonString("SystemId", this.AssetId, true) : Utils.toJsonString("AssetId", this.AssetId, true)) .append(Utils.toJsonString("AccessRequestType", this.AccessType == null ? null : this.AccessType.toString(), true)) .append(Utils.toJsonString("IsEmergency", this.IsEmergency, true)) .append(Utils.toJsonString("ReasonCodeId", this.ReasonCodeId, true)) diff --git a/tests/safeguardjavaclient/pom.xml b/tests/safeguardjavaclient/pom.xml index a015912..6f937cf 100644 --- a/tests/safeguardjavaclient/pom.xml +++ b/tests/safeguardjavaclient/pom.xml @@ -17,19 +17,19 @@ commons-cli commons-cli - 1.4 + 1.5.0 com.oneidentity.safeguard safeguardjava - 6.12.0-SNAPSHOT + 7.0.0-SNAPSHOT org.slf4j slf4j-simple - 1.7.25 + 1.7.36 diff --git a/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardJavaClient.java b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardJavaClient.java index 11f3947..57aeacc 100644 --- a/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardJavaClient.java +++ b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardJavaClient.java @@ -37,95 +37,105 @@ public static void main(String[] args) { //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client", "DEBUG"); //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG"); - while (!done) { - Integer selection = displayMenu(); - - switch(selection) { - case 1: - connection = tests.safeguardConnectByUserPassword(); - break; - case 2: - connection = tests.safeguardConnectByThumbprint(); - break; - case 3: - connection = tests.safeguardConnectByCertificate(); - break; - case 4: - connection = tests.safeguardConnectByToken(); - break; - case 5: - connection = tests.safeguardConnectAnonymous(); - break; - case 6: - connection = tests.safeguardConnectByKeystore(); - break; - case 7: - tests.safeguardTestConnection(connection); - break; - case 8: - connection = tests.safeguardDisconnect(connection); - break; - case 9: - a2aContext = tests.safeguardGetA2AContextByCertificate(); - break; - case 10: - a2aContext = tests.safeguardGetA2AContextByKeystore(); - break; - case 11: - a2aContext = tests.safeguardGetA2AContextByThumbprint(); - break; - case 12: - tests.safeguardTestA2AContext(a2aContext); - break; - case 13: - a2aContext = tests.safeguardDisconnectA2AContext(a2aContext); - break; - case 14: - eventListener = tests.safeguardEventListenerByUserPassword(); - break; - case 15: - eventListener = tests.safeguardEventListenerByCertificate(); - break; - case 16: - eventListener = tests.safeguardEventListenerByKeystore(); - break; - case 17: - eventListener = tests.safeguardEventListenerByThumbprint(); - break; - case 18: - tests.safeguardTestEventListener(eventListener); - break; - case 19: - eventListener = tests.safeguardDisconnectEventListener(eventListener); - break; - case 20: - tests.safeguardTestBackupDownload(connection); - break; - case 21: - tests.safeguardTestBackupUpload(connection); - break; - case 22: - sessionConnection = tests.safeguardSessionsConnection(); - break; - case 23: - tests.safeguardSessionsApi(sessionConnection); - break; - case 24: - tests.safeguardSessionsFileUpload(sessionConnection); - break; - case 25: - tests.safeguardSessionsStreamUpload(sessionConnection); - break; - case 26: - tests.safeguardTestManagementConnection(connection); - break; - case 27: - tests.safeguardTestAnonymousConnection(connection); - break; - default: - done = true; - break; + try { + while (!done) { + Integer selection = displayMenu(); + + switch(selection) { + case 1: + connection = tests.safeguardConnectByUserPassword(); + break; + case 2: + connection = tests.safeguardConnectByThumbprint(); + break; + case 3: + connection = tests.safeguardConnectByCertificate(); + break; + case 4: + connection = tests.safeguardConnectByToken(); + break; + case 5: + connection = tests.safeguardConnectAnonymous(); + break; + case 6: + connection = tests.safeguardConnectByKeystore(); + break; + case 7: + tests.safeguardTestConnection(connection); + break; + case 8: + connection = tests.safeguardDisconnect(connection); + break; + case 9: + a2aContext = tests.safeguardGetA2AContextByCertificate(); + break; + case 10: + a2aContext = tests.safeguardGetA2AContextByKeystore(); + break; + case 11: + a2aContext = tests.safeguardGetA2AContextByThumbprint(); + break; + case 12: + tests.safeguardTestA2AContext(a2aContext); + break; + case 13: + a2aContext = tests.safeguardDisconnectA2AContext(a2aContext); + break; + case 14: + eventListener = tests.safeguardEventListenerByUserPassword(); + break; + case 15: + eventListener = tests.safeguardEventListenerByCertificate(); + break; + case 16: + eventListener = tests.safeguardEventListenerByKeystore(); + break; + case 17: + eventListener = tests.safeguardEventListenerByThumbprint(); + break; + case 18: + tests.safeguardTestEventListener(eventListener); + break; + case 19: + eventListener = tests.safeguardDisconnectEventListener(eventListener); + break; + case 20: + tests.safeguardTestBackupDownload(connection); + break; + case 21: + tests.safeguardTestBackupUpload(connection); + break; + case 22: + sessionConnection = tests.safeguardSessionsConnection(); + break; + case 23: + tests.safeguardSessionsApi(sessionConnection); + break; + case 24: + tests.safeguardSessionsFileUpload(sessionConnection); + break; + case 25: + tests.safeguardSessionsStreamUpload(sessionConnection); + break; + case 26: + tests.safeguardSessionTestRecordingDownload(sessionConnection); + break; + case 27: + tests.safeguardTestManagementConnection(connection); + break; + case 28: + tests.safeguardTestAnonymousConnection(connection); + break; + default: + done = true; + break; + } } + } catch (Exception ex) { + System.out.println("Exception Type: " + ex.getClass().getCanonicalName()); + System.out.println("/tMessage: " + ex.getMessage()); + System.out.println("/tException Stack: "); + ex.printStackTrace(); } System.out.println("All done."); @@ -159,8 +169,9 @@ private static Integer displayMenu() { System.out.println ("\t23. Test SPS API"); System.out.println ("\t24. Test SPS Firmware Upload"); System.out.println ("\t25. Test Stream Upload"); - System.out.println ("\t26. Test Management Interface API"); - System.out.println ("\t27. Test Anonymous Connection"); + System.out.println ("\t26. Test Session Recording Download"); + System.out.println ("\t27. Test Management Interface API"); + System.out.println ("\t28. Test Anonymous Connection"); System.out.println ("\t99. Exit"); diff --git a/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardTests.java b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardTests.java index 6e867b1..db078a2 100644 --- a/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardTests.java +++ b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/SafeguardTests.java @@ -1,6 +1,10 @@ package com.oneidentity.safeguard.safeguardclient; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import static com.oneidentity.safeguard.safeguardclient.SafeguardJavaClient.readLine; +import com.oneidentity.safeguard.safeguardclient.data.SessionRecordings; import com.oneidentity.safeguard.safeguardjava.IProgressCallback; import com.oneidentity.safeguard.safeguardjava.ISafeguardA2AContext; import com.oneidentity.safeguard.safeguardjava.ISafeguardConnection; @@ -328,7 +332,8 @@ public void safeguardTestA2AContext(ISafeguardA2AContext a2aContext) { List registrations = a2aContext.getRetrievableAccounts(); System.out.println(String.format("\tRetrievable accounts:")); for (A2ARetrievableAccount reg : registrations) { - System.out.println(String.format("\t\t%d %s %s", reg.getAccountId(), reg.getAccountName(), reg.getAccountDescription())); + System.out.println(String.format("\t\tAssetId: %d AssetName: %s AccountId: %d AccountName: %s AccountDescription: %s", + reg.getAssetId(), reg.getAssetName(), reg.getAccountId(), reg.getAccountName(), reg.getAccountDescription())); } } catch (ArgumentException | ObjectDisposedException | SafeguardForJavaException ex) { System.out.println("\t[ERROR]Test connection failed: " + ex.getMessage()); @@ -580,7 +585,7 @@ public void safeguardTestBackupUpload(ISafeguardConnection connection) { } ISafeguardSessionsConnection safeguardSessionsConnection() { - String address = readLine("SPP address: ", null); + String address = readLine("SPS address: ", null); String user = readLine("User:", null); String password = readLine("Password: ", null); // boolean withCertValidator = readLine("With Certificate Validator(y/n): ", "n").equalsIgnoreCase("y"); @@ -613,7 +618,7 @@ void safeguardSessionsApi(ISafeguardSessionsConnection connection) { try { FullResponse fullResponse = connection.InvokeMethodFull(Method.Get, "configuration/network/naming", null); - System.out.println(String.format("\t\\Users full response:")); + System.out.println(String.format("\t\\Network Naming full response:")); logResponseDetails(fullResponse); } catch (ArgumentException | ObjectDisposedException | SafeguardForJavaException ex) { @@ -651,7 +656,7 @@ public void safeguardSessionsFileUpload(ISafeguardSessionsConnection connection) public void safeguardSessionsStreamUpload(ISafeguardSessionsConnection connection) { if (connection == null) { - System.out.println(String.format("Safeguard not connected")); + System.out.println(String.format("Safeguard Sessions not connected")); return; } @@ -677,6 +682,68 @@ public void safeguardSessionsStreamUpload(ISafeguardSessionsConnection connectio System.out.println("\t[ERROR]Test SPS firmware upload failed: " + ex.getMessage()); } } + + private String[] safeguardSessionsGetRecordings(ISafeguardSessionsConnection connection) { + try { + FullResponse fullResponse = connection.InvokeMethodFull(Method.Get, "audit/sessions", null); + System.out.println(String.format("\t\\Session Id's full response:")); + logResponseDetails(fullResponse); + + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + SessionRecordings sessionIds = mapper.readValue(fullResponse.getBody(), SessionRecordings.class); + return sessionIds.toArray(); + + } catch (ArgumentException | ObjectDisposedException | SafeguardForJavaException ex) { + System.out.println("\t[ERROR]Test connection failed: " + ex.getMessage()); + } catch (JsonProcessingException ex) { + System.out.println("JSON deserialization failed: " + ex.getMessage()); + } + + return null; + } + + public void safeguardSessionTestRecordingDownload(ISafeguardSessionsConnection connection) { + + if (connection == null) { + System.out.println(String.format("Safeguard not connected")); + return; + } + + String[] sessions = safeguardSessionsGetRecordings(connection); + + if (sessions == null) { + System.out.println(String.format("Failed to get the session id's")); + return; + } + + for (int x = 0; x < sessions.length; x++) { + System.out.println(String.format("\t%d. %s", x, sessions[x])); + } + + String s = readLine("Select session: ", "0"); + int sessionSelection = Integer.parseInt(s); + if (sessionSelection < 0 || sessionSelection > sessions.length-1) { + System.out.println(String.format("Invalid session selection")); + return; + } + + String sessionId = sessions[sessionSelection]; + String recordingFileName = sessionId + ".zat"; + boolean withProgress = readLine("With Progress Notification(y/n): ", "n").equalsIgnoreCase("y"); + + String filePath = Paths.get(".", recordingFileName).toAbsolutePath().toString(); + IProgressCallback progressCallback = withProgress ? new ProgressNotification() : null; + + try { + System.out.println(String.format("\tSession recording file path: %s", filePath)); + connection.getStreamingRequest().downloadStream(String.format("audit/sessions/%s/audit_trail", sessionId), filePath, progressCallback, null, null); + System.out.println(String.format("\tDownloaded session recording file: %s", recordingFileName)); + } catch (ObjectDisposedException | SafeguardForJavaException ex) { + System.out.println("\t[ERROR]Test backup download failed: " + ex.getMessage()); + } catch (Exception ex) { + System.out.println("\t[ERROR]Test backup download failed: " + ex.getMessage()); + } + } void safeguardTestManagementConnection(ISafeguardConnection connection) { if (connection == null) { diff --git a/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/data/SessionRecordings.java b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/data/SessionRecordings.java new file mode 100644 index 0000000..6ed9b53 --- /dev/null +++ b/tests/safeguardjavaclient/src/main/java/com/oneidentity/safeguard/safeguardclient/data/SessionRecordings.java @@ -0,0 +1,26 @@ +package com.oneidentity.safeguard.safeguardclient.data; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.ArrayList; +import java.util.List; + +class SessionRecording { + + @JsonProperty("key") + public String sessionId; +} + +public class SessionRecordings { + + @JsonProperty("items") + public SessionRecording[] items; + + public String[] toArray() { + List sessionIds = new ArrayList<>(); + for(SessionRecording sr : items) { + sessionIds.add(sr.sessionId); + } + return sessionIds.toArray(new String[sessionIds.size()]); + } + +} \ No newline at end of file