Add ONAP truststore and comment example client
[aaf/sms.git] / sms-client / java / src / main / example / SmsClientExample.java
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 import java.io.FileInputStream;
18 import java.lang.Boolean;
19 import java.lang.Integer;
20 import java.net.URL;
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;
32 import java.util.Map;
33 import org.onap.aaf.sms.SmsClient;
34 import org.onap.aaf.sms.SmsResponse;
35
36 /*
37  * Sample application demonstrating various operations related
38  * Secret Management Service's APIs
39  */
40
41 public class SmsClientExample {
42
43     public static SSLSocketFactory getSSLSocketFactory(String castore) {
44
45         try {
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);
55
56             //Create the context
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();
61             return ssf;
62         } catch (Exception e) {
63             e.printStackTrace();
64             return null;
65         }
66
67     }
68
69     public static void main(String[] args) throws Exception {
70
71         SSLSocketFactory ssf = SmsClientExample.getSSLSocketFactory("truststoreONAP.jks");
72
73         // Create the SMSClient
74         SmsClient sms = new SmsClient("aaf-sms.onap", 30243, ssf);
75
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("");
83         } else {
84             System.out.println("-- Error String: " + resp.getErrorMessage());
85             System.out.println("");
86         }
87
88         // Create secret data here
89         Map<String, Object> data_1 = new HashMap<String, Object>();
90         data_1.put("passwd", "gax6ChD0yft");
91
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("");
98         }
99
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");
107
108         // Store the secret
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("");
114         }
115
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("");
123         }
124
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("");
132         }
133
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());
142
143             //conditional processing of returned data
144             Boolean b = (Boolean)resp.getResponse().get("isadmin");
145             System.out.println("-- isadmin: " + b);
146             if ( b )
147                 System.out.println("-- age: " + (Integer)resp.getResponse().get("age"));
148             System.out.println("");
149         }
150
151         // Delete the secret
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("");
157         }
158
159         // Delete the domain
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("");
165         }
166     }
167 }