2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019-2021 Nokia. All rights reserved.
6 * Copyright (C) 2021 Wipro Limited.
7 * =========================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=====================================
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model;
24 import org.immutables.value.Value;
25 import org.jetbrains.annotations.Nullable;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.CbsClientConfigurationException;
27 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableTrustStoreKeys;
28 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
29 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeysStore;
30 import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Optional;
39 * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in
40 * CloudConfigurationClient.class
42 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18
43 * @version 1.0.0 can be passed to ReactiveCloudConfigurationProvider, can be constructed out of
44 * org.onap.dcaegen2.services.sdk library.
47 @Value.Immutable(prehash = true)
48 public interface CbsClientConfiguration {
49 Logger LOGGER = LoggerFactory.getLogger(CbsClientConfiguration.class);
51 String TRUST_JKS = "trust.jks";
52 String TRUST_PASS = "trust.pass";
53 Integer PORT_FOR_CBS_OVER_TLS = 10443;
56 * Name of environment variable containing path to the cacert.pem file.
58 String DCAE_CA_CERT_PATH = "DCAE_CA_CERTPATH";
61 * Name of environment variable containing Config Binding Service network hostname.
63 String ENV_CBS_HOSTNAME = "CONFIG_BINDING_SERVICE";
66 * Name of environment variable containing Config Binding Service network port.
68 String ENV_CBS_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT";
71 * Name of environment variable containing current application name.
73 String ENV_APP_NAME = "HOSTNAME";
77 * Name of environment variable containing Consul host name.
79 * @deprecated CBS lookup in Consul service should not be needed,
80 * instead {@link #ENV_CBS_HOSTNAME} should be used directly.
83 String ENV_CONSUL_HOST = "CONSUL_HOST";
86 * Name of environment variable containing Config Binding Service <em>service name</em> as registered in Consul
89 * @deprecated CBS lookup in Consul service should not be needed,
90 * instead {@link #ENV_CBS_HOSTNAME} should be used directly.
93 String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE";
111 default @Nullable TrustStoreKeys trustStoreKeys() {
117 default String consulHost() {
118 return "consul-server";
122 default Integer consulPort() {
127 default String cbsName() {
128 return "config-binding-service";
131 default String configMapFilePath() {
132 return "/app-config/application_config.yaml";
135 default String policySyncFilePath() {
136 return "/etc/policies/policies.json";
140 * Creates CbsClientConfiguration from system environment variables.
142 * @return an instance of CbsClientConfiguration
143 * @throws CbsClientConfigurationException when at least one of required parameters is missing
145 static CbsClientConfiguration fromEnvironment() {
146 String pathToCaCert = System.getenv(DCAE_CA_CERT_PATH);
148 ImmutableCbsClientConfiguration.Builder configBuilder = ImmutableCbsClientConfiguration.builder()
149 .hostname(getEnv(ENV_CBS_HOSTNAME))
150 .appName(getEnv(ENV_APP_NAME));
151 return Optional.ofNullable(pathToCaCert).filter(certPath -> !"".equals(certPath))
152 .map(certPath -> createSslHttpConfig(configBuilder, certPath))
153 .orElseGet(() -> createPlainHttpConfig(configBuilder));
156 static CbsClientConfiguration createPlainHttpConfig(ImmutableCbsClientConfiguration.Builder configBuilder) {
157 LOGGER.info("CBS client will use plain http protocol.");
160 .port(Integer.valueOf(getEnv(ENV_CBS_PORT)))
164 static CbsClientConfiguration createSslHttpConfig(ImmutableCbsClientConfiguration.Builder configBuilder,
165 String pathToCaCert) {
166 LOGGER.info("CBS client will use http over TLS.");
168 .trustStoreKeys(crateSecurityKeysFromEnvironment(createPathToJksFile(pathToCaCert)))
169 .port(PORT_FOR_CBS_OVER_TLS)
174 static TrustStoreKeys crateSecurityKeysFromEnvironment(String pathToCerts) {
175 LOGGER.info("Path to cert files: {}", pathToCerts + "/");
176 validateIfFilesExist(pathToCerts);
177 return ImmutableTrustStoreKeys.builder()
178 .trustStore(SecurityKeysStore.fromPath(Paths.get(pathToCerts + "/" + TRUST_JKS)))
179 .trustStorePassword(Passwords.fromPath(Paths.get(pathToCerts + "/" + TRUST_PASS)))
183 static String createPathToJksFile(String pathToCaCertPemFile) {
184 return pathToCaCertPemFile.substring(0, pathToCaCertPemFile.lastIndexOf("/"));
187 static String getEnv(String envName) {
188 String envValue = System.getenv(envName);
189 validateEnv(envName, envValue);
193 static void validateEnv(String envName, String envValue) {
194 if (envValue == null || "".equals(envValue)) {
195 throw new CbsClientConfigurationException("Cannot read " + envName + " from environment.");
199 static void validateIfFilesExist(String pathToFile) {
200 boolean areFilesExist = Files.exists(Paths.get(pathToFile + "/" + TRUST_JKS)) &&
201 Files.exists(Paths.get(pathToFile + "/" + TRUST_PASS));
203 if (!areFilesExist) {
204 throw new CbsClientConfigurationException("Required files do not exist in " + pathToFile + " directory.");