Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
848f42b
Updating samples so it refers to the new 'shared' service plan, so it…
dickeyf Sep 28, 2016
efeeead
Merge pull request #1 from dickeyf/master
dickeyf Oct 6, 2016
a215c9d
Merge branch 'master' of https://github.com/SolaceDev/solace-samples-…
benhaig Oct 6, 2016
6512204
Update "Common Setup"
benhaig Oct 7, 2016
a70436a
Improve deployment instructions
benhaig Oct 7, 2016
83a7513
Fixed links to tutorials
benhaig Oct 7, 2016
b6d62ef
Update VCAP_SERVICES example
benhaig Oct 7, 2016
f37c8af
Updated service instance name to match sample code
benhaig Oct 7, 2016
ca68c2b
Merge branch 'master' of https://github.com/SolaceSamples/solace-samp…
MichaelDavisSolace Nov 22, 2016
6ff051b
Added tls support
MichaelDavisSolace Nov 22, 2016
dd5e7de
Merge branch 'master' of https://github.com/SolaceSamples/solace-samp…
MichaelDavisSolace Nov 30, 2016
5b3905d
java-tls work in progress.
MichaelDavisSolace Dec 2, 2016
fba5ff4
Added secure-app, the TLS tutorial
MichaelDavisSolace Dec 9, 2016
7a7df8e
minor improvements to the secure-sessions tutorial.
MichaelDavisSolace Dec 9, 2016
f347576
Editorial changes from review
mdspielman Dec 9, 2016
02bb150
Added build dir to gitignore
MichaelDavisSolace Dec 13, 2016
320dbc7
Merge remote-tracking branch 'origin/mdspielman-patch-1' into tls-sup…
MichaelDavisSolace Dec 13, 2016
89cb009
Merge remote-tracking branch 'public/master'
MichaelDavisSolace Dec 13, 2016
77bca79
Merge branch 'master' into tls-support
MichaelDavisSolace Dec 13, 2016
9590cc9
Improvements to the secure-session tutorial based on Mark Spielman's …
MichaelDavisSolace Dec 14, 2016
c23db37
Improved secure-session tutorial based on the third review.
MichaelDavisSolace Dec 19, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ spring-cloud/build/
libs/
.DS_Store
docs/_site
/.nb-gradle/
/secure-app/build/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This repository contains a sample application modified in the following ways:

* Java Application
* Java Application using Spring Cloud Connectors
* Java Application using Spring Cloud Connectors and Transport Level Security

While not necessary for a Java Application or a straight Spring Cloud Connector applications, the samples in this repository still make use of of Spring Boot so they can easily expose a simple REST interface to provide interactive ways to subscribe, send and receive Solace messages. Spring Boot is not required. You can make use of the Spring Cloud Connectors in any Java Application. See the walk through tutorials for more details.

Expand Down Expand Up @@ -84,6 +85,15 @@ For more details and example usage, see the walk through tutorial here:
* [Online Tutorial](https://solacesamples.github.io/solace-samples-cloudfoundry-java/spring-cloud/)


## Secure Session

application name: `solace-sample-secure-session`

This application is based on the Spring Cloud Connector described above, but shows how to use
Transport Level Security (TLS) between the Java application and the Messaging Service.

* [Online Tutorial](https://solacesamples.github.io/solace-samples-cloudfoundry-java/secure-session/)

## Try out the Applications

The sample applications have a simple REST interface that allows you to subscribe, send and receive Solace messages. You can try the applications out using command like the following.
Expand Down
3 changes: 2 additions & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jcsmp_version: 10.+
docs-core-concepts: http://docs.solace.com/Features/Core-Concepts.htm
links-downloads: http://dev.solace.com/downloads/
links-pubsub-tutorial: http://dev.solace.com/get-started/java-tutorials/publish-subscribe_java/
links-tls-client: http://docs.solace.com/Solace-Messaging-APIs/Creating-Secure-Sessions.htm
links-tls-server: https://docs.pivotal.io/solace-messaging/installing.html#configure
links-vcap: http://docs.pivotal.io/solace-messaging/credentials.html

#----------------------------------------------------
# Generally don't modify items below this point
url: "solacesamples.github.io" # the base hostname & protocol for your site
Expand Down
1 change: 1 addition & 0 deletions docs/_data/tutorials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
tutorials:
- java-app
- spring-cloud
- secure-session
200 changes: 200 additions & 0 deletions docs/_tutorials/secure-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
---
layout: tutorials
title: Secure Session
summary: A simple Spring Cloud application showing how to connect with the Solace Messaging service using Transport Level Security (TLS).
icon: ssl_icon.gif
---

* [Overview](#overview)
* [Goals](#goals)
* [Assumptions](#assumptions)
* [Working with a trusted certificate](#working-with-a-trusted-certificate)
* [Code walk through - trusted certificates](#code-walk-through---trusted-certificates)
* [Working with a self-signed certificate](#working-with-a-self-signed-certificate)
* [Building](#building)
* [Cloud Foundry Setup](#cloud-foundry-setup)
* [Deploying](#deploying)
* [Trying Out the Application](#trying-out-the-application)
* [Common Problems](#common-problems)


## Overview

This tutorial is part of a series of tutorials which aims to introduce users to Solace Messaging in Pivotal Cloud Foundry. Solace Messaging in Pivotal Cloud Foundry is delivered as a tile on the [Pivotal Network](https://network.pivotal.io/){:target="_blank"}. you can see the [Solace Messaging for Pivotal Cloud Foundry documentation](http://docs.pivotal.io/solace-messaging/){:target="_blank"} for full details.


This tutorial is based on the [Spring Cloud]({{ site.baseurl }}/spring-cloud) tutorial, adding a demonstration on how to connect to the Solace Messaging service using TLS.

## Goals

The goal of this tutorial is to demonstrate how to connect to the Solace Messaging service instance using TLS. This tutorial will show you how to establish a connection to the solace messaging service using both self-signed and Certificate Authority approved certificates.

## Assumptions

This tutorial assumes the following:

* You have completed the [Spring Cloud]({{ site.baseurl }}/spring-cloud) tutorial.
* You are familiar with [Transport Level Security](https://en.wikipedia.org/wiki/Transport_Layer_Security){:target="_blank"} concepts, including how to create or obtain certificates.


## Working with a Trusted Certificate

In order to use Transport Level Security, your Solace Message Router needs to be configured for TLS with an installed servcer certificate.
The instructions for doing that are described in the Pivotal/Solace Messaging documentation -
see [Tile Installation and Configuration]({{ site.links-tls-server }}){:target="_blank"},
look for the step named Configure Message Routers RSA certificate.

This section assumes that you have a Solace Messaging Service Instance properly configured for TLS access with a certificate that has been purchased from a certificate authority. Instructions for working with self-signed certificates are below.

## Code Walk Through - Trusted Certificates

when we initialize the connection to the service, we can specify whether to use TLS or not. to use TLS, we replaced this line from the Spring Cloud tutorial:

```java
properties.setProperty(JCSMPProperties.HOST, solaceMessagingServiceInfo.getSmfHost());
```

with this:

```java
properties.setProperty(JCSMPProperties.HOST, solaceMessagingServiceInfo.getSmfTlsHost());
```

When you use the TLS host, you can also specify whether the client should validate the TLS certificate.
This should always be done in production environments. We do this by setting the `JCSMPProperties`:

```java
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE, true);
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE_DATE, true);
properties.setProperty(JCSMPProperties.SSL_TRUST_STORE, "path-to-trust-store");
properties.setProperty(JCSMPProperties.SSL_TRUST_STORE_PASSWORD, "changeit");
```
In Cloud Foundry, the path to the trust store is
`/home/vcap/app/.java-buildpack/open_jdk_jre/lib/security/cacerts`
and the password is the default JRE keystore password, `changeit`.

That is all that is required when using a Certificate Authority issued certificate.
The JRE's trusted store comes pre-configured with certificates that are sufficient
to validated your CA-issued certificate.

## Working with a Self-signed Certificate

You can install your Solace Messaging service for PCF with a self-signed certificate. For instructions on how to do this see the [Solace Messaging Documentation]({{ site.links-tls-server }}){:target="_blank"}.

With self-signed certificates you have two choices. Either you can have the client validate the self-signed certificate,
or not. For testing purposes, you can choose to not validate the certificate simply by
setting the following properties to false:

```java
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE, false);
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE_DATE, false);
```

Note that this is insecure and should never be done in production. This provides you with a TLS encrypted connection to the Solace Message Router but no validation of the identity so you're still exposed to a variety of attacks.

It is also possible to validate the self-signed certificate. This provides an environment that is a little closer to production. To do this, you must provide a certificate to the client-side trust store. This is tricky because the trust store is bundled along with the JRE together with the Cloud Foundry app. It must be added after the application is deployed but before it connects with the Solace Messaging service. Follow these steps:

1. Copy the certificate (the *.pem file) to the directory secure-app/src/main/resources.
1. Edit the file. Remove the private key section and just leave the lines starting with -----BEGIN CERTIFICATE----- and ending with ----- END CERTIFICATE-----.
1. In the CertificateUtil class, change the `CERTIFICATE_FILE_NAME` to match your certificate's file name.
1. In the SolaceController class, enable certificate validation.

```java
private static final boolean VALIDATE_CERTIFICATE = true;
// and set these further down...
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE, true);
properties.setproperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE_DATE, true);
```

When you set `INSTALL_CERTIFICATE` to `true`, it instructs the SolaceController class to install the certificate in the JRE's trusted store before trying to connect to the service.

The way it works is this. When you copy the certificate into the src/main/resources file, it gets packaged with the application. The application can then read the file from its local file system at runtime.

The standard location for a trusted store, relative to a JRE directory, is in a file at lib/security/cacerts.

The JRE is provided at runtime by Cloud Foundry, and from the point of view of an application the path to the jre is
```
/home/vcap/app/.java-buildpack/open_jdk_jre
```

The SolaceController class reads the certificate and installs it in the trusted store when the application starts running, but before it tries to connect to the service.

Note that we only need to do this when we want the client to validate a self-signed certificate. There should never be a need to do this kind of thing in a production system.

## Building

The full source code for this example is available in [GitHub]({{ site.repository }}){:target="_blank"}. To build, just clone and use gradle. Here is an example:

```
git clone {{ site.repository }}
cd {{ site.baseurl | remove: '/'}}
./gradlew build
```

## Cloud Foundry Setup

The sample application specifies a dependency on a service instance named `solace-messaging-sample-instance` in its manifiest (See `secure-session/manifest.yml`). This must be an instance of the Solace Messaging Service which can be created with this command:

```
cf create-service solace-messaging shared solace-messaging-sample-instance
```

## Deploying

To deploy this tutorial's application you first need to go inside it's project directory and then push the application:

```
cd secure-session
cf push
```

This will push the application and will give the application the name specified by the manifest: `solace-sample-secure-session`.

## Trying Out The Application

As described above, the sample application has a simple REST interface that allows you to:

* Subscribe
* Send a message
* Receive a message

In order to interact with the application you need to determine the application's URL. These shell commands can be used to quickly find out the URL:

```
export APP_NAME=solace-sample-secure-session
export APP_URL=`cf apps | grep $APP_NAME | grep started | awk '{ print $6}'`
echo "The application URL is: ${APP_URL}"
```

To demonstrate the application we will make the application send a message to itself. Then we will read the message back to confirm the successful delivery of the message :

```
# Subscribes the application to the topic "test"
curl -X POST -H "Content-Type: application/json;charset=UTF-8" -d '{"subscription": "test"}' http://$APP_URL/subscription

# Send message with topic "test" and this content: "TEST_MESSAGE"
curl -X POST -H "Content-Type: application/json;charset=UTF-8" -d '{"topic": "test", "body": "Test Message"}' http://$APP_URL/message

# The message should have been asynchronously received by the application. Check that the message was indeed received:
curl -X GET http://$APP_URL/message
```

## Common Problems

If you see:

```
JCSMPTransportException: Error communicating with the router, ConnectException: Connection Refused
```

This can happen if a TLS server certificate was not configured in the Tile in which case the VMR will reject incoming TLS connections.

If you see:
```
CertificateException: Path does not chain with any of the trust anchors
```

it is because the application could not validate the certificate it received from the server. This can happen when you install a self-signed certificate on the router, but didn't package it with the app, or didn't set INSTALL_CERTIFICATE to true.



Binary file added docs/images/ssl_icon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -37,6 +38,9 @@
public class SolaceController {

private static final Log logger = LogFactory.getLog(SolaceController.class);

@Value("${sample.app.ssl.enabled:false}")
protected boolean isSslEnabled;

private JCSMPSession session;
private XMLMessageProducer producer;
Expand Down Expand Up @@ -126,10 +130,18 @@ public void init() {
logger.info("Solace client initializing and using Credentials: " + solaceCredentials.toString(2));

final JCSMPProperties properties = new JCSMPProperties();
properties.setProperty(JCSMPProperties.HOST, solaceCredentials.getString("smfHost"));
String host = isSslEnabled ? solaceCredentials.getString("smfTlsHost") : solaceCredentials.getString("smfHost");
properties.setProperty(JCSMPProperties.HOST, host);
properties.setProperty(JCSMPProperties.VPN_NAME, solaceCredentials.getString("msgVpnName"));
properties.setProperty(JCSMPProperties.USERNAME, solaceCredentials.getString("clientUsername"));
properties.setProperty(JCSMPProperties.PASSWORD, solaceCredentials.getString("clientPassword"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need to modify this tutorial. All TLS is handled in your secure sessions

if (isSslEnabled) {
properties.setProperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE, false);
properties.setProperty(JCSMPProperties.SSL_VALIDATE_CERTIFICATE_DATE, false);
}

logger.info("Host: " + host + " isSslEnabled: " + isSslEnabled);

try {
session = JCSMPFactory.onlyInstance().createSession(properties);
Expand Down
50 changes: 50 additions & 0 deletions secure-session/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
buildscript {
ext {
springBootVersion = '1.3.3.RELEASE'
springCloudVersion = '1.2.3.RELEASE'
solaceJCSMPVersion = '10.+'
solaceCloudVersion = '0.1+'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
baseName = 'solace-sample-secure-session'
version = ''
}

repositories {
mavenCentral()
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")

// Spring Cloud Connectors
compile("org.springframework.cloud:spring-cloud-spring-service-connector:${springCloudVersion}")
compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector:${springCloudVersion}")

// Alternatively using spring-boot-starter inclusion...
//compile("org.springframework.boot:spring-boot-starter-cloud-connectors:${springBootVersion}")

// Solace Messaging Spring Cloud Connector
compile("com.solace.labs.cloud.cloudfoundry:solace-labs-spring-cloud-connector:${solaceCloudVersion}")

// Solace Java API
compile("com.solacesystems:sol-jcsmp:${solaceJCSMPVersion}")
}

task wrapper(type: Wrapper) {
gradleVersion = '2.14'
}
7 changes: 7 additions & 0 deletions secure-session/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
applications:
- name: solace-sample-secure
memory: 512M
path: build/libs/solace-sample-secure-session.jar
services:
- solace-messaging-sample-instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.
*/

package com.solace.samples.cloudfoundry.securesession;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Loading