Saturday, July 18, 2015

RabbitMQ .NET client to connect to WSO2 MB with SSL.

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging both secured and unsecured.

This post will explain on how the RabbitMQ .NET client can be used to publish or subscribe to WSO2 Message Broker 3.0.0 securely.

Creating the Certificate

Go to "<MB_HOME>/repository/resources/security" folder and run the following command. This will create a "cert" file which is the certification file for SSL communication. "wso2carbon.jks" is the trust store file. So we are exporting the certificate from the trust store. The "keytool" command comes with the JDK distribution.

keytool -export -keystore wso2carbon.jks -storepass wso2carbon -file carbon.cert -alias localhost -keypass wso2carbon

The Code

You can use the same implementation as mentioned in [1][2]. Only difference is we have to set the following properties to the "ConnectionFactory" object.

........
........
// The connection factory to connect with the broker.
ConnectionFactory factory = new ConnectionFactory();

// AMQP URL to connect to the broker.
IProtocol protocol = Protocols.AMQP_0_9_1;
factory.VirtualHost = "/carbon";
factory.UserName = "admin";
factory.Password = "admin";
factory.HostName = "localhost";
// Port for SSL
factory.Port = 8672;
factory.Protocol = protocol;

// SSL configuration
factory.Ssl.Enabled = true;
factory.Ssl.CertPassphrase = "wso2carbon";
factory.Ssl.CertPath = @"C:\Users\wso2\Documents\wso2mb-3.0.0-ALPHA\repository\resources\security\carbon.cert";
factory.Ssl.ServerName = "localhost";
factory.Ssl.AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
factory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls;

using (IConnection conn = factory.CreateConnection())
{
    ............
    ............

Set the correct path for "factory.Ssl.CertPath" to the "cert" file we generated in the earlier step.

Thats all guys!. Click here to download the sample .NET project(VS2010).

References...

[1] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Queue
[2] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Topic
[3] - https://www.rabbitmq.com/ssl.html
[4] - http://pathberiya.blogspot.com/2010_08_01_archive.html

Wednesday, July 15, 2015

RabbitMQ client to subscribe to a queue in WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging.

This post will explain on how the RabbitMQ java client can be used to subscribe to a queue WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.3</version>
</dependency>

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Creating consumer
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, false, consumer);

// Shutdown hook handler
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        try {
            // Closing the connection
            channel.close();
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
});

// Accepting messages
while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    String message = new String(delivery.getBody());

    System.out.println("Message Received : " + message);
}

Click here to download the sample maven project.

References...

RabbitMQ client to publish JMS messages to WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that supports AMQP and MQTT.

This post will explain on how the RabbitMQ java client can be used to publish JMS messages to WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.3</version>
</dependency>

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Message content to publish
String message = "This is a test message";

// Creating properties for the message.
AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
properties.messageId("ID:" + String.valueOf(UUID.randomUUID()));
properties.contentType("text/plain");

// Publishing message
channel.basicPublish("amq.direct", queueName, properties.build(), message.getBytes());

System.out.println("Message published : " + message);

// Closing the connection
channel.close();
connection.close();

Output on management console.

Click here to download the sample maven project.

References...

Friday, July 10, 2015

Running WSO2 ESB Sample 200: Using WS-Security with policy attachments for proxy services

Hi All,

An enterprise service bus (ESB) is a software architecture construct that enables communication among various applications. WSO2 Enterprise Service Bus is such a middleware.

This post will explain on how to execute the WSO2 ESB 4.8.1 sample number 200. This sample demonstrates how you can use WS-Security signing and encryption with proxy services through WS-Policy. When running this sample you may face the following exception coming through the client.

     [java] org.apache.axis2.AxisFault: Error in encryption
     [java]     at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:76)
     [java]     at org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)
     [java]     at org.apache.axis2.engine.Phase.invoke(Phase.java:313)
     [java]     at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:261)
     [java]     at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:426)
     [java]     at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:430)
     [java]     at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
     [java]     at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
     [java]     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:554)
     [java]     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:530)
     [java]     at samples.userguide.StockQuoteClient.executeClient(Unknown Source)
     [java]     at samples.userguide.StockQuoteClient.main(Unknown Source)
     [java] Caused by: org.apache.rampart.RampartException: Error in encryption
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:612)
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.build(AsymmetricBindingBuilder.java:97)
     [java]     at org.apache.rampart.MessageBuilder.build(MessageBuilder.java:147)
     [java]     at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
     [java]     ... 11 more
     [java] Caused by: org.apache.ws.security.WSSecurityException: An unsupported signature or encryption algorithm was used (unsupported key transport encryption algorithm: No such algorithm: http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p); nested exception is: 
     [java]     java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
     [java]     at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:785)
     [java]     at org.apache.ws.security.message.WSSecEncryptedKey.prepareInternal(WSSecEncryptedKey.java:205)
     [java]     at org.apache.ws.security.message.WSSecEncrypt.prepare(WSSecEncrypt.java:259)
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:578)
     [java]     ... 14 more
     [java] Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
     [java]     at javax.crypto.Cipher.getInstance(DashoA13*..)
     [java]     at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:777)
     [java]     ... 17 more

If so, you can following these steps below to overcome the issue.

Step 1

Copy the "bcprov-jdk15.jar" from <ESB_HOME>/repository/axis2/client/lib/ folder and paste it in <ESB_HOME>/repository/components/plugins folder.

Step 2

Download the Java Cryptography files for the specific java version you have installed from below.

Step 3

Take a backup of the <JRE_HOME>/lib/security folder and keep it somewhere safe as a safety precaution.

Step 4

Copy the files from the downloaded archive and paste them to <JRE_HOME>/lib/security folder.

By doing above steps I was able to run the sample without a hassle.

References...