Configuring Configuration & Secure Properties
Pre-requisites
Salesforce:
- We need to have Salesforce developer account.
- Create a Salesforce custom object with some fields, as per the requirement.
- Click on Create in the home section, which is on the right-hand side. Then click on custom object, then fill in the name of the custom object. Then click on fields and relationship, then select the fields with the respective type of information.
- In this scenario, we’ll be having 4 fields i.e. Amount__c, orderNo__c, product__c, Name__c and all the fields are having the text as the type of information stored in it.
- We need to keep the security token handy for that, click on profile and then click on setting which is on the right-hand top corner of the screen and type security token in the quick find section, and click on reset security token.
- Note: Once you have a security Token you can reuse the same as if the token is again reset the integrations that rely up on the old token don’t work if a new token is created unless it is changed.
- The security token will be sent to the registered mailId.
Database:
- We need to have a database.
- For this instance, we’ll be using the MySQL database.
- Create a database with some name and inside the database create a table with some fields as per the requirement.
- For this instance, we’ll be having the table name as customer_details and the fields are Name, OrderNo, Product and Amount, where OrderNo is the primary key.
Configuration & Secure Properties
- Configuration properties allows to manage and maintain properties. This makes it easier to update configurations without modifying them.
- Configuration files can be written in two ways:
- With an extension of the .yaml file.
- With an extension of the .properties file.
- By making use of configuration properties, we can externalize SQL queries.
- It enables to have different configurations for different environments (DEV, QA, Prod) without changing the application code.
- Secure properties are used to encrypt the sensitive data, which can be done in three ways:
- By making use of the JAR file.
- By adding one plugin to Anypoint Studio.
- By making use of cloud-based applications.
- In this blog, we’ll see how to encrypt the properties by making use of secure configuration properties. The requirement is to get the data from the Salesforce custom object and store it in DB(MySQL) & have to externalize the properties.
- For this requirement, we’ll be using the plugin to encrypt the properties.
Steps:
1.To begin, a new Mule project needs to be created. Then, a HTTP listener component dragged and dropped into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081. Set the path as /records.
2.Following the addition of the on new or updated file component, include two loggers to mark the beginning and the end of the flow. By logging the message “Start of flow ++ (flow.name as String)”, in expression mode.
3.Create two folder called config and secure inside the src/main/resource where we’ll have configuration properties and secure properties file. Then need to add the plugin to the Anypoint Studio, for that click on the help section. Then click on install new software and add this URL (http://security-update-site-1.4.s3.amazonaws.com/) to the search box, name it, and add. Then select all and add those to the studio, and hit on finish.
4.Next add the secure properties from the exchange to the mule palette. There is no component in this module. We’ll configure this in the global elements.
5.Then after we’ll write all the sensitive data in secure-file.properties which is created inside the secure folder and the insensitive data in config-file.yaml inside the config folder.
6.Next, we will configure the secure properties in the global elements. For that we need to select the file, key, algorithm for encrypting the data, and mode.
7.Then right click on secure-file.properties file and select with open with mule properties editor.
8.Double click on Name and click on encrypt, select the algorithm, mode, key which is configured in secure properties configuration.
9.Once all the properties are encrypted it will look like this:
10.We will configure the Salesforce connector configuration. Have to append secure:: with the properties so that it can refer to secure properties.
11.Test the connection and write the SOQL query to fetch the records from the custom object. In this scenario the custom object is customer_record__c.
12.Next transform message we’ll be used to transform the message response from Salesforce into the JSON format.
13.Then after add a logger, MuleSoft recommends extensive usage of the logger.
14.Add the database module to the mule palette, then drag and drop the bulk insert component into the flow and configure the database connector configuration. Here, we’ll perform an upsert operation using bulk insert.
15.The query for upserting is “INSERT INTO name_of_details(field1,field2) VALUES(:field1,:field2) ON DUPLICATE KEY UPDATE field2=:field2”.
a. Here the above query needs to be modified according to the field names in the DB.
16.Before sending the records directly to the database, there is a need to use the transform message component for Mapping it according to the Database fields and to convert it into Java format.
17.At last, we’ll add a transform message to change the response returned by the database to JSON format.
18.The flow is ready to be executed, for which right click on project ,select Run as and click on Mule Application (configure).
19. Click on Arguments and then pass the key value in VM arguments which is used to encrypt the values. Then click on apply and run.
a. Note: -D should be appended with the key in VM arguments. (-Dmule.key=******)
20. We will be using Postman to hit the request & response will look like this.