Simplify config handling.

This commit is contained in:
Kavin 2023-08-05 16:53:38 +01:00
parent 97889f393e
commit 847f80c0d4
No known key found for this signature in database
GPG Key ID: 6E4598CA5C92C41F

View File

@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.minio.MinioClient;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.utils.PageMixin;
import me.kavin.piped.utils.RequestUtils;
import me.kavin.piped.utils.obj.OidcProvider;
@ -25,10 +26,8 @@ import rocks.kavin.reqwest4j.ReqwestUtils;
import java.io.File;
import java.io.FileReader;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
@ -103,7 +102,7 @@ public class Constants {
public static final String YOUTUBE_COUNTRY;
public static final String VERSION;
public static final ArrayList<OidcProvider> OIDC_PROVIDERS;
public static final List<OidcProvider> OIDC_PROVIDERS;
public static final ObjectMapper mapper = JsonMapper.builder()
.addMixIn(Page.class, PageMixin.class)
@ -169,7 +168,9 @@ public class Constants {
MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN");
GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL");
OIDC_PROVIDERS = new ArrayList<>();
OIDC_PROVIDERS = new ObjectArrayList<>();
Map<String, Map<String, String>> oidcProviderConfig = new Object2ObjectOpenHashMap<>();
ArrayNode providerNames = frontendProperties.putArray("oidcProviders");
prop.forEach((_key, _value) -> {
String key = String.valueOf(_key), value = String.valueOf(_value);
@ -178,19 +179,26 @@ public class Constants {
else if (key.startsWith("frontend."))
frontendProperties.put(StringUtils.substringAfter(key, "frontend."), value);
else if (key.startsWith("oidc.provider")) {
String[] split = key.split("\\.");
if (split.length != 4 || !split[3].equals("name")) return;
OIDC_PROVIDERS.add(new OidcProvider(
value,
getRequiredOidcProperty(prop, value, "clientId"),
getRequiredOidcProperty(prop, value, "clientSecret"),
getRequiredOidcProperty(prop, value, "authUri"),
getRequiredOidcProperty(prop, value, "tokenUri"),
getRequiredOidcProperty(prop, value, "userinfoUri"))
);
providerNames.add(value);
String[] split = key.split("\\.");
if (split.length != 4) return;
oidcProviderConfig
.computeIfAbsent(split[2], k -> new Object2ObjectOpenHashMap<>())
.put(split[3], value);
}
});
oidcProviderConfig.forEach((provider, config) -> {
ObjectNode providerNode = frontendProperties.putObject(provider);
OIDC_PROVIDERS.add(new OidcProvider(
getRequiredMapValue(config, "name"),
getRequiredMapValue(config, "clientId"),
getRequiredMapValue(config, "clientSecret"),
getRequiredMapValue(config, "authUri"),
getRequiredMapValue(config, "tokenUri"),
getRequiredMapValue(config, "userinfoUri")
));
providerNames.add(provider);
config.forEach(providerNode::put);
});
frontendProperties.put("imageProxyUrl", IMAGE_PROXY_PART);
frontendProperties.putArray("countries").addAll(
YOUTUBE_SERVICE.getSupportedCountries().stream().map(ContentCountry::getCountryCode)
@ -247,10 +255,10 @@ public class Constants {
return prop.getProperty(key, def);
}
private static String getRequiredOidcProperty(final Properties prop, String provider, String key) {
String value = getProperty(prop, "oidc.provider." + provider + "." + key);
if(value == null || value.equals("")){
System.err.println("Missing " + key + " for oidc provider '" + provider + "'");
private static String getRequiredMapValue(final Map<?, String> map, Object key) {
String value = map.get(key);
if (StringUtils.isBlank(value)) {
System.err.println("Missing '" + key + "' in sub-configuration");
System.exit(1);
}
return value;