8369eba8597b2d72c50cd5cd90076b5294a149e6
[so.git] / common / src / main / java / org / onap / so / client / RestClientSSL.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client;
22
23 import java.io.FileInputStream;
24 import java.net.URI;
25 import java.nio.file.Paths;
26 import java.security.KeyStore;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.Optional;
29
30 import javax.net.ssl.SSLContext;
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.ClientBuilder;
33
34 public abstract class RestClientSSL extends RestClient {
35         
36         private static final String TRUE = "true";
37     public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore";
38         public static final String SSL_KEY_STORE_PASSWORD_KEY = "javax.net.ssl.keyStorePassword";
39         public static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore";
40         
41
42         protected RestClientSSL(RestProperties props, Optional<URI> path) {
43                 super(props, path);
44         }
45
46         protected RestClientSSL(RestProperties props, Optional<URI> path, String accept, String contentType) {
47                 super(props, path, accept, contentType);
48         }
49
50         @Override
51         protected Client getClient() {
52                 
53                 Client client = null;
54                 try {
55                         String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
56                         if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase(TRUE)) {
57                                 KeyStore ks = getKeyStore();
58                                 if(ks != null) {
59                                         client = ClientBuilder.newBuilder().keyStore(ks, System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY)).build();
60                                         logger.info("RestClientSSL not using default SSL context - setting keystore here.");
61                                         return client;
62                                 }
63                         }
64                         //Use default SSL context 
65                         client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
66                         logger.info("RestClientSSL using default SSL context!");
67                 } catch (NoSuchAlgorithmException e) {
68                         throw new RuntimeException(e);
69                 }
70                 return client;
71         }
72         
73         private KeyStore getKeyStore() {
74                 KeyStore ks = null;
75             char[] password = System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY).toCharArray();
76             try(FileInputStream fis = new FileInputStream(Paths.get(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY)).normalize().toString())) {
77                 ks = KeyStore.getInstance(KeyStore.getDefaultType());
78                 
79                 ks.load(fis, password);
80             }
81             catch(Exception e) {
82                 return null;
83             }
84             
85             return ks;
86         }
87 }