Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.oneidentity.safeguard.safeguardjava.exceptions.ArgumentException;
import com.oneidentity.safeguard.safeguardjava.exceptions.ObjectDisposedException;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import java.time.Duration;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface ISafeguardSessionsConnection {
* @throws SafeguardForJavaException General Safeguard for Java exception.
* @throws ArgumentException Invalid argument.
*/
String InvokeMethod(Method method, String relativeUrl, String body)
String invokeMethod(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/**
Expand All @@ -39,7 +39,7 @@ String InvokeMethod(Method method, String relativeUrl, String body)
* @throws SafeguardForJavaException General Safeguard for Java exception.
* @throws ArgumentException Invalid argument.
*/
FullResponse InvokeMethodFull(Method method, String relativeUrl, String body)
FullResponse invokeMethodFull(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public PersistentSafeguardConnection(ISafeguardConnection connection) {
_connection = connection;
}

@Override
public IStreamingRequest getStreamingRequest() {
return _connection.getStreamingRequest();
}

@Override
public void dispose()
{
public void dispose() {
_connection.dispose();
}

@Override
public FullResponse JoinSps(ISafeguardSessionsConnection spsConnection, String certificateChain, String sppAddress)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException
{
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {
if (_connection.getAccessTokenLifetimeRemaining() <= 0)
_connection.refreshAccessToken();
return _connection.JoinSps(spsConnection, certificateChain, sppAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public FullResponse JoinSps(ISafeguardSessionsConnection spsConnection, String c
request.setSpp_cert_chain(certificateChain);

Logger.getLogger(SafeguardConnection.class.getName()).log(Level.FINEST, "Sending join request.");
FullResponse joinResponse = spsConnection.InvokeMethodFull(Method.Post, "cluster/spp", request.toJson());
FullResponse joinResponse = spsConnection.invokeMethodFull(Method.Post, "cluster/spp", request.toJson());

logResponseDetails(joinResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public SafeguardSessionsConnection(String networkAddress, String username,
}

@Override
public String InvokeMethod(Method method, String relativeUrl, String body)
public String invokeMethod(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

return InvokeMethodFull(method, relativeUrl, body).getBody();
return invokeMethodFull(method, relativeUrl, body).getBody();
}

/**
Expand All @@ -81,7 +81,7 @@ public String InvokeMethod(Method method, String relativeUrl, String body)
//TODO: This API should have an additionalHeaders parameter
//TODO: This API should have an parameters parameter
//TODO: This API should have an timeout parameter
public FullResponse InvokeMethodFull(Method method, String relativeUrl, String body)
public FullResponse invokeMethodFull(Method method, String relativeUrl, String body)
throws ObjectDisposedException, SafeguardForJavaException, ArgumentException {

if (disposed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oneidentity.safeguard.safeguardjava.Utils;
import java.time.Instant;
import java.util.Date;

/**
* This class is used to define a brokered access request.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.oneidentity.safeguard.safeguardjava.data;

import com.oneidentity.safeguard.safeguardjava.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JoinRequest implements JsonObject {

Expand Down Expand Up @@ -36,12 +41,14 @@ public void setSpp_cert_chain(String spp_cert_chain) {
}

@Override
public String toJson() {
return new StringBuffer("{")
.append(Utils.toJsonString("spp", this.spp, false))
.append(Utils.toJsonString("spp_api_token", this.spp_api_token.toString(), true))
.append(Utils.toJsonString("spp_cert_chain", this.spp_cert_chain, true))
.append("}").toString();
public String toJson() throws SafeguardForJavaException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
return ow.writeValueAsString(this);
} catch (JsonProcessingException ex) {
Logger.getLogger(JoinRequest.class.getName()).log(Level.FINEST, null, ex);
throw new SafeguardForJavaException("Failed to convert request to json", ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

public class JsonBody implements JsonObject {

private String body;
private final String body;

public JsonBody(String body) {
this.body = body;
}

@Override
public String toJson() {
return body;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.oneidentity.safeguard.safeguardjava.data;

import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;

public interface JsonObject {

String toJson();
String toJson() throws SafeguardForJavaException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.oneidentity.safeguard.safeguardjava.IProgressCallback;
import com.oneidentity.safeguard.safeguardjava.data.CertificateContext;
import com.oneidentity.safeguard.safeguardjava.data.JsonObject;
import com.oneidentity.safeguard.safeguardjava.exceptions.SafeguardForJavaException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -72,7 +73,6 @@
public class RestClient {

private CloseableHttpClient client = null;
private HttpClientBuilder builder = null;
private BasicCookieStore cookieStore = new BasicCookieStore();

private String serverUrl = null;
Expand Down Expand Up @@ -154,7 +154,7 @@ public String getBaseURL() {

private Map<String,String> parseKeyValue(String value) {

HashMap<String,String> keyValues = new HashMap<String,String>();
HashMap<String,String> keyValues = new HashMap<>();
String[] parts = value.split(";");
for (String p : parts) {
String[] kv = p.split("=");
Expand Down Expand Up @@ -244,7 +244,7 @@ public CloseableHttpResponse execGETBytes(String path, Map<String, String> query
Integer timeout, IProgressCallback progressCallback) {

if (headers == null || !headers.containsKey(HttpHeaders.ACCEPT)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
headers.put(HttpHeaders.ACCEPT, "application/octet-stream");
}
RequestBuilder rb = prepareRequest(RequestBuilder.get(getBaseURI(path)), queryParams, headers, timeout);
Expand All @@ -264,7 +264,7 @@ public CloseableHttpResponse execGETBytes(String path, Map<String, String> query

if (certClient != null) {
if (headers == null || !headers.containsKey(HttpHeaders.ACCEPT)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
headers.put(HttpHeaders.ACCEPT, "application/octet-stream");
}
RequestBuilder rb = prepareRequest(RequestBuilder.get(getBaseURI(path)), queryParams, headers, timeout);
Expand Down Expand Up @@ -308,7 +308,7 @@ public CloseableHttpResponse execPOST(String path, Map<String, String> queryPara
}

public CloseableHttpResponse execPOST(String path, Map<String, String> queryParams, Map<String, String> headers, Integer timeout,
JsonObject requestEntity, CertificateContext certificateContext) {
JsonObject requestEntity, CertificateContext certificateContext) throws SafeguardForJavaException {

CloseableHttpClient certClient = getClientWithCertificate(certificateContext);

Expand All @@ -332,7 +332,7 @@ public CloseableHttpResponse execPOSTBytes(String path, Map<String, String> quer
byte[] requestEntity, IProgressCallback progressCallback) {

if (headers == null || !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
headers.put(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
}
RequestBuilder rb = prepareRequest(RequestBuilder.post(getBaseURI(path)), queryParams, headers, timeout);
Expand All @@ -353,7 +353,7 @@ public CloseableHttpResponse execPOSTBytes(String path, Map<String, String> quer

if (certClient != null) {
if (headers == null || !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
headers.put(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
}
RequestBuilder rb = prepareRequest(RequestBuilder.post(getBaseURI(path)), queryParams, headers, timeout);
Expand All @@ -378,7 +378,7 @@ public CloseableHttpResponse execPOSTFile(String path, Map<String, String> query
.addBinaryBody("firmware", file, ContentType.MULTIPART_FORM_DATA, file.getName()).build();

if (headers == null || !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
}
RequestBuilder rb = prepareRequest(RequestBuilder.post(getBaseURI(path)), queryParams, headers, timeout);

Expand All @@ -402,7 +402,7 @@ public CloseableHttpResponse execPOSTFile(String path, Map<String, String> query
.addBinaryBody("firmware", file, ContentType.MULTIPART_FORM_DATA, file.getName()).build();

if (headers == null || !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers = headers == null ? new HashMap<String,String>() : headers;
headers = headers == null ? new HashMap<>() : headers;
}
RequestBuilder rb = prepareRequest(RequestBuilder.post(getBaseURI(path)), queryParams, headers, timeout);

Expand Down Expand Up @@ -490,14 +490,14 @@ private RequestBuilder prepareRequest(RequestBuilder rb, Map<String, String> que
rb.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
headers.entrySet().forEach((entry) -> {
rb.addHeader(entry.getKey(), entry.getValue());
}
});
}
if (queryParams != null) {
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
queryParams.entrySet().forEach((entry) -> {
rb.addParameter(entry.getKey(), entry.getValue());
}
});
}
if (timeout != null) {
RequestConfig rconfig = RequestConfig.custom()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ public static void main(String[] args) {
tests.safeguardSessionTestRecordingDownload(sessionConnection);
break;
case 27:
tests.safeguardTestManagementConnection(connection);
tests.safeguardTestJoinSps(connection, sessionConnection);
break;
case 28:
tests.safeguardTestManagementConnection(connection);
break;
case 29:
tests.safeguardTestAnonymousConnection(connection);
break;
default:
Expand Down Expand Up @@ -170,8 +173,9 @@ private static Integer displayMenu() {
System.out.println ("\t24. Test SPS Firmware Upload");
System.out.println ("\t25. Test Stream Upload");
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 ("\t27. Test Join SPS");
System.out.println ("\t28. Test Management Interface API");
System.out.println ("\t29. Test Anonymous Connection");

System.out.println ("\t99. Exit");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
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.SafeguardAppliance;
import com.oneidentity.safeguard.safeguardclient.data.SafeguardApplianceStatus;
import com.oneidentity.safeguard.safeguardclient.data.SafeguardSslCertificate;
import com.oneidentity.safeguard.safeguardclient.data.SessionRecordings;
import com.oneidentity.safeguard.safeguardjava.IProgressCallback;
import com.oneidentity.safeguard.safeguardjava.ISafeguardA2AContext;
Expand Down Expand Up @@ -617,7 +620,7 @@ void safeguardSessionsApi(ISafeguardSessionsConnection connection) {
}

try {
FullResponse fullResponse = connection.InvokeMethodFull(Method.Get, "configuration/network/naming", null);
FullResponse fullResponse = connection.invokeMethodFull(Method.Get, "configuration/network/naming", null);
System.out.println(String.format("\t\\Network Naming full response:"));
logResponseDetails(fullResponse);

Expand All @@ -629,14 +632,14 @@ void safeguardSessionsApi(ISafeguardSessionsConnection connection) {
public void safeguardSessionsFileUpload(ISafeguardSessionsConnection connection) {

if (connection == null) {
System.out.println(String.format("Safeguard not connected"));
System.out.println("Safeguard not connected");
return;
}

String patchFileName = readLine("SPS Firmware File Name: ", null);

if (patchFileName == null) {
System.out.println(String.format("file name"));
System.out.println("Missing file name");
return;
}

Expand Down Expand Up @@ -685,7 +688,7 @@ public void safeguardSessionsStreamUpload(ISafeguardSessionsConnection connectio

private String[] safeguardSessionsGetRecordings(ISafeguardSessionsConnection connection) {
try {
FullResponse fullResponse = connection.InvokeMethodFull(Method.Get, "audit/sessions", null);
FullResponse fullResponse = connection.invokeMethodFull(Method.Get, "audit/sessions", null);
System.out.println(String.format("\t\\Session Id's full response:"));
logResponseDetails(fullResponse);

Expand All @@ -694,7 +697,7 @@ private String[] safeguardSessionsGetRecordings(ISafeguardSessionsConnection con
return sessionIds.toArray();

} catch (ArgumentException | ObjectDisposedException | SafeguardForJavaException ex) {
System.out.println("\t[ERROR]Test connection failed: " + ex.getMessage());
System.out.println("\t[ERROR]Get session recordings failed: " + ex.getMessage());
} catch (JsonProcessingException ex) {
System.out.println("JSON deserialization failed: " + ex.getMessage());
}
Expand Down Expand Up @@ -745,6 +748,64 @@ public void safeguardSessionTestRecordingDownload(ISafeguardSessionsConnection c
}
}

public void safeguardTestJoinSps(ISafeguardConnection sppConnection, ISafeguardSessionsConnection spsConnection) {
if (sppConnection == null) {
System.out.println(String.format("Safeguard SPP not connected"));
return;
}
if (spsConnection == null) {
System.out.println(String.format("Safeguard SPS not connected"));
return;
}

SafeguardSslCertificate[] sslCerts = null;
SafeguardApplianceStatus applianceStatus = null;
try {
FullResponse fullResponse = sppConnection.invokeMethodFull(Service.Core, Method.Get, "SslCertificates", null, null, null, null);
System.out.println(String.format("\t\\SslCertificates full response:"));
logResponseDetails(fullResponse);

ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
sslCerts = mapper.readValue(fullResponse.getBody(), SafeguardSslCertificate[].class);

fullResponse = sppConnection.invokeMethodFull(Service.Appliance, Method.Get, "ApplianceStatus", null, null, null, null);
System.out.println(String.format("\t\\ApplianceStatus full response:"));
logResponseDetails(fullResponse);

applianceStatus = mapper.readValue(fullResponse.getBody(), SafeguardApplianceStatus.class);

} catch (ArgumentException | ObjectDisposedException | SafeguardForJavaException ex) {
System.out.println("\t[ERROR]Test Join Sps failed: " + ex.getMessage());
} catch (JsonProcessingException ex) {
System.out.println("JSON deserialization failed: " + ex.getMessage());
}

if (sslCerts == null || applianceStatus == null) {
System.out.println("Test Join Sps failed: failed to get the Safeguard appliance information");
return;
}

String certChain = null;
String sppAddress = null;
for (SafeguardSslCertificate cert : sslCerts) {
for (SafeguardAppliance sa : cert.getAppliances()) {
if (sa.getId().equalsIgnoreCase(applianceStatus.getIdentity())) {
for (String c : cert.getIssuerCertificates()) {
certChain += " "+c.replaceAll("\\r", "");
}
certChain = certChain == null ? cert.getBase64CertificateData().replaceAll("\\r", "") : cert.getBase64CertificateData().replaceAll("\\r", "")+certChain;
sppAddress = sa.getIpv4Address();
}
}
}

try {
sppConnection.JoinSps(spsConnection, certChain, sppAddress);
} catch (ObjectDisposedException | SafeguardForJavaException | ArgumentException ex) {
System.out.println("\t[ERROR]Test Join Sps failed: " + ex.getMessage());
}
}

void safeguardTestManagementConnection(ISafeguardConnection connection) {
if (connection == null) {
System.out.println(String.format("Safeguard not connected. This test requires an annonymous connection."));
Expand Down
Loading