You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EsInstallation.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.application.es;
  21. import java.io.File;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import java.util.Optional;
  27. import java.util.Properties;
  28. import java.util.stream.Stream;
  29. import javax.annotation.Nullable;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.sonar.application.command.EsJvmOptions;
  32. import org.sonar.process.Props;
  33. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED;
  34. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_HTTP_KEYSTORE;
  35. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_HTTP_KEYSTORE_PASSWORD;
  36. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_KEYSTORE;
  37. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_KEYSTORE_PASSWORD;
  38. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_TRUSTSTORE;
  39. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_TRUSTSTORE_PASSWORD;
  40. import static org.sonar.process.ProcessProperties.Property.CLUSTER_SEARCH_PASSWORD;
  41. import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
  42. import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
  43. import static org.sonar.process.ProcessProperties.Property.PATH_LOGS;
  44. import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
  45. /**
  46. * Holds {@link File} to the various directories of ElasticSearch distribution embedded in SonarQube and provides
  47. * {@link File} objects to the various files of it SonarQube cares about.
  48. *
  49. * <p>
  50. * This class does not ensure files nor directories actually exist.
  51. * </p>
  52. */
  53. public class EsInstallation {
  54. private final File homeDirectory;
  55. private final List<File> outdatedSearchDirectories;
  56. private final File dataDirectory;
  57. private final File confDirectory;
  58. private final File logDirectory;
  59. private EsJvmOptions esJvmOptions;
  60. private EsYmlSettings esYmlSettings;
  61. private Properties log4j2Properties;
  62. private String host;
  63. private int httpPort;
  64. // ES authentication settings
  65. private final boolean securityEnabled;
  66. private final String bootstrapPassword;
  67. private final Path keyStoreLocation;
  68. private final Path trustStoreLocation;
  69. @Nullable
  70. private final String keyStorePassword;
  71. @Nullable
  72. private final String trustStorePassword;
  73. private final Path httpKeyStoreLocation;
  74. @Nullable
  75. private final String httpKeyStorePassword;
  76. private final boolean httpEncryptionEnabled;
  77. public EsInstallation(Props props) {
  78. File sqHomeDir = props.nonNullValueAsFile(PATH_HOME.getKey());
  79. this.homeDirectory = new File(sqHomeDir, "elasticsearch");
  80. this.outdatedSearchDirectories = buildOutdatedSearchDirs(props);
  81. this.dataDirectory = buildDataDir(props);
  82. this.confDirectory = buildConfDir(props);
  83. this.logDirectory = buildLogPath(props);
  84. this.bootstrapPassword = props.value(CLUSTER_SEARCH_PASSWORD.getKey());
  85. this.securityEnabled = props.valueAsBoolean(CLUSTER_ENABLED.getKey()) && StringUtils.isNotBlank(bootstrapPassword);
  86. this.keyStoreLocation = getPath(props.value(CLUSTER_ES_KEYSTORE.getKey()));
  87. this.keyStorePassword = props.value(CLUSTER_ES_KEYSTORE_PASSWORD.getKey());
  88. this.trustStoreLocation = getPath(props.value(CLUSTER_ES_TRUSTSTORE.getKey()));
  89. this.trustStorePassword = props.value(CLUSTER_ES_TRUSTSTORE_PASSWORD.getKey());
  90. this.httpKeyStoreLocation = getPath(props.value(CLUSTER_ES_HTTP_KEYSTORE.getKey()));
  91. this.httpKeyStorePassword = props.value(CLUSTER_ES_HTTP_KEYSTORE_PASSWORD.getKey());
  92. this.httpEncryptionEnabled = securityEnabled && httpKeyStoreLocation != null;
  93. }
  94. private static Path getPath(@Nullable String path) {
  95. return Optional.ofNullable(path).map(Paths::get).orElse(null);
  96. }
  97. private static List<File> buildOutdatedSearchDirs(Props props) {
  98. String dataPath = props.nonNullValue(PATH_DATA.getKey());
  99. return Stream.of("es", "es5", "es6", "es7")
  100. .map(t -> new File(dataPath, t))
  101. .toList();
  102. }
  103. private static File buildDataDir(Props props) {
  104. String dataPath = props.nonNullValue(PATH_DATA.getKey());
  105. return new File(dataPath, "es8");
  106. }
  107. private static File buildLogPath(Props props) {
  108. return props.nonNullValueAsFile(PATH_LOGS.getKey());
  109. }
  110. private static File buildConfDir(Props props) {
  111. File tempPath = props.nonNullValueAsFile(PATH_TEMP.getKey());
  112. return new File(new File(tempPath, "conf"), "es");
  113. }
  114. public File getHomeDirectory() {
  115. return homeDirectory;
  116. }
  117. public List<File> getOutdatedSearchDirectories() {
  118. return Collections.unmodifiableList(outdatedSearchDirectories);
  119. }
  120. public File getDataDirectory() {
  121. return dataDirectory;
  122. }
  123. public File getConfDirectory() {
  124. return confDirectory;
  125. }
  126. public File getLogDirectory() {
  127. return logDirectory;
  128. }
  129. public File getExecutable() {
  130. return new File(homeDirectory, "bin/elasticsearch");
  131. }
  132. public File getLog4j2PropertiesLocation() {
  133. return new File(confDirectory, "log4j2.properties");
  134. }
  135. public File getElasticsearchYml() {
  136. return new File(confDirectory, "elasticsearch.yml");
  137. }
  138. public File getJvmOptions() {
  139. return new File(confDirectory, "jvm.options");
  140. }
  141. public File getLibDirectory() {
  142. return new File(homeDirectory, "lib");
  143. }
  144. public EsJvmOptions getEsJvmOptions() {
  145. return esJvmOptions;
  146. }
  147. public EsInstallation setEsJvmOptions(EsJvmOptions esJvmOptions) {
  148. this.esJvmOptions = esJvmOptions;
  149. return this;
  150. }
  151. public EsYmlSettings getEsYmlSettings() {
  152. return esYmlSettings;
  153. }
  154. public EsInstallation setEsYmlSettings(EsYmlSettings esYmlSettings) {
  155. this.esYmlSettings = esYmlSettings;
  156. return this;
  157. }
  158. public Properties getLog4j2Properties() {
  159. return log4j2Properties;
  160. }
  161. public EsInstallation setLog4j2Properties(Properties log4j2Properties) {
  162. this.log4j2Properties = log4j2Properties;
  163. return this;
  164. }
  165. public String getHost() {
  166. return host;
  167. }
  168. public EsInstallation setHost(String host) {
  169. this.host = host;
  170. return this;
  171. }
  172. public int getHttpPort() {
  173. return httpPort;
  174. }
  175. public EsInstallation setHttpPort(int httpPort) {
  176. this.httpPort = httpPort;
  177. return this;
  178. }
  179. public boolean isSecurityEnabled() {
  180. return securityEnabled;
  181. }
  182. public String getBootstrapPassword() {
  183. return bootstrapPassword;
  184. }
  185. public Path getKeyStoreLocation() {
  186. return keyStoreLocation;
  187. }
  188. public Path getTrustStoreLocation() {
  189. return trustStoreLocation;
  190. }
  191. public Optional<String> getKeyStorePassword() {
  192. return Optional.ofNullable(keyStorePassword);
  193. }
  194. public Optional<String> getTrustStorePassword() {
  195. return Optional.ofNullable(trustStorePassword);
  196. }
  197. public Path getHttpKeyStoreLocation() {
  198. return httpKeyStoreLocation;
  199. }
  200. public Optional<String> getHttpKeyStorePassword() {
  201. return Optional.ofNullable(httpKeyStorePassword);
  202. }
  203. public boolean isHttpEncryptionEnabled() {
  204. return httpEncryptionEnabled;
  205. }
  206. }