Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TomcatConnectors.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.server.app;
  21. import org.apache.catalina.connector.Connector;
  22. import org.apache.catalina.startup.Tomcat;
  23. import org.sonar.process.Props;
  24. import javax.annotation.Nullable;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.HashSet;
  29. import java.util.List;
  30. import java.util.Set;
  31. /**
  32. * Configuration of Tomcat connectors
  33. */
  34. class TomcatConnectors {
  35. public static final String PROP_HTTPS_CIPHERS = "sonar.web.https.ciphers";
  36. public static final int DISABLED_PORT = -1;
  37. public static final String HTTP_PROTOCOL = "HTTP/1.1";
  38. public static final String AJP_PROTOCOL = "AJP/1.3";
  39. private TomcatConnectors() {
  40. // only static stuff
  41. }
  42. static void configure(Tomcat tomcat, Props props) {
  43. List<Connector> connectors = new ArrayList<>();
  44. connectors.addAll(Arrays.asList(newHttpConnector(props), newAjpConnector(props), newHttpsConnector(props)));
  45. connectors.removeAll(Collections.singleton(null));
  46. verify(connectors);
  47. tomcat.setConnector(connectors.get(0));
  48. for (Connector connector : connectors) {
  49. tomcat.getService().addConnector(connector);
  50. }
  51. }
  52. private static void verify(List<Connector> connectors) {
  53. if (connectors.isEmpty()) {
  54. throw new IllegalStateException("HTTP connectors are disabled");
  55. }
  56. Set<Integer> ports = new HashSet<>();
  57. for (Connector connector : connectors) {
  58. int port = connector.getPort();
  59. if (ports.contains(port)) {
  60. throw new IllegalStateException(String.format("HTTP, AJP and HTTPS must not use the same port %d", port));
  61. }
  62. ports.add(port);
  63. }
  64. }
  65. @Nullable
  66. private static Connector newHttpConnector(Props props) {
  67. Connector connector = null;
  68. // Not named "sonar.web.http.port" to keep backward-compatibility
  69. int port = props.valueAsInt("sonar.web.port", 9000);
  70. if (port > DISABLED_PORT) {
  71. connector = newConnector(props, HTTP_PROTOCOL, "http");
  72. connector.setPort(port);
  73. }
  74. return connector;
  75. }
  76. @Nullable
  77. private static Connector newAjpConnector(Props props) {
  78. Connector connector = null;
  79. int port = props.valueAsInt("sonar.ajp.port", DISABLED_PORT);
  80. if (port > DISABLED_PORT) {
  81. connector = newConnector(props, AJP_PROTOCOL, "http");
  82. connector.setPort(port);
  83. }
  84. return connector;
  85. }
  86. @Nullable
  87. private static Connector newHttpsConnector(Props props) {
  88. Connector connector = null;
  89. int port = props.valueAsInt("sonar.web.https.port", DISABLED_PORT);
  90. if (port > DISABLED_PORT) {
  91. connector = newConnector(props, HTTP_PROTOCOL, "https");
  92. connector.setPort(port);
  93. connector.setSecure(true);
  94. connector.setScheme("https");
  95. setConnectorAttribute(connector, "keyAlias", props.value("sonar.web.https.keyAlias"));
  96. String keyPassword = props.value("sonar.web.https.keyPass", "changeit");
  97. setConnectorAttribute(connector, "keyPass", keyPassword);
  98. setConnectorAttribute(connector, "keystorePass", props.value("sonar.web.https.keystorePass", keyPassword));
  99. setConnectorAttribute(connector, "keystoreFile", props.value("sonar.web.https.keystoreFile"));
  100. setConnectorAttribute(connector, "keystoreType", props.value("sonar.web.https.keystoreType", "JKS"));
  101. setConnectorAttribute(connector, "keystoreProvider", props.value("sonar.web.https.keystoreProvider"));
  102. setConnectorAttribute(connector, "truststorePass", props.value("sonar.web.https.truststorePass", "changeit"));
  103. setConnectorAttribute(connector, "truststoreFile", props.value("sonar.web.https.truststoreFile"));
  104. setConnectorAttribute(connector, "truststoreType", props.value("sonar.web.https.truststoreType", "JKS"));
  105. setConnectorAttribute(connector, "truststoreProvider", props.value("sonar.web.https.truststoreProvider"));
  106. setConnectorAttribute(connector, "clientAuth", props.value("sonar.web.https.clientAuth", "false"));
  107. setConnectorAttribute(connector, "ciphers", props.value(PROP_HTTPS_CIPHERS));
  108. // SSLv3 must not be enable because of Poodle vulnerability
  109. // See https://jira.sonarsource.com/browse/SONAR-5860
  110. setConnectorAttribute(connector, "sslEnabledProtocols", "TLSv1,TLSv1.1,TLSv1.2");
  111. setConnectorAttribute(connector, "sslProtocol", "TLS");
  112. setConnectorAttribute(connector, "SSLEnabled", true);
  113. }
  114. return connector;
  115. }
  116. private static Connector newConnector(Props props, String protocol, String scheme) {
  117. Connector connector = new Connector(protocol);
  118. connector.setURIEncoding("UTF-8");
  119. connector.setProperty("address", props.value("sonar.web.host", "0.0.0.0"));
  120. connector.setProperty("socket.soReuseAddress", "true");
  121. configurePool(props, connector, scheme);
  122. configureCompression(connector);
  123. return connector;
  124. }
  125. private static void configurePool(Props props, Connector connector, String scheme) {
  126. connector.setProperty("acceptorThreadCount", String.valueOf(2));
  127. connector.setProperty("minSpareThreads", String.valueOf(props.valueAsInt("sonar.web." + scheme + ".minThreads", 5)));
  128. connector.setProperty("maxThreads", String.valueOf(props.valueAsInt("sonar.web." + scheme + ".maxThreads", 50)));
  129. connector.setProperty("acceptCount", String.valueOf(props.valueAsInt("sonar.web." + scheme + ".acceptCount", 25)));
  130. }
  131. private static void configureCompression(Connector connector) {
  132. connector.setProperty("compression", "on");
  133. connector.setProperty("compressionMinSize", "1024");
  134. connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,text/css,application/json,application/javascript");
  135. }
  136. private static void setConnectorAttribute(Connector c, String key, @Nullable Object value) {
  137. if (value != null) {
  138. c.setAttribute(key, value);
  139. }
  140. }
  141. }