Skip to content

Commit 2a1234f

Browse files
author
Hain Zuppur
committed
Merge branch 'CDOC2-51-censor-file-name-in-logs' into 'master'
CDOC-51: Censor filename's in logs See merge request cdoc2/cdoc2-java-ref-impl!123
2 parents 1461a95 + 7b1f8a1 commit 2a1234f

5 files changed

Lines changed: 104 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Add attentional unit tests to cdoc2-cli for `secp256r1` elliptic curve support.
1212
* Add bats tests for `secp256r1` elliptic curve support.
1313
* Upgraded Spring boot 3.3.3 -> 4.0.1 + other third-party dependency updates.
14+
* Censor the file name in the logs.
1415

1516
### Maven package versions:
1617
```

cdoc2-cli/src/main/java/ee/cyber/cdoc2/cli/commands/CDocCreateCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static ee.cyber.cdoc2.cli.util.CDocCommonHelper.getServerProperties;
3535
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_CAPSULE_POST_PROPERTIES;
36+
import static ee.cyber.cdoc2.util.LoggingUtil.censorFileNames;
3637

3738
//S106 - Standard outputs should not be used directly to log anything
3839
//CLI needs to interact with standard outputs
@@ -132,7 +133,7 @@ public Void call() throws Exception {
132133
(recipient.labeledPasswordParam != null) ? "****" : null,
133134
Arrays.toString(recipient.sidCodes),
134135
Arrays.toString(recipient.midCodes),
135-
Arrays.toString(inputFiles));
136+
censorFileNames(inputFiles));
136137
}
137138

138139
CDocBuilder cDocBuilder = new CDocBuilder()

cdoc2-lib/src/main/java/ee/cyber/cdoc2/container/Tar.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.function.Function;
1616

1717
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.*;
18+
import static ee.cyber.cdoc2.util.LoggingUtil.censorFileName;
19+
import static ee.cyber.cdoc2.util.LoggingUtil.censorPathFileName;
1820

1921

2022
/**
@@ -40,7 +42,11 @@ private Tar() {
4042

4143
static void addFileToTar(TarArchiveOutputStream outputStream, Path file, String entryName) throws IOException {
4244

43-
log.debug("Adding file {} as {}", file.toAbsolutePath(), entryName);
45+
log.debug(
46+
"Adding file {} as {}",
47+
censorPathFileName(file.toAbsolutePath()),
48+
censorFileName(entryName)
49+
);
4450
if (Files.isRegularFile(file)) {
4551
TarArchiveEntry tarArchiveEntry = outputStream.createArchiveEntry(file.toFile(),
4652
entryName);

cdoc2-lib/src/main/java/ee/cyber/cdoc2/container/TarDeflate.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.LinkedList;
2121
import java.util.List;
2222

23+
import static ee.cyber.cdoc2.util.LoggingUtil.censorFileName;
24+
2325
/**
2426
* AutoCloseable tarDeflate stream extractor. If any exception is thrown
2527
* during processing {@link #process(TarEntryProcessingDelegate)}, then close() deletes extracted files.
@@ -247,7 +249,11 @@ private boolean processTarEntry(
247249
boolean processed;
248250

249251
if (tarArchiveEntry.isFile()) {
250-
log.debug("Found: {} {}B", tarArchiveEntry.getName(), tarArchiveEntry.getSize());
252+
log.debug(
253+
"Found: {} {}B",
254+
censorFileName(tarArchiveEntry.getName()),
255+
tarArchiveEntry.getSize()
256+
);
251257

252258
File createdFile = delegate.onTarEntry(tarArchiveEntry);
253259
if (createdFile != null) {
@@ -269,7 +275,7 @@ private boolean processTarEntry(
269275

270276
processed = delegate.onEndOfTarEntry();
271277

272-
log.debug("Transferred {} {}B", tarArchiveEntry.getName(), written);
278+
log.debug("Transferred {} {}B", censorFileName(tarArchiveEntry.getName()), written);
273279

274280
} else {
275281
throw Tar.logTarEntryIllegalTypeAndThrow(tarArchiveEntry.getName());
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ee.cyber.cdoc2.util;
2+
3+
import java.io.File;
4+
import java.nio.file.Path;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
9+
/**
10+
* Utility class containing methods used for logging
11+
*/
12+
public final class LoggingUtil {
13+
private LoggingUtil() {
14+
// utility class
15+
}
16+
17+
/**
18+
* Censors the file name for logs. E.g. "hello.txt" would be "xxxxx.txt".
19+
*
20+
* @param fileName filename
21+
* @return The censored filename
22+
*/
23+
public static String censorFileName(String fileName) {
24+
if (fileName == null || fileName.isEmpty()) {
25+
return fileName;
26+
}
27+
28+
int lastDotIndex = fileName.lastIndexOf('.');
29+
30+
// No extension or dot is the first character (e.g. ".gitignore")
31+
if (lastDotIndex <= 0) {
32+
return "X".repeat(fileName.length());
33+
}
34+
35+
String namePart = fileName.substring(0, lastDotIndex);
36+
String extensionPart = fileName.substring(lastDotIndex);
37+
38+
return "X".repeat(namePart.length()) + extensionPart;
39+
}
40+
41+
/**
42+
* Censors the file path for logs. E.g. "abc/aaa/hello.txt" would be "abc/aaa/xxxxx.txt".
43+
*
44+
* @param path file path
45+
* @return censored file path as string
46+
*/
47+
public static String censorPathFileName(Path path) {
48+
if (path == null) {
49+
return null;
50+
}
51+
52+
Path fileName = path.getFileName();
53+
if (fileName == null) {
54+
return path.toString();
55+
}
56+
57+
String censoredFileName = censorFileName(fileName.toString());
58+
59+
Path parent = path.getParent();
60+
if (parent == null) {
61+
return censoredFileName;
62+
}
63+
64+
return parent.resolve(censoredFileName).toString();
65+
}
66+
67+
public static List<String> censorFileNames(File[] files) {
68+
if (files == null) {
69+
return null;
70+
}
71+
72+
List<String> result = new ArrayList<>(files.length);
73+
74+
for (File file : files) {
75+
if (file == null) {
76+
result.add(null);
77+
continue;
78+
}
79+
80+
String censoredName = censorFileName(file.getName());
81+
result.add(censoredName);
82+
}
83+
84+
return result;
85+
}
86+
}

0 commit comments

Comments
 (0)