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 Random random;
48 private static Config _instance;
50 private List<OAuthProviderConfig> providers;
51 private String redirectUri;
52 private String supportOdlUsers;
53 private String tokenSecret;
54 private String tokenIssuer;
55 private String publicUrl;
59 public String toString() {
60 return "Config [providers=" + providers + ", redirectUri=" + redirectUri + ", supportOdlUsers="
61 + supportOdlUsers + ", tokenSecret=" + tokenSecret + ", tokenIssuer=" + tokenIssuer + "]";
66 public List<OAuthProviderConfig> getProviders() {
70 public void setProviders(List<OAuthProviderConfig> providers) {
71 this.providers = providers;
74 public String getRedirectUri() {
78 public void setRedirectUri(String redirectUri) {
79 this.redirectUri = redirectUri;
82 public String getSupportOdlUsers() {
83 return supportOdlUsers;
86 public void setSupportOdlUsers(String supportOdlUsers) {
87 this.supportOdlUsers = supportOdlUsers;
90 public String getTokenSecret() {
94 public void setTokenSecret(String tokenSecret) {
95 this.tokenSecret = tokenSecret;
98 public String getTokenIssuer() {
102 public void setTokenIssuer(String tokenIssuer) {
103 this.tokenIssuer = tokenIssuer;
107 public String getPublicUrl() {
111 public void setPublicUrl(String publicUrl) {
112 this.publicUrl = publicUrl;
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(publicUrl)) {
124 this.publicUrl = getProperty(publicUrl, null);
126 if (isEnvExpression(redirectUri)) {
127 this.redirectUri = getProperty(redirectUri, null);
129 if (isEnvExpression(supportOdlUsers)) {
130 this.supportOdlUsers = getProperty(supportOdlUsers, null);
132 if (this.providers != null && !this.providers.isEmpty()) {
133 for(OAuthProviderConfig cfg : this.providers) {
134 cfg.handleEnvironmentVars();
140 private void handleDefaultValues() {
141 if (tokenIssuer == null || tokenIssuer.isEmpty()) {
142 this.tokenIssuer = DEFAULT_TOKENISSUER;
144 if (tokenSecret == null || tokenSecret.isEmpty()) {
145 this.tokenSecret = DEFAULT_TOKENSECRET;
147 if (redirectUri == null || redirectUri.isEmpty() || "null".equals(redirectUri)) {
148 this.redirectUri = DEFAULT_REDIRECTURI;
150 if (publicUrl != null && (publicUrl.isEmpty() || "null".equals(publicUrl))) {
151 this.publicUrl = null;
153 if (supportOdlUsers == null || supportOdlUsers.isEmpty()) {
154 this.supportOdlUsers = DEFAULT_SUPPORTODLUSERS;
158 static boolean isEnvExpression(String key) {
159 return key != null && key.contains(ENVVARIABLE);
162 public static String generateSecret() {
163 return generateSecret(30);
166 public static String generateSecret(int targetStringLength) {
167 int leftLimit = 48; // numeral '0'
168 int rightLimit = 122; // letter 'z'
170 random = new Random();
172 String generatedString = random.ints(leftLimit, rightLimit + 1)
173 .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)).limit(targetStringLength)
174 .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
175 return generatedString;
180 * @param key environment var
181 * @param defValue default value if no env var found
184 public static String getProperty(final String key, final String defValue) {
185 String value = defValue;
186 //try to read env var
187 boolean found = false;
188 if (isEnvExpression(key)) {
190 LOG.info("try to find env var(s) for {}", key);
191 final Matcher matcher = pattern.matcher(key);
192 String tmp = new String(key);
193 while (matcher.find() && matcher.groupCount() > 0) {
194 final String mkey = matcher.group(1);
197 LOG.info("match found for v={} and env key={}", key, mkey);
198 String envvar = mkey.substring(2, mkey.length() - 1);
199 String env = System.getenv(envvar);
200 tmp = tmp.replace(mkey, env == null ? "" : env);
201 if (env != null && env.isEmpty()) {
204 } catch (SecurityException e) {
205 LOG.warn("unable to read env {}: {}", key, e);
216 public static boolean getPropertyBoolean(String key, boolean defaultValue) {
217 final String value = getProperty(key, String.valueOf(defaultValue));
218 return value.equals("true");
221 public static Config load(String filename) throws IOException {
222 CustomObjectMapper mapper = new CustomObjectMapper();
223 File file = new File(filename);
224 if (!file.exists()) {
225 throw new FileNotFoundException();
227 String content = String.join("", Files.readAllLines(file.toPath()));
228 Config cfg = mapper.readValue(content, Config.class);
229 cfg.handleEnvironmentVars();
230 cfg.handleDefaultValues();
236 public boolean doSupportOdlUsers() {
237 return "true".equals(this.supportOdlUsers);
241 public static Config getInstance() throws IOException {
242 return getInstance(DEFAULT_CONFIGFILENAME);
245 public static Config getInstance(String filename) throws IOException {
246 if (_instance == null) {
247 _instance = load(filename);