summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2018-05-24 15:50:33 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2018-05-24 15:50:37 -0700
commit7ff4d4001de8cddee013350e18fb74cd31051b81 (patch)
tree4d53a345e6b5fd3703e14720e13a557930c95e64
parentd977f32143aeef8600280c95ec09f59edf2891c3 (diff)
Add ONAP truststore and comment example client
Add ONAP truststore with certificate only in jks format Add comments to the client example illustrating various use cases Support error message read from returned error body Issue-ID: AAF-92 Change-Id: I9709005c13d739883621eed09ce34fcb4715f1fd Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
-rw-r--r--sms-client/java/src/main/example/README.md7
-rw-r--r--sms-client/java/src/main/example/SmsClientExample.java183
-rw-r--r--sms-client/java/src/main/example/truststoreONAP.jksbin0 -> 1408 bytes
-rw-r--r--sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java13
4 files changed, 130 insertions, 73 deletions
diff --git a/sms-client/java/src/main/example/README.md b/sms-client/java/src/main/example/README.md
new file mode 100644
index 0000000..e96cefc
--- /dev/null
+++ b/sms-client/java/src/main/example/README.md
@@ -0,0 +1,7 @@
+### Running the Sample Application
+Make sure you run in the example folder as it looks for the truststore here
+
+```
+$ javac -cp ".:<PATH TO SMSCLIENT JAR FILE>" SmsClientExample.java
+$ java -cp ".:<PATH TO SMSCLIENT JAR FILE>" SmsClientExample
+```
diff --git a/sms-client/java/src/main/example/SmsClientExample.java b/sms-client/java/src/main/example/SmsClientExample.java
index ca38adb..88956c5 100644
--- a/sms-client/java/src/main/example/SmsClientExample.java
+++ b/sms-client/java/src/main/example/SmsClientExample.java
@@ -39,84 +39,129 @@ import org.onap.aaf.sms.SmsResponse;
*/
public class SmsClientExample {
- public static void main(String[] args) throws Exception {
- // Set up the Sun PKCS 11 provider
- Provider p = Security.getProvider("SunPKCS11-pkcs11Test");
- if (p==null) {
- throw new RuntimeException("could not get security provider");
+
+ public static SSLSocketFactory getSSLSocketFactory(String castore) {
+
+ try {
+ // Load the CA certificate
+ // There are no private keys in the truststore
+ FileInputStream tst = new FileInputStream("truststoreONAP.jks");
+ KeyStore trustStore = KeyStore.getInstance("JKS");
+ char[] password = "password".toCharArray();
+ trustStore.load(tst, password);
+ TrustManagerFactory trustManagerFactory =
+ TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+ trustManagerFactory.init(trustStore);
+
+ //Create the context
+ SSLContext context = SSLContext.getInstance("TLSv1.2");
+ context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
+ //Create a socket factory
+ SSLSocketFactory ssf = context.getSocketFactory();
+ return ssf;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
}
- // Load the key store
- char[] pin = "45789654".toCharArray();
- KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
- keyStore.load(null, pin);
-
- // Load the CA certificate
- FileInputStream tst = new FileInputStream("/ca.jks");
- KeyStore trustStore = KeyStore.getInstance("JKS");
- trustStore.load(tst, pin);
-
- KeyManagerFactory keyManagerFactory =
- KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- //Add to keystore to key manager
- keyManagerFactory.init(keyStore, pin);
-
- TrustManagerFactory trustManagerFactory =
- TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- trustManagerFactory.init(trustStore);
-
- //Create the context
- SSLContext context = SSLContext.getInstance("TLS");
- context.init(keyManagerFactory.getKeyManagers(),
- trustManagerFactory.getTrustManagers(), new SecureRandom());
- //Create a socket factory
- SSLSocketFactory ssf = context.getSocketFactory();
- SmsClient sms = new SmsClient("onap.mydomain.com", 10443, ssf);
- SmsResponse resp1 = sms.createDomain("onap.new.test.sms0");
- if ( resp1.getSuccess() ) {
- System.out.println(resp1.getResponse());
- System.out.println(resp1.getResponseCode());
+ }
+
+ public static void main(String[] args) throws Exception {
+
+ SSLSocketFactory ssf = SmsClientExample.getSSLSocketFactory("truststoreONAP.jks");
+
+ // Create the SMSClient
+ SmsClient sms = new SmsClient("aaf-sms.onap", 30243, ssf);
+
+ // Create a test domain
+ System.out.println("CREATE DOMAIN: ");
+ SmsResponse resp = sms.createDomain("sms_test_domain");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("-- Return Data: " + resp.getResponse());
+ System.out.println("");
+ } else {
+ System.out.println("-- Error String: " + resp.getErrorMessage());
+ System.out.println("");
}
- Map<String, Object> m1 = new HashMap<String, Object>();
- m1.put("passwd", "gax6ChD0yft");
- SmsResponse resp2 = sms.storeSecret("onap.new.test.sms0", "testsec", m1);
- if ( resp2.getSuccess() ) {
- System.out.println(resp2.getResponse());
- System.out.println(resp2.getResponseCode());
+
+ // Create secret data here
+ Map<String, Object> data_1 = new HashMap<String, Object>();
+ data_1.put("passwd", "gax6ChD0yft");
+
+ // Store them in previously created domain
+ System.out.println("STORE SECRET: " + "test_secret");
+ resp = sms.storeSecret("sms_test_domain", "test_secret", data_1);
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("");
}
- Map<String, Object> m2 = new HashMap<String, Object>();
- m2.put("username", "dbuser");
- m2.put("isadmin", new Boolean(true));
- m2.put("age", new Integer(40));
- m2.put("secretkey", "asjdhkuhioeukadfjsadnfkjhsdukfhaskdjhfasdf");
- m2.put("token", "2139084553458973452349230849234234908234342");
- SmsResponse resp3 = sms.storeSecret("onap.new.test.sms0","credentials", m2);
- if ( resp3.getSuccess() ) {
- System.out.println(resp3.getResponse());
- System.out.println(resp3.getResponseCode());
+
+ // A more complex data example on the same domain
+ Map<String, Object> data_2 = new HashMap<String, Object>();
+ data_2.put("username", "dbuser");
+ data_2.put("isadmin", new Boolean(true));
+ data_2.put("age", new Integer(40));
+ data_2.put("secretkey", "asjdhkuhioeukadfjsadnfkjhsdukfhaskdjhfasdf");
+ data_2.put("token", "2139084553458973452349230849234234908234342");
+
+ // Store the secret
+ System.out.println("STORE SECRET: " + "test_credentials");
+ resp = sms.storeSecret("sms_test_domain", "test_credentials", data_2);
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("");
}
- SmsResponse resp4 = sms.getSecretNames("onap.new.test.sms0");
- if ( resp4.getSuccess() ) {
- System.out.println(resp4.getResponse());
- System.out.println(resp4.getResponseCode());
+
+ // List all secret names stored in domain
+ System.out.println("LIST SECRETS: ");
+ resp = sms.getSecretNames("sms_test_domain");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("-- Return Data: " + resp.getResponse());
+ System.out.println("");
}
- SmsResponse resp5= sms.getSecret("onap.new.test.sms0", "testsec");
- if ( resp5.getSuccess() ) {
- System.out.println(resp5.getResponse());
- System.out.println(resp5.getResponseCode());
+
+ // Retrieve a secret from stored domain
+ System.out.println("GET SECRET: " + "test_secret");
+ resp= sms.getSecret("sms_test_domain", "test_secret");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("-- Return Data: " + resp.getResponse());
+ System.out.println("");
}
- SmsResponse resp6= sms.getSecret("onap.new.test.sms0", "credentials");
- if ( resp6.getSuccess() ) {
- Boolean b = (Boolean)resp6.getResponse().get("isadmin");
+
+ // Retrieve the second secret from stored domain
+ // getResponse() on the return value retrieves the
+ // map containing the key, values for the secret
+ System.out.println("GET SECRET: " + "test_credentials");
+ resp= sms.getSecret("sms_test_domain", "test_credentials");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("-- Return Data: " + resp.getResponse());
+
+ //conditional processing of returned data
+ Boolean b = (Boolean)resp.getResponse().get("isadmin");
+ System.out.println("-- isadmin: " + b);
if ( b )
- System.out.println("Age=" + (Integer)resp6.getResponse().get("age"));
- System.out.println(resp6.getResponse());
- System.out.println(resp6.getResponseCode());
+ System.out.println("-- age: " + (Integer)resp.getResponse().get("age"));
+ System.out.println("");
}
- SmsResponse resp7=sms.deleteDomain("onap.new.test.sms0");
- if ( resp7.getSuccess() ) {
- System.out.println(resp7.getResponse());
- System.out.println(resp7.getResponseCode());
+
+ // Delete the secret
+ System.out.println("DELETE SECRET: " + "test_credentials");
+ resp=sms.deleteSecret("sms_test_domain", "test_credentials");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("");
+ }
+
+ // Delete the domain
+ System.out.println("DELETE DOMAIN: " + "sms_test_domain");
+ resp=sms.deleteDomain("sms_test_domain");
+ if ( resp.getSuccess() ) {
+ System.out.println("-- Return Code: " + resp.getResponseCode());
+ System.out.println("");
}
}
}
diff --git a/sms-client/java/src/main/example/truststoreONAP.jks b/sms-client/java/src/main/example/truststoreONAP.jks
new file mode 100644
index 0000000..a05bf47
--- /dev/null
+++ b/sms-client/java/src/main/example/truststoreONAP.jks
Binary files differ
diff --git a/sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java b/sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java
index 8bc88fe..f2d4923 100644
--- a/sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java
+++ b/sms-client/java/src/main/java/org/onap/aaf/sms/SmsClient.java
@@ -139,8 +139,14 @@ public class SmsClient implements SmsInterface {
// An implicit connection happens here
errorcode = conn.getResponseCode();
if ( output && errorcode > 0 ) {
- InputStream inputstream = conn.getInputStream();
- InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
+ InputStream inputStream;
+ if ( errorcode/100 == 2) {
+ inputStream = conn.getInputStream();
+ } else {
+ inputStream = conn.getErrorStream();
+ }
+
+ InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String response;
@@ -149,7 +155,7 @@ public class SmsClient implements SmsInterface {
save = save + response;
}
if ( !save.isEmpty() ) {
- if ( errorcode/100 == 2 ) {
+ if ( errorcode/100 == 2) {
resp.setResponse(strtomap(save));
} else {
resp.setErrorMessage(save);
@@ -157,7 +163,6 @@ public class SmsClient implements SmsInterface {
}
}
} catch ( Exception e ) {
- e.printStackTrace();
resp.setResponseCode(errorcode);
return(resp);
}