Introduction
This post covers the basic procedures to setup simple Mule API security. I assume that the audience have not knowledge of applying API Security on the Mule AnyPoint Platform. Here are the key take-aways
- Write a simple RAML in the Design Center of the Anypoint Platform
- Publish the API (RAML) to the Exchange
- Using API Manage to apply simple security
- Explain the details on how it works
Design & Publish API
In the new version of AnyPoint Platform, the api management contains 3 separate areas:
- Design Center
- Exchange
- API Manager
There are many editors we can use to design our API (RAML). I find design center and ATOM are the two most powerful tools. Both are easy to use. For the demo purpose, I use AnyPoint Platform's Design Center. First, create a new project as shown in the snapshot below:
Choose "API Specification", enter project name, then you can write your API in RAML. The details can be found in many documents. The code below is the simplest RAML file for the purpose of demonstrate API security.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #%RAML 1.0 title: Basic Auth API version: v1 protocols: [ HTTP ] baseUri: https: //mocksvc.mulesoft.com/mocks/09212943-e570-413d-92f6-ef5e634f33cb/{version} # baseUri: http://esb.ggl-consulting.com/{version} mediaType: application/json securitySchemes: basicAuth: description: First simple auth type: Basic Authentication describedBy: headers: Authorization: description: Base64-encoded "username:password" type: string responses: 401: description: | Unauthorized: username or password or the combination is invalid types: Account: properties: id: integer type: string name: string Error: properties: code: integer errorMessage: string /accounts: /{id}: get: description: get an account information by id responses: 200: body: application/json: type: Account example: { "id" : 1234, "name" : "Gary Liu" , "type" : "checking" } |
Once the API is completed, we need to published the api to the Exchange. To publish the API to Exchange, refer the snapshot below:
Now, we can view our API in the Exchange as shown in the following snapshot:
Once the API is published to the Exchange, we can go to API Manager to import the API as shown in the following snapshot:
We can view the API as shown in the following snapshots:
By viewing API, we need few important information for the purpose of auto-discovery. The Mule 3, we need "API Name" and "API Version", respectively. For Mule 4, we need API ID.
Setup The AnyPoint Studio
In order to apply the security policy to our local running applications, we need to connect our local runtime with Anypoint Platform. To do so, we need to apply client ID and client secret of our environment to the Anypoint Studio. Firsly, we need to get the client id and client secret: Access Management --> Environment (left panel) --> Environemnt (Sandbox):
Now, go to AnypointStudio, Preference --> Anypoint Platform For Apis --> fill the client id and client secret --> Validate:
Once you validate the client id and client secret, that means our AnypointStudio or the embedded runtime can communicate with the Anypoint Platform.
Apply Security Policy In API Manager
Once the API is imported to the API Manager, we can apply the security policies, SLA Tier, alert, etc. The main purpose of this post is to demo how to apply security policies. I will cover the other area in the later posts. In this case, I plan to apply simple security and Basic Http Authentication as shown in the following snapshot:
When you apply the simple security, the platform will ask for the user name and password. Note down these credentials, we will need them when we perform the http request.At this point, we have setup the security for the API from administrative side. Now, we need to apply the security policy (user name and password) to our application. I will cover these in the next section.
Apply Security Policy To Mule Application Using Auto Discovery
The key to control application API access is via auto-discovery and communication between API Manager and application. To achieve auto-discovery of the application, or to let api manager control the application access, we need to create an auto-discovery component as shown in the following snapshots:
1 2 | <api-platform-gw:api apiname= "${api.name}" version= "${api.version}" flowref= "simple-security-main-Flow" create= "false" apikitref= "Router" doc:name= "API Autodiscovery" > </api-platform-gw:api> |
1 2 3 | <apikit:config name= "Router" raml= "api-manager-explained.raml" consoleenabled= "true" consolepath= "console" keepramlbaseuri= "false" doc:name= "Router" > <apikit:flow-mapping resource= "/accounts/{id}" action= "get" flow-ref= "get-account-by-id-Flow" > </apikit:flow-mapping></apikit:config> |
Run The Application
To test the security policy applying to our application, we can use PostMan as shown in the following snapshots:
The Authorization is "Basic Auth", the user name and password are shown. PostMan will automatically generate the token which is base64. And PostMan will send Authorization : [{"key":"Authorization","type":"text","name":"Authorization","value":"Basic R2FyeTEyMzQ6R2FyeTEyMzQk"...] to the server.
We can also perform the same using curl. First we need to generate the basic token as the following:
1 2 3 | gl17@garyliu17smbp:~$ echo "Gary1234:Gary1234$" | base64 R2FyeTEyMzQ6R2FyeTEyMzQkCg== gl17@garyliu17smbp:~$ |
Then, we can send the request as the following:
1 | curl -X GET -H "Authorization: Basic R2FyeTEyMzQ6R2FyeTEyMzQk" http: //localhost:18081/api/accounts/1234 |
What Is Under The Hood?
At this point, we may ask ourselves the question: How does it work? How the Anypoint Platform enforce the security policies?
First of all, we noticed that when we run the application in our local, in our console, there are following lines:
The highlighted line showing that the policy has been applied successfully.In the meantime, there is a file written in our workspace/.mule/http-basic-authentication-282686.xml as shown in the following snapshot:
And the contents of the file are the following:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!--xml version= "1.0" encoding= "UTF-8" ?--> <policy xmlns= "http://www.mulesoft.org/schema/mule/policy" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:mule-ss= "http://www.mulesoft.org/schema/mule/spring-security" xmlns:api-platform-gw= "http://www.mulesoft.org/schema/mule/api-platform-gw" xmlns:spring= "http://www.springframework.org/schema/beans" xmlns:ss= "http://www.springframework.org/schema/security" xsi:schemalocation= "http://www.mulesoft.org/schema/mule/policy http://www.mulesoft.org/schema/mule/policy/current/mule-policy.xsd http://www.mulesoft.org/schema/mule/spring-security http://www.mulesoft.org/schema/mule/spring-security/3.3/mule-spring-security.xsd http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/schema/mule/api-platform-gw/current/mule-api-platform-gw.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd" online= "true" id= "282686" policyname= "HTTP Basic Authentication" order= "2" > <spring:beans xmlns:spring= "http://www.springframework.org/schema/beans" xmlns:ss= "http://www.springframework.org/schema/security" > <ss:authentication-manager alias= "282686-authentication-manager" > <ss:authentication-provider> <ss:user-service id= "userService" > <ss:user name= "Gary1234" password= "Gary1234$" authorities= "ROLE_ADMIN" > </ss:user></ss:user-service> </ss:authentication-provider> </ss:authentication-manager> </spring:beans> <mule-ss:security-manager xmlns:mule-ss= "http://www.mulesoft.org/schema/mule/spring-security" name= "282686-security-manager" > <mule-ss:delegate-security-provider name= "282686-security-provider" delegate-ref= "282686-authentication-manager" > </mule-ss:delegate-security-provider></mule-ss:security-manager> <before> <mule-ss:http-security-filter xmlns:mule-ss= "http://www.mulesoft.org/schema/mule/spring-security" realm= "mule-realm" securitymanager-ref= "282686-security-manager" > </mule-ss:http-security-filter></before> <pointcut> <api-platform-gw:api-pointcut xmlns:api-platform-gw= "http://www.mulesoft.org/schema/mule/api-platform-gw" apiname= "groupId:d1ba8782-0846-4b64-9ced-e68e7a9c952e:assetId:api-manager-explained" apiversion= "v1:9083625" > </api-platform-gw:api-pointcut></pointcut> </policy> |
Another interesting point should be noted is the network connection. Here is what I can see from my local environment by using the command of lsof:
1 2 3 4 5 | AnypointS 987 gl17 98u IPv6 0x3c5bcba541fe6469 0t0 TCP localhost:50687->localhost:6666 (ESTABLISHED) AnypointS 987 gl17 223u IPv6 0x3c5bcba541fe9269 0t0 TCP localhost:50681->localhost:50683 (ESTABLISHED) java 1107 gl17 4u IPv4 0x3c5bcba53fdc68b1 0t0 TCP localhost:50683->localhost:50681 (ESTABLISHED) java 1107 gl17 498u IPv4 0x3c5bcba5417148b1 0t0 TCP localhost:6666->localhost:50687 (ESTABLISHED) java 1107 gl17 526u IPv4 0x3c5bcba544131211 0t0 TCP garyliu17smbp.frontierlocal.net:50927->ec2-34-231-107-145.compute-1.amazonaws.com:https (ESTABLISHED) |
the blog is good and Interactive it is about Mulesoft Anypoint Studio it is useful for students and Mulesoft Developers for more updates on Mulesoft mulesoft Online course india
ReplyDeleteVery informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDeleteValuable post useful for everyone.Keep on sharing this type of articles.
ReplyDeleteMulesoft Online Training
Mulesoft Training in Hyderabad
Thanks for sharing valuable information and very well explained. Keep posting.
ReplyDeletebest mulesoft online training
mulesoft training courses
I really liked your blog post.Much thanks again. Awesome
ReplyDeletemulesoft self learning
mulesoft online Course in Hyderabad
Docuspace provides clean and simple design eliminates frustration and promotes productivity so you can setup and use our platform with as little friction as possible. You don't need to be an IT genius to set up and configure our platform; you can get it up and running in a matter of minutes.
ReplyDeletenice ..................!
ReplyDeletemulesoft training
pega training
servicenow training training