Skip to content

Fix potential NullPointerException for Kafka headers with null value #414

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

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/com/splunk/kafka/connect/SplunkSinkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public String headerId(SinkRecord sinkRecord) {

StringBuilder headerString = new StringBuilder();

if(indexHeader != null) {
if(indexHeader != null && indexHeader.value() != null) {
headerString.append(indexHeader.value().toString());
} else {
if(metas != null) {
Expand All @@ -232,7 +232,7 @@ public String headerId(SinkRecord sinkRecord) {

headerString.append(insertHeaderToken());

if(hostHeader != null) {
if(hostHeader != null && hostHeader.value() != null) {
headerString.append(hostHeader.value().toString());
} else {
if(metas != null) {
Expand All @@ -242,7 +242,7 @@ public String headerId(SinkRecord sinkRecord) {

headerString.append(insertHeaderToken());

if(sourceHeader != null) {
if(sourceHeader != null && sourceHeader.value() != null) {
headerString.append(sourceHeader.value().toString());
} else {
if(metas != null) {
Expand All @@ -252,7 +252,7 @@ public String headerId(SinkRecord sinkRecord) {

headerString.append(insertHeaderToken());

if(sourcetypeHeader != null) {
if(sourcetypeHeader != null && sourcetypeHeader.value() != null) {
headerString.append(sourcetypeHeader.value().toString());
} else {
if(metas != null) {
Expand Down Expand Up @@ -450,16 +450,16 @@ private Event addHeaders(Event event, SinkRecord record) {
Header headerSource = headers.lastWithName(connectorConfig.headerSource);
Header headerSourcetype = headers.lastWithName(connectorConfig.headerSourcetype);

if (headerIndex != null) {
if (headerIndex != null && headerIndex.value() != null) {
event.setIndex(headerIndex.value().toString());
}
if (headerHost != null) {
if (headerHost != null && headerHost.value() != null) {
event.setHost(headerHost.value().toString());
}
if (headerSource != null) {
if (headerSource != null && headerSource.value() != null) {
event.setSource(headerSource.value().toString());
}
if (headerSourcetype != null) {
if (headerSourcetype != null && headerSourcetype.value() != null) {
event.setSourcetype(headerSourcetype.value().toString());
}

Expand Down
29 changes: 26 additions & 3 deletions src/test/java/com/splunk/kafka/connect/SplunkSinkTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@

import com.splunk.hecclient.Event;
import com.splunk.hecclient.EventBatch;
import com.splunk.hecclient.JsonEvent;
import com.splunk.hecclient.RawEventBatch;
import org.apache.commons.logging.Log;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.record.TimestampType;
import org.apache.kafka.connect.errors.RetriableException;
import org.apache.kafka.connect.sink.SinkRecord;
import org.junit.Assert;
import org.junit.Test;

import java.text.ParseException;
import java.util.*;

public class SplunkSinkTaskTest {
Expand Down Expand Up @@ -242,6 +240,25 @@ public void putWithNullEvent() {
task.stop();
}

@Test
public void putWithNullIndexHeaderValue() {
UnitUtil uu = new UnitUtil(0);
Map<String, String> config = uu.createTaskConfig();
config.put(SplunkSinkConnectorConfig.RAW_CONF, String.valueOf(true));
config.put(SplunkSinkConnectorConfig.ACK_CONF, String.valueOf(true));
config.put(SplunkSinkConnectorConfig.MAX_BATCH_SIZE_CONF, String.valueOf(1));
config.put(SplunkSinkConnectorConfig.HEADER_SUPPORT_CONF, String.valueOf("true"));
config.put(SplunkSinkConnectorConfig.HEADER_INDEX_CONF, "index");
SplunkSinkTask task = new SplunkSinkTask();
HecMock hec = new HecMock(task);
hec.setSendReturnResult(HecMock.success);
task.setHec(hec);
task.start(config);
task.put(createSinkRecordWithNullIndexHeaderValue());
Assert.assertEquals(1, hec.getBatches().size());
task.stop();
}

@Test
public void putWithRawAndAck() {
putWithSuccess(true, true);
Expand Down Expand Up @@ -455,6 +472,12 @@ private Collection<SinkRecord> createNullSinkRecord() {
return records;
}

private Collection<SinkRecord> createSinkRecordWithNullIndexHeaderValue() {
List<SinkRecord> records = new ArrayList<>(createSinkRecords(1));
records.get(0).headers().add("index", null, null);
return records;
}

private List<TopicPartition> createTopicPartitionList() {
ArrayList<TopicPartition> tps = new ArrayList<>();
tps.add(new TopicPartition("mytopic", 1));
Expand Down