Skip to content

Commit

Permalink
Fixes to issues reported by coverity (#63)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
StrongestNumber9 authored Jul 25, 2023
1 parent f2a2f20 commit 551a9da
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/com/teragrep/k8s_01/K8SConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -184,5 +190,6 @@ public static void main(String[] args) throws IOException {
}
}
prometheusMetrics.close();
statefulFileReader.close();
}
}

0 comments on commit 551a9da

Please sign in to comment.