|
7 | 7 |
|
8 | 8 | import org.elasticsearch.ElasticsearchParseException; |
9 | 9 | import org.elasticsearch.client.Client; |
| 10 | +import org.elasticsearch.common.ValidationException; |
10 | 11 | import org.elasticsearch.common.bytes.BytesArray; |
| 12 | +import org.elasticsearch.common.bytes.BytesReference; |
| 13 | +import org.elasticsearch.common.settings.SecureString; |
| 14 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
11 | 15 | import org.elasticsearch.common.xcontent.XContentType; |
12 | 16 | import org.elasticsearch.test.ESTestCase; |
13 | 17 | import org.elasticsearch.xpack.core.security.action.user.PutUserRequest; |
|
16 | 20 |
|
17 | 21 | import java.io.IOException; |
18 | 22 | import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.LinkedHashMap; |
19 | 25 |
|
20 | 26 | import static org.hamcrest.Matchers.arrayContaining; |
21 | 27 | import static org.hamcrest.Matchers.containsString; |
| 28 | +import static org.hamcrest.Matchers.equalTo; |
22 | 29 | import static org.hamcrest.Matchers.is; |
23 | 30 | import static org.hamcrest.Matchers.nullValue; |
24 | 31 | import static org.mockito.Mockito.mock; |
@@ -135,4 +142,69 @@ public void testWithEnabled() throws IOException { |
135 | 142 | builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT).request(); |
136 | 143 | assertFalse(request.enabled()); |
137 | 144 | } |
| 145 | + |
| 146 | + public void testWithValidPasswordHash() throws IOException { |
| 147 | + final Hasher hasher = Hasher.BCRYPT4; // this is the fastest hasher we officially support |
| 148 | + final char[] hash = hasher.hash(new SecureString("secret".toCharArray())); |
| 149 | + final String json = "{\n" + |
| 150 | + " \"password_hash\": \"" + new String(hash) + "\"," + |
| 151 | + " \"roles\": []\n" + |
| 152 | + "}"; |
| 153 | + |
| 154 | + PutUserRequestBuilder requestBuilder = new PutUserRequestBuilder(mock(Client.class)); |
| 155 | + PutUserRequest request = requestBuilder.source("hash_user", |
| 156 | + new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher).request(); |
| 157 | + assertThat(request.passwordHash(), equalTo(hash)); |
| 158 | + assertThat(request.username(), equalTo("hash_user")); |
| 159 | + } |
| 160 | + |
| 161 | + public void testWithMismatchedPasswordHash() throws IOException { |
| 162 | + final Hasher systemHasher = Hasher.BCRYPT8; |
| 163 | + final Hasher userHasher = Hasher.BCRYPT4; // this is the fastest hasher we officially support |
| 164 | + final char[] hash = userHasher.hash(new SecureString("secret".toCharArray())); |
| 165 | + final String json = "{\n" + |
| 166 | + " \"password_hash\": \"" + new String(hash) + "\"," + |
| 167 | + " \"roles\": []\n" + |
| 168 | + "}"; |
| 169 | + |
| 170 | + PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); |
| 171 | + final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> { |
| 172 | + builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); |
| 173 | + }); |
| 174 | + assertThat(ex.getMessage(), containsString(userHasher.name())); |
| 175 | + assertThat(ex.getMessage(), containsString(systemHasher.name())); |
| 176 | + } |
| 177 | + |
| 178 | + public void testWithPasswordHashThatsNotReallyAHash() throws IOException { |
| 179 | + final Hasher systemHasher = Hasher.PBKDF2; |
| 180 | + final String json = "{\n" + |
| 181 | + " \"password_hash\": \"not-a-hash\"," + |
| 182 | + " \"roles\": []\n" + |
| 183 | + "}"; |
| 184 | + |
| 185 | + PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); |
| 186 | + final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> { |
| 187 | + builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); |
| 188 | + }); |
| 189 | + assertThat(ex.getMessage(), containsString(Hasher.NOOP.name())); |
| 190 | + assertThat(ex.getMessage(), containsString(systemHasher.name())); |
| 191 | + } |
| 192 | + |
| 193 | + public void testWithBothPasswordAndHash() throws IOException { |
| 194 | + final Hasher hasher = randomFrom(Hasher.BCRYPT4, Hasher.PBKDF2_1000); |
| 195 | + final String password = randomAlphaOfLength(12); |
| 196 | + final char[] hash = hasher.hash(new SecureString(password.toCharArray())); |
| 197 | + final LinkedHashMap<String, Object> fields = new LinkedHashMap<>(); |
| 198 | + fields.put("password", password); |
| 199 | + fields.put("password_hash", new String(hash)); |
| 200 | + fields.put("roles", Collections.emptyList()); |
| 201 | + BytesReference json = BytesReference.bytes(XContentBuilder.builder(XContentType.JSON.xContent()) |
| 202 | + .map(shuffleMap(fields, Collections.emptySet()))); |
| 203 | + |
| 204 | + PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); |
| 205 | + final IllegalArgumentException ex = expectThrows(ValidationException.class, () -> { |
| 206 | + builder.source("hash_user", json, XContentType.JSON, hasher).request(); |
| 207 | + }); |
| 208 | + assertThat(ex.getMessage(), containsString("password_hash has already been set")); |
| 209 | + } |
138 | 210 | } |
0 commit comments