2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019-2021 Nokia. All rights reserved.
6 * Copyright (C) 2021 Wipro Limited.
7 * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved.
8 * =========================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================
23 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model;
25 import org.immutables.value.Value;
26 import org.jetbrains.annotations.Nullable;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.CbsClientConfigurationException;
28 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableTrustStoreKeys;
29 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
30 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeysStore;
31 import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.Optional;
40 * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in
41 * CloudConfigurationClient.class
43 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18
44 * @version 1.0.0 can be passed to ReactiveCloudConfigurationProvider, can be constructed out of
45 * org.onap.dcaegen2.services.sdk library.
48 @Value.Immutable(prehash = true)
49 public interface CbsClientConfiguration {
50 Logger LOGGER = LoggerFactory.getLogger(CbsClientConfiguration.class);
52 String TRUST_JKS = "trust.jks";
53 String TRUST_PASS = "trust.pass";
57 * Name of environment variable containing path to the cacert.pem file.
59 String DCAE_CA_CERT_PATH = "DCAE_CA_CERTPATH";
62 * Name of environment variable containing current application name.
64 String ENV_APP_NAME = "HOSTNAME";
67 * Name of environment variable containing path to application config file.
69 String ENV_CBS_CLIENT_CONFIG_PATH = "CBS_CLIENT_CONFIG_PATH";
72 * Name of environment variable containing path to policies file.
74 String ENV_CBS_CLIENT_POLICY_PATH = "CBS_CLIENT_POLICY_PATH";
81 default @Nullable TrustStoreKeys trustStoreKeys() {
86 default String configMapFilePath() {
87 return "/app-config/application_config.yaml";
90 default String policySyncFilePath() {
91 return "/etc/policies/policies.json";
96 * Creates CbsClientConfiguration from system environment variables.
98 * @return an instance of CbsClientConfiguration
99 * @throws CbsClientConfigurationException when at least one of required parameters is missing
101 static CbsClientConfiguration fromEnvironment() {
102 String pathToCaCert = System.getenv(DCAE_CA_CERT_PATH);
104 ImmutableCbsClientConfiguration.Builder configBuilder = ImmutableCbsClientConfiguration.builder()
105 .appName(getEnv(ENV_APP_NAME));
107 Optional.ofNullable(System.getenv(ENV_CBS_CLIENT_CONFIG_PATH))
108 .ifPresent(configBuilder::configMapFilePath);
110 Optional.ofNullable(System.getenv(ENV_CBS_CLIENT_POLICY_PATH))
111 .ifPresent(configBuilder::policySyncFilePath);
112 return configBuilder.build();
115 static String getEnv(String envName) {
116 String envValue = System.getenv(envName);
117 validateEnv(envName, envValue);
121 static void validateEnv(String envName, String envValue) {
122 if (envValue == null || "".equals(envValue)) {
123 throw new CbsClientConfigurationException("Cannot read " + envName + " from environment.");