Tuesday, September 5, 2017

Mulesoft Operationalization - Log4j Overrides

In this blog posting I will explain how to create environment specific properties for log4j configurations.  While this may seem like a very rudimentary topic I have seen at times when log4j configurations have not been managed and has lead to sensitive production data being saved to disk or even passwords being logged to disk as well.  I assume a basic understanding of Mulesoft for this this posting and will skip over the basic steps

Create the Mulesoft Domain

  1. Create a standard Mulesoft Domain via the wizard in Anypoint Studio
  2. Update the Mule-domain-config.xml with the following snippet
    1
    2
    3
    4
    5
    6
    7
    <spring:beans>
            <spring:bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                <spring:property name="location" value="test_${MULE_ENV}.properties"/>
                <spring:property name="ignoreUnresolvablePlaceholders" value="true"/>
                <spring:property name="ignoreResourceNotFound" value="true"/>
            </spring:bean>
        </spring:beans>
    

    The bean will read in the property file (Name:Value)
    The location property has ${MULE_ENV} and this is used at run time to inject the environment value
  3. Create your environment property files under src/main/resources.  My example I have two and the name pattern is app_${env}.properties:
    1. test_dev.properties
      1
      2
      simple_log4j_example_log_level=INFO
      app_2_log_level=DEBUG
      

    2. test_prod.properties
      1
      2
      simple_log4j_example_log_level=INFO
      app_2_log_level=INFO
      

Create a Simple Mulesoft Application

This step will be creating a simple Mulesoft project that references the above domain.  The application will contains a single flow that listens on port 8081(http://localhost:8081/test) and returns the current log level and logs both the simple_log4j_example_log_level and the app_2_log_level from the property file specified at run time.

The following edits have been made for this: 

The change required to override the log4j configuration is done on the log4j2.xml in src/main/resources/ by updating the AsyncRoot to reference the property name from the domain property file:

1
2
3
<AsyncRoot level="${simple_log4j_example_log_level}">
   <AppenderRef ref="file" />
</AsyncRoot>

Pulling this all together

Inside Anypoint Studio you will need to update the Run Configuration to include the following VM argument:

  • -DMULE_ENV
    • To use the dev properties file it would be:  -DMULE_ENV=dev
    • To use the prod properties file it would be:  -DMULE_ENV=prod
This argument will be used by the domain at startup to pull in the correct properties file.  

Once your application is up and running you can go to http://localhost:8081/test and see the response of the current log level for the simple_log4j_example_log_level.  The log connectors can be changed to validate the properties file.  

This is one of many ways this could be managed and I hope this helps others keep their logs clear of information that shouldn't be there.

Helpful Links

No comments:

Post a Comment