Every development project needs a way to have certain values change when an application is moved from environment to environment. Many teams opt for environment variables on the server. I’m not a fan of these, because they are “hidden”. Only a server admin can see that they exist, and what their values are. MuleSoft, Mule 4 has a method for using Secure Properties in your apps, you may want to consider using these.
The main advantage of Secure Properties is that they are easily maintained in the Runtime Manager. This means that they are easy to administrate by whomever has rights to deploy applications. Hence, the values for each environment can be part of your roll-out documentation. If the property values are sensitive, i.e. passwords, then your documentation can state that the values are to be retrieved by other means, but still show where they should be set.
Alright then, enough with the back story, let’s get started!
First thing we’ll need to do is to setup a YAML file that has the properties we want to use in our app. We can easily add to this list over time as needs arise. For this example, I’m going to call the file secure-config.yaml, but feel free to name it as you see fit.
Let’s get started
- Right click on your Mule project
- Select “New” -> “File”
- Navigate to your project and into the src\main\resources folder
- Enter the file name you’d like with a .yaml extension
- Click “Finish”
Open your new file and add your new properties, you can select to create them in a hierarchy, or not. As always, I recommend organizing your thoughts, as you don’t know how many properties you may eventually have. It could get difficult to find the one you want in the future if there are dozens.
Once this is saved, you need to declare the properties you’re using that you wish to set in the Runtime Manager. In order to do this, you’ll need to edit your mule-artifact.json file in the root of your project. We’ll be adding a new line to declare the properties:
Make sure to add a comma before your new line to keep the integrity of the properties in this file. Notice how the property that I had created in the config-secure.yaml file under the “defaultAppSettings” is depicted here with a period. This is the same way you’ll use it in the Runtime Manager and any documentation that you create.
After saving your changes to the mule-artifact.json file, there is one more step. We need to add this new config-secure.yaml file as a known configuration file for your project.
- With any of your flow files open from your “src/main/mule” folder, click on the “Global Elements” tab.
- Click on “Create”
- Now Click on “Global Configurations” -> “Configuration properties”
- Click “OK”
- In the “File” box, select your new config-secure.yaml file. You can use the ellipses to make sure you have the path correct.
- Click “OK”
- Save all your changes
NOTE: Make sure and save all your changes in Anypoint Studio.
Now you can use these properties as you would any other properties in a .yaml file.
For this example, I have a simple application that pulls my two config settings and saves them to variables. I then display the variables as JSON.
Deploying an application is beyond the scope of this post, so I’ll assume you know how to do that.
After Deploying Anypoint Manager
After deploying, I run my application and view the following response message in my browser:
Now, back in Anypoint Manager – Runtime Manager, I hover over the name of my application and click on the little leaping arrow:
Select “Manage Application” on the right side pane.
On this screen, look for the properties tab and click it.
Here is where we can change the values of the properties we created in the config.secure.yaml file. By also adding them to the mule-artifact.json file, we have exposed them to this tab. So I’ll change the two values for my two sample properties, like so:
There are three things I want to mention about this screen.
- Make sure you click the save button after adding each property. If you add several and click save, I’ve had it wipe out all but the one I clicked.
- Note the key is exactly the same as the way that I access them from within my Mule application. Make sure and double check the names of your properties.
- You MUST click “Redeploy Application” for any of these change to not only take effect, but to be saved at all.
Click on “Redeploy Application”. Once it is started. I’ll refresh the application in my browser and see what we have:
So now I include the settings for these properties in my deployment document and every time the application is run on a server, it picks up the values that are appropriate for that environment.
Happy Muling, and remember, don’t make an ass of yourself.