Skip to content

Commit caff4d4

Browse files
waldororeschke
authored andcommitted
OAK-11899 - use default value in case config value can not be parsed (#2509)
* OAK-11899 - use default value in case config value can not be parsed * OAK-11899 - if config parameter can not be converted, use default value * OAK-11899 - increase org.apache.jackrabbit.oak.spi.security version
1 parent acb8ef5 commit caff4d4

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenProviderImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,10 @@ class TokenProviderImpl implements TokenProvider, TokenConstants {
133133
this.root = root;
134134
this.options = options;
135135
this.credentialsSupport = credentialsSupport;
136-
137-
this.tokenExpiration = options.getConfigValue(PARAM_TOKEN_EXPIRATION, DEFAULT_TOKEN_EXPIRATION);
136+
this.tokenExpiration = options.getConfigValueOrDefault(PARAM_TOKEN_EXPIRATION, DEFAULT_TOKEN_EXPIRATION);
138137
this.userManager = userConfiguration.getUserManager(root, NamePathMapper.DEFAULT);
139138
this.identifierManager = new IdentifierManager(root);
140-
this.cleanupThreshold = options.getConfigValue(PARAM_TOKEN_CLEANUP_THRESHOLD, NO_TOKEN_CLEANUP);
139+
this.cleanupThreshold = options.getConfigValueOrDefault(PARAM_TOKEN_CLEANUP_THRESHOLD, NO_TOKEN_CLEANUP);
141140
}
142141

143142
//------------------------------------------------------< TokenProvider >---
@@ -433,7 +432,7 @@ private TokenInfo createTokenNode(@NotNull Tree parent, @NotNull String tokenNam
433432
Tree tokenNode = TreeUtil.addChild(parent, tokenName, TOKEN_NT_NAME);
434433
tokenNode.setProperty(JcrConstants.JCR_UUID, uuid);
435434

436-
String key = generateKey(options.getConfigValue(PARAM_TOKEN_LENGTH, DEFAULT_KEY_SIZE));
435+
String key = generateKey(options.getConfigValueOrDefault(PARAM_TOKEN_LENGTH, DEFAULT_KEY_SIZE));
437436
String nodeId = getIdentifier(tokenNode);
438437
String token = nodeId + DELIM + key;
439438

@@ -576,7 +575,7 @@ public boolean isExpired(long loginTime) {
576575
@Override
577576
public boolean resetExpiration(long loginTime) {
578577
// for backwards compatibility use true as default value for the 'tokenRefresh' configuration
579-
if (options.getConfigValue(PARAM_TOKEN_REFRESH, true)) {
578+
if (options.getConfigValueOrDefault(PARAM_TOKEN_REFRESH, true)) {
580579
Tree tokenTree = getTokenTree(this);
581580
if (tokenTree.exists()) {
582581
if (isExpired(loginTime)) {

oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/ConfigurationParameters.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ public <T> T getConfigValue(@NotNull String key, @NotNull T defaultValue) {
245245
}
246246
}
247247

248+
/**
249+
* Returns the value of the configuration entry with the given {@code key}
250+
* applying the following rules:
251+
*
252+
* <ul>
253+
* <li>If this instance doesn't contain a configuration entry with that
254+
* key, or if the entry is {@code null}, the specified {@code defaultValue} will be returned.</li>
255+
* <li>If the configured value is not {@code null} an attempt is made to convert the configured value to
256+
* match the type of the default value.</li>
257+
* <li>If the configured value can not be converted to the default value type the default value is returned.</li>
258+
* </ul>
259+
*
260+
* @param key The name of the configuration option.
261+
* @param defaultValue The default value to return if no such entry exists
262+
* or to use for conversion.
263+
* @return The original or converted configuration value or {@code defaultValue} if no entry for the given key exists
264+
* or if a conversion error occurred.
265+
*/
266+
@NotNull
267+
public <T> T getConfigValueOrDefault(@NotNull String key, @NotNull T defaultValue) {
268+
try {
269+
return getConfigValue(key, defaultValue);
270+
} catch (IllegalArgumentException e) {
271+
return defaultValue;
272+
}
273+
}
274+
248275
//--------------------------------------------------------< private >---
249276
@NotNull
250277
private static Class<?> getTargetClass(@NotNull Object configProperty, @Nullable Object defaultValue, @Nullable Class<?> targetClass) {

oak-security-spi/src/main/java/org/apache/jackrabbit/oak/spi/security/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
@Version("2.3.0")
17+
@Version("2.4.0")
1818
package org.apache.jackrabbit.oak.spi.security;
1919

2020
import org.osgi.annotation.versioning.Version;

oak-security-spi/src/test/java/org/apache/jackrabbit/oak/spi/security/ConfigurationParametersTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,49 @@ public void testClear() {
601601
options.clear();
602602
}
603603

604+
@Test
605+
public void testConfigValueOrDefault() {
606+
ConfigurationParameters options = ConfigurationParameters.of(Collections.singletonMap("test", null));
607+
Object defaultObject = new TestObject("t");
608+
String defaultValue = "defaultValue";
609+
610+
assertEquals(defaultValue, options.getConfigValueOrDefault("test", defaultValue));
611+
assertEquals(1L, options.getConfigValueOrDefault("test", 1L).longValue());
612+
assertEquals(1.1f, options.getConfigValueOrDefault("test", 1.1f), 0.01);
613+
assertEquals(1.1d, options.getConfigValueOrDefault("test", 1.1d), 0.01);
614+
assertEquals(1, options.getConfigValueOrDefault("test", 1).intValue());
615+
assertEquals(defaultObject, options.getConfigValueOrDefault("test", defaultObject));
616+
assertFalse(options.getConfigValueOrDefault("test", false));
617+
}
618+
619+
@Test
620+
public void testConfigValueOrDefault1() {
621+
ConfigurationParameters options = ConfigurationParameters.of(Collections.singletonMap("test", ""));
622+
Object defaultObject = new TestObject("t");
623+
624+
assertEquals("", options.getConfigValueOrDefault("test", "value"));
625+
assertEquals(1L, options.getConfigValueOrDefault("test", 1L).longValue());
626+
assertEquals(1.1f, options.getConfigValueOrDefault("test", 1.1f), 0.01);
627+
assertEquals(1.1d, options.getConfigValueOrDefault("test", 1.1d), 0.01);
628+
assertEquals(1, options.getConfigValueOrDefault("test", 1).intValue());
629+
assertEquals(defaultObject, options.getConfigValueOrDefault("test", defaultObject));
630+
assertFalse(options.getConfigValueOrDefault("test", false));
631+
}
632+
633+
@Test
634+
public void testConfigValueOrDefault2() {
635+
ConfigurationParameters options = ConfigurationParameters.of(Collections.singletonMap("test", " "));
636+
Object defaultObject = new TestObject("t");
637+
638+
assertEquals(" ", options.getConfigValueOrDefault("test", "value"));
639+
assertEquals(1L, options.getConfigValueOrDefault("test", 1L).longValue());
640+
assertEquals(1.1f, options.getConfigValueOrDefault("test", 1.1f), 0.01);
641+
assertEquals(1.1d, options.getConfigValueOrDefault("test", 1.1d), 0.01);
642+
assertEquals(1, options.getConfigValueOrDefault("test", 1).intValue());
643+
assertEquals(defaultObject, options.getConfigValueOrDefault("test", defaultObject));
644+
assertFalse(options.getConfigValueOrDefault("test", false));
645+
}
646+
604647
private static class TestObject {
605648

606649
private final String name;
@@ -621,4 +664,4 @@ public boolean equals(Object object) {
621664
return object == this || object instanceof TestObject && name.equals(((TestObject) object).name);
622665
}
623666
}
624-
}
667+
}

0 commit comments

Comments
 (0)