Skip to content

Commit 6411ce5

Browse files
authored
Add additional guards for cases where AuditLogger params may be null (#370)
Also add @Nullable/@NotNull annotations to enable detection of bugs in future
1 parent 82485c9 commit 6411ce5

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

core/src/main/java/org/testcontainers/utility/AuditLogger.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import lombok.experimental.UtilityClass;
77
import lombok.extern.slf4j.Slf4j;
88
import org.apache.commons.lang.StringUtils;
9+
import org.jetbrains.annotations.NotNull;
910
import org.jetbrains.annotations.Nullable;
1011
import org.slf4j.MDC;
1112

1213
import java.util.List;
1314

15+
import static com.google.common.base.Strings.nullToEmpty;
16+
1417
/**
1518
* Logger for tracking potentially destructive actions, intended for usage in a shared Docker environment where
1619
* traceability is needed. This class uses SLF4J, logging at TRACE level and capturing common fields as MDC fields.
@@ -25,14 +28,28 @@ public class AuditLogger {
2528
private static final ObjectMapper objectMapper = new ObjectMapper();
2629
public static final String MDC_PREFIX = AuditLogger.class.getCanonicalName();
2730

28-
public static void doLog(String action, String image, String containerId, DockerCmd<?> cmd) {
31+
public static void doLog(@NotNull String action,
32+
@Nullable String image,
33+
@Nullable String containerId,
34+
@NotNull DockerCmd<?> cmd) {
35+
2936
doLog(action, image, containerId, cmd, null);
3037
}
3138

32-
public static void doLog(String action, String image, String containerId, DockerCmd<?> cmd, @Nullable Exception e) {
33-
MDC.put(MDC_PREFIX + ".Action", action);
34-
MDC.put(MDC_PREFIX + ".Image", image);
35-
MDC.put(MDC_PREFIX + ".ContainerId", containerId);
39+
public static void doLog(@NotNull String action,
40+
@Nullable String image,
41+
@Nullable String containerId,
42+
@NotNull DockerCmd<?> cmd,
43+
@Nullable Exception e) {
44+
45+
if (! log.isTraceEnabled()) {
46+
return;
47+
}
48+
49+
MDC.put(MDC_PREFIX + ".Action", nullToEmpty(action));
50+
MDC.put(MDC_PREFIX + ".Image", nullToEmpty(image));
51+
MDC.put(MDC_PREFIX + ".ContainerId", nullToEmpty(containerId));
52+
3653
try {
3754
MDC.put(MDC_PREFIX + ".Command", objectMapper.writeValueAsString(cmd));
3855
} catch (JsonProcessingException ignored) {
@@ -52,11 +69,21 @@ public static void doLog(String action, String image, String containerId, Docker
5269
MDC.remove(MDC_PREFIX + ".Exception");
5370
}
5471

55-
public static void doComposeLog(String[] commandParts, List<String> env) {
72+
public static void doComposeLog(@NotNull String[] commandParts,
73+
@Nullable List<String> env) {
74+
75+
if (! log.isTraceEnabled()) {
76+
return;
77+
}
78+
5679
MDC.put(MDC_PREFIX + ".Action", "COMPOSE");
80+
81+
if (env != null) {
82+
MDC.put(MDC_PREFIX + ".Compose.Env", env.toString());
83+
}
84+
5785
final String command = StringUtils.join(commandParts, ' ');
5886
MDC.put(MDC_PREFIX + ".Compose.Command", command);
59-
MDC.put(MDC_PREFIX + ".Compose.Env", env.toString());
6087

6188
log.trace("COMPOSE action with command: {}, env: {}", command, env);
6289

0 commit comments

Comments
 (0)