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 3d90686723..9e96540091 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 @@ -49,7 +49,13 @@ public class RedisSecurityStore implements EditableSecurityStore { private final List listeners = new CopyOnWriteArrayList<>(); - private RedisSecurityStore(Builder builder) { + public RedisSecurityStore(Pool pool) { + this.pool = pool; + this.securityInfoByEndpointPrefix = "SECSTORE#SEC#EP#"; + this.endpointByPskIdKey = "SECSTORE#EP#PSKID"; + } + + protected RedisSecurityStore(Builder builder) { this.pool = builder.pool; this.securityInfoByEndpointPrefix = builder.securityInfoByEndpointPrefix; this.endpointByPskIdKey = builder.endpointByPskIdKey; @@ -196,7 +202,7 @@ public void setPool(Pool pool) { /** * Set the key prefix for security info lookup by endpoint. *

- * Default value is {@literal SEC#EP#}. + * Default value is {@literal SEC#EP#}. Should not be {@code null} or empty. */ public void setSecurityInfoByEndpointPrefix(String securityInfoByEndpointPrefix) { this.securityInfoByEndpointPrefix = securityInfoByEndpointPrefix; @@ -205,7 +211,7 @@ public void setSecurityInfoByEndpointPrefix(String securityInfoByEndpointPrefix) /** * Set the key for endpoint lookup by PSK identity. *

- * Default value is {@literal EP#PSKID}. + * Default value is {@literal EP#PSKID}. Should not be {@code null} or empty. */ public void setEndpointByPskIdKey(String endpointByPskIdKey) { this.endpointByPskIdKey = endpointByPskIdKey; @@ -215,7 +221,7 @@ public void setEndpointByPskIdKey(String endpointByPskIdKey) { * Set the prefix for all keys and prefixes including {@link #securityInfoByEndpointPrefix} and * {@link #endpointByPskIdKey}. *

- * Default value is {@literal SECSTORE#}. + * Default value is {@literal SECSTORE#}. Should not be {@code null}, can be empty. */ public void setPrefix(String prefix) { this.prefix = prefix; @@ -231,11 +237,27 @@ public Builder(Pool pool) { /** * Create the {@link RedisSecurityStore}. *

- * @return the Redis security store. + * Throws {@link IllegalArgumentException} when {@link #securityInfoByEndpointPrefix} or + * {@link #endpointByPskIdKey} are not set or are equal to each other. */ - public RedisSecurityStore build() { - this.securityInfoByEndpointPrefix = this.prefix + this.securityInfoByEndpointPrefix; - this.endpointByPskIdKey = this.prefix + this.endpointByPskIdKey; + public RedisSecurityStore build() throws IllegalArgumentException { + if (this.securityInfoByEndpointPrefix == null || this.securityInfoByEndpointPrefix.isEmpty()) { + throw new IllegalArgumentException("securityInfoByEndpointPrefix should not be empty"); + } + + if (this.endpointByPskIdKey == null || this.endpointByPskIdKey.isEmpty()) { + throw new IllegalArgumentException("endpointByPskIdKey should not be empty"); + } + + if (this.securityInfoByEndpointPrefix.equals(this.endpointByPskIdKey)) { + throw new IllegalArgumentException( + "securityInfoByEndpointPrefix should not be equal to endpointByPskIdKey"); + } + + if (this.prefix != null) { + this.securityInfoByEndpointPrefix = this.prefix + this.securityInfoByEndpointPrefix; + this.endpointByPskIdKey = this.prefix + this.endpointByPskIdKey; + } return new RedisSecurityStore(this); }