summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties3
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java15
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/server_id_configuration_controller.rb30
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/server_id_configuration/index.html.erb4
-rw-r--r--sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java27
5 files changed, 64 insertions, 15 deletions
diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
index b3417a36a20..88eb667f1b7 100644
--- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
+++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
@@ -1487,9 +1487,12 @@ server_id_configuration.bad_key=The ID is not valid anymore. Please check the or
server_id_configuration.information=The Server ID is a unique identifier of this Sonar instance. It is used for example to obtain a license key for the SonarSource's commercial plugins. Two fields have to be provided to generate the ID : organisation name and one of the IP addresses of the machine that hosts this server.
server_id_configuration.organisation.title=Organisation
server_id_configuration.organisation.desc=Name of the organisation
+server_id_configuration.organisation.pattern=Only letters, digits and whitespaces are allowed.
server_id_configuration.ip.title=Fixed IP Address
server_id_configuration.ip.desc=A server ID is linked to the IP address of the hosting machine that runs Sonar. If the server IP address was to change, the server ID will have to be regenerated. The valid addresses are :
server_id_configuration.generation_error=Organisation and/or IP address are not valid.
+server_id_configuration.fields_cannot_be_blank=Organisation and IP address cannot be blank.
+server_id_configuration.does_not_match_organisation_pattern=Organisation does not match the required pattern.
#------------------------------------------------------------------------------
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java
index 58841291d68..3dc6d958f5c 100644
--- a/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java
+++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java
@@ -31,12 +31,15 @@ import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;
+import java.util.regex.Pattern;
/**
* @since 2.11
*/
public class ServerIdGenerator {
+ private static final Pattern ORGANIZATION_PATTERN = Pattern.compile("[a-zA-Z0-9]+[a-zA-Z0-9 ]*");
+
/**
* Increment this version each time the algorithm is changed. Do not exceed 9.
*/
@@ -54,10 +57,12 @@ public class ServerIdGenerator {
this.acceptPrivateAddress = acceptPrivateAddress;
}
- public String generate(String organisation, String ipAddress) {
+ public String generate(String organisationName, String ipAddress) {
String id = null;
- if (StringUtils.isNotBlank(organisation) && StringUtils.isNotBlank(ipAddress)) {
- InetAddress inetAddress = toValidAddress(ipAddress);
+ String organisation = organisationName.trim();
+ String ip = ipAddress.trim();
+ if (StringUtils.isNotBlank(organisation) && StringUtils.isNotBlank(ip) && isValidOrganizationName(organisation)) {
+ InetAddress inetAddress = toValidAddress(ip);
if (inetAddress != null) {
id = toId(organisation, inetAddress);
}
@@ -65,6 +70,10 @@ public class ServerIdGenerator {
return id;
}
+ boolean isValidOrganizationName(String organisation) {
+ return ORGANIZATION_PATTERN.matcher(organisation).matches();
+ }
+
boolean isFixed(InetAddress address) {
// Loopback addresses are in the range 127/8.
// Link local addresses are in the range 169.254/16 (IPv4) or fe80::/10 (IPv6). They are "autoconfiguration" addresses.
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/server_id_configuration_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/server_id_configuration_controller.rb
index 9ba42a3839d..e3abec7ed2c 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/server_id_configuration_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/server_id_configuration_controller.rb
@@ -29,10 +29,11 @@ class ServerIdConfigurationController < ApplicationController
def index
@server_id = Property.value(PROPERTY_SERVER_ID)
- @organisation = Property.value(PROPERTY_ORGANISATION)
- @address = Property.value(PROPERTY_IP_ADDRESS)
+ @organisation = Property.value(PROPERTY_ORGANISATION) || ''
+ @address = Property.value(PROPERTY_IP_ADDRESS) || ''
@valid_addresses = java_facade.getValidInetAddressesForServerId()
@bad_id = false
+
if @server_id.present?
id = java_facade.generateServerId(@organisation, @address)
@bad_id = (@server_id != id)
@@ -41,20 +42,27 @@ class ServerIdConfigurationController < ApplicationController
end
def generate
- organisation = params[:organisation]
+ organisation = params[:organisation].strip
Property.set(PROPERTY_ORGANISATION, organisation)
-
- ip_address=params[:address]
+ ip_address=params[:address].strip
Property.set(PROPERTY_IP_ADDRESS, ip_address)
- id = java_facade.generateServerId(organisation, ip_address)
- if id
- Property.set(PROPERTY_SERVER_ID, id)
+ if organisation.blank? || ip_address.blank?
+ flash[:error] = Api::Utils.message('server_id_configuration.fields_cannot_be_blank')
+ elsif !(organisation =~ /^[a-zA-Z0-9]+[a-zA-Z0-9 ]*$/)
+ flash[:error] = Api::Utils.message('server_id_configuration.does_not_match_organisation_pattern')
else
- Property.clear(PROPERTY_SERVER_ID)
- flash[:error] = Api::Utils.message('server_id_configuration.generation_error')
+ id = java_facade.generateServerId(organisation, ip_address)
+ if id
+ # Success!
+ Property.set(PROPERTY_SERVER_ID, id)
+ else
+ # Something unexpected happened during the generation
+ Property.clear(PROPERTY_SERVER_ID)
+ flash[:error] = Api::Utils.message('server_id_configuration.generation_error')
+ end
end
-
+
redirect_to :action => 'index'
end
end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/server_id_configuration/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/server_id_configuration/index.html.erb
index 6b751f7bd57..ee7269441b7 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/server_id_configuration/index.html.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/server_id_configuration/index.html.erb
@@ -19,7 +19,7 @@
<% if @server_id %>
<big><b><span class="<%= @bad_id ? 'error' : 'notice' -%>" id="server_id"><%= @server_id -%></span></b></big>
<% if @bad_id %>
- <span class="error"><%= message('server_id_configuration.bad_id') -%></span>
+ <span class="error"><%= message('server_id_configuration.bad_key') -%></span>
<% end %>
<br/>
<% end %>
@@ -31,6 +31,8 @@
<p class="marginbottom10"><%= message('server_id_configuration.organisation.desc') -%></p>
<input type="text" name="organisation" value="<%= @organisation -%>" size="50"/>
+ <br/>
+ <span class="note"><%= message('server_id_configuration.organisation.pattern') -%></span>
</td>
</tr>
<tr class="even">
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java
index 57a84a7396d..874ce40c895 100644
--- a/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java
@@ -27,6 +27,7 @@ import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import static org.fest.assertions.Assertions.assertThat;
import static org.hamcrest.text.StringStartsWith.startsWith;
import static org.junit.Assert.assertThat;
@@ -40,6 +41,32 @@ public class ServerIdGeneratorTest {
}
@Test
+ public void shouldNotGenerateIdIfBlankParams() {
+ ServerIdGenerator generator = new ServerIdGenerator(true);
+ assertThat(generator.generate(" ", "127.0.0.1")).isNull();
+ assertThat(generator.generate("SonarSource", " ")).isNull();
+ }
+
+ @Test
+ public void organizationShouldRespectPattern() {
+ ServerIdGenerator generator = new ServerIdGenerator(true);
+ assertThat(generator.generate("SonarSource", "127.0.0.1")).isNotNull();
+ assertThat(generator.generate("SonarSource$", "127.0.0.1")).isNull();
+ }
+
+ @Test
+ public void checkValidOrganizationName() {
+ ServerIdGenerator generator = new ServerIdGenerator();
+ assertThat(generator.isValidOrganizationName("Sonar Source")).isTrue();
+ assertThat(generator.isValidOrganizationName("Sonar Source 5")).isTrue();
+ assertThat(generator.isValidOrganizationName("Sonar Source $")).isFalse();
+ assertThat(generator.isValidOrganizationName("Sonar Source Héhé")).isFalse();
+ assertThat(generator.isValidOrganizationName("Sonar Source \n")).isFalse();
+ assertThat(generator.isValidOrganizationName(" ")).isFalse();
+ assertThat(generator.isValidOrganizationName("\tBar ")).isFalse();
+ }
+
+ @Test
public void idShouldHaveTenCharacters() {
String id = new ServerIdGenerator().toId("SonarSource", localhost);
assertThat(id.length(), Is.is(15)); // first character is version + 14 characters for checksum