From 8a7ca5e3b1073273260cf3014f3dc8a155e64b9a Mon Sep 17 00:00:00 2001 From: sameerag Date: Tue, 5 Nov 2019 11:18:48 +0530 Subject: [PATCH] Add environment variable support for user-mgt configuration Fix https://github.com/wso2/product-ei/issues/4724 --- .../org/wso2/carbon/base/CarbonBaseUtils.java | 28 +++++++++++++++++++ .../core/config/RealmConfigXMLProcessor.java | 4 +++ .../carbon/user/core/util/DatabaseUtil.java | 3 ++ 3 files changed, 35 insertions(+) diff --git a/core/org.wso2.carbon.base/src/main/java/org/wso2/carbon/base/CarbonBaseUtils.java b/core/org.wso2.carbon.base/src/main/java/org/wso2/carbon/base/CarbonBaseUtils.java index 1d33a24eb95..0c5e4ec6ef0 100644 --- a/core/org.wso2.carbon.base/src/main/java/org/wso2/carbon/base/CarbonBaseUtils.java +++ b/core/org.wso2.carbon.base/src/main/java/org/wso2/carbon/base/CarbonBaseUtils.java @@ -126,4 +126,32 @@ public static String getCarbonHome() { } return carbonHome; } + + public static String replaceSystemProperty(String text) { + int indexOfStartingChars = -1; + int indexOfClosingBrace; + + // The following condition deals with properties. + // Properties are specified as ${system.property}, + // and are assumed to be System properties + while (indexOfStartingChars < text.indexOf("${") + && (indexOfStartingChars = text.indexOf("${")) != -1 + && (indexOfClosingBrace = text.indexOf('}')) != -1) { + String sysProp = text.substring(indexOfStartingChars + 2, + indexOfClosingBrace); + String propValue = System.getProperty(sysProp); + if (propValue == null) { + propValue = System.getenv(sysProp); + } + if (propValue != null) { + text = text.substring(0, indexOfStartingChars) + propValue + + text.substring(indexOfClosingBrace + 1); + } + if (sysProp.equals("carbon.home") && propValue != null + && propValue.equals(".")) { + text = new File(".").getAbsolutePath() + File.separator + text; + } + } + return text; + } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/config/RealmConfigXMLProcessor.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/config/RealmConfigXMLProcessor.java index 1a79c725076..be515fe8bb8 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/config/RealmConfigXMLProcessor.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/config/RealmConfigXMLProcessor.java @@ -27,6 +27,7 @@ import org.osgi.framework.BundleContext; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonException; +import org.wso2.carbon.base.CarbonBaseUtils; import org.wso2.carbon.user.api.RealmConfiguration; import org.wso2.carbon.user.core.UserCoreConstants; import org.wso2.carbon.user.core.UserStoreException; @@ -181,6 +182,9 @@ private static void addPropertyElements(OMFactory factory, OMElement parent, Str Map.Entry entry = ite.next(); String name = entry.getKey(); String value = entry.getValue(); + if (value != null) { + value = CarbonBaseUtils.replaceSystemProperty(value); + } OMElement propElem = factory.createOMElement(new QName( UserCoreConstants.RealmConfig.LOCAL_NAME_PROPERTY)); OMAttribute propAttr = factory.createOMAttribute( diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/DatabaseUtil.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/DatabaseUtil.java index 221723b28fd..50ceedac12e 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/DatabaseUtil.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/util/DatabaseUtil.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.PoolProperties; +import org.wso2.carbon.base.CarbonBaseUtils; import org.wso2.carbon.user.api.RealmConfiguration; import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.jdbc.JDBCRealmConstants; @@ -95,6 +96,7 @@ public static DataSource createUserStoreDataSource(RealmConfiguration realmConfi String dataSourceName = realmConfig.getUserStoreProperty(JDBCRealmConstants.DATASOURCE); if (dataSourceName != null) { + dataSourceName = CarbonBaseUtils.replaceSystemProperty(dataSourceName); return lookupDataSource(dataSourceName); } @@ -330,6 +332,7 @@ private static DataSource createRealmDataSource(RealmConfiguration realmConfig) String dataSourceName = realmConfig.getRealmProperty(JDBCRealmConstants.DATASOURCE); if (dataSourceName != null) { + dataSourceName = CarbonBaseUtils.replaceSystemProperty(dataSourceName); return lookupDataSource(dataSourceName); }