Skip to content

Commit 52d8536

Browse files
committed
Remove validation that made keys starting or ending with . - or _ invalid, catch all exceptions in the parse json processor
Signed-off-by: Taylor Gray <tylgry@amazon.com>
1 parent 1dd8bd3 commit 52d8536

3 files changed

Lines changed: 31 additions & 40 deletions

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/JacksonEvent.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,9 @@ private String trimKey(final String key) {
373373
}
374374

375375
private boolean isValidKey(final String key) {
376-
char previous = ' ';
377-
char next = ' ';
378376
for (int i = 0; i < key.length(); i++) {
379377
char c = key.charAt(i);
380378

381-
if (i < key.length() - 1) {
382-
next = key.charAt(i + 1);
383-
}
384-
385-
if ((i == 0 || i == key.length() - 1 || previous == '/' || next == '/') && (c == '_' || c == '.' || c == '-')) {
386-
return false;
387-
}
388-
389379
if (!(c >= 48 && c <= 57
390380
|| c >= 65 && c <= 90
391381
|| c >= 97 && c <= 122
@@ -397,7 +387,6 @@ private boolean isValidKey(final String key) {
397387

398388
return false;
399389
}
400-
previous = c;
401390
}
402391
return true;
403392
}

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/JacksonEventTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,8 @@ public void testIsValueAList_withNull() {
323323
}
324324

325325
@ParameterizedTest
326-
@ValueSource(strings = {"", "withSpecialChars*$%", "-withPrefixDash", "\\-withEscapeChars", "\\\\/withMultipleEscapeChars",
327-
"withDashSuffix-", "withDashSuffix-/nestedKey", "withDashPrefix/-nestedKey", "_withUnderscorePrefix", "withUnderscoreSuffix_",
328-
".withDotPrefix", "withDotSuffix.", "with,Comma", "with:Colon", "with[Bracket", "with|Brace"})
326+
@ValueSource(strings = {"", "withSpecialChars*$%", "\\-withEscapeChars", "\\\\/withMultipleEscapeChars",
327+
"with,Comma", "with:Colon", "with[Bracket", "with|Brace"})
329328
void testKey_withInvalidKey_throwsIllegalArgumentException(final String invalidKey) {
330329
assertThrowsForKeyCheck(IllegalArgumentException.class, invalidKey);
331330
}

data-prepper-plugins/parse-json-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/parsejson/ParseJsonProcessor.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,37 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
6464
final boolean doUsePointer = Objects.nonNull(pointer);
6565

6666
for (final Record<Event> record : records) {
67-
final Event event = record.getData();
6867

69-
if (Objects.nonNull(parseWhen) && !expressionEvaluator.evaluateConditional(parseWhen, event)) {
70-
continue;
71-
}
72-
73-
final String message = event.get(source, String.class);
74-
if (Objects.isNull(message)) {
75-
continue;
76-
}
77-
78-
try {
79-
final TypeReference<HashMap<String, Object>> hashMapTypeReference = new TypeReference<HashMap<String, Object>>() {};
80-
Map<String, Object> parsedJson = objectMapper.readValue(message, hashMapTypeReference);
81-
82-
if (doUsePointer) {
83-
parsedJson = parseUsingPointer(event, parsedJson, pointer, doWriteToRoot);
84-
}
85-
86-
if (doWriteToRoot) {
87-
writeToRoot(event, parsedJson);
88-
} else {
89-
event.put(destination, parsedJson);
68+
final Event event = record.getData();
69+
try {
70+
if (Objects.nonNull(parseWhen) && !expressionEvaluator.evaluateConditional(parseWhen, event)) {
71+
continue;
72+
}
73+
74+
final String message = event.get(source, String.class);
75+
if (Objects.isNull(message)) {
76+
continue;
77+
}
78+
final TypeReference<HashMap<String, Object>> hashMapTypeReference = new TypeReference<HashMap<String, Object>>() {
79+
};
80+
Map<String, Object> parsedJson = objectMapper.readValue(message, hashMapTypeReference);
81+
82+
if (doUsePointer) {
83+
parsedJson = parseUsingPointer(event, parsedJson, pointer, doWriteToRoot);
84+
}
85+
86+
if (doWriteToRoot) {
87+
writeToRoot(event, parsedJson);
88+
} else {
89+
event.put(destination, parsedJson);
90+
}
91+
} catch (final JsonProcessingException jsonException) {
92+
event.getMetadata().addTags(tagsOnFailure);
93+
LOG.error(EVENT, "An exception occurred due to invalid JSON while reading event [{}]", event, jsonException);
94+
} catch (final Exception e) {
95+
event.getMetadata().addTags(tagsOnFailure);
96+
LOG.error(EVENT, "An exception occurred while using the parse_json processor on Event [{}]", event, e);
9097
}
91-
} catch (final JsonProcessingException jsonException) {
92-
event.getMetadata().addTags(tagsOnFailure);
93-
LOG.error(EVENT, "An exception occurred due to invalid JSON while reading event [{}]", event, jsonException);
94-
}
9598
}
9699
return records;
97100
}

0 commit comments

Comments
 (0)