Skip to content

Commit 551a9da

Browse files
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
1 parent f2a2f20 commit 551a9da

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM rockylinux:8
22
COPY rpm/target/rpm/com.teragrep-k8s_01/RPMS/noarch/com.teragrep-k8s_01-*.rpm /rpm/
3-
RUN dnf -y install jq /rpm/*.rpm && yum clean all
3+
RUN dnf -y install jq /rpm/*.rpm && dnf clean all
44
VOLUME /opt/teragrep/k8s_01/var
55
VOLUME /opt/teragrep/k8s_01/etc
66
WORKDIR /opt/teragrep/k8s_01

src/main/java/com/teragrep/k8s_01/K8SConsumer.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434

35+
import java.nio.charset.StandardCharsets;
3536
import java.time.Instant;
3637
import java.time.ZoneId;
3738
import java.time.ZonedDateTime;
@@ -95,12 +96,12 @@ public void accept(FileRecord record) {
9596
KubernetesLogFilePOJO log;
9697
try {
9798
// We want to read the kubernetes log event into a POJO
98-
log = gson.fromJson(new String(record.getRecord()), KubernetesLogFilePOJO.class);
99+
log = gson.fromJson(new String(record.getRecord(), StandardCharsets.UTF_8), KubernetesLogFilePOJO.class);
99100
} catch (JsonParseException e) {
100101
LOGGER.trace(
101102
"[{}] Invalid syntax message: {}",
102103
uuid,
103-
new String(record.getRecord())
104+
new String(record.getRecord(), StandardCharsets.UTF_8)
104105
);
105106
throw new RuntimeException(
106107
String.format(
@@ -122,7 +123,7 @@ public void accept(FileRecord record) {
122123
LOGGER.debug(
123124
"[{}] Can't parse this properly: {}",
124125
uuid,
125-
new String(record.getRecord())
126+
new String(record.getRecord(), StandardCharsets.UTF_8)
126127
);
127128
throw new RuntimeException(
128129
String.format(
@@ -144,7 +145,7 @@ public void accept(FileRecord record) {
144145
catch(DateTimeParseException e) {
145146
throw new RuntimeException(
146147
String.format(
147-
"[%s] Can't parse timestamp <%s> properly for event from pod <%s/%s> on container <%s> in file %s/%s at offset %s: ",
148+
"[%s] Can't parse timestamp <%s> properly for event from pod <[%s]/[%s]> on container <%s> in file %s/%s at offset %s: ",
148149
uuid,
149150
log.getTimestamp(),
150151
namespace,
@@ -157,6 +158,21 @@ public void accept(FileRecord record) {
157158
e
158159
);
159160
}
161+
if(instant == null) {
162+
throw new RuntimeException(
163+
String.format(
164+
"[%s] Unknown failure while parsing timestamp <%s> for event from pod <[%s]/[%s]> on container <%s> in file %s/%s at offset %s",
165+
uuid,
166+
log.getTimestamp(),
167+
namespace,
168+
podname,
169+
containerId,
170+
record.getPath(),
171+
record.getFilename(),
172+
record.getStartOffset()
173+
)
174+
);
175+
}
160176
ZonedDateTime zdt = instant.atZone(timezoneId);
161177
String timestamp = zdt.format(format);
162178

@@ -287,7 +303,7 @@ public void accept(FileRecord record) {
287303
.withAppName(appName)
288304
.withFacility(Facility.USER)
289305
.withSDElement(SDMetadata)
290-
.withMsg(new String(record.getRecord()));
306+
.withMsg(new String(record.getRecord(), StandardCharsets.UTF_8));
291307
try {
292308
RelpOutput output = relpOutputPool.take();
293309
output.send(syslog);

src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public static void main(String[] args) throws IOException {
4242
AppConfig appConfig;
4343
try {
4444
appConfig = gson.fromJson(new FileReader("etc/config.json"), AppConfig.class);
45-
appConfig.validate();
4645
}
4746
catch (FileNotFoundException e) {
4847
LOGGER.error(
@@ -58,16 +57,23 @@ public static void main(String[] args) throws IOException {
5857
);
5958
return;
6059
}
61-
catch (InvalidConfigurationException e) {
60+
catch (Exception e) {
6261
LOGGER.error(
63-
"Failed to validate config 'etc/config.json':",
62+
"Caught exception while handling config:",
6463
e
6564
);
6665
return;
6766
}
68-
catch (Exception e) {
67+
if (appConfig == null) {
68+
LOGGER.error("Unknown parsing failure happened with config 'etc/config.json', can't continue. Check if the configuration file is empty?");
69+
return;
70+
}
71+
try {
72+
appConfig.validate();
73+
}
74+
catch (InvalidConfigurationException e) {
6975
LOGGER.error(
70-
"Unknown exception while handling config:",
76+
"Failed to validate config 'etc/config.json':",
7177
e
7278
);
7379
return;
@@ -184,5 +190,6 @@ public static void main(String[] args) throws IOException {
184190
}
185191
}
186192
prometheusMetrics.close();
193+
statefulFileReader.close();
187194
}
188195
}

0 commit comments

Comments
 (0)