From 551a9dac50cae9bf4bb5e171395296ef015d134f Mon Sep 17 00:00:00 2001 From: Strongest Number 9 <16169054+StrongestNumber9@users.noreply.github.com> Date: Tue, 25 Jul 2023 12:06:21 +0300 Subject: [PATCH] Fixes to issues reported by coverity (#63) * Should fix situations where instant from timestamp parsing is somehow null * Check for appConfig to be null before continuing, validate in own block * Close statefulFileReader * Dnf clean all instead of yum clean all on Dockerfile * Adds explicit charset for log reading * Adds more explicit charsets * Adds defaultCharset to withMsg part * Use StandardCharsets.UTF_8 instead of Charset.defaultCharset() * Fix Exception handling message --- Dockerfile | 2 +- .../java/com/teragrep/k8s_01/K8SConsumer.java | 26 +++++++++++++++---- .../teragrep/k8s_01/KubernetesLogReader.java | 17 ++++++++---- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index e49e880..407d494 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM rockylinux:8 COPY rpm/target/rpm/com.teragrep-k8s_01/RPMS/noarch/com.teragrep-k8s_01-*.rpm /rpm/ -RUN dnf -y install jq /rpm/*.rpm && yum clean all +RUN dnf -y install jq /rpm/*.rpm && dnf clean all VOLUME /opt/teragrep/k8s_01/var VOLUME /opt/teragrep/k8s_01/etc WORKDIR /opt/teragrep/k8s_01 diff --git a/src/main/java/com/teragrep/k8s_01/K8SConsumer.java b/src/main/java/com/teragrep/k8s_01/K8SConsumer.java index 19a6b44..1258ea0 100644 --- a/src/main/java/com/teragrep/k8s_01/K8SConsumer.java +++ b/src/main/java/com/teragrep/k8s_01/K8SConsumer.java @@ -32,6 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -95,12 +96,12 @@ public void accept(FileRecord record) { KubernetesLogFilePOJO log; try { // We want to read the kubernetes log event into a POJO - log = gson.fromJson(new String(record.getRecord()), KubernetesLogFilePOJO.class); + log = gson.fromJson(new String(record.getRecord(), StandardCharsets.UTF_8), KubernetesLogFilePOJO.class); } catch (JsonParseException e) { LOGGER.trace( "[{}] Invalid syntax message: {}", uuid, - new String(record.getRecord()) + new String(record.getRecord(), StandardCharsets.UTF_8) ); throw new RuntimeException( String.format( @@ -122,7 +123,7 @@ public void accept(FileRecord record) { LOGGER.debug( "[{}] Can't parse this properly: {}", uuid, - new String(record.getRecord()) + new String(record.getRecord(), StandardCharsets.UTF_8) ); throw new RuntimeException( String.format( @@ -144,7 +145,7 @@ public void accept(FileRecord record) { catch(DateTimeParseException e) { throw new RuntimeException( String.format( - "[%s] Can't parse timestamp <%s> properly for event from pod <%s/%s> on container <%s> in file %s/%s at offset %s: ", + "[%s] Can't parse timestamp <%s> properly for event from pod <[%s]/[%s]> on container <%s> in file %s/%s at offset %s: ", uuid, log.getTimestamp(), namespace, @@ -157,6 +158,21 @@ public void accept(FileRecord record) { e ); } + if(instant == null) { + throw new RuntimeException( + String.format( + "[%s] Unknown failure while parsing timestamp <%s> for event from pod <[%s]/[%s]> on container <%s> in file %s/%s at offset %s", + uuid, + log.getTimestamp(), + namespace, + podname, + containerId, + record.getPath(), + record.getFilename(), + record.getStartOffset() + ) + ); + } ZonedDateTime zdt = instant.atZone(timezoneId); String timestamp = zdt.format(format); @@ -287,7 +303,7 @@ public void accept(FileRecord record) { .withAppName(appName) .withFacility(Facility.USER) .withSDElement(SDMetadata) - .withMsg(new String(record.getRecord())); + .withMsg(new String(record.getRecord(), StandardCharsets.UTF_8)); try { RelpOutput output = relpOutputPool.take(); output.send(syslog); diff --git a/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java b/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java index 5e10334..667cdf8 100644 --- a/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java +++ b/src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java @@ -42,7 +42,6 @@ public static void main(String[] args) throws IOException { AppConfig appConfig; try { appConfig = gson.fromJson(new FileReader("etc/config.json"), AppConfig.class); - appConfig.validate(); } catch (FileNotFoundException e) { LOGGER.error( @@ -58,16 +57,23 @@ public static void main(String[] args) throws IOException { ); return; } - catch (InvalidConfigurationException e) { + catch (Exception e) { LOGGER.error( - "Failed to validate config 'etc/config.json':", + "Caught exception while handling config:", e ); return; } - catch (Exception e) { + if (appConfig == null) { + LOGGER.error("Unknown parsing failure happened with config 'etc/config.json', can't continue. Check if the configuration file is empty?"); + return; + } + try { + appConfig.validate(); + } + catch (InvalidConfigurationException e) { LOGGER.error( - "Unknown exception while handling config:", + "Failed to validate config 'etc/config.json':", e ); return; @@ -184,5 +190,6 @@ public static void main(String[] args) throws IOException { } } prometheusMetrics.close(); + statefulFileReader.close(); } }