Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions server/src/main/java/org/elasticsearch/common/util/Maps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common.util;

import java.util.Map;
import java.util.Objects;

public class Maps {

/**
* Returns {@code true} if the two specified maps are equal to one another. Two maps are considered equal if both represent identical
* mappings where values are checked with Objects.deepEquals. The primary use case is to check if two maps with array values are equal.
*
* @param left one map to be tested for equality
* @param right the other map to be tested for equality
* @return {@code true} if the two maps are equal
*/
public static <K, V> boolean deepEquals(Map<K, V> left, Map<K, V> right) {
if (left == right) {
return true;
}
if (left == null || right == null || left.size() != right.size()) {
return false;
}
return left.entrySet().stream()
.allMatch(e -> right.containsKey(e.getKey()) && Objects.deepEquals(e.getValue(), right.get(e.getKey())));
}

}
64 changes: 64 additions & 0 deletions server/src/test/java/org/elasticsearch/common/util/MapsTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common.util;

import org.elasticsearch.test.ESTestCase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toMap;

public class MapsTests extends ESTestCase {

public void testDeepEquals() {
final Supplier<String> keyGenerator = () -> randomAlphaOfLengthBetween(1, 5);
final Supplier<int[]> arrayValueGenerator = () -> random().ints(randomInt(5)).toArray();
final Map<String, int[]> map = randomMap(randomInt(5), keyGenerator, arrayValueGenerator);
final Map<String, int[]> mapCopy = map.entrySet().stream()
.collect(toMap(Map.Entry::getKey, e -> Arrays.copyOf(e.getValue(), e.getValue().length)));

assertTrue(Maps.deepEquals(map, mapCopy));

final Map<String, int[]> mapModified = mapCopy;
if (mapModified.isEmpty()) {
mapModified.put(keyGenerator.get(), arrayValueGenerator.get());
} else {
if (randomBoolean()) {
final String randomKey = mapModified.keySet().toArray(new String[0])[randomInt(mapModified.size() - 1)];
final int[] value = mapModified.get(randomKey);
mapModified.put(randomKey, randomValueOtherThanMany((v) -> Arrays.equals(v, value), arrayValueGenerator));
} else {
mapModified.put(randomValueOtherThanMany(mapModified::containsKey, keyGenerator), arrayValueGenerator.get());
}
}

assertFalse(Maps.deepEquals(map, mapModified));
}

private static <K, V> Map<K, V> randomMap(int size, Supplier<K> keyGenerator, Supplier<V> valueGenerator) {
final Map<K, V> map = new HashMap<>();
IntStream.range(0, size).forEach(i -> map.put(keyGenerator.get(), valueGenerator.get()));
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/
package org.elasticsearch.xpack.security.authc.file;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.env.Environment;
import org.elasticsearch.watcher.FileChangesListener;
import org.elasticsearch.watcher.FileWatcher;
Expand Down Expand Up @@ -191,9 +192,13 @@ public void onFileDeleted(Path file) {
@Override
public void onFileChanged(Path file) {
if (file.equals(FileUserPasswdStore.this.file)) {
logger.info("users file [{}] changed. updating users... )", file.toAbsolutePath());
final Map<String, char[]> previousUsers = users;
users = parseFileLenient(file, logger, settings);
notifyRefresh();

if (Maps.deepEquals(previousUsers, users) == false) {
logger.info("users file [{}] changed. updating users... )", file.toAbsolutePath());
notifyRefresh();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
*/
package org.elasticsearch.xpack.security.authc.file;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.env.Environment;
import org.elasticsearch.watcher.FileChangesListener;
import org.elasticsearch.watcher.FileWatcher;
Expand Down Expand Up @@ -206,9 +207,13 @@ public void onFileDeleted(Path file) {
@Override
public void onFileChanged(Path file) {
if (file.equals(FileUserRolesStore.this.file)) {
logger.info("users roles file [{}] changed. updating users roles...", file.toAbsolutePath());
final Map<String, String[]> previousUserRoles = userRoles;
userRoles = parseFileLenient(file, logger);
notifyRefresh();

if (Maps.deepEquals(previousUserRoles, userRoles) == false) {
logger.info("users roles file [{}] changed. updating users roles...", file.toAbsolutePath());
notifyRefresh();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,14 @@ public void onFileDeleted(Path file) {
@Override
public void onFileChanged(Path file) {
if (file.equals(DnRoleMapper.this.file)) {
logger.info("role mappings file [{}] changed for realm [{}/{}]. updating mappings...", file.toAbsolutePath(),
config.type(), config.name());
final Map<String, List<String>> previousDnRoles = dnRoles;
dnRoles = parseFileLenient(file, logger, config.type(), config.name());
notifyRefresh();

if (previousDnRoles.equals(dnRoles) == false) {
logger.info("role mappings file [{}] changed for realm [{}/{}]. updating mappings...", file.toAbsolutePath(),
config.type(), config.name());
notifyRefresh();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,6 @@ public synchronized void onFileChanged(Path file) {
final Map<String, RoleDescriptor> previousPermissions = permissions;
try {
permissions = parseFile(file, logger, settings, licenseState, xContentRegistry);
logger.info("updated roles (roles file [{}] {})", file.toAbsolutePath(),
Files.exists(file) ? "changed" : "removed");
} catch (Exception e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage(
Expand All @@ -383,7 +381,11 @@ public synchronized void onFileChanged(Path file) {
.collect(Collectors.toSet());
final Set<String> addedRoles = Sets.difference(permissions.keySet(), previousPermissions.keySet());
final Set<String> changedRoles = Collections.unmodifiableSet(Sets.union(changedOrMissingRoles, addedRoles));
listeners.forEach(c -> c.accept(changedRoles));
if (changedRoles.isEmpty() == false) {
logger.info("updated roles (roles file [{}] {})", file.toAbsolutePath(),
Files.exists(file) ? "changed" : "removed");
listeners.forEach(c -> c.accept(changedRoles));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class FileUserPasswdStoreTests extends ESTestCase {
@Before
public void init() {
settings = Settings.builder()
.put("resource.reload.interval.high", "2s")
.put("resource.reload.interval.high", "100ms")
.put("path.home", createTempDir())
.put("xpack.security.authc.password_hashing.algorithm", randomFrom("bcrypt", "bcrypt11", "pbkdf2", "pbkdf2_1000",
"pbkdf2_50000"))
Expand Down Expand Up @@ -103,6 +103,20 @@ public void testStore_AutoReload() throws Exception {

watcherService.start();

try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.append("\n");
}

watcherService.notifyNow(ResourceWatcherService.Frequency.HIGH);
if (latch.getCount() != 1) {
fail("Listener should not be called as users passwords are not changed.");
}

assertThat(store.userExists(username), is(true));
result = store.verifyPassword(username, new SecureString("test123"), () -> user);
assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS));
assertThat(result.getUser(), is(user));

try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.append("foobar:").append(new String(hasher.hash(new SecureString("barfoo"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class FileUserRolesStoreTests extends ESTestCase {
@Before
public void init() {
settings = Settings.builder()
.put("resource.reload.interval.high", "2s")
.put("resource.reload.interval.high", "100ms")
.put("path.home", createTempDir())
.build();
env = TestEnvironment.newEnvironment(settings);
Expand Down Expand Up @@ -102,6 +102,17 @@ public void testStoreAutoReload() throws Exception {

watcherService.start();

try (BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.append("\n");
}

watcherService.notifyNow(ResourceWatcherService.Frequency.HIGH);
if (latch.getCount() != 1) {
fail("Listener should not be called as users roles are not changed.");
}

assertThat(store.roles("user1"), arrayContaining("role1", "role2", "role3"));

try (BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.append("role4:user4\nrole5:user4\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ public void testMapper_AutoReload() throws Exception {

watcherService.start();

try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.append("\n");
}

watcherService.notifyNow(ResourceWatcherService.Frequency.HIGH);
if (latch.getCount() != 1) {
fail("Listener should not be called as roles mapping is not changed.");
}

roles = mapper.resolveRoles("", Collections.singletonList("cn=shield,ou=marvel,o=superheros"));
assertThat(roles, contains("security"));

try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.append("fantastic_four:\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void testAutoReload() throws Exception {
}

Settings.Builder builder = Settings.builder()
.put("resource.reload.interval.high", "500ms")
.put("resource.reload.interval.high", "100ms")
.put("path.home", home);
Settings settings = builder.build();
Environment env = TestEnvironment.newEnvironment(settings);
Expand All @@ -354,6 +354,19 @@ public void testAutoReload() throws Exception {

watcherService.start();

try (BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.append("\n");
}

watcherService.notifyNow(ResourceWatcherService.Frequency.HIGH);
if (latch.getCount() != 1) {
fail("Listener should not be called as roles are not changed.");
}

descriptors = store.roleDescriptors(Collections.singleton("role1"));
assertThat(descriptors, notNullValue());
assertEquals(1, descriptors.size());

try (BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
writer.newLine();
writer.newLine();
Expand Down