fa265d85245ea4dc2418f530c312a6b09efc33e7
[dcaegen2/services/sdk.git] /
1 /*
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
12  *
13  *       http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================
21  */
22
23 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model;
24
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;
34
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.Optional;
38
39 /**
40  * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in
41  * CloudConfigurationClient.class
42  *
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.
46  * @since 1.0.0
47  */
48 @Value.Immutable(prehash = true)
49 public interface CbsClientConfiguration {
50     Logger LOGGER = LoggerFactory.getLogger(CbsClientConfiguration.class);
51
52     String TRUST_JKS = "trust.jks";
53     String TRUST_PASS = "trust.pass";
54
55
56     /**
57      * Name of environment variable containing path to the cacert.pem file.
58      */
59     String DCAE_CA_CERT_PATH = "DCAE_CA_CERTPATH";
60
61     /**
62      * Name of environment variable containing current application name.
63      */
64     String ENV_APP_NAME = "HOSTNAME";
65
66     /**
67      * Name of environment variable containing path to application config file.
68      */
69     String ENV_CBS_CLIENT_CONFIG_PATH = "CBS_CLIENT_CONFIG_PATH";
70
71     /**
72      * Name of environment variable containing path to policies file.
73      */
74     String ENV_CBS_CLIENT_POLICY_PATH = "CBS_CLIENT_POLICY_PATH";
75
76
77     @Value.Parameter
78     String appName();
79
80     @Value.Default
81     default @Nullable TrustStoreKeys trustStoreKeys() {
82         return null;
83     }
84
85     @Value.Default
86     default String configMapFilePath() {
87         return "/app-config/application_config.yaml";
88     }
89     @Value.Default
90     default String policySyncFilePath() {
91         return "/etc/policies/policies.json";
92     }
93
94
95     /**
96      * Creates CbsClientConfiguration from system environment variables.
97      *
98      * @return an instance of CbsClientConfiguration
99      * @throws CbsClientConfigurationException when at least one of required parameters is missing
100      */
101     static CbsClientConfiguration fromEnvironment() {
102         String pathToCaCert = System.getenv(DCAE_CA_CERT_PATH);
103
104         ImmutableCbsClientConfiguration.Builder configBuilder = ImmutableCbsClientConfiguration.builder()
105                 .appName(getEnv(ENV_APP_NAME));
106
107         Optional.ofNullable(System.getenv(ENV_CBS_CLIENT_CONFIG_PATH))
108             .ifPresent(configBuilder::configMapFilePath);
109
110         Optional.ofNullable(System.getenv(ENV_CBS_CLIENT_POLICY_PATH))
111             .ifPresent(configBuilder::policySyncFilePath);
112         return configBuilder.build();
113     }
114
115     static String getEnv(String envName) {
116         String envValue = System.getenv(envName);
117         validateEnv(envName, envValue);
118         return envValue;
119     }
120
121     static void validateEnv(String envName, String envValue) {
122         if (envValue == null || "".equals(envValue)) {
123             throw new CbsClientConfigurationException("Cannot read " + envName + " from environment.");
124         }
125     }
126
127
128 }