Skip to content

Commit

Permalink
Allow to set custom REDIS keys for security endpoint and PSK ID
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakseiz committed Feb 13, 2023
1 parent dbe5aa2 commit 4ad7307
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,30 @@
*/
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<Jedis> pool;

private final List<SecurityStoreListener> listeners = new CopyOnWriteArrayList<>();

public RedisSecurityStore(Pool<Jedis> pool) {
this.pool = pool;
this.secEp = "SEC#EP#";
this.pskIdSec = "PSKID#SEC";
}

public RedisSecurityStore(Pool<Jedis> 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 {
Expand All @@ -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 {
Expand All @@ -90,7 +98,7 @@ public SecurityInfo getByOscoreIdentity(OscoreIdentity pskIdentity) {
@Override
public Collection<SecurityInfo> getAll() {
try (Jedis j = pool.getResource()) {
ScanParams params = new ScanParams().match(SEC_EP + "*").count(100);
ScanParams params = new ScanParams().match(secEp + "*").count(100);
Collection<SecurityInfo> list = new LinkedList<>();
String cursor = "0";
do {
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 4ad7307

Please sign in to comment.