Search in sources :

Example 26 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class RemoveSettingsCommand method processNodePaths.

@Override
protected void processNodePaths(Terminal terminal, Path[] dataPaths, int nodeLockId, OptionSet options, Environment env) throws IOException, UserException {
    final List<String> settingsToRemove = arguments.values(options);
    if (settingsToRemove.isEmpty()) {
        throw new UserException(ExitCodes.USAGE, "Must supply at least one setting to remove");
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
    final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
    final ClusterState oldClusterState = termAndClusterState.v2();
    final Settings oldPersistentSettings = oldClusterState.metadata().persistentSettings();
    terminal.println(Terminal.Verbosity.VERBOSE, "persistent settings: " + oldPersistentSettings);
    final Settings.Builder newPersistentSettingsBuilder = Settings.builder().put(oldPersistentSettings);
    for (String settingToRemove : settingsToRemove) {
        boolean matched = false;
        for (String settingKey : oldPersistentSettings.keySet()) {
            if (Regex.simpleMatch(settingToRemove, settingKey)) {
                newPersistentSettingsBuilder.remove(settingKey);
                if (matched == false) {
                    terminal.println("The following settings will be removed:");
                }
                matched = true;
                terminal.println(settingKey + ": " + oldPersistentSettings.get(settingKey));
            }
        }
        if (matched == false) {
            throw new UserException(ExitCodes.USAGE, "No persistent cluster settings matching [" + settingToRemove + "] were found on this node");
        }
    }
    final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(Metadata.builder(oldClusterState.metadata()).persistentSettings(newPersistentSettingsBuilder.build()).build()).build();
    terminal.println(Terminal.Verbosity.VERBOSE, "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]");
    confirm(terminal, CONFIRMATION_MSG);
    try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) {
        writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState);
    }
    terminal.println(SETTINGS_REMOVED_MSG);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) UserException(org.opensearch.cli.UserException) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService) Settings(org.opensearch.common.settings.Settings)

Example 27 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class LogConfigurator method configure.

private static void configure(final Settings settings, final Path configsPath, final Path logsPath) throws IOException, UserException {
    Objects.requireNonNull(settings);
    Objects.requireNonNull(configsPath);
    Objects.requireNonNull(logsPath);
    loadLog4jPlugins();
    setLogConfigurationSystemProperty(logsPath, settings);
    // we initialize the status logger immediately otherwise Log4j will complain when we try to get the context
    configureStatusLogger();
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final Set<String> locationsWithDeprecatedPatterns = Collections.synchronizedSet(new HashSet<>());
    final List<AbstractConfiguration> configurations = new ArrayList<>();
    /*
         * Subclass the properties configurator to hack the new pattern in
         * place so users don't have to change log4j2.properties in
         * a minor release. In 7.0 we'll remove this and force users to
         * change log4j2.properties. If they don't customize log4j2.properties
         * then they won't have to do anything anyway.
         *
         * Everything in this subclass that isn't marked as a hack is copied
         * from log4j2's source.
         */
    final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory() {

        @Override
        public PropertiesConfiguration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
            final Properties properties = new Properties();
            try (InputStream configStream = source.getInputStream()) {
                properties.load(configStream);
            } catch (final IOException ioe) {
                throw new ConfigurationException("Unable to load " + source.toString(), ioe);
            }
            // Hack the new pattern into place
            for (String name : properties.stringPropertyNames()) {
                if (false == name.endsWith(".pattern"))
                    continue;
                // Null is weird here but we can't do anything with it so ignore it
                String value = properties.getProperty(name);
                if (value == null)
                    continue;
                // Tests don't need to be changed
                if (value.contains("%test_thread_info"))
                    continue;
                /*
                     * Patterns without a marker are sufficiently customized
                     * that we don't have an opinion about them.
                     */
                if (false == value.contains("%marker"))
                    continue;
                if (false == value.contains("%node_name")) {
                    locationsWithDeprecatedPatterns.add(source.getLocation());
                    properties.setProperty(name, value.replace("%marker", "[%node_name]%marker "));
                }
            }
            // end hack
            return new PropertiesConfigurationBuilder().setConfigurationSource(source).setRootProperties(properties).setLoggerContext(loggerContext).build();
        }
    };
    final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(configsPath, options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().equals("log4j2.properties")) {
                configurations.add((PropertiesConfiguration) factory.getConfiguration(context, file.toString(), file.toUri()));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    if (configurations.isEmpty()) {
        throw new UserException(ExitCodes.CONFIG, "no log4j2.properties found; tried [" + configsPath + "] and its subdirectories");
    }
    context.start(new CompositeConfiguration(configurations));
    configureLoggerLevels(settings);
    final String deprecatedLocationsString = String.join("\n  ", locationsWithDeprecatedPatterns);
    if (deprecatedLocationsString.length() > 0) {
        LogManager.getLogger(LogConfigurator.class).warn("Some logging configurations have %marker but don't have %node_name. " + "We will automatically add %node_name to the pattern to ease the migration for users who customize " + "log4j2.properties but will stop this behavior in 7.0. You should manually replace `%node_name` with " + "`[%node_name]%marker ` in these locations:\n  {}", deprecatedLocationsString);
    }
    // Redirect stdout/stderr to log4j. While we ensure Elasticsearch code does not write to those streams,
    // third party libraries may do that
    System.setOut(new PrintStream(new LoggingOutputStream(LogManager.getLogger("stdout"), Level.INFO), false, StandardCharsets.UTF_8.name()));
    System.setErr(new PrintStream(new LoggingOutputStream(LogManager.getLogger("stderr"), Level.WARN), false, StandardCharsets.UTF_8.name()));
}
Also used : FileVisitOption(java.nio.file.FileVisitOption) ArrayList(java.util.ArrayList) Properties(java.util.Properties) PropertiesConfiguration(org.apache.logging.log4j.core.config.properties.PropertiesConfiguration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) PropertiesConfigurationFactory(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) UserException(org.opensearch.cli.UserException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) InputStream(java.io.InputStream) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) PropertiesConfigurationBuilder(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder)

Example 28 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class EvilLoggerConfigurationTests method testMissingConfigFile.

public void testMissingConfigFile() {
    final Path configDir = getDataPath("does_not_exist");
    final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    final Environment environment = new Environment(settings, configDir);
    UserException e = expectThrows(UserException.class, () -> LogConfigurator.configure(environment));
    assertThat(e, hasToString(containsString("no log4j2.properties found; tried")));
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) UserException(org.opensearch.cli.UserException) Settings(org.opensearch.common.settings.Settings)

Example 29 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class EvilLoggerTests method testConcurrentDeprecationLogger.

public void testConcurrentDeprecationLogger() throws IOException, UserException, BrokenBarrierException, InterruptedException {
    setupLogging("deprecation");
    final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger("deprecation");
    final int numberOfThreads = randomIntBetween(2, 4);
    final CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    final List<Thread> threads = new ArrayList<>();
    final int iterations = randomIntBetween(1, 4);
    for (int i = 0; i < numberOfThreads; i++) {
        final Thread thread = new Thread(() -> {
            final List<Integer> ids = IntStream.range(0, 128).boxed().collect(Collectors.toList());
            Randomness.shuffle(ids);
            final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
            HeaderWarning.setThreadContext(threadContext);
            try {
                barrier.await();
            } catch (final BrokenBarrierException | InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int j = 0; j < iterations; j++) {
                for (final Integer id : ids) {
                    deprecationLogger.deprecate(Integer.toString(id), "This is a maybe logged deprecation message" + id);
                }
            }
            try {
                barrier.await();
            } catch (final BrokenBarrierException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        threads.add(thread);
        thread.start();
    }
    // synchronize the start of all threads
    barrier.await();
    // wait for all threads to complete their iterations
    barrier.await();
    final String deprecationPath = System.getProperty("opensearch.logs.base_path") + System.getProperty("file.separator") + System.getProperty("opensearch.logs.cluster_name") + "_deprecation.log";
    final List<String> deprecationEvents = Files.readAllLines(PathUtils.get(deprecationPath));
    // we appended an integer to each log message, use that for sorting
    deprecationEvents.sort(Comparator.comparingInt(s -> Integer.parseInt(s.split("message")[1])));
    assertThat(deprecationEvents.size(), equalTo(128));
    for (int i = 0; i < 128; i++) {
        assertLogLine(deprecationEvents.get(i), DEPRECATION, "org.opensearch.common.logging.DeprecationLogger\\$DeprecationLoggerBuilder.withDeprecation", "This is a maybe logged deprecation message" + i);
    }
    for (final Thread thread : threads) {
        thread.join();
    }
}
Also used : IntStream(java.util.stream.IntStream) UserException(org.opensearch.cli.UserException) RegexMatcher(org.opensearch.test.hamcrest.RegexMatcher) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Appender(org.apache.logging.log4j.core.Appender) Level(org.apache.logging.log4j.Level) Node(org.opensearch.node.Node) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) Configurator(org.apache.logging.log4j.core.config.Configurator) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) PrintWriter(java.io.PrintWriter) CyclicBarrier(java.util.concurrent.CyclicBarrier) Setting(org.opensearch.common.settings.Setting) Files(java.nio.file.Files) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) StringWriter(java.io.StringWriter) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Collectors(java.util.stream.Collectors) Matchers.startsWith(org.hamcrest.Matchers.startsWith) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Constants(org.apache.lucene.util.Constants) Matchers.hasItem(org.hamcrest.Matchers.hasItem) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Randomness(org.opensearch.common.Randomness) Matchers.equalTo(org.hamcrest.Matchers.equalTo) DEPRECATION(org.opensearch.common.logging.DeprecationLogger.DEPRECATION) CountingNoOpAppender(org.apache.logging.log4j.core.appender.CountingNoOpAppender) ClusterName(org.opensearch.cluster.ClusterName) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Matchers.endsWith(org.hamcrest.Matchers.endsWith) PathUtils(org.opensearch.common.io.PathUtils) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 30 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class CreateKeyStoreCommand method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
    try (SecureString password = options.has(passwordOption) ? readPassword(terminal, true) : new SecureString(new char[0])) {
        Path keystoreFile = KeyStoreWrapper.keystorePath(env.configDir());
        if (Files.exists(keystoreFile)) {
            if (terminal.promptYesNo("An opensearch keystore already exists. Overwrite?", false) == false) {
                terminal.println("Exiting without creating keystore.");
                return;
            }
        }
        KeyStoreWrapper keystore = KeyStoreWrapper.create();
        keystore.save(env.configDir(), password.getChars());
        terminal.println("Created opensearch keystore in " + KeyStoreWrapper.keystorePath(env.configDir()));
    } catch (SecurityException e) {
        throw new UserException(ExitCodes.IO_ERROR, "Error creating the opensearch keystore.");
    }
}
Also used : Path(java.nio.file.Path) UserException(org.opensearch.cli.UserException)

Aggregations

UserException (org.opensearch.cli.UserException)76 Path (java.nio.file.Path)44 Matchers.containsString (org.hamcrest.Matchers.containsString)38 Environment (org.opensearch.env.Environment)29 Matchers.hasToString (org.hamcrest.Matchers.hasToString)25 TestEnvironment (org.opensearch.env.TestEnvironment)25 IOException (java.io.IOException)16 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)12 BufferedReader (java.io.BufferedReader)11 Files (java.nio.file.Files)11 MockTerminal (org.opensearch.cli.MockTerminal)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)11 InputStream (java.io.InputStream)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 Terminal (org.opensearch.cli.Terminal)10 Tuple (org.opensearch.common.collect.Tuple)10 StringReader (java.io.StringReader)9 URL (java.net.URL)9