|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.opensearch.security.hash; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import org.awaitility.Awaitility; |
| 18 | +import org.junit.BeforeClass; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 22 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 23 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 24 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 25 | + |
| 26 | +import static org.apache.http.HttpStatus.SC_OK; |
| 27 | +import static org.apache.http.HttpStatus.SC_UNAUTHORIZED; |
| 28 | +import static org.hamcrest.Matchers.equalTo; |
| 29 | +import static org.opensearch.security.support.ConfigConstants.ARGON2; |
| 30 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ALGORITHM; |
| 31 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_ITERATIONS; |
| 32 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_LENGTH; |
| 33 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_MEMORY; |
| 34 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_PARALLELISM; |
| 35 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_TYPE; |
| 36 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_PASSWORD_HASHING_ARGON2_VERSION; |
| 37 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; |
| 38 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; |
| 39 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 40 | + |
| 41 | +public class Argon2CustomConfigHashingTests extends HashingTests { |
| 42 | + |
| 43 | + public static LocalCluster cluster; |
| 44 | + |
| 45 | + private static String type; |
| 46 | + private static int memory, iterations, length, parallelism, version; |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void startCluster() { |
| 50 | + |
| 51 | + type = randomFrom(List.of("D", "I", "ID")); |
| 52 | + memory = randomFrom(List.of(4096, 8192, 15360)); |
| 53 | + iterations = randomFrom((List.of(1, 2, 3, 4))); |
| 54 | + length = randomFrom((List.of(4, 8, 16, 32, 64))); |
| 55 | + parallelism = randomFrom(List.of(1, 2)); |
| 56 | + version = randomFrom(List.of(16, 19)); |
| 57 | + |
| 58 | + TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS) |
| 59 | + .hash(generateArgon2Hash("secret", type, memory, iterations, length, parallelism, version)); |
| 60 | + |
| 61 | + cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) |
| 62 | + .authc(AUTHC_HTTPBASIC_INTERNAL) |
| 63 | + .users(ADMIN_USER) |
| 64 | + .anonymousAuth(false) |
| 65 | + .nodeSettings( |
| 66 | + Map.of( |
| 67 | + SECURITY_RESTAPI_ROLES_ENABLED, |
| 68 | + List.of("user_" + ADMIN_USER.getName() + "__" + ALL_ACCESS.getName()), |
| 69 | + SECURITY_PASSWORD_HASHING_ALGORITHM, |
| 70 | + ARGON2, |
| 71 | + SECURITY_PASSWORD_HASHING_ARGON2_TYPE, |
| 72 | + type, |
| 73 | + SECURITY_PASSWORD_HASHING_ARGON2_MEMORY, |
| 74 | + memory, |
| 75 | + SECURITY_PASSWORD_HASHING_ARGON2_ITERATIONS, |
| 76 | + iterations, |
| 77 | + SECURITY_PASSWORD_HASHING_ARGON2_LENGTH, |
| 78 | + length, |
| 79 | + SECURITY_PASSWORD_HASHING_ARGON2_PARALLELISM, |
| 80 | + parallelism, |
| 81 | + SECURITY_PASSWORD_HASHING_ARGON2_VERSION, |
| 82 | + version |
| 83 | + ) |
| 84 | + ) |
| 85 | + .build(); |
| 86 | + cluster.before(); |
| 87 | + |
| 88 | + try (TestRestClient client = cluster.getRestClient(ADMIN_USER.getName(), "secret")) { |
| 89 | + Awaitility.await() |
| 90 | + .alias("Load default configuration") |
| 91 | + .until(() -> client.securityHealth().getTextFromJsonBody("/status"), equalTo("UP")); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void shouldAuthenticateWithCorrectPassword() { |
| 97 | + String hash = generateArgon2Hash(PASSWORD, type, memory, iterations, length, parallelism, version); |
| 98 | + |
| 99 | + createUserWithHashedPassword(cluster, "user_1", hash); |
| 100 | + testPasswordAuth(cluster, "user_1", PASSWORD, SC_OK); |
| 101 | + |
| 102 | + createUserWithPlainTextPassword(cluster, "user_2", PASSWORD); |
| 103 | + testPasswordAuth(cluster, "user_2", PASSWORD, SC_OK); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void shouldNotAuthenticateWithIncorrectPassword() { |
| 108 | + String hash = generateArgon2Hash(PASSWORD, type, memory, iterations, length, parallelism, version); |
| 109 | + createUserWithHashedPassword(cluster, "user_3", hash); |
| 110 | + testPasswordAuth(cluster, "user_3", "wrong_password", SC_UNAUTHORIZED); |
| 111 | + |
| 112 | + createUserWithPlainTextPassword(cluster, "user_4", PASSWORD); |
| 113 | + testPasswordAuth(cluster, "user_4", "wrong_password", SC_UNAUTHORIZED); |
| 114 | + } |
| 115 | +} |
0 commit comments