Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail fast and loud for invalid GELF messages #3972

Merged
merged 5 commits into from Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -124,6 +124,8 @@ public Message decode(@Nonnull final RawMessage rawMessage) {
throw new IllegalStateException("JSON is null/could not be parsed (invalid JSON)", e);
}

validateGELFMessage(node);

// Timestamp.
final double messageTimestamp = doubleValue(node, Message.FIELD_TIMESTAMP);
final DateTime timestamp;
Expand Down Expand Up @@ -218,6 +220,33 @@ public Message decode(@Nonnull final RawMessage rawMessage) {
return message;
}

private void validateGELFMessage(JsonNode jsonNode) {
final JsonNode hostNode = jsonNode.path("host");
if (hostNode.isMissingNode()) {
throw new IllegalArgumentException("GELF message is missing mandatory \"host\" field.");
}
if (!hostNode.isTextual()) {
throw new IllegalArgumentException("GELF message has invalid \"host\": " + hostNode.asText());
}
if (StringUtils.isBlank(hostNode.asText())) {
throw new IllegalArgumentException("GELF message has empty mandatory \"host\" field.");
}
final JsonNode shortMessageNode = jsonNode.path("short_message");
if (shortMessageNode.isMissingNode()) {
throw new IllegalArgumentException("GELF message is missing mandatory \"short_message\" field.");
}
if (!shortMessageNode.isTextual()) {
throw new IllegalArgumentException("GELF message has invalid \"short_message\": " + shortMessageNode.asText());
}
if (StringUtils.isBlank(shortMessageNode.asText())) {
throw new IllegalArgumentException("GELF message has empty mandatory \"short_message\" field.");
}
final JsonNode timestampNode = jsonNode.path("timestamp");
if (timestampNode.isValueNode() && !timestampNode.isNumber()) {
throw new IllegalArgumentException("GELF message has invalid \"timestamp\": " + timestampNode.asText());
}
}

@Nullable
@Override
public CodecAggregator getAggregator() {
Expand Down
Expand Up @@ -148,4 +148,138 @@ public void decodeLargeCompressedMessageFails() throws Exception {
public void getAggregatorReturnsGelfChunkAggregator() throws Exception {
assertThat(codec.getAggregator()).isSameAs(aggregator);
}

@Test
public void decodeFailsWithoutHost() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"short_message\": \"A short message that helps you identify what is going on\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message is missing mandatory \"host\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithWrongTypeForHost() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": 42,"
+ "\"short_message\": \"A short message that helps you identify what is going on\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has invalid \"host\": 42");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithEmptyHost() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"\","
+ "\"short_message\": \"A short message that helps you identify what is going on\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has empty mandatory \"host\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithBlankHost() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \" \","
+ "\"short_message\": \"A short message that helps you identify what is going on\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has empty mandatory \"host\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithoutShortMessage() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"example.org\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message is missing mandatory \"short_message\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithWrongTypeForShortMessage() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"example.org\","
+ "\"short_message\": 42"
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has invalid \"short_message\": 42");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithEmptyShortMessage() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"example.org\","
+ "\"short_message\": \"\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has empty mandatory \"short_message\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithBlankShortMessage() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"example.org\","
+ "\"short_message\": \" \""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has empty mandatory \"short_message\" field.");

codec.decode(rawMessage);
}

@Test
public void decodeFailsWithWrongTypeForTimestamp() throws Exception {
final String json = "{"
+ "\"version\": \"1.1\","
+ "\"host\": \"example.org\","
+ "\"short_message\": \"A short message that helps you identify what is going on\","
+ "\"timestamp\": \"Foobar\""
+ "}";

final RawMessage rawMessage = new RawMessage(json.getBytes(StandardCharsets.UTF_8));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("GELF message has invalid \"timestamp\": Foobar");

codec.decode(rawMessage);
}
}