Skip to content

CC-26777: Cherry pick fix for potential NullPointerException for Kafka headers with null value. #57

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
Mar 28, 2025
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 @@ -212,7 +212,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 @@ -222,7 +222,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 @@ -232,7 +232,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 @@ -242,7 +242,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 @@ -438,16 +438,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
25 changes: 25 additions & 0 deletions src/test/java/com/splunk/kafka/connect/SplunkSinkTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,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 @@ -341,6 +360,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