Reading mule config from database - mule

Is it possible to read mule configuration file path (file endpoints), smtp host /user/password (smtp endpoints) from database.We finally want to provide a User Interface , where the user can edit the properties through the screen.The normal properties file approach (key/Value) pairs was used earlier but needs to change to read these properties from the database.Any help on this will be greatly appreciated.

Yes, you can use a custom properties provider.
Its configuration would look like this:
<spring:bean class="org.mule.DatabasePropertiesProvider" id="DatabasePropertiesProvider"/>
<spring:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<spring:property name="properties">
<spring:bean factory-bean="DatabasePropertiesProvider" factory-method="getProperties" />
</spring:property>
</spring:bean>
And the code for DatabasePropertiesProvider is as simple as this:
public class DatabasePropertiesProvider {
public Properties getProperties() throws Exception {
Properties properties = new Properties();
// get properties from the database
return properties;
}
}

Related

CXF with Camel - HTTPS

I am trying to implement a module to send messages from a CXF client to a server (SOAP endpoint) using HTTPS. I am able to achieve this by following the guide here: https://camel.apache.org/how-to-switch-the-cxf-consumer-between-http-and-https-without-touching-the-spring-configuration.html
The following configuration is key:
<ctx:property-placeholder location="classpath:orderEntry.cfg" />
<!-- other properties -->
<http:conduit name="{http://www.company.com/product/orderEntry/service/1}OrderEntry.http-conduit">
<http:tlsClientParameters disableCNCheck="true">
<sec:trustManagers>
<sec:keyStore type="JKS" password="${trustStore.password}" file="${trustStore.file}"/>
</sec:trustManagers>
<!-- other config -->
</http:tlsClientParameters>
</http:conduit>
The above configuration refers to a config file that has these properties stored:
orderEntry.cfg
--------------
endpointUri=https://localhost:8181/OrderEntry
trustStore.password=password
trustStore.file=etc/myApp.ts
As noted earlier, I am able to send messages via https when I follow the guide.
But I am concerned about the password being stored in plain text here. Is there a way that I can have the password wired from Java code (which can probably read the password from an encrypted source) and provide it to the http conduit when it needs it?
Have you tried location attribute value with file prefix?
E.g. location="file:/my/custom/location/orderEntry.cfg"
See: https://stackoverflow.com/a/17303537
Update:
If it works with your custom bean, you can try create trust managers as a bean and inject it into the conduit configuration like bellow:
blueprint.xml
<bean id="serviceTrustManager"
class="my.app.security.KeyStores" factory-method="loadTrustManagers">
<argument index="0" value="${my.app.service.trustStorePath}"/>
<argument index="1" value="${my.app.service.trustStoreEncryptedPassword}"/>
</bean>
<http:conduit name="{http://www.company.com/product/orderEntry/service/1}OrderEntry.http-conduit">
<http:tlsClientParameters disableCNCheck="true">
<sec:trustManagers ref="serviceTrustManager"/>
</http:tlsClientParameters>
</http:conduit>
Java code:
public class KeyStores {
public static TrustManager[] loadTrustManagers(String trustStorePath, String trustStoreEncryptedPassword) {
String trustStoreDecryptedPassword = PasswordDescriptor.decryptPassword(trustStoreEncryptedPassword); //Password decryption logic here
KeyStore trustStore = KeyStores.loadKeyStore("JKS", trustStorePath, trustStoreDecryptedPassword); //IO logic here
TrustManagerFactory trustFactory;
try {
trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustFactory.init(trustStore);
} catch (NoSuchAlgorithmException | KeyStoreException ex) {
throw new IllegalStateException(ex);
}
return trustFactory.getTrustManagers();
}
}

Read properties from database and use it instead of mule-app.properties

I have been able to read the properties from a table in the database as it was described here Reading mule config from database
Now, I am not able to apply these properties to the flow configs and also access them as out bound properties in the Java Processor classes through the MuleEventContext's message.
Update: below is my flow XML code
<flow name="push-data">
<poll doc:name="Push Poll">
<fixed-frequency-scheduler frequency="${push.data.poll.frequency}" timeUnit="MINUTES" />
<set-property propertyName="tempFilePath" value="${temp.csv.file.path}" doc:name="Property"/>
<component class="com.reports.processors.PushDataProcessor" doc:name="PushDataProcessor"/>
<logger message="worked!!!" level="INFO" doc:name="Logger"/>
<exception-strategy ref="push-report-data_Catch_Exception_Strategy" doc:name="Reference Exception Strategy"/>
</flow>
I am trying to set the properties "push.data.poll.frequency" and "temp.csv.file.path". Earlier, these properties existed in the "mule-app.properties" file.
So, My question is, How do I set the properties loaded from the database to the flow. Please keep in mind that I have already loaded the properties from the database as described in the link above. I just want to be able to set these properties to the flow rather than taking them from the mule-app.properties.
EDIT: To add some more information,
I am using a class with #Configuration annotation. The class as described in the link above, loads the properties from the database. Below is the source code.
#Configuration(name="databasePropertiesProvider")
#Component
public class DatabasePropertiesProvider {
#Autowired(required=true)
private MyService myService;
#Bean
public Properties getProperties() throws Exception {
Properties properties = new Properties();
// get properties from the database
Map<String,String> propertiesMap = myService.getMuleAppPropertiesFromDB();
if(null != propertiesMap && !CollectionUtils.isEmpty(propertiesMap))
properties.putAll(propertiesMap);
return properties;
}
#Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}}
But this class runs after the app is initialized. Previously, I had configured the PropertySourcesPlaceholderConfigurer in the xml config with the factory-bean as the DatabasePropertiesProvider class. But since DatabasePropertiesProvider has a dependency on MyService class, and the dependency was not getting resolved due to MyService bean not initializing in the container before the property config, I had to make some changes to DatabasePropertiesProvider(the version above) so that this runs after the app initialization.
But now, the problem is that I am unable to access those properties that are loaded from the database.
UPDATE 2: I found a solution. Apparently I was trying to autowire the #Service MyService in the databasePropertiesProvider class. The autowiring was failing with null due to which I made some more modifications to the databasePropertiesProvider class so that It runs after the app is initialized.
Now when I look at it, I realized that I dont need to connect to the database through all the service and repository layers. I moved the query execution code from the repository class to the databasePropertiesProvider class and now the properties are loaded during initialization time and the flows can get the properties without making any changes.
Thanks for all your help guys. Made me do a lot of thinking.
Regards,
Zulfiqar
I found a solution. Apparently I was trying to autowire the #Service MyService in the databasePropertiesProvider class. The autowiring was failing with null due to which I made some more modifications to the databasePropertiesProvider class so that It runs after the app is initialized.
Now when I look at it, I realized that I dont need to connect to the database through all the service and repository layers. I moved the query execution code from the repository class to the databasePropertiesProvider class and now the properties are loaded during initialization time and the flows can get the properties without making any changes.
The whole code looks like this
XML Config:-
<bean class="net.intigral.reports.provider.properties.DatabasePropertiesProvider" id="databasePropertiesProvider">
<property name="entityManager" ref="entityManager" />
</bean>
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties">
<bean factory-bean="databasePropertiesProvider" factory-method="getProperties" />
</property>
</bean>
Java Code:-
public class DatabasePropertiesProvider {
EntityManager entityManager;
public Properties getProperties() throws Exception {
Properties properties = new Properties();
// get properties from the database
Map<String,String> propertiesMap = getMuleAppPropertiesFromDB();
if(null != propertiesMap && !CollectionUtilsIntg.isEmpty(propertiesMap))
properties.putAll(propertiesMap);
return properties;
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
#SuppressWarnings("unchecked")
private Map<String,String> getMuleAppPropertiesFromDB() {
Map<String,String> collect = null;
String query = "select key, value from MuleAppProps muleAppProps";
List<Object[]> results = entityManager.createQuery(query).getResultList();
if (CollectionUtilsIntg.isNotEmpty(results)) {
collect = results.stream().collect(Collectors.toMap(o -> (String)o[0], o -> (String)o[1]));
}
return collect;
}}
Now, I am able to load the properties the same way I used to load from mule-app.properties in the FLOWs.
Let your db contains following properties values with key/value pair as below :-
A simple example like below you can refer to read values from Database:-
<spring:beans>
<spring:bean id="dataSource" name="myCon" class="org.enhydra.jdbc.standard.StandardDataSource">
<spring:property name="url" value="jdbc:sqlserver://YourIpAddress\\SQLEXPRESS:1433;databaseName=YourDB;user=sa;password=yourDBPassword" />
<spring:property name="driverName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
</spring:bean>
<!-- Required to connect to datasource -->
<spring:bean name="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<spring:property name="properties" ref="CommonsConfigurationFactoryBean" />
</spring:bean>
<spring:bean name="CommonsConfigurationFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<spring:constructor-arg ref="DatabaseConfiguration" />
</spring:bean>
<spring:bean name="DatabaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<spring:constructor-arg type="javax.sql.DataSource" ref="dataSource" />
<spring:constructor-arg index="1" value="YourTableName" />
<spring:constructor-arg index="2" value="Key" />
<spring:constructor-arg index="3" value="Value" />
</spring:bean>
</spring:beans>
<db:generic-config name="Database_Configuration" dataSource-ref="dataSource" doc:name="Generic Database Configuration" />
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<flow name="firstflow" processingStrategy="synchronous">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" />
<set-payload value="File name ${file.name} File path ${file.path}" doc:name="Set Payload" />
</flow>
You need to add commons-configuration.jar, spring.jar and spring-modules-jakarta-commons.jar in your classpath
If you want to access properties values in Java class you can inject it using Spring property in init-method of Spring bean.
refer:- http://www.codeproject.com/Articles/28893/Loading-Application-Properties-from-a-Database

jasypt property placeholder not working

I have this properties file:
secret.key = ENC(foobar)
region = ABC
Then in the config.xml:
<spring:beans>
<encryption:encryptor-config id="eConf" password-sys-property-name="MULE_ENCRYPTION_PASSWORD" algorithm="PBEWithMD5AndDES" password="" />
<encryption:string-encryptor id="stringEnc" config-bean="eConf" />
<encryption:encryptable-property-placeholder encryptor="stringEnc" location="${env}.properties" />
</spring:beans>
But the property placeholders don't work, for example:
<sqs:config secretKey="${secret.key}" region="${region}"></sqs-config>
Does anyone know why?
Encrypted password needs to be write within ENC() function and should be encrypted.
Let's consider in properties file where password value is Login#123... Now the encrypted value in properties file will be :-
password=ENC(B0u7D8wLwq/ugin31KNpP78gBcLP7VIN)
Step1 :- We can generate Key using following commands in Command prompt of \jasypt-1.9.2\bin directory :- encrypt input="Login#123" password=sqlpassword algorithm=PBEWithMD5AndDES
Step2 :- In runtime Environment we need to give (Right click->Run As->Run Configuration->Environment) :- Variable :- MULE_ENCRYPTION_PASSWORD and Value:-sqlpassword
In your Mule config, you need configure it as following :-
<spring:beans>
<spring:bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<spring:property name="algorithm" value="PBEWithMD5AndDES"/>
<spring:property name="passwordEnvName" value="MULE_ENCRYPTION_PASSWORD"/>
</spring:bean>
<!-- The will be the encryptor used for decrypting configuration values. -->
<spring:bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<spring:property name="config" ref="environmentVariablesConfiguration"/>
</spring:bean>
<!-- The EncryptablePropertyPlaceholderConfigurer will read the -->
<!-- .properties files and make their values accessible as ${var} -->
<!-- Our "configurationEncryptor" bean (which implements -->
<!-- org.jasypt.encryption.StringEncryptor) is set as a constructor arg. -->
<spring:bean id="propertyConfigurer" class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
<spring:constructor-arg ref="configurationEncryptor"/>
<spring:property name="locations">
<spring:list>
<spring:value>conf/yourPropertyFile.properties</spring:value>
</spring:list>
</spring:property>
</spring:bean>
Then you can use encrypted values like :- ${password}
Reference :- http://blogs.mulesoft.org/encrypting-passwords-in-mule/
and http://pragmaticintegrator.wordpress.com/2014/03/09/using-encrypted-passwords-with-mule-esb/
and https://code.google.com/p/soi-toolkit/issues/detail?id=183
and http://soi-toolkit.googlecode.com/svn-history/r2022/wiki/UG_PropertyFile.wiki
I had similar issue, i did configure everything as explained in Activemq web site . In my case issue was PropertyPlaceholderConfigurer bean was loaded for to load other properties before the EncryptablePropertyPlaceholderConfigurer bean. So remove PropertyPlaceholderConfigurer bean's if any just add EncryptablePropertyPlaceholderConfigurer bean even for other non-encrypted properties as explained in Activemq then it works fine.

Why is my WCF Data Services method not appearing in the OData collections list?

When I view the root of my WCF Data Services service (http://localhost/MyService.svc/) in a browser I see this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<service xml:base="http://localhost/MyService.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
I would expect to see a list of collections.
When I go to the $metadata URL I see this:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="MyApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<ComplexType Name="Package">
<Property Name="Id" Type="Edm.String" Nullable="true" />
</ComplexType>
</Schema>
<Schema Namespace="MyApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<EntityContainer Name="PackageService" m:IsDefaultEntityContainer="true">
<FunctionImport Name="GetQueryablePackages" ReturnType="Collection(MyApp.Package)" m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Why might my GetQueryablePackages collection not be appearing?
I'm using these access settings:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Service operations (the function import in the EDM) is not exposed in the service document. Only entity sets are exposed there.
If you want your data to be exposed in the service document make an entity set out of it. Depending on the provider model this differs. Typically it means exposing a property of type IQueryable on your context class. Note that T has to be an entity type (must have a key).
Can you share the context definition where you have defined the IQueryable <> properties. There are 2 things that come to my mind: First the properties must be of type IQueryable<> or some type that derives from it. Second, the element type refered by the IQueryable<> must be an entity type i.e. they must have key properties declared in them.
Hope this helps.
Thanks
Pratik
Or you can create an extension method like this:
public static class TestEntitiesExtensions
{
public static IEnumerable<Package> GetQueryablePackages(this TestEntities context)
{
var uri = new Uri(context.BaseUri, "GetQueryablePackages");
return context.Execute<Package>(uri);
}
}

Unity unable to correctly resolve WCF InstanceContext implementation

I'm attempting to set up the client for a duplex WCF service via Unity 2.0. To do so, I want to insert an implementation of my CallbackContract - IUpdateClient - into an InstanceContext, which is then inserted into my service proxy, in this case a subclass of DuplexClientBase<IUpdateService> called UpdateProxy.
The problem I encounter is, when attempting to use the Proxy as stored in my Unity container to subscribe the client to updates from the service, I receive the following exception:
The InstanceContext provided to the
ChannelFactory contains a UserObject
that does not implement the
CallbackContractType
'..Services..ServiceContracts.IUpdateClient'.
I am accessing the proxy like so:
_container.Resolve<IUpdateService>("updateServiceImpl").Subscribe();
Given my Unity config:
<!-- Interface to implementation mappings -->
<register type="RepositoryInterface" mapTo="Repository" name="repositoryImpl">
<constructor>
<param name="proxy" dependencyName="proxyImpl"/>
</constructor>
</register>
<!-- Here's the bit that doesn't seem to be resolving as expected -->
<register type="UpdateClientInterface" mapTo="UpdateClient" name="updateClientImpl">
<lifetime type="singleton"/>
<constructor>
<param name="repository" dependencyName="repositoryImpl"/>
</constructor>
</register>
<register type="System.ServiceModel.InstanceContext, System.ServiceModel,
Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="instanceContext">
<constructor>
<param name="implementation" dependencyName="updateClientImpl"/>
</constructor>
</register>
<!-- This is the type I'm resolving with the above _container.Resolve() statement -->
<register type="UpdateServiceInterface" mapTo="UpdateService" name="updateServiceImpl">
<constructor>
<param name="callbackInstance" dependencyName="instanceContext"/>
</constructor>
</register>
<register type="ProxyInterface" mapTo="Proxy" name="proxyImpl">
<constructor>
<param name="configurationName">
<value value="ServiceEndpointFromAppConfig"/>
</param>
</constructor>
</register>
I would expect that when I resolve the UpdateService class, seen here:
public class UpdateProxy : DuplexClientBase<IUpdateService>, IUpdateService
{
public UpdateProxy(InstanceContext callbackInstance)
: base(callbackInstance) {}
public void Subscribe() {}
[...]
}
That the Unity container instantiates an InstanceContext (registered as "instanceContext" in config) and, when doing that, it must instantiate the type registered as "updateClientImpl" - which does, in fact, implement IUpdateClient - and pass that into the InstanceContext's constructor as its implementation parameter.
Nonetheless, I receive the error as above.
In Summary (aka "the tl;dr version"): When the Unity container resolves an InstanceContext, it doesn't seem to create its implementation correctly. I don't know if this is an error in configuration, or if I'm fundamentally misunderstanding how the Unity container resolves a set of dependent types. Any guidance on this would be helpful.
The issue you're running into is because you registered the InstanceContext with a name. However, the UpdateProxy type isn't registered at all. So what's happening is the container will attempt to resolve InstanceContext using the default, unnamed registration.
However, since there is no default registration, the defaults kick in and it looks like it is choosing a different constructor.
The fix would be to either register UpdateProxy and set that registration to use the named registration for InstanceContext or to remove the name from the registration for InstanceContext.