2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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.ccsdk.features.sdnr.wt.oauthprovider.data;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.List;
30 import java.util.Random;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
38 private static final Logger LOG = LoggerFactory.getLogger(Config.class);
39 private static final String DEFAULT_CONFIGFILENAME = "etc/oauth-provider.config.json";
40 private static final String ENVVARIABLE = "${";
41 private static final String REGEXENVVARIABLE = "(\\$\\{[A-Z0-9_-]+\\})";
42 private static final Pattern pattern = Pattern.compile(REGEXENVVARIABLE);
43 private static final String DEFAULT_TOKENISSUER = "Opendaylight";
44 private static final String DEFAULT_TOKENSECRET = generateSecret();
45 private static final String DEFAULT_REDIRECTURI = "/odlux/index.html#/oauth?token=";
46 private static final String DEFAULT_SUPPORTODLUSERS = "true";
47 private static Config _instance;
50 private List<OAuthProviderConfig> providers;
52 private String redirectUri;
53 private String supportOdlUsers;
54 private String tokenSecret;
55 private String tokenIssuer;
59 public String toString() {
60 return "Config [providers=" + providers + ", host=" + host + ", redirectUri=" + redirectUri
61 + ", supportOdlUsers=" + supportOdlUsers + ", tokenSecret=" + tokenSecret + ", tokenIssuer="
67 public List<OAuthProviderConfig> getProviders() {
71 public void setProviders(List<OAuthProviderConfig> providers) {
72 this.providers = providers;
75 public String getHost() {
79 public void setHost(String host) {
83 public String getRedirectUri() {
87 public void setRedirectUri(String redirectUri) {
88 this.redirectUri = redirectUri;
91 public String getSupportOdlUsers() {
92 return supportOdlUsers;
95 public void setSupportOdlUsers(String supportOdlUsers) {
96 this.supportOdlUsers = supportOdlUsers;
99 public String getTokenSecret() {
103 public void setTokenSecret(String tokenSecret) {
104 this.tokenSecret = tokenSecret;
107 public String getTokenIssuer() {
111 public void setTokenIssuer(String tokenIssuer) {
112 this.tokenIssuer = tokenIssuer;
116 private void handleEnvironmentVars() {
117 if (isEnvExpression(tokenIssuer)) {
118 this.tokenIssuer = getProperty(tokenIssuer, null);
120 if (isEnvExpression(tokenSecret)) {
121 this.tokenSecret = getProperty(tokenSecret, null);
123 if (isEnvExpression(host)) {
124 this.host = getProperty(host, null);
126 if (isEnvExpression(redirectUri)) {
127 this.redirectUri = getProperty(redirectUri, null);
129 if (isEnvExpression(supportOdlUsers)) {
130 this.supportOdlUsers = getProperty(supportOdlUsers, null);
135 private void handleDefaultValues() {
136 if (tokenIssuer == null || tokenIssuer.isEmpty()) {
137 this.tokenIssuer = DEFAULT_TOKENISSUER;
139 if (tokenSecret == null || tokenSecret.isEmpty()) {
140 this.tokenSecret = DEFAULT_TOKENSECRET;
142 if (redirectUri == null || redirectUri.isEmpty()) {
143 this.redirectUri = DEFAULT_REDIRECTURI;
145 if (supportOdlUsers == null || supportOdlUsers.isEmpty()) {
146 this.supportOdlUsers = DEFAULT_SUPPORTODLUSERS;
150 static boolean isEnvExpression(String key) {
151 return key != null && key.contains(ENVVARIABLE);
154 private static String generateSecret() {
155 int leftLimit = 48; // numeral '0'
156 int rightLimit = 122; // letter 'z'
157 int targetStringLength = 30;
158 Random random = new Random();
160 String generatedString = random.ints(leftLimit, rightLimit + 1)
161 .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)).limit(targetStringLength)
162 .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
163 return generatedString;
168 * @param key environment var
169 * @param defValue default value if no env var found
172 public static String getProperty(final String key, final String defValue) {
173 String value = defValue;
174 //try to read env var
175 boolean found = false;
176 if (isEnvExpression(key)) {
178 LOG.info("try to find env var(s) for {}", key);
179 final Matcher matcher = pattern.matcher(key);
180 String tmp = new String(key);
181 while (matcher.find() && matcher.groupCount() > 0) {
182 final String mkey = matcher.group(1);
185 LOG.info("match found for v={} and env key={}", key, mkey);
186 String envvar = mkey.substring(2, mkey.length() - 1);
187 String env = System.getenv(envvar);
188 tmp = tmp.replace(mkey, env == null ? "" : env);
189 if (env != null && env != "") {
192 } catch (SecurityException e) {
193 LOG.warn("unable to read env {}: {}", key, e);
204 public static boolean getPropertyBoolean(String key, boolean defaultValue) {
205 final String value = getProperty(key, String.valueOf(defaultValue));
206 return value.equals("true");
209 public static Config load(String filename) throws IOException {
210 CustomObjectMapper mapper = new CustomObjectMapper();
211 File file = new File(filename);
212 if (!file.exists()) {
213 throw new FileNotFoundException();
215 String content = String.join("", Files.readAllLines(file.toPath()));
216 Config cfg = mapper.readValue(content, Config.class);
217 cfg.handleEnvironmentVars();
218 cfg.handleDefaultValues();
224 public boolean doSupportOdlUsers() {
225 return "true".equals(this.supportOdlUsers);
230 public static Config getInstance() throws IOException {
231 if(_instance==null) {
232 _instance = load(DEFAULT_CONFIGFILENAME);