Skip to content

Commit

Permalink
Forbid setting empty prefixes for RedisSecurityStore
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakseiz committed Mar 2, 2023
1 parent 58270cc commit bab6ffb
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ public class RedisSecurityStore implements EditableSecurityStore {

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

private RedisSecurityStore(Builder builder) {
public RedisSecurityStore(Pool<Jedis> 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;
Expand Down Expand Up @@ -196,7 +202,7 @@ public void setPool(Pool<Jedis> pool) {
/**
* Set the key prefix for security info lookup by endpoint.
* <p>
* 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;
Expand All @@ -205,7 +211,7 @@ public void setSecurityInfoByEndpointPrefix(String securityInfoByEndpointPrefix)
/**
* Set the key for endpoint lookup by PSK identity.
* <p>
* 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;
Expand All @@ -215,7 +221,7 @@ public void setEndpointByPskIdKey(String endpointByPskIdKey) {
* Set the prefix for all keys and prefixes including {@link #securityInfoByEndpointPrefix} and
* {@link #endpointByPskIdKey}.
* <p>
* 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;
Expand All @@ -231,11 +237,27 @@ public Builder(Pool<Jedis> pool) {
/**
* Create the {@link RedisSecurityStore}.
* <p>
* @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);
}
Expand Down

0 comments on commit bab6ffb

Please sign in to comment.