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;
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
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 getRedirectUri() {
79 public void setRedirectUri(String redirectUri) {
80 this.redirectUri = redirectUri;
83 public String getSupportOdlUsers() {
84 return supportOdlUsers;
87 public void setSupportOdlUsers(String supportOdlUsers) {
88 this.supportOdlUsers = supportOdlUsers;
91 public String getTokenSecret() {
95 public void setTokenSecret(String tokenSecret) {
96 this.tokenSecret = tokenSecret;
99 public String getTokenIssuer() {
103 public void setTokenIssuer(String tokenIssuer) {
104 this.tokenIssuer = tokenIssuer;
108 public String getPublicUrl() {
112 public void setPublicUrl(String publicUrl) {
113 this.publicUrl = publicUrl;
117 private void handleEnvironmentVars() {
118 if (isEnvExpression(tokenIssuer)) {
119 this.tokenIssuer = getProperty(tokenIssuer, null);
121 if (isEnvExpression(tokenSecret)) {
122 this.tokenSecret = getProperty(tokenSecret, null);
124 if (isEnvExpression(publicUrl)) {
125 this.publicUrl = getProperty(publicUrl, null);
127 if (isEnvExpression(redirectUri)) {
128 this.redirectUri = getProperty(redirectUri, null);
130 if (isEnvExpression(supportOdlUsers)) {
131 this.supportOdlUsers = getProperty(supportOdlUsers, null);
136 private void handleDefaultValues() {
137 if (tokenIssuer == null || tokenIssuer.isEmpty()) {
138 this.tokenIssuer = DEFAULT_TOKENISSUER;
140 if (tokenSecret == null || tokenSecret.isEmpty()) {
141 this.tokenSecret = DEFAULT_TOKENSECRET;
143 if (redirectUri == null || redirectUri.isEmpty() || "null".equals(redirectUri)) {
144 this.redirectUri = DEFAULT_REDIRECTURI;
146 if (publicUrl != null && (publicUrl.isEmpty() || "null".equals(publicUrl))) {
147 this.publicUrl = null;
149 if (supportOdlUsers == null || supportOdlUsers.isEmpty()) {
150 this.supportOdlUsers = DEFAULT_SUPPORTODLUSERS;
154 static boolean isEnvExpression(String key) {
155 return key != null && key.contains(ENVVARIABLE);
157 public static String generateSecret() {
158 return generateSecret(30);
160 public static String generateSecret(int targetStringLength) {
161 int leftLimit = 48; // numeral '0'
162 int rightLimit = 122; // letter 'z'
163 Random random = new Random();
165 String generatedString = random.ints(leftLimit, rightLimit + 1)
166 .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97)).limit(targetStringLength)
167 .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
168 return generatedString;
173 * @param key environment var
174 * @param defValue default value if no env var found
177 public static String getProperty(final String key, final String defValue) {
178 String value = defValue;
179 //try to read env var
180 boolean found = false;
181 if (isEnvExpression(key)) {
183 LOG.info("try to find env var(s) for {}", key);
184 final Matcher matcher = pattern.matcher(key);
185 String tmp = new String(key);
186 while (matcher.find() && matcher.groupCount() > 0) {
187 final String mkey = matcher.group(1);
190 LOG.info("match found for v={} and env key={}", key, mkey);
191 String envvar = mkey.substring(2, mkey.length() - 1);
192 String env = System.getenv(envvar);
193 tmp = tmp.replace(mkey, env == null ? "" : env);
194 if (env != null && env != "") {
197 } catch (SecurityException e) {
198 LOG.warn("unable to read env {}: {}", key, e);
209 public static boolean getPropertyBoolean(String key, boolean defaultValue) {
210 final String value = getProperty(key, String.valueOf(defaultValue));
211 return value.equals("true");
214 public static Config load(String filename) throws IOException {
215 CustomObjectMapper mapper = new CustomObjectMapper();
216 File file = new File(filename);
217 if (!file.exists()) {
218 throw new FileNotFoundException();
220 String content = String.join("", Files.readAllLines(file.toPath()));
221 Config cfg = mapper.readValue(content, Config.class);
222 cfg.handleEnvironmentVars();
223 cfg.handleDefaultValues();
229 public boolean doSupportOdlUsers() {
230 return "true".equals(this.supportOdlUsers);
234 public static Config getInstance() throws IOException {
235 return getInstance(DEFAULT_CONFIGFILENAME);
237 public static Config getInstance(String filename) throws IOException {
238 if(_instance==null) {
239 _instance = load(filename);