Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config resolver #2226

Merged
merged 24 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0b098f5
Add config resolver
kalaiyarasiganeshalingam Oct 28, 2024
9a6747d
Format the code
kalaiyarasiganeshalingam Oct 28, 2024
f31d090
Add cert deployment
kalaiyarasiganeshalingam Oct 29, 2024
3675ade
Add license
kalaiyarasiganeshalingam Oct 29, 2024
e70e8d8
Change class name
kalaiyarasiganeshalingam Oct 30, 2024
29405ab
Remove config from variable xpath
kalaiyarasiganeshalingam Nov 5, 2024
19c9e46
Clean the code
kalaiyarasiganeshalingam Nov 5, 2024
74dbf1c
Merge branch 'wso2:master' into master
kalaiyarasiganeshalingam Nov 7, 2024
00c0819
Create workflow.yml
kalaiyarasiganeshalingam Nov 7, 2024
5f76f7c
Update workflow.yml
kalaiyarasiganeshalingam Nov 7, 2024
c19614e
Update workflow.yml
kalaiyarasiganeshalingam Nov 7, 2024
22e69be
Create java21.yml
kalaiyarasiganeshalingam Nov 7, 2024
1deefb3
Create java_21.yml
kalaiyarasiganeshalingam Nov 7, 2024
f1b85ff
Update java21.yml
kalaiyarasiganeshalingam Nov 7, 2024
beb2479
Remove workflow
kalaiyarasiganeshalingam Nov 11, 2024
cc06c27
Merge branch 'master' of https://github.com/wso2/wso2-synapse
kalaiyarasiganeshalingam Nov 18, 2024
4d2e316
Update config expression
kalaiyarasiganeshalingam Nov 19, 2024
b4c66a9
Remove unused import
kalaiyarasiganeshalingam Nov 19, 2024
ecf3a38
Remove code duplication
kalaiyarasiganeshalingam Nov 19, 2024
0b8b623
Merge branch 'master' of https://github.com/kalaiyarasiganeshalingam/…
kalaiyarasiganeshalingam Nov 19, 2024
bd08c02
Remove extra line
kalaiyarasiganeshalingam Nov 19, 2024
4b0a8fb
Fix reviewed comments
kalaiyarasiganeshalingam Nov 19, 2024
d6a4e66
Improve error msg
kalaiyarasiganeshalingam Nov 19, 2024
890bc23
Change the exception
kalaiyarasiganeshalingam Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.commons.property;

import java.util.concurrent.ConcurrentHashMap;

/**
* Property Holder is the class used to store the properties in the map
*/
public class PropertyHolder {

private ConcurrentHashMap<String, String> properties;

private static class PropertyLoaderHelper {
private static final PropertyHolder INSTANCE = new PropertyHolder();
}

private PropertyHolder() {}

public static PropertyHolder getInstance() {
return PropertyLoaderHelper.INSTANCE;
}

public void setProperty(String key, String value) {
getProperties().put(key, value);
}

public String getPropertyValue(String key) {
return getProperties().get(key);
}

public Boolean hasKey(String key) {
return getProperties().containsKey(key);
}

public ConcurrentHashMap<String, String> getProperties() {
if (properties == null) {
this.properties = new ConcurrentHashMap<>();
}
return this.properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.commons.resolvers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.commons.property.PropertyHolder;

/**
* Config Resolver can be used to resolve configurable property variables in the synapse config.
*/
public class ConfigResolver implements Resolver {

private static final Log LOG = LogFactory.getLog(FilePropertyResolver.class);

private String input;

@Override
public void setVariable(String input) {
this.input = input;
}

@Override
public String resolve() {
String propertyValue = PropertyHolder.getInstance().getPropertyValue(this.input);
if (propertyValue == null) {
throw new ResolverException("Parameter key: " + input + " could not be found");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Resolving parameter key: "+ input + " value: " + propertyValue);
}
return propertyValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class ResolverFactory {

private static final Log LOG = LogFactory.getLog(ResolverFactory.class);
private static final int RESOLVER_INDEX = 2;
private static ResolverFactory resolverFactory = new ResolverFactory();
private static final ResolverFactory resolverFactory = new ResolverFactory();
private final Pattern rePattern = Pattern.compile("(\\$)([_a-zA-Z0-9]+):([_a-zA-Z0-9]+)");
private static final String SYSTEM_VARIABLE_PREFIX = "$SYSTEM";
private static final String FILE_PROPERTY_VARIABLE_PREFIX = "$FILE";
private static final String CUSTOM_PROPERTY_VARIABLE_PREFIX = "$CUSTOM_";
private static final String CONFIGURABLE_VARIABLE_PREFIX = "$config:";

private Map<String, Class<? extends Resolver>> resolverMap = new HashMap<>();
private final Map<String, Class<? extends Resolver>> resolverMap = new HashMap<>();

/**
* This function return an object of Resolver factory
Expand All @@ -61,58 +62,25 @@ public Resolver getResolver(String input) {
if (input == null) {
return null;
}

if (input.startsWith(SYSTEM_VARIABLE_PREFIX)) {
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
Matcher matcher = rePattern.matcher(input);
if (input.startsWith(CONFIGURABLE_VARIABLE_PREFIX)) {
if (matcher.find()) {
Class<? extends Resolver> resolverClass = resolverMap.get(matcher.group(RESOLVER_INDEX).toLowerCase());
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be found");
}
} else {
throw new ResolverException("Resolver could not be found");
}
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(FILE_PROPERTY_VARIABLE_PREFIX)){
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
if (matcher.find()){
Class<? extends Resolver> resolverClass = resolverMap.get(matcher.group(RESOLVER_INDEX).toLowerCase());
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
}
} else if (input.startsWith(SYSTEM_VARIABLE_PREFIX)) {
if (matcher.find()) {
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(FILE_PROPERTY_VARIABLE_PREFIX)) {
if (matcher.find()) {
return getResolver(matcher.group(RESOLVER_INDEX).toLowerCase(), matcher);
}
} else if(input.startsWith(CUSTOM_PROPERTY_VARIABLE_PREFIX)) {
Matcher matcher = rePattern.matcher(input);
Resolver resolverObject = null;
if (matcher.find()){
String nameWithPlaceholder = matcher.group(RESOLVER_INDEX).toLowerCase();
String className = nameWithPlaceholder.substring(CUSTOM_PROPERTY_VARIABLE_PREFIX.length() - 1);
Class<? extends Resolver> resolverClass = resolverMap.get(className);
if (resolverClass != null) {
try {
resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
if (matcher.find()) {
return getResolver(className, matcher);
}
}
}
Expand All @@ -126,6 +94,7 @@ public Resolver getResolver(String input) {
private void registerResolvers() {
resolverMap.put("system", SystemResolver.class);
resolverMap.put("file", FilePropertyResolver.class);
resolverMap.put("config", ConfigResolver.class);
}

private void registerExterns() {
Expand All @@ -147,4 +116,19 @@ private void registerExterns() {
}
}
}

private Resolver getResolver(String className, Matcher matcher) {
Class<? extends Resolver> resolverClass = resolverMap.get(className);
if (resolverClass != null) {
try {
Resolver resolverObject = resolverClass.newInstance();
resolverObject.setVariable(matcher.group(3));
return resolverObject;
} catch (IllegalAccessException | InstantiationException e) {
throw new ResolverException("Resolver could not be initialized", e);
}
} else {
throw new ResolverException("Resolver could not be found");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import org.apache.synapse.commons.SynapseCommonsException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -101,7 +102,7 @@ private void loadPropertiesFile() throws SynapseCommonsException {
File file = new File(propertiesFilePath);
if (file.exists()) {
if (file.lastModified() > lastModifiedTimestamp) {
try (InputStream in = new FileInputStream(propertiesFilePath)) {
try (InputStream in = Files.newInputStream(Paths.get(propertiesFilePath))) {
Properties rawProps = new Properties();
Map<String, String> tempPropertyMap = new HashMap<>();
rawProps.load(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public int compare(String o1, String o2) {
org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
headersMap);
}
}else if(XMLConfigConstants.SCOPE_OPERATION.equals(scope)
} else if(XMLConfigConstants.SCOPE_OPERATION.equals(scope)
&& synCtx instanceof Axis2MessageContext){
//Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
Expand Down Expand Up @@ -504,7 +504,7 @@ private String getMatchedValue(String value, SynapseLog synLog) {
matchedValue = matcher.group(group);
} else {
if (synLog.isTraceOrDebugEnabled()) {
String msg = "Failed to get a match for regx : " +
String msg = "Failed to get a match for regex : " +
pattern.toString() + " with the property value :" +
value + " for group :" + group;
synLog.traceOrDebug(msg);
Expand All @@ -515,7 +515,7 @@ private String getMatchedValue(String value, SynapseLog synLog) {

} else {
if (synLog.isTraceOrDebugEnabled()) {
String msg = "Unable to find a match for regx : " +
String msg = "Unable to find a match for regex : " +
pattern.toString() + " with the property value :" + value;
synLog.traceOrDebug(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.synapse.util.synapse.expression.ast;

import org.apache.synapse.SynapseConstants;
import org.apache.synapse.commons.property.PropertyHolder;
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;

Expand Down Expand Up @@ -60,6 +61,12 @@ public ExpressionResult evaluate(EvaluationContext context) {
Object value;
if (Type.HEADER.equals(type)) {
value = context.getHeader(name);
} else if (Type.CONFIG.equals(type)) {
try {
value = PropertyHolder.getInstance().getPropertyValue(name);
} catch (Exception e) {
kalaiyarasiganeshalingam marked this conversation as resolved.
Show resolved Hide resolved
throw new EvaluationException("The value of the key:[" + name + "] is null");
}
} else {
if (SynapseConstants.URI_PARAM.equals(scope)) {
value = context.getProperty("uri.var." + name, SynapseConstants.SYNAPSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ private SSLContext createSSLContext(OMElement keyStoreElt, OMElement trustStoreE
if (log.isDebugEnabled()) {
log.debug(name + " Loading Trust Keystore from : " + location);
}

SslSenderTrustStoreHolder.getInstance().setLocation(location);
SslSenderTrustStoreHolder.getInstance().setPassword(passwordElement.getText());
SslSenderTrustStoreHolder.getInstance().setType(type);
trustStore.load(fis, storePassword.toCharArray());
TrustManagerFactory trustManagerfactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.transport.nhttp.config;

/**
* The SSL Sender TrustStore Holder class to store the client trust store's configurable details.
*/
public class SslSenderTrustStoreHolder {

private static volatile SslSenderTrustStoreHolder instance;

private SslSenderTrustStoreHolder() {}

private String location;
private String password;
private String type;

public static SslSenderTrustStoreHolder getInstance() {

if (instance == null) {
synchronized (TrustStoreHolder.class) {
if (instance == null) {
instance = new SslSenderTrustStoreHolder();
}
}
}
return instance;
}

public void setLocation(String location) {
this.location = location;
}

public String getLocation() {
return this.location;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return this.password;
}

public void setType(String type) {
this.type = type;
}

public String getType() {
return this.type;
}
}
Loading