Skip to content

Commit

Permalink
🐛 Fix error message not logged if configuration file or configuration…
Browse files Browse the repository at this point in the history
… override file is not found
  • Loading branch information
ujibang committed Dec 15, 2023
1 parent 61ac748 commit 063e756
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
*/
package org.restheart.configuration;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.google.common.collect.Maps;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import static org.restheart.configuration.Utils.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
Expand All @@ -40,18 +33,29 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.mongodb.ConnectionString;

import org.apache.commons.jxpath.JXPathContext;
import org.restheart.configuration.Utils.RhOverride;
import static org.restheart.configuration.Utils.asMap;
import static org.restheart.configuration.Utils.findOrDefault;
import static org.restheart.configuration.Utils.overrides;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import com.google.common.collect.Maps;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mongodb.ConnectionString;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;

/**
* Class that holds the configuration.
*
Expand Down Expand Up @@ -97,7 +101,7 @@ public class Configuration {
private final Logging logging;
private final Map<String, Object> connectionOptions;

private Map<String, Object> conf;
private final Map<String, Object> conf;

/**
* Creates a new instance of Configuration from the configuration file For any
Expand Down Expand Up @@ -238,15 +242,11 @@ public static Path getPath() {
return PATH;
}

static boolean isParametric(final Path confFilePath) throws IOException {
try (var sc = new Scanner(confFilePath, "UTF-8")) {
return sc.findAll(Pattern.compile("\\{\\{.*\\}\\}")).limit(1).count() > 0;
}
}

public class Builder {
/**
*
* @param standaloneConfiguration
* @param silent
* @return the default configuration
*/
public static Configuration build(boolean standaloneConfiguration, boolean silent) {
Expand All @@ -255,7 +255,10 @@ public static Configuration build(boolean standaloneConfiguration, boolean silen

/**
*
* @param confFile
* @param confFilePath
* @param confOverridesFilePath
* @param standaloneConfiguration
* @param silent
* @return return the configuration from confFile and propFile
*/
public static Configuration build(Path confFilePath, Path confOverridesFilePath, boolean standaloneConfiguration, boolean silent) throws ConfigurationException {
Expand Down Expand Up @@ -369,7 +372,7 @@ private static String fromYmlToRho(Reader yml) throws JsonParseException {
final var gson = new GsonBuilder().serializeNulls().create();

return _yml.entrySet().stream()
.map(e -> e.getKey() + "->" + gson.toJson(e.getValue()).toString())
.map(e -> e.getKey() + "->" + gson.toJson(e.getValue()))
.collect(Collectors.joining(";"));
}

Expand All @@ -389,7 +392,7 @@ private static Map<String, Object> overrideConfiguration(Map<String, Object> con
overrides.stream().forEachOrdered(o -> {
if (!silent) {
if (o.value() instanceof HashMap<?, ?> mapValue) {
var maskedValue = new HashMap<String, Object>();
var maskedValue = new HashMap<>();
mapValue.keySet().stream()
.filter(k -> k instanceof String)
.map(k -> (String) k)
Expand Down
53 changes: 31 additions & 22 deletions core/src/main/java/org/restheart/Bootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@
*/
package org.restheart;

import static io.undertow.Handlers.resource;
import static org.fusesource.jansi.Ansi.ansi;
import static org.fusesource.jansi.Ansi.Color.GREEN;
import static org.fusesource.jansi.Ansi.Color.RED;
import static org.restheart.exchange.Exchange.MAX_CONTENT_SIZE;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.PROXY;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.STATIC_RESOURCE;
import static org.restheart.handlers.PipelinedHandler.pipe;
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_AFTER_AUTH;
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_BEFORE_AUTH;
import static org.restheart.plugins.InitPoint.AFTER_STARTUP;
import static org.restheart.plugins.InitPoint.BEFORE_STARTUP;
import static org.restheart.plugins.InterceptPoint.REQUEST_AFTER_AUTH;
import static org.restheart.plugins.InterceptPoint.REQUEST_BEFORE_AUTH;
import static org.restheart.utils.PluginUtils.defaultURI;
import static org.restheart.utils.PluginUtils.initPoint;
import static org.restheart.utils.PluginUtils.uriMatchPolicy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -64,22 +46,31 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import static org.fusesource.jansi.Ansi.Color.GREEN;
import static org.fusesource.jansi.Ansi.Color.RED;
import static org.fusesource.jansi.Ansi.ansi;
import org.fusesource.jansi.AnsiConsole;
import org.restheart.configuration.Configuration;
import org.restheart.configuration.ConfigurationException;
import org.restheart.configuration.Utils;
import org.restheart.configuration.ProxiedResource;
import org.restheart.configuration.Utils;
import org.restheart.exchange.Exchange;
import static org.restheart.exchange.Exchange.MAX_CONTENT_SIZE;
import org.restheart.exchange.ExchangeKeys;
import org.restheart.exchange.PipelineInfo;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.PROXY;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.STATIC_RESOURCE;
import org.restheart.graal.NativeImageBuildTimeChecker;
import org.restheart.handlers.BeforeExchangeInitInterceptorsExecutor;
import org.restheart.handlers.ConfigurableEncodingHandler;
import org.restheart.handlers.ErrorHandler;
import org.restheart.handlers.PipelinedHandler;
import static org.restheart.handlers.PipelinedHandler.pipe;
import org.restheart.handlers.PipelinedWrappingHandler;
import org.restheart.handlers.ProxyExchangeBuffersCloser;
import org.restheart.handlers.QueryStringRebuilder;
Expand All @@ -91,20 +82,35 @@
import org.restheart.handlers.injectors.ConduitInjector;
import org.restheart.handlers.injectors.PipelineInfoInjector;
import org.restheart.handlers.injectors.RequestContentInjector;
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_AFTER_AUTH;
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_BEFORE_AUTH;
import org.restheart.handlers.injectors.XForwardedHeadersInjector;
import org.restheart.handlers.injectors.XPoweredByInjector;
import static org.restheart.plugins.InitPoint.AFTER_STARTUP;
import static org.restheart.plugins.InitPoint.BEFORE_STARTUP;
import static org.restheart.plugins.InterceptPoint.REQUEST_AFTER_AUTH;
import static org.restheart.plugins.InterceptPoint.REQUEST_BEFORE_AUTH;
import org.restheart.plugins.PluginRecord;
import org.restheart.plugins.PluginsRegistryImpl;
import org.restheart.plugins.RegisterPlugin;
import org.restheart.plugins.security.AuthMechanism;
import org.restheart.plugins.security.Authorizer;
import org.restheart.plugins.security.TokenManager;
import org.restheart.plugins.security.Authorizer.TYPE;
import org.restheart.plugins.security.TokenManager;
import org.restheart.security.handlers.SecurityHandler;
import static org.restheart.utils.BootstrapperUtils.checkPidFile;
import static org.restheart.utils.BootstrapperUtils.initLogging;
import static org.restheart.utils.BootstrapperUtils.logLoggingConfiguration;
import static org.restheart.utils.BootstrapperUtils.logStartMessages;
import static org.restheart.utils.BootstrapperUtils.pidFile;
import static org.restheart.utils.BootstrapperUtils.setJsonpathDefaults;
import org.restheart.utils.FileUtils;
import org.restheart.utils.LoggingInitializer;
import org.restheart.utils.OSChecker;
import org.restheart.utils.PluginUtils;
import static org.restheart.utils.PluginUtils.defaultURI;
import static org.restheart.utils.PluginUtils.initPoint;
import static org.restheart.utils.PluginUtils.uriMatchPolicy;
import org.restheart.utils.RESTHeartDaemon;
import org.restheart.utils.ResourcesExtractor;
import org.slf4j.Logger;
Expand All @@ -113,6 +119,8 @@
import org.xnio.Options;
import org.xnio.SslClientAuthMode;
import org.xnio.Xnio;

import static io.undertow.Handlers.resource;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.server.handlers.AllowedMethodsHandler;
Expand All @@ -128,7 +136,6 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import static org.restheart.utils.BootstrapperUtils.*;

/**
*
Expand Down Expand Up @@ -222,7 +229,9 @@ public static void main(final String[] args) throws ConfigurationException, IOEx
try {
configuration = Configuration.Builder.build(CONFIGURATION_FILE_PATH, CONF_OVERRIDES_FILE_PATH, standaloneConfiguration, true);
} catch(ConfigurationException ce) {
logErrorAndExit(ce.getMessage(), ce, true, true, -1);
var confJustForError = Configuration.Builder.build(true, true);
initLogging(confJustForError, null, IS_FORKED);
logErrorAndExit(ce.getMessage(), ce, false, true, -1);
}

run();
Expand Down
17 changes: 9 additions & 8 deletions core/src/main/java/org/restheart/utils/BootstrapperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@
import java.util.EnumSet;
import java.util.Set;

import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

import static org.fusesource.jansi.Ansi.ansi;
import static org.fusesource.jansi.Ansi.Color.RED;

import static org.fusesource.jansi.Ansi.ansi;
import org.restheart.Bootstrapper;
import org.restheart.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

public class BootstrapperUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(Bootstrapper.class);

Expand Down Expand Up @@ -109,7 +108,8 @@ public static Path pidFile(Path confFilePath, Path confOverridesFilePath) {
/**
* initLogging
*
* @param args
* @param configuration
* @param isForked
* @param d
*/
public static void initLogging(Configuration configuration, final RESTHeartDaemon d, boolean isForked) {
Expand Down Expand Up @@ -138,6 +138,7 @@ public static void logStartMessages(Configuration configuration) {
/**
* logLoggingConfiguration
*
* @param configuration
* @param fork
*/
public static void logLoggingConfiguration(Configuration configuration, boolean fork) {
Expand Down

0 comments on commit 063e756

Please sign in to comment.