Wednesday, September 12, 2018

RSocket

http://rsocket.io/
RSocket is a binary protocol for use on byte stream transports such as TCP, WebSockets, and Aeron.

  • Efficient.
  • Reactive.
  • Binary.
  • Back-pressure support.
  • Polyglot (Java, JS, C++, ...).
  • Streams multiplexed over a single socket.
Spring 5 (not implemented): https://jira.spring.io/browse/SPR-16751


Algorithm visualization software

https://hownot2code.com/2017/06/15/8-services-for-the-visualization-of-algorithms/

Thursday, May 24, 2018

SSO on QlikSense using the Session API

Certificate:

keytool.exe -importkeystore -srckeystore "client.pfx" -srcstoretype pkcs12 -destkeystore "client.jks" -deststoretype JKS -srcstorepass secret -deststorepass secret -noprompt

keytool.exe -import -alias QlikCA -keystore "root.jks" -file "root.cer" -storepass secret -noprompt   

Java code:

import javax.net.ssl.*; 
import java.io.*; 
import java.net.URL; 
import java.security.*; 

public class QlikSenseSessionManager 

private static final String VIRTUAL_PROXY = "myvp"; // The prefix for the virtual proxy configured in Qlik Sense  
private static final String XRF_KEY = "7rBHABt65vFflaZ7"; // Xrfkey to prevent cross-site issues

private final File certFolder;
private final String proxyCertPass;
private final String rootCertPass;
private final String host;

public QlikSenseSessionManager(File certFolder, String proxyCertPass, String rootCertPass, String host) {
this.certFolder = certFolder;
this.proxyCertPass = proxyCertPass;
this.rootCertPass = rootCertPass;
this.host = host;
}

public void registerSession(String sessionId, String userDirectory, String userId) throws Exception {
final SSLSocketFactory sslSocketFactory = buildSslSocketFactory();

final URL url = new URL("https://" + host + ":4243/qps/" + VIRTUAL_PROXY + "/session?xrfkey=" + XRF_KEY); 
System.out.println("HTTP POST: " + url); 
final HttpsURLConnection connection = (HttpsURLConnection ) url.openConnection();
try {
connection.setSSLSocketFactory(sslSocketFactory); 
connection.setRequestProperty("x-qlik-xrfkey", XRF_KEY); 
connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setRequestProperty("Content-Type","application/json"); 
connection.setRequestProperty("Accept", "application/json"); 
connection.setRequestMethod("POST");

final String body = "{ 'UserId':'" + userId + "','UserDirectory':'" + userDirectory +"','SessionId':'" + sessionId + "','Attributes': [] }"; 
System.out.println("Payload: " + body);

final OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(body); 
wr.flush(); 

final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
try {
final StringBuilder builder = new StringBuilder(); 
String inputLine; 
while ((inputLine = in.readLine()) != null) 

builder.append(inputLine); 

final String data = builder.toString();
System.out.println("Response: " + data);
}
finally {
in.close();
}
}
finally {
connection.disconnect();
}
}

public void deleteSession(String sessionId) throws Exception {
final SSLSocketFactory sslSocketFactory = buildSslSocketFactory();

final URL url = new URL("https://" + host + ":4243/qps/" + VIRTUAL_PROXY + "/session/" + sessionId +"?xrfkey=" + XRF_KEY); 
System.out.println("HTTP DELETE: " + url); 
final HttpsURLConnection connection = (HttpsURLConnection ) url.openConnection();
try { 
connection.setSSLSocketFactory(sslSocketFactory); 
connection.setRequestProperty("x-qlik-xrfkey", XRF_KEY); 
connection.setRequestMethod("DELETE");
connection.getResponseCode();
}
finally {
connection.disconnect();
}
}

protected SSLSocketFactory buildSslSocketFactory() throws Exception {
// Certificate Acquisition 
final File proxyCertFile = new File(certFolder, "client.jks"); // Client certificate with private key 
final File rootCertFile = new File(certFolder, "root.jks"); // Root certificate for the client cert. Required because Qlik Sense certs are used.
final KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(proxyCertFile), proxyCertPass.toCharArray());
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
kmf.init(ks, proxyCertPass.toCharArray());
final KeyStore ksTrust = KeyStore.getInstance("JKS"); 
ksTrust.load(new FileInputStream(rootCertFile), rootCertPass.toCharArray()); 

// Certificate configuration for use in connection 
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
tmf.init(ksTrust); 
final SSLContext context = SSLContext.getInstance("SSL");
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
return context.getSocketFactory();
}

public static void main(String[] args) throws Exception 
{
final File certFolder = new File("/myfolder");
final String proxyCertPass = "secret";
final String rootCertPass = "secret";
final String host = "myhost";

final QlikSenseSessionManager mgr = new QlikSenseSessionManager(certFolder, proxyCertPass, rootCertPass, host);
mgr.registerSession("mysessionid", "mywinuserdir", "mywinuser");
System.out.println("Try: https://" + host + "/" + VIRTUAL_PROXY + "/hub");

//mgr.deleteSession("mysessionid");
}
}