Encryption in Mulesoft Example

Encryption in Mulesoft Example

Raml for encryption and decryption API:

 

 After importing raml a mule configuration file is generated with HTTP listener and endpoints:

 

 Here is the encryption component configuration like:

 

 We are getting id to be encrypted from decryptedId param in the request body.

 

 We are using message enricher to set the target payload from encryption component to the flowVar id.

 After that we i am getting the decrypted id from flowVar id ans set it in the payload as response.

API Response:

 

 Request ans Response will be like as if we hit from Postman:

XML Configuration :

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:encryption="http://www.mulesoft.org/schema/mule/encryption" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.mulesoft.org/schema/mule/encryption http://www.mulesoft.org/schema/mule/encryption/current/mule-encryption.xsd http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="httpListenerConfig" host="0.0.0.0" port="${http.port}" doc:name="HTTP Listener Configuration" />
    <apikit:config name="apiConfig" raml="api.raml" consoleEnabled="false" doc:name="Router" />
    <context:property-placeholder location="config-${env}.properties" />
    <encryption:config name="Encryption" doc:name="Encryption" />
    <flow name="api-main">
        <http:listener config-ref="httpListenerConfig" path="/api/*" doc:name="HTTP" />
        <apikit:router config-ref="apiConfig" doc:name="APIkit Router" />
        <exception-strategy ref="apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy" />
    </flow>
    <flow name="post:/encrypt:apiConfig">
        <dw:transform-message doc:name="create-payload">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
        </dw:transform-message>
        <enricher target="flowVars.id" doc:name="Message Enricher">
            <encryption:encrypt config-ref="Encryption" input-ref="#[payload.decryptedId]" using="JCE_ENCRYPTER" doc:name="Encryption">
                <encryption:jce-encrypter key="${encryption.key}" keyPassword="${encryption.password}" algorithm="AES" encryptionMode="CBC" />
            </encryption:encrypt>
        </enricher>
        <dw:transform-message doc:name="API Response">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
"encryptedId": flowVars.id]]></dw:set-payload>
        </dw:transform-message>
    </flow>
    <flow name="post:/decrypt:apiConfig">
        <dw:transform-message doc:name="create-payload">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
        </dw:transform-message>
        <enricher target="flowVars.id" doc:name="Message Enricher">
            <encryption:encrypt config-ref="Encryption" input-ref="#[payload.encryptedId]" using="JCE_ENCRYPTER" doc:name="Decryption">
                <encryption:jce-encrypter key="${encryption.key}" algorithm="DES" encryptionMode="CBC" keyPassword="${encryption.password}" />
            </encryption:encrypt>
        </enricher>
        <dw:transform-message doc:name="API Response">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
"decryptedId": flowVars.id]]></dw:set-payload>
        </dw:transform-message>
    </flow>
    <apikit:mapping-exception-strategy name="apiKitGlobalExceptionMapping">
        <apikit:mapping statusCode="404">
            <apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property" />
            <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload" />
        </apikit:mapping>
        <apikit:mapping statusCode="405">
            <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property" />
            <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload" />
        </apikit:mapping>
        <apikit:mapping statusCode="415">
            <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property" />
            <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload" />
        </apikit:mapping>
        <apikit:mapping statusCode="406">
            <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property" />
            <set-payload value="{ &quot;message&quot;: &quot;Not acceptable&quot; }" doc:name="Set Payload" />
        </apikit:mapping>
        <apikit:mapping statusCode="400">
            <apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
            <set-property propertyName="Content-Type" value="application/json" doc:name="Property" />
            <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload" />
        </apikit:mapping>
    </apikit:mapping-exception-strategy>
</mule>


All the properties we have used in xml are defined in the properties file "mule-app.properties".



Comments

Post a Comment

Popular posts from this blog