Merge "Allow unicode in metric definitions"

This commit is contained in:
Jenkins 2015-04-23 14:11:07 +00:00 committed by Gerrit Code Review
commit 6b779a84b3
4 changed files with 9 additions and 8 deletions

View File

@ -34,7 +34,8 @@ public final class DimensionValidation {
private static final Map<String, DimensionValidator> VALIDATORS; private static final Map<String, DimensionValidator> VALIDATORS;
private static final Pattern UUID_PATTERN = Pattern private static final Pattern UUID_PATTERN = Pattern
.compile("\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}"); .compile("\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}");
private static final Pattern VALID_DIMENSION_NAME = Pattern.compile("^[a-zA-Z0-9_\\.\\-]+$"); private static final Pattern VALID_DIMENSION_NAME = Pattern.compile("[^><={}(), '\";&]+$");
private static final String INVALID_CHAR_STRING = "> < = { } ( ) ' \" , ; &";
private DimensionValidation() {} private DimensionValidation() {}
@ -135,7 +136,7 @@ public final class DimensionValidation {
} }
if (!VALID_DIMENSION_NAME.matcher(name).matches()) if (!VALID_DIMENSION_NAME.matcher(name).matches())
throw Exceptions.unprocessableEntity( throw Exceptions.unprocessableEntity(
"Dimension name %s may only contain: a-z A-Z 0-9 _ - .", name); "Dimension name %s may not contain: %s", name, INVALID_CHAR_STRING);
// Service specific validations // Service specific validations
if (service != null) { if (service != null) {
@ -173,7 +174,7 @@ public final class DimensionValidation {
} }
if (!VALID_DIMENSION_NAME.matcher(name).matches()) if (!VALID_DIMENSION_NAME.matcher(name).matches())
throw Exceptions.unprocessableEntity( throw Exceptions.unprocessableEntity(
"Dimension name '%s' may only contain: a-z A-Z 0-9 _ - .", name); "Dimension name '%s' may not contain: %s", name, INVALID_CHAR_STRING);
} }
} }
} }

View File

@ -28,7 +28,7 @@ import com.sun.jersey.spi.container.WebApplication;
* Utilities for validating metric names. * Utilities for validating metric names.
*/ */
public class MetricNameValidation { public class MetricNameValidation {
private static final Pattern VALID_METRIC_NAME = Pattern.compile("^[a-zA-Z0-9_\\.\\-]+$"); private static final Pattern VALID_METRIC_NAME = Pattern.compile("[^><={}(), '\";&]+$");
private MetricNameValidation() {} private MetricNameValidation() {}
@ -60,7 +60,7 @@ public class MetricNameValidation {
throw Exceptions.unprocessableEntity("Metric name %s must be %d characters or less", throw Exceptions.unprocessableEntity("Metric name %s must be %d characters or less",
metricName, CreateMetricCommand.MAX_NAME_LENGTH); metricName, CreateMetricCommand.MAX_NAME_LENGTH);
if (!Services.isReserved(metricName) && !VALID_METRIC_NAME.matcher(metricName).matches()) if (!Services.isReserved(metricName) && !VALID_METRIC_NAME.matcher(metricName).matches())
throw Exceptions.unprocessableEntity("Metric name %s may only contain: a-z A-Z 0-9 _ - .", throw Exceptions.unprocessableEntity("Metric name %s may not contain: > < = { } ( ) ' \" , ; &",
metricName); metricName);
// Service specific validations // Service specific validations

View File

@ -183,7 +183,7 @@ public class AlarmDefinitionResourceTest extends AbstractMonApiResourceTest {
public void shouldErrorOnCreateWithInvalidOperator() { public void shouldErrorOnCreateWithInvalidOperator() {
String expression = String expression =
"avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) & 90"; "avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) ^ 90";
ClientResponse response = ClientResponse response =
createResponseFor(new CreateAlarmDefinitionCommand("Disk Exceeds 1k Operations", null, createResponseFor(new CreateAlarmDefinitionCommand("Disk Exceeds 1k Operations", null,
expression, Arrays.asList("service", "instance_id"), "LOW", alarmActions, null, null)); expression, Arrays.asList("service", "instance_id"), "LOW", alarmActions, null, null));

View File

@ -160,11 +160,11 @@ public class MetricResourceTest extends AbstractMonApiResourceTest {
public void shouldErrorOnCreateWithIllegalCharsInName() { public void shouldErrorOnCreateWithIllegalCharsInName() {
ClientResponse response = ClientResponse response =
createResponseFor(new CreateMetricCommand("hpcs@.compute%", dimensions, timestamp, 22.0, createResponseFor(new CreateMetricCommand("hpcs{.compute%", dimensions, timestamp, 22.0,
valueMeta)); valueMeta));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422, ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"Metric name hpcs@.compute% may only contain: a-z A-Z 0-9 _ - ."); "Metric name hpcs{.compute% may not contain: > < = { } ( ) ' \" , ; &");
} }
public void shouldErrorOnCreateWithTooLongName() { public void shouldErrorOnCreateWithTooLongName() {