summaryrefslogtreecommitdiffstats
path: root/sms-client/java/src/main/example/SmsClientExample.java
blob: 88956c5a9e26f10f4018734340151911d5d438f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2018 Intel Corporation, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileInputStream;
import java.lang.Boolean;
import java.lang.Integer;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import java.security.KeyStore;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import org.onap.aaf.sms.SmsClient;
import org.onap.aaf.sms.SmsResponse;

/*
 * Sample application demonstrating various operations related
 * Secret Management Service's APIs
 */

public class SmsClientExample {

    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;
        }

    }

    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("");
        }

        // 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("");
        }

        // 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("");
        }

        // 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("");
        }

        // 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("");
        }

        // 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)resp.getResponse().get("age"));
            System.out.println("");
        }

        // 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("");
        }
    }
}