From 4ad7307777446f07bcdd12a958c3041ae5157c45 Mon Sep 17 00:00:00 2001 From: Aliaksei Zhuk Date: Mon, 13 Feb 2023 20:05:36 +0300 Subject: [PATCH] Allow to set custom REDIS keys for security endpoint and PSK ID --- .../server/redis/RedisSecurityStore.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/leshan-server-redis/src/main/java/org/eclipse/leshan/server/redis/RedisSecurityStore.java b/leshan-server-redis/src/main/java/org/eclipse/leshan/server/redis/RedisSecurityStore.java index a9d0878166..38f1129f7c 100644 --- a/leshan-server-redis/src/main/java/org/eclipse/leshan/server/redis/RedisSecurityStore.java +++ b/leshan-server-redis/src/main/java/org/eclipse/leshan/server/redis/RedisSecurityStore.java @@ -40,9 +40,9 @@ */ public class RedisSecurityStore implements EditableSecurityStore { - private static final String SEC_EP = "SEC#EP#"; + private final String secEp; - private static final String PSKID_SEC = "PSKID#SEC"; + private final String pskIdSec; private final Pool pool; @@ -50,12 +50,20 @@ public class RedisSecurityStore implements EditableSecurityStore { public RedisSecurityStore(Pool pool) { this.pool = pool; + this.secEp = "SEC#EP#"; + this.pskIdSec = "PSKID#SEC"; + } + + public RedisSecurityStore(Pool pool, String secEp, String pskIdSec) { + this.pool = pool; + this.secEp = secEp; + this.pskIdSec = pskIdSec; } @Override public SecurityInfo getByEndpoint(String endpoint) { try (Jedis j = pool.getResource()) { - byte[] data = j.get((SEC_EP + endpoint).getBytes()); + byte[] data = j.get((secEp + endpoint).getBytes()); if (data == null) { return null; } else { @@ -67,11 +75,11 @@ public SecurityInfo getByEndpoint(String endpoint) { @Override public SecurityInfo getByIdentity(String identity) { try (Jedis j = pool.getResource()) { - String ep = j.hget(PSKID_SEC, identity); + String ep = j.hget(pskIdSec, identity); if (ep == null) { return null; } else { - byte[] data = j.get((SEC_EP + ep).getBytes()); + byte[] data = j.get((secEp + ep).getBytes()); if (data == null) { return null; } else { @@ -90,7 +98,7 @@ public SecurityInfo getByOscoreIdentity(OscoreIdentity pskIdentity) { @Override public Collection getAll() { try (Jedis j = pool.getResource()) { - ScanParams params = new ScanParams().match(SEC_EP + "*").count(100); + ScanParams params = new ScanParams().match(secEp + "*").count(100); Collection list = new LinkedList<>(); String cursor = "0"; do { @@ -111,19 +119,19 @@ public SecurityInfo add(SecurityInfo info) throws NonUniqueSecurityInfoException try (Jedis j = pool.getResource()) { if (info.getPskIdentity() != null) { // populate the secondary index (security info by PSK id) - String oldEndpoint = j.hget(PSKID_SEC, info.getPskIdentity()); + String oldEndpoint = j.hget(pskIdSec, info.getPskIdentity()); if (oldEndpoint != null && !oldEndpoint.equals(info.getEndpoint())) { throw new NonUniqueSecurityInfoException( "PSK Identity " + info.getPskIdentity() + " is already used"); } - j.hset(PSKID_SEC.getBytes(), info.getPskIdentity().getBytes(), info.getEndpoint().getBytes()); + j.hset(pskIdSec.getBytes(), info.getPskIdentity().getBytes(), info.getEndpoint().getBytes()); } - byte[] previousData = j.getSet((SEC_EP + info.getEndpoint()).getBytes(), data); + byte[] previousData = j.getSet((secEp + info.getEndpoint()).getBytes(), data); SecurityInfo previous = previousData == null ? null : deserialize(previousData); String previousIdentity = previous == null ? null : previous.getPskIdentity(); if (previousIdentity != null && !previousIdentity.equals(info.getPskIdentity())) { - j.hdel(PSKID_SEC, previousIdentity); + j.hdel(pskIdSec, previousIdentity); } return previous; @@ -133,14 +141,14 @@ public SecurityInfo add(SecurityInfo info) throws NonUniqueSecurityInfoException @Override public SecurityInfo remove(String endpoint, boolean infosAreCompromised) { try (Jedis j = pool.getResource()) { - byte[] data = j.get((SEC_EP + endpoint).getBytes()); + byte[] data = j.get((secEp + endpoint).getBytes()); if (data != null) { SecurityInfo info = deserialize(data); if (info.getPskIdentity() != null) { - j.hdel(PSKID_SEC.getBytes(), info.getPskIdentity().getBytes()); + j.hdel(pskIdSec.getBytes(), info.getPskIdentity().getBytes()); } - j.del((SEC_EP + endpoint).getBytes()); + j.del((secEp + endpoint).getBytes()); for (SecurityStoreListener listener : listeners) { listener.securityInfoRemoved(infosAreCompromised, info); }