AT&T 1712 and 1802 release code
[so.git] / common / src / main / java / org / openecomp / mso / client / policy / 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.openecomp.mso.client.policy;
22
23 import java.io.FileInputStream;
24 import java.net.URI;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.KeyStore;
27 import java.util.Optional;
28 import java.util.UUID;
29
30 import javax.net.ssl.SSLContext;
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.ClientBuilder;
33
34 import org.openecomp.mso.client.RestProperties;
35 import org.openecomp.mso.logger.MessageEnum;
36 import org.openecomp.mso.logger.MsoLogger;
37
38 public abstract class RestClientSSL extends RestClient {
39         
40         public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore";
41         public static final String SSL_KEY_STORE_PASSWORD_KEY = "javax.net.ssl.keyStorePassword";
42         public static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore";
43         
44
45         protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path) {
46                 super(props, requestId, path);
47         }
48
49         protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path, String accept, String contentType) {
50                 super(props, requestId, path, accept, contentType);
51         }
52
53         @Override
54         protected Client getClient() {
55                 
56                 Client client = null;
57                 try {
58                         String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
59                         if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase("true")) {
60                                 KeyStore ks = getKeyStore();
61                                 if(ks != null) {
62                                         client = ClientBuilder.newBuilder().keyStore(ks, System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY)).build();
63                                         this.msoLogger.debug("RestClientSSL not using default SSL context - setting keystore here.");
64                                         return client;
65                                 }
66                         }
67                         //Use default SSL context 
68                         client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
69                         this.msoLogger.debug("RestClientSSL using default SSL context!");
70                 } catch (NoSuchAlgorithmException e) {
71                         this.msoLogger.error(MessageEnum.APIH_GENERAL_EXCEPTION, "AAI", "Client init", MsoLogger.ErrorCode.UnknownError, "could not create SSL client", e);
72                         throw new RuntimeException(e);
73                 }
74                 return client;
75         }
76         
77         private KeyStore getKeyStore() {
78                 KeyStore ks = null;
79             char[] password = System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY).toCharArray();
80             FileInputStream fis = null;
81             try {
82                 ks = KeyStore.getInstance(KeyStore.getDefaultType());
83                 fis = new FileInputStream(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY));
84                 ks.load(fis, password);
85             }
86             catch(Exception e) {
87                 return null;
88             }
89             finally {
90                 if (fis != null) {
91                     try { 
92                         fis.close();
93                     }
94                     catch(Exception e) {}
95                 }
96             }
97             return ks;
98         }
99 }