Load configuration from env or system props (#3294)

This commit is contained in:
Luck 2022-04-03 15:21:18 +01:00
parent 2e0a4d71a5
commit 126631fce8
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
12 changed files with 366 additions and 69 deletions

View File

@ -32,11 +32,9 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class BukkitConfigAdapter implements ConfigurationAdapter {
private final LuckPermsPlugin plugin;
@ -75,17 +73,6 @@ public class BukkitConfigAdapter implements ConfigurationAdapter {
return this.configuration.isSet(path) ? list : def;
}
@Override
public List<String> getKeys(String path, List<String> def) {
ConfigurationSection section = this.configuration.getConfigurationSection(path);
if (section == null) {
return def;
}
Set<String> keys = section.getKeys(false);
return keys == null ? def : new ArrayList<>(keys);
}
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {
Map<String, String> map = new HashMap<>();

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.common.cacheddata.type.MonitoredMetaCache;
import me.lucko.luckperms.common.cacheddata.type.PermissionCache;
import me.lucko.luckperms.common.calculator.processor.DirectProcessor;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
@ -470,7 +471,12 @@ public class LuckPermsVaultPermission extends AbstractVaultPermission {
}
String getVaultServer() {
return this.plugin.getConfiguration().get(ConfigKeys.VAULT_SERVER);
LuckPermsConfiguration configuration = this.plugin.getConfiguration();
if (configuration.get(ConfigKeys.USE_VAULT_SERVER)) {
return configuration.get(ConfigKeys.VAULT_SERVER);
} else {
return configuration.get(ConfigKeys.SERVER);
}
}
boolean isIncludeGlobal() {

View File

@ -34,7 +34,6 @@ import net.md_5.bungee.config.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -80,16 +79,6 @@ public class BungeeConfigAdapter implements ConfigurationAdapter {
return Optional.ofNullable(this.configuration.getStringList(path)).orElse(def);
}
@Override
public List<String> getKeys(String path, List<String> def) {
Configuration section = this.configuration.getSection(path);
if (section == null) {
return def;
}
return Optional.of((List<String>) new ArrayList<>(section.getKeys())).orElse(def);
}
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {
Map<String, String> map = new HashMap<>();

View File

@ -88,16 +88,7 @@ public final class ConfigKeys {
/**
* The name of the server
*/
public static final ConfigKey<String> SERVER = key(c -> {
String server = c.getString("server", "global").toLowerCase(Locale.ROOT);
if (server.equals("load-from-system-property")) {
server = System.getProperty("luckperms.server", "global").toLowerCase(Locale.ROOT);
}
if (server.equals("load-from-environment-variable")) {
server = System.getenv().getOrDefault("LUCKPERMS_SERVER", "global").toLowerCase(Locale.ROOT);
}
return server;
});
public static final ConfigKey<String> SERVER = lowercaseStringKey("server", "global");
/**
* How many minutes to wait between syncs. A value <= 0 will disable syncing.
@ -501,14 +492,7 @@ public final class ConfigKeys {
/**
* The name of the server to use for Vault.
*/
public static final ConfigKey<String> VAULT_SERVER = key(c -> {
// default to true for backwards compatibility
if (USE_VAULT_SERVER.get(c)) {
return c.getString("vault-server", "global").toLowerCase(Locale.ROOT);
} else {
return SERVER.get(c);
}
});
public static final ConfigKey<String> VAULT_SERVER = lowercaseStringKey("vault-server", "global");
/**
* If Vault should apply global permissions

View File

@ -95,16 +95,6 @@ public abstract class ConfigurateConfigAdapter implements ConfigurationAdapter {
return node.getList(Object::toString);
}
@Override
public List<String> getKeys(String path, List<String> def) {
ConfigurationNode node = resolvePath(path);
if (node.isVirtual() || !node.isMap()) {
return def;
}
return node.getChildrenMap().keySet().stream().map(Object::toString).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {

View File

@ -44,8 +44,6 @@ public interface ConfigurationAdapter {
List<String> getStringList(String path, List<String> def);
List<String> getKeys(String path, List<String> def);
Map<String, String> getStringMap(String path, Map<String, String> def);
}

View File

@ -0,0 +1,68 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.config.generic.adapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Locale;
public class EnvironmentVariableConfigAdapter extends StringBasedConfigurationAdapter {
private static final String PREFIX = "LUCKPERMS_";
private final LuckPermsPlugin plugin;
public EnvironmentVariableConfigAdapter(LuckPermsPlugin plugin) {
this.plugin = plugin;
}
@Override
protected @Nullable String resolveValue(String path) {
// e.g.
// 'server' -> LUCKPERMS_SERVER
// 'data.table_prefix' -> LUCKPERMS_DATA_TABLE_PREFIX
String key = PREFIX + path.toUpperCase(Locale.ROOT)
.replace('-', '_')
.replace('.', '_');
String value = System.getenv(key);
if (value != null) {
this.plugin.getLogger().info("Resolved configuration value from environment variable: " + key + " = " + (path.contains("password") ? "*****" : value));
}
return value;
}
@Override
public LuckPermsPlugin getPlugin() {
return this.plugin;
}
@Override
public void reload() {
// no-op
}
}

View File

@ -0,0 +1,117 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.config.generic.adapter;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.util.List;
import java.util.Map;
/**
* A {@link ConfigurationAdapter} composed of one or more other ConfigurationAdapters.
*/
public class MultiConfigurationAdapter implements ConfigurationAdapter {
private final LuckPermsPlugin plugin;
private final List<ConfigurationAdapter> adapters;
/**
* Creates a {@link MultiConfigurationAdapter}.
*
* <p>The first adapter in the list has priority (the final say) in deciding what the value is.
* All adapters are tried in reverse order, and the value returned from the previous adapter
* is passed into the next as the {@code def} value.</p>
*
* @param plugin the plugin
* @param adapters a list of adapters
*/
public MultiConfigurationAdapter(LuckPermsPlugin plugin, List<ConfigurationAdapter> adapters) {
this.plugin = plugin;
this.adapters = ImmutableList.copyOf(adapters).reverse();
}
public MultiConfigurationAdapter(LuckPermsPlugin plugin, ConfigurationAdapter... adapters) {
this(plugin, ImmutableList.copyOf(adapters));
}
@Override
public LuckPermsPlugin getPlugin() {
return this.plugin;
}
@Override
public void reload() {
for (ConfigurationAdapter adapter : this.adapters) {
adapter.reload();
}
}
@Override
public String getString(String path, String def) {
String result = def;
for (ConfigurationAdapter adapter : this.adapters) {
result = adapter.getString(path, result);
}
return result;
}
@Override
public int getInteger(String path, int def) {
int result = def;
for (ConfigurationAdapter adapter : this.adapters) {
result = adapter.getInteger(path, result);
}
return result;
}
@Override
public boolean getBoolean(String path, boolean def) {
boolean result = def;
for (ConfigurationAdapter adapter : this.adapters) {
result = adapter.getBoolean(path, result);
}
return result;
}
@Override
public List<String> getStringList(String path, List<String> def) {
List<String> result = def;
for (ConfigurationAdapter adapter : this.adapters) {
result = adapter.getStringList(path, result);
}
return result;
}
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {
Map<String, String> result = def;
for (ConfigurationAdapter adapter : this.adapters) {
result = adapter.getStringMap(path, result);
}
return result;
}
}

View File

@ -0,0 +1,99 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.config.generic.adapter;
import com.google.common.base.Splitter;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
import java.util.Map;
public abstract class StringBasedConfigurationAdapter implements ConfigurationAdapter {
private static final Splitter LIST_SPLITTER = Splitter.on(',');
private static final Splitter.MapSplitter MAP_SPLITTER = Splitter.on(',').withKeyValueSeparator('=');
protected abstract @Nullable String resolveValue(String path);
@Override
public String getString(String path, String def) {
String value = resolveValue(path);
if (value == null) {
return def;
}
return value;
}
@Override
public int getInteger(String path, int def) {
String value = resolveValue(path);
if (value == null) {
return def;
}
try {
return Integer.parseInt(value);
} catch (IllegalArgumentException e) {
return def;
}
}
@Override
public boolean getBoolean(String path, boolean def) {
String value = resolveValue(path);
if (value == null) {
return def;
}
try {
return Boolean.parseBoolean(value);
} catch (IllegalArgumentException e) {
return def;
}
}
@Override
public List<String> getStringList(String path, List<String> def) {
String value = resolveValue(path);
if (value == null) {
return def;
}
return LIST_SPLITTER.splitToList(value);
}
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {
String value = resolveValue(path);
if (value == null) {
return def;
}
return MAP_SPLITTER.split(value);
}
}

View File

@ -0,0 +1,64 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.config.generic.adapter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import org.checkerframework.checker.nullness.qual.Nullable;
public class SystemPropertyConfigAdapter extends StringBasedConfigurationAdapter {
private static final String PREFIX = "luckperms.";
private final LuckPermsPlugin plugin;
public SystemPropertyConfigAdapter(LuckPermsPlugin plugin) {
this.plugin = plugin;
}
@Override
protected @Nullable String resolveValue(String path) {
// e.g.
// 'server' -> luckperms.server
// 'data.table_prefix' -> luckperms.data.table-prefix
String key = PREFIX + path;
String value = System.getProperty(key);
if (value != null) {
this.plugin.getLogger().info("Resolved configuration value from system property: " + key + " = " + (path.contains("password") ? "*****" : value));
}
return value;
}
@Override
public LuckPermsPlugin getPlugin() {
return this.plugin;
}
@Override
public void reload() {
// no-op
}
}

View File

@ -32,6 +32,9 @@ import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.config.generic.adapter.EnvironmentVariableConfigAdapter;
import me.lucko.luckperms.common.config.generic.adapter.MultiConfigurationAdapter;
import me.lucko.luckperms.common.config.generic.adapter.SystemPropertyConfigAdapter;
import me.lucko.luckperms.common.context.calculator.ConfigurationContextCalculator;
import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.dependencies.DependencyManager;
@ -132,7 +135,12 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
// load configuration
getLogger().info("Loading configuration...");
this.configuration = new LuckPermsConfiguration(this, provideConfigurationAdapter());
ConfigurationAdapter configFileAdapter = provideConfigurationAdapter();
this.configuration = new LuckPermsConfiguration(this, new MultiConfigurationAdapter(this,
new SystemPropertyConfigAdapter(this),
new EnvironmentVariableConfigAdapter(this),
configFileAdapter
));
// setup a bytebin instance
this.httpClient = new OkHttpClient.Builder()

View File

@ -32,11 +32,9 @@ import cn.nukkit.utils.Config;
import cn.nukkit.utils.ConfigSection;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class NukkitConfigAdapter implements ConfigurationAdapter {
private final LuckPermsPlugin plugin;
@ -75,17 +73,6 @@ public class NukkitConfigAdapter implements ConfigurationAdapter {
return list == null ? def : list;
}
@Override
public List<String> getKeys(String path, List<String> def) {
ConfigSection section = this.configuration.getSection(path);
if (section == null) {
return def;
}
Set<String> keys = section.getKeys(false);
return keys == null ? def : new ArrayList<>(keys);
}
@Override
public Map<String, String> getStringMap(String path, Map<String, String> def) {
Map<String, String> map = new HashMap<>();