Skip to content

Commit 34dda75

Browse files
committed
Ensure SHA256 is not used in tests (#42289)
SHA256 was recently added to the Hasher class in order to be used in the TokenService. A few tests were still using values() to get the available algorithms from the Enum and it could happen that SHA256 would be picked up by these. This change adds an extra convenience method (Hasher#getAvailableAlgoCacheHash) and enures that only this and Hasher#getAvailableAlgoStoredHash are used for getting the list of available password hashing algorithms in our tests.
1 parent cdf9485 commit 34dda75

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

x-pack/plugin/core/build.gradle

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ forbiddenPatterns {
8787
exclude '**/*.zip'
8888
}
8989

90+
forbiddenApisMain {
91+
signaturesFiles += files('forbidden/hasher-signatures.txt')
92+
}
93+
94+
if (isEclipse) {
95+
// in eclipse the project is under a fake root, we need to change around the source sets
96+
sourceSets {
97+
if (project.path == ":libs:core") {
98+
main.java.srcDirs = ['java']
99+
main.resources.srcDirs = ['resources']
100+
} else {
101+
test.java.srcDirs = ['java']
102+
test.resources.srcDirs = ['resources']
103+
}
104+
}
105+
}
106+
90107
compileJava.options.compilerArgs << "-Xlint:-rawtypes,-unchecked"
91108
compileTestJava.options.compilerArgs << "-Xlint:-rawtypes,-unchecked"
92109

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@defaultMessage values should not be used as it can contain unwanted algorithms. Use Hasher#getAvailableAlgoStoredHash and Hasher#getAvailableAlgoCacheHash instead
2+
org.elasticsearch.xpack.core.security.authc.support.Hasher#values()

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.elasticsearch.ElasticsearchException;
99
import org.elasticsearch.common.CharArrays;
10+
import org.elasticsearch.common.SuppressForbidden;
1011
import org.elasticsearch.common.hash.MessageDigests;
1112
import org.elasticsearch.common.settings.SecureString;
1213

@@ -565,12 +566,25 @@ private static boolean verifyBcryptHash(SecureString text, char[] hash) {
565566
* combinations that can be used for password hashing. The identifiers can be used to get
566567
* an instance of the appropriate {@link Hasher} by using {@link #resolve(String) resolve()}
567568
*/
569+
@SuppressForbidden(reason = "This is the only allowed way to get available values")
568570
public static List<String> getAvailableAlgoStoredHash() {
569571
return Arrays.stream(Hasher.values()).map(Hasher::name).map(name -> name.toLowerCase(Locale.ROOT))
570572
.filter(name -> (name.startsWith("pbkdf2") || name.startsWith("bcrypt")))
571573
.collect(Collectors.toList());
572574
}
573575

576+
/**
577+
* Returns a list of lower case String identifiers for the Hashing algorithm and parameter
578+
* combinations that can be used for password hashing in the cache. The identifiers can be used to get
579+
* an instance of the appropriate {@link Hasher} by using {@link #resolve(String) resolve()}
580+
*/
581+
@SuppressForbidden(reason = "This is the only allowed way to get available values")
582+
public static List<String> getAvailableAlgoCacheHash() {
583+
return Arrays.stream(Hasher.values()).map(Hasher::name).map(name -> name.toLowerCase(Locale.ROOT))
584+
.filter(name -> (name.equals("sha256") == false))
585+
.collect(Collectors.toList());
586+
}
587+
574588
public abstract char[] hash(SecureString data);
575589

576590
public abstract boolean verify(SecureString data, char[] hash);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmSettingsTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@
1717
import org.elasticsearch.xpack.core.security.authc.RealmSettings;
1818
import org.elasticsearch.xpack.core.security.authc.support.Hasher;
1919

20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.HashSet;
2322
import java.util.List;
2423
import java.util.Set;
25-
import java.util.stream.Collectors;
2624

2725
import static org.hamcrest.Matchers.containsString;
2826
import static org.hamcrest.Matchers.notNullValue;
2927

3028
public class RealmSettingsTests extends ESTestCase {
31-
private static final List<String> CACHE_HASHING_ALGOS = Arrays.stream(Hasher.values()).map(Hasher::name).collect(Collectors.toList());
29+
private static final List<String> CACHE_HASHING_ALGOS = Hasher.getAvailableAlgoCacheHash();
3230

3331
public void testRealmWithBlankTypeDoesNotValidate() throws Exception {
3432
final Settings.Builder builder = baseSettings(false);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.junit.Before;
2323
import org.mockito.stubbing.Answer;
2424

25-
import java.util.Locale;
2625
import java.util.Map;
2726
import java.util.function.Supplier;
2827

@@ -94,7 +93,7 @@ private RealmConfig getRealmConfig(Settings settings) {
9493
public void testAuthenticateCaching() throws Exception {
9594
Settings settings = Settings.builder()
9695
.put(RealmSettings.realmSettingPrefix(REALM_IDENTIFIER) + "cache.hash_algo",
97-
Hasher.values()[randomIntBetween(0, Hasher.values().length - 1)].name().toLowerCase(Locale.ROOT))
96+
randomFrom(Hasher.getAvailableAlgoCacheHash()))
9897
.put(globalSettings)
9998
.build();
10099
RealmConfig config = getRealmConfig(settings);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import java.util.ArrayList;
3232
import java.util.List;
33-
import java.util.Locale;
3433
import java.util.concurrent.CountDownLatch;
3534
import java.util.concurrent.atomic.AtomicInteger;
3635
import java.util.concurrent.atomic.AtomicReference;
@@ -66,7 +65,7 @@ public void stop() {
6665

6766
@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/42267")
6867
public void testCacheSettings() {
69-
String cachingHashAlgo = Hasher.values()[randomIntBetween(0, Hasher.values().length - 1)].name().toLowerCase(Locale.ROOT);
68+
String cachingHashAlgo = randomFrom(Hasher.getAvailableAlgoCacheHash());
7069
int maxUsers = randomIntBetween(10, 100);
7170
TimeValue ttl = TimeValue.timeValueMinutes(randomIntBetween(10, 20));
7271
final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("caching", "test_realm");

0 commit comments

Comments
 (0)