public ManagedProcess launch(AbstractCommand command) {
    EsInstallation esInstallation = command.getEsInstallation();
    if (esInstallation != null) {
      cleanupOutdatedEsData(esInstallation);
      writeConfFiles(esInstallation);
    }

    Process process;
    if (command instanceof JavaCommand<?> javaCommand) {
      process = launchJava(javaCommand);
    } else {
      throw new IllegalStateException("Unexpected type of command: " + command.getClass());
    }

    ProcessId processId = command.getProcessId();
    try {
      if (processId == ProcessId.ELASTICSEARCH) {
        checkArgument(esInstallation != null, "Incorrect configuration EsInstallation is null");
        EsConnectorImpl esConnector = new EsConnectorImpl(singleton(HostAndPort.fromParts(esInstallation.getHost(),
          esInstallation.getHttpPort())), esInstallation.getBootstrapPassword(), esInstallation.getHttpKeyStoreLocation(),
          esInstallation.getHttpKeyStorePassword().orElse(null));
        return new EsManagedProcess(process, processId, esConnector);
      } else {
        ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex());
        return new ProcessCommandsManagedProcess(process, processId, commands);
      }
    } catch (Exception e) {
      // just in case
      if (process != null) {
        process.destroyForcibly();
      }
      throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getHumanReadableName()), e);
    }
  }
@Test
  public void enabling_es_security_should_execute_keystore_cli_if_truststore_and_keystore_provided() throws Exception {
    File tempDir = temp.newFolder();
    File truststoreFile = temp.newFile("truststore.pk12");
    File keystoreFile = temp.newFile("keystore.pk12");
    TestProcessBuilder processBuilder = new TestProcessBuilder();
    ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder);

    EsInstallation esInstallation = createEsInstallation(new Props(new Properties())
      .set("sonar.cluster.enabled", "true")
      .set("sonar.cluster.search.password", "bootstrap-password")
      .set("sonar.cluster.es.ssl.keystore", keystoreFile.getAbsolutePath())
      .set("sonar.cluster.es.ssl.keystorePassword", "keystore-password")
      .set("sonar.cluster.es.ssl.truststore", truststoreFile.getAbsolutePath())
      .set("sonar.cluster.es.ssl.truststorePassword", "truststore-password"));

    JavaCommand<JvmOptions> command = new JavaCommand<>(ProcessId.ELASTICSEARCH, temp.newFolder());
    command.addClasspath("lib/*.class");
    command.addClasspath("lib/*.jar");
    command.setArgument("foo", "bar");
    command.setClassName("org.sonarqube.Main");
    command.setEnvVariable("VAR1", "valueOfVar1");
    command.setJvmOptions(new JvmOptions<>()
      .add("-Dfoo=bar")
      .add("-Dfoo2=bar2"));
    command.setEsInstallation(esInstallation);

    ManagedProcess monitor = underTest.launch(command);
    assertThat(monitor).isNotNull();
    assertThat(Paths.get(esInstallation.getConfDirectory().getAbsolutePath(), "truststore.pk12")).exists();
    assertThat(Paths.get(esInstallation.getConfDirectory().getAbsolutePath(), "keystore.pk12")).exists();
  }