aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/main/java/org/sonar/application/es/EsKeyStoreCli.java
blob: 19f4cb2294154f4cf8b2d3266e3c561d22136061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.application.es;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.sonar.application.command.JavaCommand;
import org.sonar.application.command.JvmOptions;
import org.sonar.process.ProcessId;

import static java.util.Objects.requireNonNull;

public class EsKeyStoreCli {
  public static final String BOOTSTRAP_PASSWORD_PROPERTY_KEY = "bootstrap.password";
  public static final String KEYSTORE_PASSWORD_PROPERTY_KEY = "xpack.security.transport.ssl.keystore.secure_password";
  public static final String TRUSTSTORE_PASSWORD_PROPERTY_KEY = "xpack.security.transport.ssl.truststore.secure_password";
  public static final String HTTP_KEYSTORE_PASSWORD_PROPERTY_KEY = "xpack.security.http.ssl.keystore.secure_password";

  private static final String MAIN_CLASS_NAME = "org.elasticsearch.launcher.CliToolLauncher";

  private final Map<String, String> properties = new LinkedHashMap<>();
  private final JavaCommand<EsKeyStoreJvmOptions> command;

  private EsKeyStoreCli(EsInstallation esInstallation) {
    String esHomeAbsolutePath = esInstallation.getHomeDirectory().getAbsolutePath();
    command = new JavaCommand<EsKeyStoreJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
      .setClassName(MAIN_CLASS_NAME)
      .setJvmOptions(new EsKeyStoreJvmOptions(esInstallation))
      .addClasspath(Paths.get(esHomeAbsolutePath, "lib", "").toAbsolutePath() + File.separator + "*")
      .addClasspath(Paths.get(esHomeAbsolutePath, "lib", "cli-launcher", "").toAbsolutePath() + File.separator + "*")
      .addParameter("add")
      .addParameter("-x")
      .addParameter("-f");
  }

  public static EsKeyStoreCli getInstance(EsInstallation esInstallation) {
    return new EsKeyStoreCli(esInstallation);
  }

  public EsKeyStoreCli store(String key, String value) {
    requireNonNull(key, "Property key cannot be null");
    requireNonNull(value, "Property value cannot be null");
    properties.computeIfAbsent(key, s -> {
      command.addParameter(key);
      return value;
    });
    return this;
  }

  public Process executeWith(Function<JavaCommand<?>, Process> commandLauncher) {
    Process process = commandLauncher.apply(command);
    writeValues(process);
    waitFor(process);
    checkExitValue(process.exitValue());
    return process;
  }

  private void writeValues(Process process) {
    try (OutputStream stdin = process.getOutputStream();
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin, StandardCharsets.UTF_8))) {
      for (Entry<String, String> entry : properties.entrySet()) {
        writer.write(entry.getValue());
        writer.write("\n");
      }
      writer.flush();

    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }

  private static void waitFor(Process process) {
    try {
      process.waitFor(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new IllegalStateException("EsKeyStoreCli has been interrupted", e);
    }
  }

  private static void checkExitValue(int code) {
    if (code != 0) {
      throw new IllegalStateException("Elasticsearch KeyStore tool exited with code: " + code);
    }
  }

  public static class EsKeyStoreJvmOptions extends JvmOptions<EsKeyStoreJvmOptions> {

    public EsKeyStoreJvmOptions(EsInstallation esInstallation) {
      super(mandatoryOptions(esInstallation));
    }

    private static Map<String, String> mandatoryOptions(EsInstallation esInstallation) {
      Map<String, String> res = new LinkedHashMap<>(7);
      res.put("-Xms4m", "");
      res.put("-Xmx64m", "");
      res.put("-XX:+UseSerialGC", "");
      res.put("-Dcli.name=", "");
      res.put("-Dcli.script=", "bin/elasticsearch-keystore");
      res.put("-Dcli.libs=", "lib/tools/keystore-cli");
      res.put("-Des.path.home=", esInstallation.getHomeDirectory().getAbsolutePath());
      res.put("-Des.path.conf=", esInstallation.getConfDirectory().getAbsolutePath());
      return res;
    }
  }
}