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