Friday, August 19, 2016

Virtual Networking for a static IP based local cluster with Oracle Virtual Box

Working in a clustered environment was one of the main tasks which I had to go through recently. Before going into an actual clustered environment where I could mess things up I took up the challenge of setting one up on my own. The luxury of going into a commercial virtual server provider was not an option therefore opting to do it locally through a virtual environment was the best solution.


Since I’ve been using Oracle Virtual Box for a quite some time I went ahead and started deploying servers. Although I’ve been managing one or two servers in a virtual box, managing a cluster with 4 nodes and maintaining communication within the nodes into several ports became the problem.


Although using a NAT adapter with port forwarding can be used. Configuring several ports for each server was the problem with maintaining a cluster. Also assigning a static IP address to be used for communication apart from 10.0.2.15 which is used by Virtual Box was also out of options in this method. Then after some reading I figured host only adapter would be the solution for me. This solved the above problems I faced while using NAT adapter.


Initially you will have to add a Host-only network adapter to your virtual box instance. To do so got Preference -> Networks -> Host-only Networks  



Here in this panel by clicking the + icon on right hand corner you can add a Host-only adapter to your Virtual Box. Click on the new adapter that is created and do the configurations for IP's that you require. Basically this would use 19.168.xx.xx IP range since it is the private IP address range used.



The IP which will be given default to the Host-only adapter will be assigned to the host that the virtual box is running therefore in this scenario you can use IP addresses from 192.168.56.2 onwards for the virtual servers that you are using. After configuring click OK and start configuring a server.



Choose the server that you want to add the network to and select Settings -> Network -> adapter 2 (We will keep the adapter 1 as NAT since this wouldn’t be a blocker to go ahead and can be used for initial setting up and debugging without the new port we are adding).


Select Enable Network  Adapter and Under Attached to drop down select Host-only Adapter and assign the Name with Host-only adapter created above.  




Click Ok and we are ready to start the server. For this task I have been using ubuntu server 14.04 and the configurations in the server maybe a bit different to the OS version that you are using.


After starting the server run ifconfig command and you will only see eth0 port which is bound to 10.0.2.15 as inet address. Open /etc/network/interfaces and add below configurations to it after eth0 interface


auto eth1
iface eth1 inet static
address 192.168.56.4
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.254


Save the file and run ifconfig eth0 up. It will setup the new interface with the relevant IP address. You can check it by running ifconfig and you will see below. Try pinging the IP you’ve assigned from your local host and confirm that IP is assigned properly.  


Do this for all the servers with several IPs and enjoy the luxury of a cluster which is running under a set of IPs that would be used to ssh, clustering, load balancing and etc.

Friday, June 17, 2016

Adding an output logger event adapter to WSO2 DAS event stream

Working in WSO2 Cloud Team was capable of putting me through events which I wondered what should I do now ? Specially while in Cloud Support. Once there was an issue (L1) we got reported that API Statistics are not visible on WSO2 API Manager. Oh great by this time I was not much of an expert in this area and I was like HOW ?

WSO2 API Cloud uses WSO2 APIM 1.10 and DAS 3.0.1 by that time was and I knew that DAS is working in a cluster which has two receivers, analyzers and indexers. It wasn't hard to realize that DAS analyzers aren't having required raw data stored for the time being. HOw to check whether API Gateway is publishing data to das analyzers ? Here is what was suggested to me from a DAS team member to check whether data is getting published to das receiver node. Here what I used was a adding an Output logger event adapter [1] to the Input stream that I wanted to check whether the data is getting published to DAS receiver nodes.

1. Login to DAS management console
2. Go to Main -> Event -> Publishers -> Add event publisher


3. On the Create a New Event Publisher add the Event Publisher Name and select the Event stream which needs to be logged from Event Source
4. Then from Output Event Adapter Type select logger as the value and click Add Event Publisher.



5. You will see a message Event publisher added successfully and the added Event publisher will be visible on Available Event Publishers.



6. Start publishing to the stream and you will see logs getting printed on <DAS-HOME>/repository/logs/wso2carbon.log



[1] https://docs.wso2.com/display/CEP300/Output+logger+Event+Adapter



Saturday, June 11, 2016

Deleting Admin users from your organization in WSO2 Cloud

Currently in WSO2 Cloud the Organization owners can add multiple Admin users to his organization. It is a common scenario that some people leave the organizations and if he is an admin of your organization you should remove the Admin from the organization.

In current implementation of WSO2 Cloud you are not able to delete an Admin through the Members page. As you can notice on the below image the users with Admin Role doesn't have the check box in front of them. Therefore we are not able to edit or delete the specific Admin from the organization directly.


In order to delete them the Organization owner (The creator of the organization on WSO2 Cloud) should revoke the Amin role from the user. This is not provided through WSO2 Cloud UI directly. Here is how you can achieve this.

1. You can log in to WSO2 Cloud management console using the steps described in
https://docs.wso2.com/display/APICloud/FAQ#FAQ-HowcanIlogintotheCloud'sManagementConsole?


2. On the left menu of management console go to Main -> User and Roles -> List -> Users. This will list out the Users in your organization.


3. Select the preferred user with Admin role and select View Roles and remove the admin role from that user privileges from that user and update.


4. Afterwards log out of the management console and re login through cloud ui and go to members page. You will see the check box in front of the particular user will be enabled and you will be able to delete the user (Due to caching on server side it will take some time for the changes to be depicted on UI).



* Note that you must not delete the user through management console since there are records which are not getting deleted for the user which is explicitly used in for WSO2 Cloud data in the front end. Therefore you should delete the user through UI after removing the role.

Saturday, January 2, 2016

WSO2 API Manager Using custom headers via swagger ui

Due to various reasons most of the back end APIs are expecting headers which are not allowed by the default CORS Handler used in WSO2 API Manager. Due to the restrictions by CORS Handler while invoking APIs with custom headers fail if we use the swagger UI. This is due to the OPTION call sent to API gateway from publisher to get the allowed header types (if we are using curl or other api clients this doesn't fail because we don't us the OPTION call in it).

List down below is how to add a custom header on WSO2 API Manager and make it usable via swagger UI.

1. Start creating an API according to [1]
2. Click on EDIT Source in the step 3 provided in the tutorial

add the following swagger file to the swagger file and save

paths:
  /weather:
    post:
      x-auth-type: "Application & Application User"
      x-throttling-tier: Unlimited
      parameters:
        - schema:
            type: object
          description: Request Body
          name: Payload
          required: false
          in: body
        - name: ActionHeader
          description: The Header
          type: string
          required: false
          in: header
        - name: q
          type: string
          required: false
          in: query
        - name: appId
          type: string
          required: false
          in: query
      responses:
        "200": {}
    get:
      x-auth-type: "Application & Application User"
      x-throttling-tier: Unlimited
      parameters:
        - name: ActionHeader
          description: The Header
          type: string
          required: false
          in: header
        - name: q
          type: string
          required: false
          in: query
        - name: appId
          type: string
          required: false
          in: query
      responses:
        "200": {}
swagger: "2.0"
info:
  title: HeaderTutorial
  version: v1


Note that following block adds the ActionHeader as a header parameter to the resource.

        - name: ActionHeader
          description: The custom Header
          type: string
          required: false
          in: header

3. Then continue with the step 5 in tutorial.

4. After successfuly publishing the API you can subscribe to it through API store and invoke the API. When API is invoked you can find that below type of error message is vibile on the swagger console.



5. The above happens because the ActionHeader is not allowed in the CORS Configuration. In order to allow it first stop the running API Manager Instance

6. Open <APIM HOME>/repository/conf/api-manager.xml

7. Add the ActionHeader value to <Access-Control-Allow-Headers> under <CORSConfiguration>tag as follows


After the adding the value the Access-Control-Allow-Headers should like following

<Access-Control-Allow-Headers>authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,Authorization,ActionHeader</Access-Control-Allow-Headers>

8. Save the file and restart API Manager

9. Invoke the api using the swagger UI you will find the API getting successfully invoked now.

The below video tutorial show the steps of how to achieve this





[1] https://docs.wso2.com/display/AM191/Create+and+Publish+an+API 

Adding a Tenant Specific Custom Handler WSO2 API Manager

Writing a Handler for WSO2 API Manager is pretty straight forward and you can find it in [1]. When it comes to multi tenancy there might be instances that this particular handler should be applied only to a specific tenant or a set of tenants. In doing so we have to edit the /repository/resources/api_templates/velocity_template.xml to include the handler to only a specific tenant. This is the template file used in WSO2 API Manager to create synapse file required to create the api. Here we have to write a simple velocity logic to add the handler only for the specific tenants. If this logic is not added the handler will be applied to all the tenant hence resulting in a performance tradeoff. 

You can add the below logic right after the handlers tag in the velocity_template.xml

        #if ( $apiContext.contains("/abc.com/") )
           <handler class="package.path.to.class" />
        #end

This will add the handler to the abc.com tenant only. You can also extend this to multiple tenant with having a logical OR "||" operator for the contains check.

Just write and save the file and will be added to the template.

[1] https://docs.wso2.com/display/AM191/Writing+Custom+Handlers