2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import java.io.FileInputStream;
18 import java.lang.Boolean;
19 import java.lang.Integer;
21 import javax.net.ssl.HttpsURLConnection;
22 import javax.net.ssl.KeyManagerFactory;
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.SSLSessionContext;
25 import javax.net.ssl.SSLSocketFactory;
26 import javax.net.ssl.TrustManagerFactory;
27 import java.security.KeyStore;
28 import java.security.Provider;
29 import java.security.SecureRandom;
30 import java.security.Security;
31 import java.util.HashMap;
33 import org.onap.aaf.sms.SmsClient;
34 import org.onap.aaf.sms.SmsResponse;
37 * Sample application demonstrating various operations related
38 * Secret Management Service's APIs
41 public class SmsClientExample {
43 public static SSLSocketFactory getSSLSocketFactory(String castore) {
46 // Load the CA certificate
47 // There are no private keys in the truststore
48 FileInputStream tst = new FileInputStream("truststoreONAP.jks");
49 KeyStore trustStore = KeyStore.getInstance("JKS");
50 char[] password = "password".toCharArray();
51 trustStore.load(tst, password);
52 TrustManagerFactory trustManagerFactory =
53 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
54 trustManagerFactory.init(trustStore);
57 SSLContext context = SSLContext.getInstance("TLSv1.2");
58 context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
59 //Create a socket factory
60 SSLSocketFactory ssf = context.getSocketFactory();
62 } catch (Exception e) {
69 public static void main(String[] args) throws Exception {
71 SSLSocketFactory ssf = SmsClientExample.getSSLSocketFactory("truststoreONAP.jks");
73 // Create the SMSClient
74 SmsClient sms = new SmsClient("aaf-sms.onap", 30243, ssf);
76 // Create a test domain
77 System.out.println("CREATE DOMAIN: ");
78 SmsResponse resp = sms.createDomain("sms_test_domain");
79 if ( resp.getSuccess() ) {
80 System.out.println("-- Return Code: " + resp.getResponseCode());
81 System.out.println("-- Return Data: " + resp.getResponse());
82 System.out.println("");
84 System.out.println("-- Error String: " + resp.getErrorMessage());
85 System.out.println("");
88 // Create secret data here
89 Map<String, Object> data_1 = new HashMap<String, Object>();
90 data_1.put("passwd", "gax6ChD0yft");
92 // Store them in previously created domain
93 System.out.println("STORE SECRET: " + "test_secret");
94 resp = sms.storeSecret("sms_test_domain", "test_secret", data_1);
95 if ( resp.getSuccess() ) {
96 System.out.println("-- Return Code: " + resp.getResponseCode());
97 System.out.println("");
100 // A more complex data example on the same domain
101 Map<String, Object> data_2 = new HashMap<String, Object>();
102 data_2.put("username", "dbuser");
103 data_2.put("isadmin", new Boolean(true));
104 data_2.put("age", new Integer(40));
105 data_2.put("secretkey", "asjdhkuhioeukadfjsadnfkjhsdukfhaskdjhfasdf");
106 data_2.put("token", "2139084553458973452349230849234234908234342");
109 System.out.println("STORE SECRET: " + "test_credentials");
110 resp = sms.storeSecret("sms_test_domain", "test_credentials", data_2);
111 if ( resp.getSuccess() ) {
112 System.out.println("-- Return Code: " + resp.getResponseCode());
113 System.out.println("");
116 // List all secret names stored in domain
117 System.out.println("LIST SECRETS: ");
118 resp = sms.getSecretNames("sms_test_domain");
119 if ( resp.getSuccess() ) {
120 System.out.println("-- Return Code: " + resp.getResponseCode());
121 System.out.println("-- Return Data: " + resp.getResponse());
122 System.out.println("");
125 // Retrieve a secret from stored domain
126 System.out.println("GET SECRET: " + "test_secret");
127 resp= sms.getSecret("sms_test_domain", "test_secret");
128 if ( resp.getSuccess() ) {
129 System.out.println("-- Return Code: " + resp.getResponseCode());
130 System.out.println("-- Return Data: " + resp.getResponse());
131 System.out.println("");
134 // Retrieve the second secret from stored domain
135 // getResponse() on the return value retrieves the
136 // map containing the key, values for the secret
137 System.out.println("GET SECRET: " + "test_credentials");
138 resp= sms.getSecret("sms_test_domain", "test_credentials");
139 if ( resp.getSuccess() ) {
140 System.out.println("-- Return Code: " + resp.getResponseCode());
141 System.out.println("-- Return Data: " + resp.getResponse());
143 //conditional processing of returned data
144 Boolean b = (Boolean)resp.getResponse().get("isadmin");
145 System.out.println("-- isadmin: " + b);
147 System.out.println("-- age: " + (Integer)resp.getResponse().get("age"));
148 System.out.println("");
152 System.out.println("DELETE SECRET: " + "test_credentials");
153 resp=sms.deleteSecret("sms_test_domain", "test_credentials");
154 if ( resp.getSuccess() ) {
155 System.out.println("-- Return Code: " + resp.getResponseCode());
156 System.out.println("");
160 System.out.println("DELETE DOMAIN: " + "sms_test_domain");
161 resp=sms.deleteDomain("sms_test_domain");
162 if ( resp.getSuccess() ) {
163 System.out.println("-- Return Code: " + resp.getResponseCode());
164 System.out.println("");