Showing posts with label Cloud. Show all posts
Showing posts with label Cloud. Show all posts

Tuesday, July 21, 2015

How to pass an Authorization token to the back end sever, in WSO2 API Cloud

There can be scenarios where the back-end service is expecting an Authorization token which is different from the Authorization token used in API Cloud. However when a request is sent to WSO2 API Cloud with the Authorization header, API Gateway will use it for API authentication/authorization and it will be dropped from the out going message to the back-end service.

This requirement can be achived with the use of two headers; Authorization header containing the API Clouds token and a different header which contains the value of the token expected by the back-end. Using a custom mediation extension, the value of this second header can be extracted and set it to Authorization header, which will be then send to the back-end. For example the two headers, Authorization (API Clouds token) and Authentication (the token expected by back end) can be used.

For this scenario, per API extension can be used. There is a naming pattern of a per-API extension sequence which is explained here. In API Cloud it should be similar to (assuming the user email as user@email.com);

user.email.com-AT-yourOrganizationKey--YourAPIName:v1.0.0--In

You can find the Organization Key from Organization Manage page in WSO2 Cloud.

Following is a sample synapse configuration of a per-API extension sequence for this scenario.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse"
          name="user.email.com-AT-yourOrganizationKey--YourAPIName:v1.0.0--In">
   <property xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
             xmlns:ns3="http://org.apache.synapse/xsd"
             name="Authentication"
             expression="get-property('transport', 'Authentication')"/>
   <property xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
             xmlns:ns3="http://org.apache.synapse/xsd"
             name="Authorization"
             expression="get-property('Authentication')"
             scope="transport"
             type="STRING"/>
   <property name="Authentication" scope="transport" action="remove"/>
</sequence>

A XML file should be created using the above configuration and it should be uploaded to the Governance registry of the API Cloud using the management console UI of Gateway (https://gateway.api.cloud.wso2.com/carbon).

You can get the user name from the top right corner of the Publisher and then enter your password and log in. Once you are logged in select Resources (left hand side of the Management console) and click on Browse and then navigate to /_system/governance/apimgt/customsequences registry location. Since this sequence need to be invoked in the In direction (or the request path) navigate to the in collection. Click Add Resource and upload the XML file of the sequence configuration and add it. (Note: Once you add the sequence it might take up-to 15 minutes until it is deployed into the publisher)

Now go to the Publisher and select the required API and go to edit wizard by clicking edit and then navigate into Manage section. Click on Sequences check box and select the sequence we added from the In Flow. After that Save and Publish the API.

Now you should invoke the API passing the above two headers and the back-end will receive the required Authorization header. A sample curl request would be as follows;

curl -H "Authorization: Bearer a414d15ebfe45a4542580244e53615b" -H "Authentication: Bearer custom-bearer-token-value" http://gateway.api.cloud.wso2.com:8280/t/clouddemo/authsample/1.0

What happens will be as follows;

Client (headers: Authorization, Authentication) -> 
           Gateway (drop: Authorization, convert: Authentication-Authorization) -> Backend 


References:
[1] http://sanjeewamalalgoda.blogspot.com/2014/11/how-to-use-custom-authentication-header.html

Tuesday, April 7, 2015

Disable Chunking in APIs of WSO2 API Cloud

Sometimes it may be required to disable chunking in the API requests done via WSO2 API Cloud. Some back-end servers does not accept chunked content and therefore you may need to disable chunking.

In a Standalone WSO2 APIM, this can be done by editing the velocity template as described in this blog post. However in WSO2 API Cloud it is not possible to edit the velocity template.

This can be done with the use of a custom mediation extension which will disable chunking, as described below.

The corresponding sequence would be like;

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse"
          name="disable-chunking">
        <property name="DISABLE_CHUNKING" value="true" scope="axis2" />
</sequence>

You can find a sample sequence (disable-chunking.xml) which you can upload to the Governance registry of the API Cloud using the management console UI of Gateway (https://gateway.api.cloud.wso2.com/carbon).

You can get the user name from the top right corner of the Publisher and then enter your password and log in. Once you are logged in select Resources (left hand side of the Management console) and click on Browse and then navigate to /_system/governance/apimgt/customsequences registry location. Since this sequence need to be invoked in the In direction (or the request path) navigate to the in collection. Click Add Resource and upload the XML file of the sequence configuration and add it.  (Note: Once you add the sequence it might take up-to 15 minutes until it is deployed into the publisher)

In Publisher select the required API and go to edit wizard by clicking edit and then navigate into Manage section. Click on Sequences check box and select the sequence which disable chunking, from the In Flow. After that Save and Publish the API.

Now invoke the API. If you investigate your requests, you will notice that chunking is disabled.

Chunking Enabled

POST customerservice/customers HTTP/1.1
Content-Type: text/xml; charset=ISO-8859-1
Accept: text/xml
Transfer-Encoding: chunked
Host: 192.168.77.1:8080
Connection: Keep-Alive
User-Agent: Synapse-PT-HttpComponents-NIO

Chunking Disabled
POST customerservice/customers HTTP/1.1
Content-Type: text/xml; charset=ISO-8859-1
Accept: text/xml
Content-Length: 42
Host: 192.168.77.1:8080
Connection: Keep-Alive
User-Agent: Synapse-PT-HttpComponents-NIO

Tuesday, May 14, 2013

Setting up WSO2 Stratos 1.6 in your PC - The most complete, enterprise-grade and open PaaS


WSO2 Stratos is an open source PaaS which is complete, enterprise grade and which includes many services which can empower your businesses. WSO2 Stratos is the backbone behind WSO2 StratosLive, the PaaS operated by WSO2 itself.
With the following guide you can setup Stratos locally in your PC and try the features easily.
Prerequisites:
1.    MySQL Server
In order to run Stratos you will need MySQL server running on your PC.
        You can download it from: http://dev.mysql.com/downloads/mysql/
        In Ubuntu: sudo apt-get install mysql-server
Stratos assumes the password of your MySQL root to be 'root'. If you have not configured it, you can do it by the following command.
    mysqladmin -u root -p 'oldpassword' password root
2.    Download the Stratos Local setup from: http://wso2.com/cloud/stratos/
 After downloading the setup, extract it to a convenient location.
3.    Also you need to have Java on your machine in order to try this.
Steps (We will assume a Ubuntu environment)
1.    You need to modify the /etc/hosts file in order to redirect the Stratos specific URLs to localhost. Add the following entries to the file.    
      127.0.0.1        stratos-local.wso2.com
      127.0.0.1        management.appserver.stratos-local.wso2.com
      127.0.0.1        appserver.stratos-local.wso2.com
      127.0.0.1        management.esb.stratos-local.wso2.com
      127.0.0.1        esb.stratos-local.wso2.com
      127.0.0.1        management.data.stratos-local.wso2.com
      127.0.0.1        data.stratos-local.wso2.com
      127.0.0.1        monitor.stratos-local.wso2.com
      127.0.0.1        management.process.stratos-local.wso2.com
      127.0.0.1        process.stratos-local.wso2.com
      127.0.0.1        governance.stratos-local.wso2.com
      127.0.0.1        identity.stratos-local.wso2.com
      127.0.0.1        storage.stratos-local.wso2.com
      127.0.0.1        cep.stratos-local.wso2.com
      127.0.0.1        task.stratos-local.wso2.com
      127.0.0.1        messaging.stratos-local.wso2.com
      127.0.0.1        management.rule.stratos-local.wso2.com
      127.0.0.1        rule.stratos-local.wso2.com
      127.0.0.1        cloudgateway.stratos-local.wso2.com     

2.    Go inside to the extracted folder and run the initialization script as follows
      ./initialize.sh
      This will initiate the required data tables within the MySQL database.
3.    Then start the ‘elb’ (WSO2 Elastic Load Balancer)  and ‘manager’ (Stratos Manager) services as below
               elb:                     sudo ./stratos start elb
               manager:          ./stratos.sh start manager 
Note: You may need super user privileges to start or stop elb as it runs on standard http and https ports. Also the ports should be free to use.
You may get the following error when you try to start elb as supper user.

               Starting elb
       Error: JAVA_HOME is not defined correctly.
       CARBON cannot execute java


This might be because JAVA_HOME has not been set to the  super user environment. As a workaround you can use 'sudo -E ./stratos start elb' to start elb.              
4.    Then start the services of your preference
You can obtain the available list of services by the following command. Also it will provide details on starting services.
            ./stratos.sh
To start a service (for example WSO2 Appserver Management)
     ./stratos.sh start as_mgt
To stop
     ./stratos.sh stop as_mgt
To start multiple services
     ./stratos.sh start 'service names separated by spaces'
For example WSO2 Appserver Manager and Worker,  WSO2 ESB Manager and Worker
     ./stratos.sh start ‘as_mgt as_worker esb_mgt esb_worker’