Skip to content

Commit

Permalink
Added Redis based stores (security & clients)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvermillard authored and sbernard31 committed Jun 14, 2016
1 parent 86c9f9f commit 117f192
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 23 deletions.
5 changes: 4 additions & 1 deletion leshan-core/src/main/java/org/eclipse/leshan/LinkObject.java
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,7 +31,9 @@
*/
// TODO this class should not have a lwM2M flavor.
// TODO we should have a look at org.eclipse.californium.core.coap.LinkFormat
public class LinkObject {
public class LinkObject implements Serializable {

private static final long serialVersionUID = 1L;

private final String url;

Expand Down
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.server.client;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;
Expand All @@ -31,7 +32,9 @@
/**
* An immutable structure which represent a LW-M2M client registered on the server
*/
public class Client {
public class Client implements Serializable {

private static final long serialVersionUID = 1L;

private static final long DEFAULT_LIFETIME_IN_SEC = 86400L;

Expand Down Expand Up @@ -109,9 +112,9 @@ protected Client(String registrationId, String endpoint, InetAddress address, in
this.bindingMode = bindingMode == null ? BindingMode.U : bindingMode;
this.registrationDate = registrationDate == null ? new Date() : registrationDate;
this.lastUpdate = lastUpdate == null ? new Date() : lastUpdate;
this.additionalRegistrationAttributes = additionalRegistrationAttributes == null ? Collections
.unmodifiableMap(new HashMap<String, String>()) : Collections
.unmodifiableMap(additionalRegistrationAttributes);
this.additionalRegistrationAttributes = additionalRegistrationAttributes == null
? Collections.unmodifiableMap(new HashMap<String, String>())
: Collections.unmodifiableMap(additionalRegistrationAttributes);

}

Expand Down Expand Up @@ -280,10 +283,10 @@ public Map<String, String> getAdditionalRegistrationAttributes() {

@Override
public String toString() {
return String
.format("Client [registrationDate=%s, address=%s, port=%s, registrationEndpoint=%s, lifeTimeInSec=%s, smsNumber=%s, lwM2mVersion=%s, bindingMode=%s, endpoint=%s, registrationId=%s, objectLinks=%s, lastUpdate=%s]",
registrationDate, address, port, registrationEndpointAddress, lifeTimeInSec, smsNumber,
lwM2mVersion, bindingMode, endpoint, registrationId, Arrays.toString(objectLinks), lastUpdate);
return String.format(
"Client [registrationDate=%s, address=%s, port=%s, registrationEndpoint=%s, lifeTimeInSec=%s, smsNumber=%s, lwM2mVersion=%s, bindingMode=%s, endpoint=%s, registrationId=%s, objectLinks=%s, lastUpdate=%s]",
registrationDate, address, port, registrationEndpointAddress, lifeTimeInSec, smsNumber, lwM2mVersion,
bindingMode, endpoint, registrationId, Arrays.toString(objectLinks), lastUpdate);
}

/**
Expand Down
Expand Up @@ -83,6 +83,9 @@ public String getEndpoint() {
return endpoint;
}

/**
* The Pre-Shared-Key identity
*/
public String getIdentity() {
return identity;
}
Expand Down
6 changes: 6 additions & 0 deletions leshan-server-demo/pom.xml
Expand Up @@ -60,6 +60,12 @@ Contributors:
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>

<!-- runtime dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Expand Up @@ -41,6 +41,8 @@
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.leshan.server.californium.LeshanServerBuilder;
import org.eclipse.leshan.server.californium.impl.LeshanServer;
import org.eclipse.leshan.server.demo.cluster.RedisClientRegistry;
import org.eclipse.leshan.server.demo.cluster.RedisSecurityRegistry;
import org.eclipse.leshan.server.demo.servlet.ClientServlet;
import org.eclipse.leshan.server.demo.servlet.EventServlet;
import org.eclipse.leshan.server.demo.servlet.ObjectSpecServlet;
Expand All @@ -50,6 +52,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.util.Pool;

public class LeshanServerDemo {

private static final Logger LOG = LoggerFactory.getLogger(LeshanServerDemo.class);
Expand All @@ -67,6 +73,8 @@ public static void main(String[] args) {
options.addOption("slh", "coapshost", true, "Set the secure local CoAP address.\nDefault: any local address.");
options.addOption("slp", "coapsport", true, "Set the secure local CoAP port.\nDefault: 5684.");
options.addOption("wp", "webport", true, "Set the HTTP port for web server.\nDefault: 8080.");
options.addOption("r", "redis", true,
"Set the redis hostname:port for running in cluster mode.\nDefault: none, no redis connection.");
HelpFormatter formatter = new HelpFormatter();
formatter.setOptionComparator(null);

Expand Down Expand Up @@ -131,35 +139,50 @@ public static void main(String[] args) {
webPort = Integer.parseInt(webPortOption);
}

// get the Redis hostname:port
String redisUrl = null;
if (cl.hasOption("r")) {
redisUrl = cl.getOptionValue("r");
}

try {
createAndStartServer(webPort, localAddress, localPort, secureLocalAddress, secureLocalPort);
createAndStartServer(webPort, localAddress, localPort, secureLocalAddress, secureLocalPort, redisUrl);
} catch (BindException e) {
System.out.println(String.format("Web port %s is alreay used, you could change it using 'webport' option.",
webPort));
System.out.println(
String.format("Web port %s is alreay used, you could change it using 'webport' option.", webPort));
formatter.printHelp(USAGE, null, options, FOOTER);
} catch (Exception e) {
LOG.error("Jetty stopped with unexcepted error ...", e);
}
}

public static void createAndStartServer(int webPort, String localAddress, int localPort, String secureLocalAddress,
int secureLocalPort) throws Exception {
int secureLocalPort, String redisUrl) throws Exception {
// Prepare LWM2M server
LeshanServerBuilder builder = new LeshanServerBuilder();
builder.setLocalAddress(localAddress, localPort);
builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);

// connect to redis if needed
Pool<Jedis> jedis = null;
if (redisUrl != null) {
String[] parts = redisUrl.split("\\:");

// TODO: support sentinel pool and make pool configurable
jedis = new JedisPool(parts[0], parts.length > 1 ? Integer.valueOf(parts[1]) : 6379);
}

// Get public and private server key
PrivateKey privateKey = null;
PublicKey publicKey = null;
try {
// Get point values
byte[] publicX = Hex.decodeHex("fcc28728c123b155be410fc1c0651da374fc6ebe7f96606e90d927d188894a73"
.toCharArray());
byte[] publicY = Hex.decodeHex("d2ffaa73957d76984633fc1cc54d0b763ca0559a9dff9706e9f4557dacc3f52a"
.toCharArray());
byte[] privateS = Hex.decodeHex("1dae121ba406802ef07c193c1ee4df91115aabd79c1ed7f4c0ef7ef6a5449400"
.toCharArray());
byte[] publicX = Hex
.decodeHex("fcc28728c123b155be410fc1c0651da374fc6ebe7f96606e90d927d188894a73".toCharArray());
byte[] publicY = Hex
.decodeHex("d2ffaa73957d76984633fc1cc54d0b763ca0559a9dff9706e9f4557dacc3f52a".toCharArray());
byte[] privateS = Hex
.decodeHex("1dae121ba406802ef07c193c1ee4df91115aabd79c1ed7f4c0ef7ef6a5449400".toCharArray());

// Get Elliptic Curve Parameter spec for secp256r1
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
Expand All @@ -175,9 +198,17 @@ public static void createAndStartServer(int webPort, String localAddress, int lo
publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
privateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec);

builder.setSecurityRegistry(new SecurityRegistryImpl(privateKey, publicKey));
if (jedis == null) {
// in memory security registry (with file persistence)
builder.setSecurityRegistry(new SecurityRegistryImpl(privateKey, publicKey));
} else {
// use Redis
builder.setSecurityRegistry(new RedisSecurityRegistry(jedis, privateKey, publicKey));
builder.setClientRegistry(new RedisClientRegistry(jedis));
}
} catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidParameterSpecException e) {
LOG.warn("Unable to load RPK.", e);
LOG.error("Unable to initialize RPK.", e);
System.exit(-1);
}

// Create and start LWM2M server
Expand All @@ -196,8 +227,8 @@ public static void createAndStartServer(int webPort, String localAddress, int lo
ServletHolder eventServletHolder = new ServletHolder(eventServlet);
root.addServlet(eventServletHolder, "/event/*");

ServletHolder clientServletHolder = new ServletHolder(new ClientServlet(lwServer, lwServer.getSecureAddress()
.getPort()));
ServletHolder clientServletHolder = new ServletHolder(
new ClientServlet(lwServer, lwServer.getSecureAddress().getPort()));
root.addServlet(clientServletHolder, "/api/clients/*");

ServletHolder securityServletHolder = new ServletHolder(new SecurityServlet(lwServer.getSecurityRegistry()));
Expand Down

0 comments on commit 117f192

Please sign in to comment.