How to externalise application properties for Mule Application?

This article is on loading properties based on the environment in mule applications. Let's say we have a Database connector with properties like host, database name, port. Now, these properties are environment-specific which means we have different databases for each environment like DEV, TEST, UAT, PROD. Also, if we are using the same database properties at three more places maybe in other flows we have to update the same properties again. To avoid updating the same properties repeatedly and to use the environment-specific properties without manually changing all the configuration properties we need to externalize application properties and also secure them.

1) Configuring application properties to a .yaml file

1) Create a Mule flow and add a Database connector.

2) Update Database connector configuration for Oracle/MySQL as given below










3) Instead of setting multiple environment variables, we can store all the properties to a .yaml file.

Create dev-properties.yaml (to store DEV environment variables) as given below



4) Similarly create prod-properties.yaml (to store PROD environment variables) with different values

5) Click on "Global Elements" and Click on Create button. Type Property  and select "Configuration properties" as shown below :

6) Configure Configuration Properties  as shown below :



7) Deploy the application and test by giving a request to http://localhost:8082/db?empname=Uday

8) Now modify the property placeholder as below :



You can pass the value of mule_env as Java environment variables while running the mule application.

Right-click on your Project ->Run as -> Run Configurations. Now set the argument -Dmyenv as below and Run


2) Encrypting application properties to externalize


1) By default Secure Properties module is not available. We need to add “Secure Properties” module now. Click on the “Search in exchange” button in the Mule palette on the right side. Then click on the “Add Module” button.

2) To add a module from Anypoint Exchange, we need an account in the Anypoint module.

  • Go to https://anypoint.mulesoft.com  in browser and sign up for an account.

  • Open Anypoint studio and click on the “Please log in” link and give your credentials.

  • Then search for “Secure Properties” and Select “Mule Secure Properties” module.


3) Now, Select the Global elements tab in your XML and click on “Create”. Then select “Secure properties Config” as shown below :

Now Configure Secure Properties Config with below details

  • File: prod-properties.yaml

  • Key: some random key

  • Algorithm: Select an algorithm

  • Mode: Select applicable Mode


Now we have to generate db-secure-dev.yaml file with encrypted property values

I have given a jar file with the name secure-properties-tool.jar.  This jar can also be downloaded from the following documentation page of mulesoft

https://docs.mulesoft.com/mule-runtime/4.1/secure-configuration-properties

We can use the following command :
java -jar secure-properties-tool.jar file <encrypt|decrypt> <algorithm> <mode> <key> <input file> <output file>

java -jar secure-properties-tool.jar file encrypt Blowfish CBC abcdefghijklmnop db-dev.yaml db-secure-dev.yaml

Now, copy the generated db-secure-devproperties.yaml into src/main/resources.

Now, edit the Database Config as shown below :

Run the application and give a request to http://localhost:8082/db. Make sure that the environment properties file is fetched correctly and there are no exceptions seen.


Post a Comment

0 Comments