Merge branch 'master' into junit5-migration

# Conflicts:
#	src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java
This commit is contained in:
ljacqu 2023-12-20 09:54:07 +01:00
commit ee424378ca
9 changed files with 60 additions and 47 deletions

View File

@ -403,6 +403,8 @@ Protection:
# Apply the protection also to registered usernames
enableProtectionRegistered: true
geoIpDatabase:
# Enable GeoIp database
enabled: true
# The MaxMind clientId used to download the GeoIp database,
# get one at https://www.maxmind.com/en/accounts/current/license-key
# The EssentialsX project has a very useful tutorial on how to generate

View File

@ -760,7 +760,7 @@
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>configme</artifactId>
<version>1.4.0</version>
<version>1.3.0</version>
<optional>true</optional>
<exclusions>
<exclusion>

View File

@ -5,7 +5,6 @@ import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.convertresult.PropertyValue;
import ch.jalu.configme.resource.PropertyReader;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.updater.MessageUpdater.MessageKeyProperty;
import java.util.List;
import java.util.Map;
@ -40,19 +39,15 @@ public class MessageKeyConfigurationData extends ConfigurationDataImpl {
}
@SuppressWarnings("unchecked")
public List<MessageKeyProperty> getAllMessageProperties() {
public List<Property<String>> getAllMessageProperties() {
return (List) getProperties();
}
public String getMessage(MessageKey messageKey) {
return getValue(new MessageKeyProperty(messageKey));
}
public String getMessage(MessageKeyProperty property) {
return (String) getValues().get(property.getPath());
return getValue(new MessageUpdater.MessageKeyProperty(messageKey));
}
public void setMessage(MessageKey messageKey, String message) {
setValue(new MessageKeyProperty(messageKey), message);
setValue(new MessageUpdater.MessageKeyProperty(messageKey), message);
}
}

View File

@ -4,13 +4,13 @@ import ch.jalu.configme.exception.ConfigMeException;
import ch.jalu.configme.resource.PropertyReader;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -36,8 +36,8 @@ final class MessageMigraterPropertyReader implements PropertyReader {
* @param file the file to load
* @return the created property reader
*/
public static MessageMigraterPropertyReader loadFromFile(Path file) {
try (InputStream is = Files.newInputStream(file)) {
public static MessageMigraterPropertyReader loadFromFile(File file) {
try (InputStream is = new FileInputStream(file)) {
return loadFromStream(is);
} catch (IOException e) {
throw new IllegalStateException("Error while reading file '" + file + "'", e);

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.PropertyListBuilder;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
import ch.jalu.configme.resource.PropertyReader;
@ -9,8 +10,8 @@ import ch.jalu.configme.resource.PropertyResource;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.util.FileUtils;
import java.io.File;
@ -101,9 +102,9 @@ public class MessageUpdater {
private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) {
List<String> addedKeys = new ArrayList<>();
for (MessageKeyProperty property : configurationData.getAllMessageProperties()) {
for (Property<String> property : configurationData.getAllMessageProperties()) {
final String key = property.getPath();
if (configurationData.getMessage(property) == null) {
if (configurationData.getValue(property) == null) {
configurationData.setValue(property, jarMessageSource.getMessageFromJar(property));
addedKeys.add(key);
}

View File

@ -2,14 +2,8 @@ package fr.xephi.authme.message.updater;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilder;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilderImpl;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
import java.io.File;
@ -18,43 +12,36 @@ import java.io.File;
*/
public class MigraterYamlFileResource extends YamlFileResource {
private Yaml singleQuoteYaml;
public MigraterYamlFileResource(File file) {
super(file);
}
@Override
public PropertyReader createReader() {
return MessageMigraterPropertyReader.loadFromFile(getPath());
return MessageMigraterPropertyReader.loadFromFile(getFile());
}
@Override
protected Yaml createNewYaml() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
options.setProcessComments(true);
options.setIndent(4);
// Overridden setting: don't split lines
options.setSplitLines(false);
return new Yaml(options);
if (singleQuoteYaml == null) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
// Overridden setting: don't split lines
options.setSplitLines(false);
singleQuoteYaml = new Yaml(options);
}
return singleQuoteYaml;
}
// Because we set the YAML object to put strings in single quotes, this method by default uses that YAML object
// and also puts all paths as single quotes. Override to just always return the same string since we know those
// are only message names (so never any conflicting strings like "true" or "0").
@Override
protected @NotNull SnakeYamlNodeBuilder createNodeBuilder() {
return new MigraterYamlNodeBuilder();
}
/** Extended to represent all strings with single quotes in the YAML. */
private static final class MigraterYamlNodeBuilder extends SnakeYamlNodeBuilderImpl {
@Override
protected @NotNull Node createStringNode(@NotNull String value) {
return new ScalarNode(Tag.STR, value, null, null, DumperOptions.ScalarStyle.SINGLE_QUOTED);
}
@Override
public @NotNull Node createKeyNode(@NotNull String key) {
return super.createStringNode(key); // no single quotes
}
protected String escapePathElementIfNeeded(String path) {
return path;
}
}

View File

@ -89,6 +89,12 @@ public class GeoIpService {
* @return True if the data is available, false otherwise.
*/
private synchronized boolean isDataAvailable() {
// If this feature is disabled, just stop
if (!settings.getProperty(ProtectionSettings.ENABLE_GEOIP)) {
return false;
}
if (downloading) {
// we are currently downloading the database
return false;

View File

@ -20,6 +20,10 @@ public final class ProtectionSettings implements SettingsHolder {
public static final Property<Boolean> ENABLE_PROTECTION_REGISTERED =
newProperty("Protection.enableProtectionRegistered", true);
@Comment("Enable GeoIp database")
public static final Property<Boolean> ENABLE_GEOIP =
newProperty("Protection.geoIpDatabase.enabled", true);
@Comment({"The MaxMind clientId used to download the GeoIp database,",
"get one at https://www.maxmind.com/en/accounts/current/license-key",
"The EssentialsX project has a very useful tutorial on how to generate",

View File

@ -4,6 +4,7 @@ import com.maxmind.db.GeoIp2Provider;
import com.maxmind.db.model.Country;
import com.maxmind.db.model.CountryResponse;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -21,6 +22,7 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Test for {@link GeoIpService}.
@ -59,6 +61,7 @@ class GeoIpServiceTest {
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
// when
String result = geoIpService.getCountryCode(ip.getHostAddress());
@ -93,6 +96,7 @@ class GeoIpServiceTest {
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
// when
String result = geoIpService.getCountryName(ip.getHostAddress());
@ -114,4 +118,18 @@ class GeoIpServiceTest {
assertThat(result, equalTo("LocalHost"));
verify(lookupService, never()).getCountry(ip);
}
@Test
public void shouldNotLookUpCountryNameIfDisabled() throws Exception {
// given
InetAddress ip = InetAddress.getByName("24.45.167.89");
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(false);
// when
String result = geoIpService.getCountryName(ip.getHostAddress());
// then
assertThat(result, equalTo("N/A"));
verifyNoInteractions(lookupService);
}
}