Showing posts with label wso2. Show all posts
Showing posts with label wso2. Show all posts

Thursday, July 18, 2013

How to get a user profile for OAuth Token using JWT headers


In this blog post lets discuss how you can retrieve user profile and view attributes of a user using only the OAuth token in WSO2 Identity server. I assume that reader is familiar with WSO2 identity server and if you need more help you can follow the documentation at http://docs.wso2.org/wiki/display/IS400/WSO2+Identity+Server+Documentation

1) Change the CARBON_HOME/repository/conf/identity.xml and make following attribute true. Then start the WSO2 Identity server.

   <AuthorizationContextTokenGeneration>
                        <Enabled>true</Enabled>
                        <TokenGeneratorImplClass>org.wso2.carbon.identity.oauth2.authcontext.JWTTokenGenerator</TokenGeneratorImplClass>
                        <ClaimsRetrieverImplClass>org.wso2.carbon.identity.oauth2.authcontext.DefaultClaimsRetriever</ClaimsRetrieverImplClass>
                        <ConsumerDialectURI>http://wso2.org/claims</ConsumerDialectURI>
                        <SignatureAlgorithm>SHA256withRSA</SignatureAlgorithm>
                        <AuthorizationContextTTL>15</AuthorizationContextTTL>
                </AuthorizationContextTokenGeneration>

2) Get the oauth Token ( you need to get the base64 encoded value for (client id:client secret) value.


You can use an online service for encoding such as http://www.base64encode.org/ and then issue following CURL command to obtain the access token. (make sure you have the correct port in the server URL)

curl -v -X POST -H "Authorization: Basic VE01enNFem9FZG9NRERjVEJjbXRBcWJGdTBFYTpYUU9URExINlBBOHJvUHJfSktrTHhUSTNseGNh" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -k -d "grant_type=password&username=admin&password=admin" https://localhost:9443/oauth2endpoints/token



 3) Now you need to invoke and admin service in the Identity server. Please follow

http://charithaka.blogspot.com/2013/06/invoking-wso2-carbon-admin-services.html on how to invoke admin services using SOAP UI.

We shall now invoke the following service.
https://localhost:9443/services/OAuth2TokenValidationService

Request



Response



Now you will be getting the authorization context token which is encoded in base64 and delimited with "." values. Middle part will contain the user profile value and we can get it decoded using http://www.base64decode.org/

<ax2303:tokenString>eyJ0eXAiOiJKV1QiLCJhbGciOiJTSEEyNTZ3aXRoUlNB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T5BvfATVMsvqO/023BFprhdDShuPlpI9pJAEeuvmUiRIBPVVeoj09Ec=</ax2303:tokenString>
               <ax2303:tokenType>JWT</ax2303:tokenType>

Following is the output of the decoded value which has the user profile details of the user who generated the OAuth access token.




Sunday, February 3, 2013

Application Governance with WSO2 GReg and Jenkins

This tutorial will show how you can automate the application Governance using WSO2 Governance Registry and Jenkins (for Build Automation).

Pre-Requisites 


Things to note

  • Jenkins or similar build automation tool should be used to build the artifact and deploy it to the specific Dev SVN location. 
  • There are three SVN locations required for Dev , QA and Production artifact deployment. 
  • User needs to specify the exact name of the artifact (without the extension) as Application Name when adding the application to WSO2 Governance Registry. 
  • This particular sample is tested with WSO2 Governance Registry 4.5.1. Therefore it is recommended to use the same version of the product.

Resource Location

Please download resources from following location.
https://svn.wso2.org/repos/wso2/scratch/application-governance/

Workflow




Configurations

n this workflow build automation is excluded and user needs to add the built artifact and first deploy to Dev SVN location. (Developer may use Jenkins to build and its plugin to add it to a SVN location)

1) Create the Registry Extension file for Applications as specified below. Deploy the Application RXT file to Governance Registry as described in http://docs.wso2.org/wiki/display/Governance450/Deploying+an+Extension+File


<artifactType type="application/vnd.wso2-application+xml"

shortName="applications" singularLabel="Application" pluralLabel="Applications" hasNamespace="false" iconSet="9">
    <storagePath>/applications/@{overview_name}</storagePath>
 <nameAttribute>overview_name</nameAttribute>
    <ui>
        <list>
            <column name="Name">
                <data type="path" value="overview_name" href="/applications/@{name}"/>
            </column>
            <column name="type">
                <data type="type" value="overview_type"/>
            </column>
        </list>
    </ui>
    <content>
        <table name="Overview">
            <field type="text" required="true">
                <name>Name</name>
            </field>
            <field type="options">
     <name>Type</name>
     <values>
      <value>car</value>
      <value>war</value>
     </values>
    </field>
            <field type="text-area">
                <name>Description</name>
            </field>
        </table>
    </content>
</artifactType>





2) Create an Application in Governance Registry.

Add→ Application


Specify Name of the car/war file. (This should be the exact name in the svn location without extension)
eg: myCapp


3) Add Svn Executor to Governance Registry Server.

  • Edit the svn-executor-1.0.jar’s svn.properties file and add your svn credentials and Jenkins URL.

  • Copy the svn-executor-1.0.jar and svnkit-bundle-1.0.0.jar (provided separately) to G-REG_HOME/repository/components/lib folder.

  • Copy svn-client-adapter-1.6.18.wso2v2.jar to G-REG_HOME/repository/components/dropins folder.



4) Add a new lifecycle in Governance Registry names AppLifeCycle by following the instructions provided in
http://docs.wso2.org/wiki/display/Governance450/Adding+Lifecycles. Use the lifecycle configuration specified below.

 <!--
 ~ Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 ~
 ~ WSO2 Inc. licenses this file to you under the Apache License,
 ~ Version 2.0 (the "License"); you may not use this file except
 ~ in compliance with the License.
 ~ You may obtain a copy of the License at
 ~
 ~    http://www.apache.org/licenses/LICENSE-2.0
 ~
 ~ Unless required by applicable law or agreed to in writing,
 ~ software distributed under the License is distributed on an
 ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 ~ KIND, either express or implied.  See the License for the
 ~ specific language governing permissions and limitations
 ~ under the License.
 -->
<aspect name="AppLifeCycle" class="org.wso2.carbon.governance.registry.extensions.aspects.DefaultLifeCycle">
    <configuration type="literal">
        <lifecycle>
            <scxml xmlns="http://www.w3.org/2005/07/scxml"
                   version="1.0"
                   initialstate="Development">
                <state id="Development">
                    <datamodel>
                        <data name="checkItems">
                            <item name="Code Completed" forEvent="">                            
                            </item>
                          
                            <item name="Added to SVN" forEvent="">
                            </item>
                        </data>

   <data name="transitionExecution">
                            <execution forEvent="Promote" class="org.wso2.carbon.executor.SvnDeploymentExecutor">
     <parameter name="currentEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/dev"/>
                                <parameter name="targetEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa"/>
                            </execution>   
                        </data>  
                    </datamodel>
                    <transition event="Promote" target="Testing"/>                  
                </state>
                <state id="Testing">
                    <datamodel>
                        <data name="checkItems">
                            <item name="Effective Inspection Completed" forEvent="">
                            </item>
                            <item name="Test Cases Passed" forEvent="">
                            </item>
                            <item name="Smoke Test Passed" forEvent="">
                            </item>
                        </data>
                         <data name="transitionExecution">
                            <execution forEvent="Promote" class="org.wso2.carbon.executor.SvnDeploymentExecutor">
                                <parameter name="currentEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa"/>
                                <parameter name="targetEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/prod"/>
                            </execution>

                            <execution forEvent="Demote" class="org.wso2.carbon.executor.SvnDeploymentExecutor">
                             <parameter name="currentEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa"/>
                            </execution>
                        </data>
                           
   
                    </datamodel>
                    <transition event="Promote" target="Production"/>
                    <transition event="Demote" target="Development"/>
                </state>
                <state id="Production">  
                    <datamodel>
                        <data name="checkItems">
                            <item name="Verify No one using the application" forEvent="">
                            </item>
                        </data>
   <data name="transitionExecution">
                            <execution forEvent="Retire" class="org.wso2.carbon.executor.SvnDeploymentExecutor">
    <parameter name="currentEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa"/>
                                <parameter name="targetEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/prod"/>
                            </execution>   
                        </data>  
                    </datamodel>
                    <transition event="Retire" target="Retired"/>
                </state>  
                
                <state id="Retired">  
                    <datamodel>     
                    </datamodel>
                </state>
                              
            </scxml>
        </lifecycle>
    </configuration>
</aspect> 


In the above lifecycle configuration you need to change your svn locations that are used in promotions.

Eg:

From Dev to QA
<parameter name="currentEnvironment" value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/dev"/>
  <parameter name="targetEnvironment" 
value="https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa"/>

5) Attach the AppLifeCycle to your application.


6) Promote Application from Development to Testing.
This will copy external CApp from Dev environment to QA environment (in the relevant SVN locations)






7) Demote Application from Testing to Development back
This will remove the artifact from QA environment (QA SVN location)



Deployment Automation with Jenkins


1) Start Jenkins and add the URL to svn-executor-1.0.jar’s properties file.


2) Create two Build jobs with name “promote” and “demote”.


3) Apply “This build is parameterized” check box and add following parameters to each of the job separately .


  • name
  • svnUrl
  • state

4) Create a local repository (A folder in you local file system) to check out the artifacts.
eg: /home/user1/repository

5) for promote Build job add the following script ( Build → Add build step → execute shell). Please change the values appropriately to suit your environment

#!/bin/bash

if  [[ "${state}" == "Testing" ]] ;
then
##  repository location
cd /media/data/wso2/support/repository/

svn checkout $svnUrl


## svn checkout location (This will be repository url + ‘qa’ as
## QA artifact was store in our repository
## https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/qa/


cd /media/data/wso2/support/repository/qa
cp -r ./$name 

## Location of the Testing server
/media/data/wso2/support/wso2qaesb-4.5.0/repository/carbonapps/0/
fi

if [[ "${state}" == "Production" ]] ;
then
cd /media/data/wso2/support/repository/
svn checkout $svnUrl

## svn checkout location (This will be repository url + ‘prod’ as
## Production artifact was store in our repository
## https://svn.wso2.org/repos/wso2/scratch/greg-scm-test/prod/

cd /media/data/wso2/support/repository/prod
cp -r ./$name 

## url of the Production server
/media/data/wso2/support/wso2prodesb-4.5.0/repository/carbonapps/0/
fi 

6) For demote Build job use the following action ( Build → Add build step → execute shell)

#!/bin/bash

if  [[ "${state}" == "Testing" ]] ;
then

## location of the local ‘QA’ artifacts are stored.
cd /media/data/wso2/support/repository/qa

svn update 

cd /media/data/wso2/support/wso2qaesb-4.5.0/repository/carbonapps/0

rm -rf ./$name
fi

7) When you promote as in the script it will deploy the artifact to Development server and when demote it will remove the Application from Carbon server which will respectively deploy/ undeploy

8) You need to have similar script for artifact deployment from initial project to developer location which is not covered in the sample.

Monday, June 18, 2012

Cassandra GUI 2.0 - Making things a little bit easier

Update (2014 October 31st)

You can download WSO2 Storage server  1.1.0 which ships Cassandra explorer and many interesting tools to manage storages. Please follow the following links for documentation to use Cassandra Explorer.

Download the product
https://docs.wso2.com/display/SS110/Downloading+the+Product

Extract the binary and run the product. (See "Starting the server" after extracting )
https://docs.wso2.com/display/SS110/Running+the+Product

Documentation about Explorer
https://docs.wso2.com/display/SS110/Explore+Cassandra+Cluster

========================================================================

Cassandra GUI evolved from its first version and new version includes bug fixes and enhanced features.

New features.

  • Complete pagination for Row view of explorer
  • Search rows by their names. (Filtered on the fly as you type.)
  • Filtering non displayable data and label them with warnings.

Bug Fixes

  • Remote connection problem
  • Connect to Remote Cassandra server without restarting the server.

Start the Server


Extract the downloaded product and lets refer extracted folder as CARBON_HOME.

go to CARBON_HOME/bin and run sh wso2server.sh (linux) or
wso2server.bat

Log in to the admin console using https://localhost:9443/
default user name and password : admin, admin

Following screen shots include a quick flow on how it works.


1) Click connect to cluster on right hand side panel. Give connection URL and credentials (if there is any) to connect.

eg: URL = localhost:9160,  or  10.100.3.234:

2) After a Successful connection you will be directed to keyspace listing page. Which will include Keyspaces and clickable column family names. Click on a Column Family to Explore data.




3) Row view Page.
After clicking the column family you have landed in the row view page. It includes rows of your column family and a slice of column data as a summary.

You can search, paginate or change no of items to filter your data. Click "view more" to explore a single row.



4) Column family view page will list all the columns in a single row. You can filter the data with column name,value or time stamp. full numbered pagination is available. 


Saturday, May 12, 2012

Cassandra Explorer - GUI for Cassandra

Please go to following url for the newest version.
http://blog.shelan.org/2012/06/cassandra-gui-20-making-things-little.html


WSO2 products can manage a Cassandra Cluster using Cassandra feature.[1]  (No need to follow the instructions to setup cassandra I have added a pre installed version [2])
If you have a Cassandra cluster i am sure you have the problem of viewing the data instantly. Cassandra CLI is one of the tools but it would be easier if it has a nice UI and would more convenient for administrators. Cassandra Explore has been developed to make the things easier for Cassandra users.

Download the Cassandra Feature pre installed WSO2 Carbon Server. [2] . (Until we officially release Cassandra Explorer with Cassandra feature lets use this.)

Extract the downloaded product and lets refer extracted folder as CARBON_HOME.

You have two different options with Cassandra feature.

1. Use internal Cassandra Server .

In this it will start internal Cassandra Server and you can use it to store data.

2. User External Cassandra Cluster.

This option if you already have setup your Cassandra Cluster and you need to view data. You need to
add your Cassandra Clusters details in CARBON_HOME/repository/conf/etc/cassandra-component.xml

Starting Carbon Server

1) If you need to use internal server. go to CARBON_HOME/bin and run sh wso2server.sh (linux) or
wso2server.bat

 If you need to connect to an external cassandra cluster you need to run same wso2server.sh with following parameter

sh wso2server.sh -Ddisable.cassandra.server.startup=true

and log in with default username and password (admin, admin)

List Key Spaces.

Click Cassandra Keyspaces ---> List to list down keyspaces.




Select a Key Space

Select a keyspace from available keyspaces.


Select a Column Family to Explore Data.

Click on Explore icon to view data in a Column Family.


View Data on Column Family.

This will display rows and columns in the Column Family. In a Row you can paginate through Columns. If you need to retrieve a key quickly enter your row ID and click explore row.



View data on a Row

Click on a row link to view data.

You can search  a Name or Value through search Box and will update the results as you type.


[1] http://wso2.org/library/knowledge-base/2012/04/deploy-cassandra-feature-wso2-carbon-product

[2] https://www.dropbox.com/s/m00uodj1ymkpdzb/wso2carbon-4.0.0-SNAPSHOT.zip

Sunday, March 11, 2012

How to change default temporary folder in WSO2 carbon based products

In wso2 carbon based products default temporary folder is {product_home}/tmp.

 If you need to change that location to a different location here is the way to do it.

 You can change the location by changing the locations in start up scripts.

 1) In Linux (/bin/wso2server.sh) change following parameter's location.

 -Djava.io.tmpdir="$CARBON_HOME/tmp"

 2) In Windows (/bin/wso2server.bat) change the following parameter's location.
 -Djava.io.tmpdir="%CARBON_HOME%\tmp"

Monday, March 5, 2012

How to call MySQL stored procedures from WSO2 Data Services Server

 Stored procedures allows you to expose pre defined queries as an api allowing users to consume them in a much faster way.

Requirements.

MySQL Database server
WSO2 Data Service Server (DSS) 2.6.3
MySQL JDBC driver

Note: <DSS_HOME> is the root folder of your unpacked DSS server.

1.) Unpack WSO2 DSS and drop MySQL JDBC driver in <DSS_HOME>/repository/componets/lib folder.
2) Start the server using the start script <DSS_HOME>/bin/wso2server.sh  (In linux or wso2server.bat in windows)

Prepare Database

use following queries to generate a database with a stored procedure. [1]


DROP DATABASE IF EXISTS DATASERVICE_SAMPLE;
CREATE DATABASE DATASERVICE_SAMPLE;
GRANT ALL ON DATASERVICE_SAMPLE.* TO 'dsuser'@'localhost' IDENTIFIED BY 'user123';

USE DATASERVICE_SAMPLE;

DROP TABLE IF EXISTS Employees;

CREATE TABLE Employees(
        employeeNumber INTEGER,
        lastName VARCHAR(50),
        firstName VARCHAR(50),
        extension VARCHAR(10),
        email VARCHAR(100),
        officeCode VARCHAR(10),
        reportsTo INTEGER,
        jobTitle VARCHAR(50) 
);


insert into Employees values (1002,'Murphy','Diane','x5800','dmurphy@classicmodelcars.com','1',null,'President');
insert into Employees values (1056,'Patterson','Mary','x4611','mpatterso@classicmodelcars.com','1',1002,'VP Sales');
insert into Employees values (1076,'Firrelli','Jeff','x9273','jfirrelli@classicmodelcars.com','1',1002,'VP Marketing');
insert into Employees values (1088,'Patterson','William','x4871','wpatterson@classicmodelcars.com','6',1056,'Sales Manager (APAC)');
insert into Employees values (1102,'Bondur','Gerard','x5408','gbondur@classicmodelcars.com','4',1056,'Sale Manager (EMEA)');
insert into Employees values (1143,'Bow','Anthony','x5428','abow@classicmodelcars.com','1',1056,'Sales Manager (NA)');
insert into Employees values (1165,'Jennings','Leslie','x3291','ljennings@classicmodelcars.com','1',1143,'Sales Rep');

DROP PROCEDURE If EXISTS getEmployee;
CREATE PROCEDURE getEmployee(empNo INTEGER) SELECT employeeNumber,lastName,firstName,email FROM Employees where employeeNumber = empNo;
 Create Data Service.

1) click on DataService -----> Create

2) Add new Data Source.



3) Click next and Add new Query.


Here ? denotes the parameter required for the SQL query.

4) Add input parameter mapping 

Since we need to have an input parameter for the Data Service lets define it.


5) Add output Mapping.

There are elements in the output result we need to map. There are four output Mappings we need to define and i following screenshot shows adding one of them.


After adding all of them, Here how it looks like.


6) Click save and go to next page to add new operation. Click on "Add query params as operation params" to add input parameters for operation.


7) Click save and then click finish to deploy the Data Service. After few seconds refresh the web services list to view newly added Data Service.

8) Click on "Try this Service" to test the service. And then test your StoredProcedure exposed as a data service through WSO2 Data Services Server.

(here i have used 1002 as the employeeNo)




Thursday, March 1, 2012

Adding JMS transport for WSO2 Data Services Server

Pre requisites


1) Configuring JMS transport.
  • After extracting Data Services Binary navigate to {DSS-HOME}/repository/conf
  • Open axis2.xml in your editor
  • Uncomment the following section
for Transport Listner
 <!--Uncomment this and configure as appropriate for JMS transport support, after setting up your JMS environment (e.g. ActiveMQ)-->
    <transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
        <parameter name="myTopicConnectionFactory" locked="false">
         <parameter name="java.naming.factory.initial" locked="false">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
         <parameter name="java.naming.provider.url" locked="false">tcp://localhost:61616</parameter>
         <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">TopicConnectionFactory</parameter>
  <parameter name="transport.jms.ConnectionFactoryType" locked="false">topic</parameter>
        </parameter>

        <parameter name="myQueueConnectionFactory" locked="false">
         <parameter name="java.naming.factory.initial" locked="false">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
         <parameter name="java.naming.provider.url" locked="false">tcp://localhost:61616</parameter>
         <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">QueueConnectionFactory</parameter>
  <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
        </parameter>

        <parameter name="default" locked="false">
         <parameter name="java.naming.factory.initial" locked="false">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
         <parameter name="java.naming.provider.url" locked="false">tcp://localhost:61616</parameter>
         <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">QueueConnectionFactory</parameter>
  <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
        </parameter>
    </transportReceiver>
For Transport Sender
 <!-- uncomment this and configure to use connection pools for sending messages-->
     <transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>
  • Add the required jars to <DSS_HOME>/repository/components/lib folder. 
  • Start ActiveMQ using command "sh <ActiveMQ_HOME>/bin/activemq start"

You can explore your ActiveMQ queues for services using JConsole. ( Type Jconsole in linux shell.)