Wednesday, September 23, 2015

How WSO2 API Manager gateway to back end secure connection works and troubleshoot SSL gateway issues.

Whenever Java(here in this care WSO2 API Manager act as JVM) attempts to connect to another application over SSL, it will only be able to connect to that application if it can trust it. The way trust is handled in the Java world is that you have a key store (typically $JAVA_HOME/lib/security/cacerts), also known as the trust store. This contains a list of all known Certificate Authority (CA) certificates, and Java will only trust certificates that are signed by one of those CAs or public certificates that exist within that key store.

This problem is therefore caused by a certificate that is self-signed (a CA did not sign it) or a certificate chain that does not exist within the Java key store. Java does not trust the certificate and fails to connect to the application.

We can simply test it with following commands. Here i will use external class to perform SSL poke(class) using our client trust store.

For 1st URL

sanjeewa@sanjeewa-ThinkPad-T530:~/Downloads$ java SSLPoke 443
    Successfully connected

For 2nd URL

    sanjeewa@sanjeewa-ThinkPad-T530:~/Downloads$ java SSLPoke 443 PKIX path building failed: unable to find valid certification path to requested target
    at SSLPoke.main(
    Caused by: unable to find valid certification path to requested target
    ... 15 more

As you can see first will print "Successfully connected". Then clearly 2nd one is self signed certificate which does not reside in our trust store. And that cause issue here.
Even if we have host name verification or not it doesn't matter here.

Host name verifier will come next.
If we didn't put host name verifier AllowAll in axis2.xml for first end point ( then you will see following.

    [2015-09-22 18:36:36,765] ERROR - TargetHandler I/O error: Host name verification failed for host : Host name verification failed for host :
    at org.apache.synapse.transport.http.conn.ClientSSLSetupHandler.verify(
    at org.apache.http.nio.reactor.ssl.SSLIOSession.doHandshake(
    at org.apache.http.nio.reactor.ssl.SSLIOSession.isAppInputReady(
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(
    at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$

During handshaking, if the URL's host name and the server's identification host name mismatch, the verification mechanism can call back to implements of interface(what we have done is override it with property defined in axis2.xml file and return true if property is there) to determine if this connection should be allowed. The policies can be certificate-based or may depend on other authentication schemes. Here second URL failed not because host name issue its failing because its self signed certificate and java does not trusted it. Even first one will fail if we didn't enabled host name verification.

Please refer SSLPoke(class) class for your reference.

Monday, September 14, 2015

How to add 2 step authentication flow to WSO2 APP Manager.

WSO2 App Manager provides a unique one-stop store app management solution where users can pick and choose apps required for them to do their jobs efficiently. Businesses can leverage its single-sign-on (SSO) functionality, which can reduce help desk and administrative costs with no long lists of passwords to memorise. It automatically applies a security layer on top of the web apps published in the store, which eliminates the need to embed security rules at the application layer.

In this article we will discuss how we can add two step authentication when we register service provider.

When you create application in app manager it will call to SSOConfigurator class we defined in app-manager.xml to create service providers.
So i implemented custom class to do it. Class name is TESTIS500SAMLSSOConfigurator and it implemented SSOConfigurator.

Within this class we will create SP and store it. So when we create SP we will add 2 authentication steps.
1. Authentication step to redirect users to Dictao to login system.
2. Then add local authentication step to validate subscriptions and let user to accept terms and conditions if he haven't already done.

We have added few parameters to app-manager.xml to make this configurable. With that new configuration would be something like this. Actually if you need you can add more parameters according to your requirement.

        <!--class name of new sso configurator class-->                
        <!--IDP name which need to be engage in authentication step 01. You have to create IDP with this name-->    
        <!--The name of authenticator we should used in step 02. This authenticator is another custom implementation -->    
            <!--Authentication mechanism of step one--> 

Following code block will be use to add 2 step authentication and engage IDP, Authenticators to authentication flow. You can modify TESTIS500SAMLSSOConfigurator class by adding following.

   if (idpName != null && authenticationStep != null && authenticationStep.equalsIgnoreCase("federated")) {
            if (log.isDebugEnabled()) {
                log.debug("Adding federated authentication step. Added IDP named: " + idpName);
            //Following code will set external IDP as authentication EP
            InboundProvisioningConfig inBoundProConfig = new InboundProvisioningConfig();
            serviceProvider.setOutboundProvisioningConfig(new OutboundProvisioningConfig());
            AuthenticationStep[] steps = new AuthenticationStep[2];

            //Add local authenticator
            AuthenticationStep step1 = new AuthenticationStep();
            List<LocalAuthenticatorConfig> localAuthList = new ArrayList<LocalAuthenticatorConfig>();
            LocalAuthenticatorConfig localAuth = new LocalAuthenticatorConfig();
            step1.setLocalAuthenticatorConfigs(localAuthList.toArray(new LocalAuthenticatorConfig[localAuthList.size()]));

            //Add federated authenticator
            AuthenticationStep step = new AuthenticationStep();
            List<IdentityProvider> federatedAuthList = new ArrayList<IdentityProvider>();
            FederatedAuthenticatorConfig federatedAuthenticatorConfig = new FederatedAuthenticatorConfig();
            IdentityProvider identityProvider = new IdentityProvider();
            identityProvider.setFederatedAuthenticatorConfigs(new FederatedAuthenticatorConfig[]{federatedAuthenticatorConfig});
            step.setFederatedIdentityProviders(federatedAuthList.toArray(new IdentityProvider[federatedAuthList.size()]));
             //Here federated authenticator would be added as step 01 and local authenticator would be step 02.
            steps[0] = step;
            steps[1] = step1;
            serviceProvider.setPermissionAndRoleConfig(new PermissionsAndRoleConfig());

Then once you create application it will automatically register service provider with 2 step authentication.
Once you go to service provider user interface you will see authentication steps as follows.

Complete source code.

Tuesday, September 1, 2015

How to set password validation policy in WSO2 Identity Server

If you need to add custom password policy there are multiple layers you can add that. First one is user-mgt.xml file and other configuration file is file.

If identity management listener is enabled(only), user passwords should be satisfied both both regrEx defined in user-mgt.xml and files. Otherwise we will check user-mgt.xml to validate password policy.

 <Property name="PasswordJavaRegEx">^[\S]{5,30}$</Property>

Following properties will be picked only if we enabled identity listener(Identity.Listener.Enable=true). Otherwise configurations on user management xml will only affect.