2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.client.policy;
23 import java.io.FileInputStream;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.KeyStore;
27 import java.util.Optional;
28 import java.util.UUID;
30 import javax.net.ssl.SSLContext;
31 import javax.ws.rs.client.Client;
32 import javax.ws.rs.client.ClientBuilder;
34 import org.openecomp.mso.client.RestProperties;
35 import org.openecomp.mso.logger.MessageEnum;
36 import org.openecomp.mso.logger.MsoLogger;
38 public abstract class RestClientSSL extends RestClient {
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";
45 protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path) {
46 super(props, requestId, path);
49 protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path, String accept, String contentType) {
50 super(props, requestId, path, accept, contentType);
54 protected Client getClient() {
58 String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
59 if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase("true")) {
60 KeyStore ks = getKeyStore();
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.");
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);
77 private KeyStore getKeyStore() {
79 char[] password = System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY).toCharArray();
80 FileInputStream fis = null;
82 ks = KeyStore.getInstance(KeyStore.getDefaultType());
83 fis = new FileInputStream(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY));
84 ks.load(fis, password);