Skip to content

Commit 2d3633c

Browse files
author
Zhang Wenhao
committed
<feature>[log]: add LongText type for logging long content safely
Add LongText type to NoLogging annotation to handle long content like base64-encoded image data in logs. When content length >= 20 characters, only show first 10 and last 10 characters with ... in between to improve log readability. Applied to: - APITakeVmConsoleScreenshotEvent.imageData - TakeVmConsoleScreenshotReply.imageData - KVMAgentCommands.VmHostFileTO.contentBase64 - KVMAgentCommands.TakeVmConsoleScreenshotRsp.imageData Related: ZSV-11310 Resolves: ZSV-11443 Change-Id: I747a68626a717563727373767975647863786977
1 parent 4db6aab commit 2d3633c

5 files changed

Lines changed: 17 additions & 2 deletions

File tree

core/src/main/java/org/zstack/core/log/LogSafeGson.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ String getMaskedValue(String raw) {
9999
return "*****";
100100
} else if (annotation.type().tag()) {
101101
return tagInfoHider.call(raw);
102+
} else if (annotation.type().longText()) {
103+
if (raw == null)
104+
return null;
105+
return (raw.length() < 20) ? raw : raw.substring(0, 10) + "..." + raw.substring(raw.length() - 10);
102106
} else {
103107
return Utils.getLogMaskWords().getOrDefault(raw, uriPattern.matcher(raw).replaceFirst(":*****@"));
104108
}

header/src/main/java/org/zstack/header/log/NoLogging.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public boolean mask() {
2626
enum Type {
2727
Simple,
2828
Tag,
29-
Uri;
29+
Uri,
30+
LongText,
31+
;
3032

3133
public boolean simple() {
3234
return this == Simple;
@@ -39,6 +41,10 @@ public boolean tag() {
3941
public boolean uri() {
4042
return this == Uri;
4143
}
44+
45+
public boolean longText() {
46+
return this == LongText;
47+
}
4248
}
4349

4450
Behavior behavior() default Behavior.Mask;

header/src/main/java/org/zstack/header/vm/APITakeVmConsoleScreenshotEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.zstack.header.vm;
22

3+
import org.zstack.header.log.NoLogging;
34
import org.zstack.header.message.APIEvent;
45
import org.zstack.header.rest.RestResponse;
56

@@ -9,6 +10,7 @@
910
*/
1011
@RestResponse(fieldsTo = {"imageData"})
1112
public class APITakeVmConsoleScreenshotEvent extends APIEvent {
13+
@NoLogging(type = NoLogging.Type.LongText)
1214
private String imageData;
1315

1416
public APITakeVmConsoleScreenshotEvent() {

header/src/main/java/org/zstack/header/vm/TakeVmConsoleScreenshotReply.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.zstack.header.vm;
22

3+
import org.zstack.header.log.NoLogging;
34
import org.zstack.header.message.MessageReply;
45

56
/**
67
* @author shanshan.ning
78
* @date 2023-09-11
89
*/
910
public class TakeVmConsoleScreenshotReply extends MessageReply {
11+
@NoLogging(type = NoLogging.Type.LongText)
1012
private String imageData;
1113

1214
public String getImageData() {

plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,7 @@ public static class VmHostFileTO {
28442844
/**
28452845
* null if operation is Prepare or Delete
28462846
*/
2847-
@NoLogging
2847+
@NoLogging(type = NoLogging.Type.LongText)
28482848
private String contentBase64;
28492849
private String error;
28502850

@@ -4760,6 +4760,7 @@ public void setVmUuid(String vmUuid) {
47604760
}
47614761

47624762
public static class TakeVmConsoleScreenshotRsp extends AgentResponse {
4763+
@NoLogging(type = NoLogging.Type.LongText)
47634764
private String imageData;
47644765

47654766
public String getImageData() {

0 commit comments

Comments
 (0)