Friday, October 17, 2014

Adding new permissions to permission tree in WSO2 Identity Server programmatically

Let's start the WSO2 Identity Server (IS) first. To download and run the WSO2 IS, look here.

The permission tree in WSO2 identity Server is simply,  a few collections stored in the underlying registry. Therefore adding new permissions to the permission tree involves adding a meaningful collection underneath a needed permission (collection). All these resources are stored in the registry path; /_system/governance/permission/admin. You can view this permission tree by navigating to an existing roles permission. To do that, navigate to Configure -> Users and Roles -> Roles -> Internal/everyone (Permissions) in the WSO2 IS. In this article I'm gonna write a sample Registry Client to add collections under /_system/governance/permission/admin to add new permissions to the permission tree. There's a permission named /_system/governance/permission/admin/login. We are going to add a permission named WebLogin under this. What needs to happen is simply creating a collection under this collection and assign a property called 'name' with its value being the name that needs to get displayed in the permission tree. This can be easily done using the management console of the WSO2 IS. What if someone needs to do this programmatically? Read further down to see how to!!

Adding a resource or collection under another collection in a product like WSO2 IS involves few works. We can obtain some registry based services from a service client named WSRegistryServiceClient. Retrieving this service and performing operations like get and put on top of Registry instance is the easiest way to manipulate the resources in the registry tree. But this service feature is not shipped with some of the WSO2 products including WSO2 IS by default. Therefore it's required to add this feature to WSO2 IS. Following URL explains how to add a feature to any WSO2 product.

https://docs.wso2.com/display/Carbon420/Installing+Features+via+the+UI

Look for WS API feature under the category of Governance Registry section. Once installed you can use the Registry API's to add collections under the above collection /_system/governance/permission/admin.

Following sample client java program adds the said collection.
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient;

import java.io.File;

public class RegistryClient {
    private static final String CARBON_HOME = "/home/shazni/Downloads/WSO2/wso2is-5.0.0";
    private static final String axis2Repo = CARBON_HOME + File.separator + "repository" + File.separator + "deployment" + File.separator + "client";
    private static final String axis2Conf = CARBON_HOME + "/repository/conf/axis2/axis2_client.xml";
    private static final String username = "admin";
    private static final String password = "admin";
    private static final String serverURL = "https://localhost:9443/services/";

    private static WSRegistryServiceClient initialize() throws Exception {
        System.setProperty("javax.net.ssl.trustStore", CARBON_HOME + File.separator + "repository" +
                File.separator + "resources" + File.separator + "security" + File.separator +
                "wso2carbon.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
        System.setProperty("javax.net.ssl.trustStoreType", "JKS");
        System.setProperty("carbon.repo.write.mode", "true");
        ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(axis2Repo, axis2Conf);
        return new WSRegistryServiceClient(serverURL, username, password, configContext);
    }

    public static void main(String[] args) throws Exception {
        Registry registry = initialize();

        Collection newCol = registry.newCollection();
        newCol.setProperty("name", "WebLogin");

        registry.put("/_system/governance/permission/admin/login/WebLogin/", newCol);
    }
}
Change the CARBON_HOME variable path to the IS_HOME of your server. Further, to build this you need to add the plugins directory of a Governance Registry product to the class path. Then if you run this code, you get a collection called WebLogin created under /_system/governance/permission/admin

Hope this article was useful for any WSO2 product user.

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Shazni,

    After adding all the dependencies to the class path, I am getting the below error on eclipse.

    :: axis ::C:/Softwares/wso2is-5.3.0/wso2is-5.3.0/repository/deployment/client
    :: axis ::C:/Softwares/wso2is-5.3.0/wso2is-5.3.0/repository/conf/axis2/axis2_client.xml
    Jan 12, 2018 8:14:04 PM org.apache.axis2.deployment.DeploymentEngine doDeploy
    SEVERE: Invalid module : addressing-1.6.1-wso2v20.mar caused null; nested exception is:
    java.lang.InstantiationException
    org.apache.axis2.AxisFault: null; nested exception is:
    java.lang.InstantiationException
    at org.apache.axis2.deployment.util.Utils.addFlowHandlers(Utils.java:64)
    at org.apache.axis2.deployment.DeploymentEngine.addNewModule(DeploymentEngine.java:159)
    at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:480)
    at org.apache.axis2.deployment.RepositoryListener.init(RepositoryListener.java:155)
    at org.apache.axis2.deployment.RepositoryListener.(RepositoryListener.java:53)
    at org.apache.axis2.deployment.DeploymentEngine.load(DeploymentEngine.java:558)
    at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:59)
    at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:33)
    at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:82)
    at RegistryClient.initialize(RegistryClient.java:26)
    at RegistryClient.main(RegistryClient.java:33)
    Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.axis2.deployment.util.Utils.addFlowHandlers(Utils.java:60)
    ... 10 more

    Exception in thread "main" java.lang.NoSuchMethodError: org.apache.axis2.description.HandlerDescription.(Ljava/lang/String;)V
    at org.apache.rampart.handler.RampartReceiver.(RampartReceiver.java:57)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.axis2.deployment.util.Utils.getHandlerClass(Utils.java:145)
    at org.apache.axis2.deployment.util.Utils.addFlowHandlers(Utils.java:57)
    at org.apache.axis2.deployment.DeploymentEngine.addNewModule(DeploymentEngine.java:159)
    at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:480)
    at org.apache.axis2.deployment.RepositoryListener.init(RepositoryListener.java:155)
    at org.apache.axis2.deployment.RepositoryListener.(RepositoryListener.java:53)
    at org.apache.axis2.deployment.DeploymentEngine.load(DeploymentEngine.java:558)
    at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:59)
    at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:33)
    at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:82)
    at RegistryClient.initialize(RegistryClient.java:26)
    at RegistryClient.main(RegistryClient.java:33)

    What could be the issue?

    ReplyDelete
    Replies
    1. Hi Shazni,

      I have resolved the above issue. Now I am getting the below exception.

      Exception in thread "main" org.wso2.carbon.registry.core.exceptions.RegistryException: Failed to perform put operation.
      at org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient.put(WSRegistryServiceClient.java:406)
      at RegistryClient.main(RegistryClient.java:50)
      Caused by: org.apache.axis2.AxisFault: The service cannot be found for the endpoint reference (EPR) https://localhost:9443/services/WSRegistryService
      at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
      at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:370)
      at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:445)
      at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
      at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
      at org.wso2.carbon.registry.ws.stub.WSRegistryServiceStub.wSput(WSRegistryServiceStub.java:4678)
      at org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient.put(WSRegistryServiceClient.java:395)
      ... 1 more

      Delete
    2. After resolving the dependency jars, I am able to add permissions to the tree now. Great Post.

      Thanks a bunch.

      Delete