Remove hardcoded literals
[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 import javax.net.ssl.SSLContext;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.ClientBuilder;
32
33 public abstract class RestClientSSL extends RestClient {
34
35     private static final String TRUE = "true";
36     private static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore";
37     private static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore";
38
39
40     protected RestClientSSL(RestProperties props, Optional<URI> path) {
41         super(props, path);
42     }
43
44     protected RestClientSSL(RestProperties props, Optional<URI> path, String accept, String contentType) {
45         super(props, path, accept, contentType);
46     }
47
48     @Override
49     protected Client getClient() {
50
51         Client client = null;
52         try {
53             String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
54             if (loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase(TRUE)) {
55                 KeyStore ks = getKeyStore();
56                 if (ks != null) {
57                     client = ClientBuilder.newBuilder().keyStore(ks, getSSlKeyStorePassword()).build();
58                     logger.info("RestClientSSL not using default SSL context - setting keystore here.");
59                     return client;
60                 }
61             }
62             // Use default SSL context
63             client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
64             logger.info("RestClientSSL using default SSL context!");
65         } catch (NoSuchAlgorithmException e) {
66             throw new RuntimeException(e);
67         }
68         return client;
69     }
70
71     private KeyStore getKeyStore() {
72         KeyStore ks = null;
73         char[] password = getSSlKeyStorePassword().toCharArray();
74         try (FileInputStream fis = new FileInputStream(
75                 Paths.get(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY)).normalize().toString())) {
76             ks = KeyStore.getInstance(KeyStore.getDefaultType());
77
78             ks.load(fis, password);
79         } catch (Exception e) {
80             return null;
81         }
82
83         return ks;
84     }
85
86     private String getSSlKeyStorePassword() {
87         return System.getProperty("javax.net.ssl.keyStorePassword");
88     }
89 }