-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Security should not reload files that haven't changed (#50724)
In security we currently monitor a set of files for changes: - config/role_mapping.yml (or alternative configured path) - config/roles.yml - config/users - config/users_roles This commit prevents unnecessary reloading when the file change actually doesn't change the internal structure. Backport of: #50207 Co-authored-by: Anton Shuvaev <anton.shuvaev91@gmail.com>
- Loading branch information
1 parent
c1c0b47
commit 293661d
Showing
10 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
server/src/main/java/org/elasticsearch/common/util/Maps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
server/src/test/java/org/elasticsearch/common/util/MapsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters