diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-03-29 08:57:42 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-04-11 20:48:58 +0200 |
commit | 0c55471ff857cbe6c6a12c73e6e5a7514ab7ba7b (patch) | |
tree | b0e1d4a054449b4fa8edb9146911f7df35401db6 /server/sonar-process-monitor | |
parent | 6782a76dcf2e42fea32116a22d1c3ecb8d2975fa (diff) | |
download | sonarqube-0c55471ff857cbe6c6a12c73e6e5a7514ab7ba7b.tar.gz sonarqube-0c55471ff857cbe6c6a12c73e6e5a7514ab7ba7b.zip |
SONAR-9082 H2 should bind to localhost interface
Diffstat (limited to 'server/sonar-process-monitor')
2 files changed, 16 insertions, 5 deletions
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/config/JdbcSettings.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/config/JdbcSettings.java index 52f772958fc..9011f167c8e 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/application/config/JdbcSettings.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/config/JdbcSettings.java @@ -20,6 +20,8 @@ package org.sonar.application.config; import java.io.File; +import java.net.Inet6Address; +import java.net.InetAddress; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -33,6 +35,7 @@ import org.sonar.process.MessageException; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; +import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.isEmpty; import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.sonar.process.ProcessProperties.JDBC_EMBEDDED_PORT; @@ -99,18 +102,25 @@ public class JdbcSettings implements Consumer<Props> { Pattern pattern = Pattern.compile("jdbc:(\\w+):.+"); Matcher matcher = pattern.matcher(url); if (!matcher.find()) { - throw new MessageException(String.format("Bad format of JDBC URL: %s", url)); + throw new MessageException(format("Bad format of JDBC URL: %s", url)); } String key = matcher.group(1); try { return Provider.valueOf(StringUtils.upperCase(key)); } catch (IllegalArgumentException e) { - throw new MessageException(String.format("Unsupported JDBC driver provider: %s", key)); + throw new MessageException(format("Unsupported JDBC driver provider: %s", key)); } } private static String buildH2JdbcUrl(int embeddedDatabasePort) { - return "jdbc:h2:tcp://localhost:" + embeddedDatabasePort + "/sonar"; + InetAddress ip = InetAddress.getLoopbackAddress(); + String host; + if (ip instanceof Inet6Address) { + host = "[" + ip.getHostAddress() + "]"; + } else { + host = ip.getHostAddress(); + } + return format("jdbc:h2:tcp://%s:%d/sonar", host, embeddedDatabasePort); } void checkUrlParameters(Provider provider, String url) { @@ -145,7 +155,7 @@ public class JdbcSettings implements Consumer<Props> { private static void checkRequiredParameter(String url, String val) { if (!url.contains(val)) { - throw new MessageException(String.format("JDBC URL must have the property '%s'", val)); + throw new MessageException(format("JDBC URL must have the property '%s'", val)); } } diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/config/JdbcSettingsTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/config/JdbcSettingsTest.java index 45510230779..5b4eb945364 100644 --- a/server/sonar-process-monitor/src/test/java/org/sonar/application/config/JdbcSettingsTest.java +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/config/JdbcSettingsTest.java @@ -20,6 +20,7 @@ package org.sonar.application.config; import java.io.File; +import java.net.InetAddress; import java.util.Properties; import org.apache.commons.io.FileUtils; import org.junit.Before; @@ -56,7 +57,7 @@ public class JdbcSettingsTest { assertThat(underTest.resolveProviderAndEnforceNonnullJdbcUrl(props)) .isEqualTo(Provider.H2); - assertThat(props.nonNullValue(JDBC_URL)).isEqualTo("jdbc:h2:tcp://localhost:9092/sonar"); + assertThat(props.nonNullValue(JDBC_URL)).isEqualTo(String.format("jdbc:h2:tcp://%s:9092/sonar", InetAddress.getLoopbackAddress().getHostAddress())); } @Test |