4d43d3ba2199cbd3cb1d03fa5798f471b49d9482
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / SystemPropertyHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. 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.onap.vid.aai.util;
22
23 import org.eclipse.jetty.util.security.Password;
24 import org.onap.portalsdk.core.util.SystemProperties;
25 import org.onap.vid.aai.exceptions.InvalidPropertyException;
26
27 import java.io.UnsupportedEncodingException;
28 import java.util.Base64;
29 import java.util.Optional;
30
31 public class SystemPropertyHelper {
32
33     public Optional<String> getAAIUseClientCert(){
34         return getSystemProperty(AAIProperties.AAI_USE_CLIENT_CERT);
35     }
36
37     public Optional<String> getAAIServerUrl(){
38         return getSystemProperty(AAIProperties.AAI_SERVER_URL);
39     }
40
41     public Optional<String> getAAIServerBaseUrl(){
42         return getSystemProperty(AAIProperties.AAI_SERVER_URL_BASE);
43     }
44
45     public Optional<String> getAAIVIDUsername(){
46         return getSystemProperty(AAIProperties.AAI_VID_USERNAME);
47     }
48
49     public Optional<String> getAAIVIDPasswd(){
50         return getSystemProperty(AAIProperties.AAI_VID_PASSWD_X);
51     }
52
53     public Optional<String> getAAITruststorePasswd(){
54         return getSystemProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X);
55     }
56
57     public Optional<String> getAAITruststoreFilename(){
58         return getSystemProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME);
59     }
60
61     public Optional<String> getAAIKeystoreFilename(){
62         return getSystemProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
63     }
64
65     public Optional<String> getAAIKeystorePasswd(){
66         return getSystemProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
67     }
68
69     public boolean isClientCertEnabled() {
70         return getAAIUseClientCert().orElse("false").equalsIgnoreCase("true");
71     }
72
73     public String getFullServicePath(String path) {
74         return getAAIServerUrl().orElse("") + path;
75     }
76
77     public String getServiceBasePath(String path) {
78         return getAAIServerBaseUrl().orElse("") + path;
79     }
80
81     public String getEncodedCredentials() throws InvalidPropertyException, UnsupportedEncodingException {
82         String vidUsername = getAAIVIDUsername().orElseThrow(InvalidPropertyException::new);
83         String vidPassword = Password.deobfuscate(getAAIVIDPasswd().orElseThrow(InvalidPropertyException::new));
84         return Base64.getEncoder().encodeToString((vidUsername + ":" + vidPassword).getBytes("utf-8"));
85     }
86
87     public String getDecryptedTruststorePassword(){
88         return Password.deobfuscate(getAAITruststorePasswd().orElse(""));
89     }
90
91     public String getDecryptedKeystorePassword(){
92         return Password.deobfuscate(getAAIKeystorePasswd().orElse(""));
93     }
94
95     private Optional<String> getSystemProperty(String propertyKey){
96         return Optional.ofNullable(SystemProperties.getProperty(propertyKey));
97     }
98 }