From 9640f9649a85dac9efd19b151c521e48aeddba90 Mon Sep 17 00:00:00 2001 From: Parag Jain Date: Tue, 10 Dec 2019 01:32:11 +0530 Subject: [PATCH] fix npe while logging sql/query request (#9001) * fix npe while logging sql/query request * forbid forbidden DateTime API --- .../apache/druid/server/RequestLogLine.java | 5 +- .../druid/server/RequestLogLineTest.java | 91 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 server/src/test/java/org/apache/druid/server/RequestLogLineTest.java diff --git a/server/src/main/java/org/apache/druid/server/RequestLogLine.java b/server/src/main/java/org/apache/druid/server/RequestLogLine.java index 4a8451ffffe..1aba4d317fe 100644 --- a/server/src/main/java/org/apache/druid/server/RequestLogLine.java +++ b/server/src/main/java/org/apache/druid/server/RequestLogLine.java @@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; +import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.query.Query; import org.joda.time.DateTime; @@ -55,9 +56,9 @@ public class RequestLogLine { this.query = query; this.sql = sql; - this.sqlQueryContext = sqlQueryContext; + this.sqlQueryContext = sqlQueryContext != null ? sqlQueryContext : ImmutableMap.of(); this.timestamp = Preconditions.checkNotNull(timestamp, "timestamp"); - this.remoteAddr = remoteAddr; + this.remoteAddr = StringUtils.nullToEmptyNonDruidDataString(remoteAddr); this.queryStats = Preconditions.checkNotNull(queryStats, "queryStats"); } diff --git a/server/src/test/java/org/apache/druid/server/RequestLogLineTest.java b/server/src/test/java/org/apache/druid/server/RequestLogLineTest.java new file mode 100644 index 00000000000..6051310e161 --- /dev/null +++ b/server/src/test/java/org/apache/druid/server/RequestLogLineTest.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.server; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.query.Query; +import org.apache.druid.query.TableDataSource; +import org.apache.druid.query.timeboundary.TimeBoundaryQuery; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class RequestLogLineTest +{ + private Query query; + + @Before + public void setUp() throws Exception + { + query = new TimeBoundaryQuery( + new TableDataSource("test"), + null, + null, + null, + null + ); + } + + @Test(expected = NullPointerException.class) + public void nullTimestamp() throws JsonProcessingException + { + RequestLogLine requestLogLine = RequestLogLine.forNative( + query, + null, + "", + new QueryStats(ImmutableMap.of()) + ); + } + + @Test(expected = NullPointerException.class) + public void nullQueryStats() throws JsonProcessingException + { + RequestLogLine requestLogLine = RequestLogLine.forNative( + query, + DateTimes.nowUtc(), + "", + null + ); + } + + @Test + public void nullRemoteAddressAndNullSqlQueryContext() throws JsonProcessingException + { + RequestLogLine requestLogLine = RequestLogLine.forNative( + query, + DateTimes.nowUtc(), + null, + new QueryStats(ImmutableMap.of()) + ); + Assert.assertEquals("", requestLogLine.getRemoteAddr()); + requestLogLine.getNativeQueryLine(new ObjectMapper()); // call should not throw exception + + requestLogLine = RequestLogLine.forSql( + "", null, DateTimes.nowUtc(), null, new QueryStats(ImmutableMap.of()) + ); + Assert.assertEquals("", requestLogLine.getRemoteAddr()); + Assert.assertEquals(ImmutableMap.of(), requestLogLine.getSqlQueryContext()); + requestLogLine.getSqlQueryLine(new ObjectMapper()); // call should not throw exception + } + +}