Skip to content

Commit 3239ccf

Browse files
authored
kms: separate error logs from audit / API event logs (#37)
This commit fixes a design mistake in the logging system. Before, the `LogRecord` type represented any kind of log record - error log, audit log and API event. This was problematic since almost all error logs (esp. with log levels < `ERROR`) did not have an associated request. Similarly, audit logs and API events usually don't include stack traces. Overall, unifying all logs into a single type doesn't seem to be the correct design since different logs have different purposes. This commit addresses this partially by removing HTTP/API information from the `LogRecord` type. A `LogRecord` represents a KMS server error log record. In subsequent commits, dedicated types for audit log records and API calls will be added. Signed-off-by: Andreas Auernhammer <github@aead.dev>
1 parent 505cea0 commit 3239ccf

File tree

6 files changed

+166
-421
lines changed

6 files changed

+166
-421
lines changed

kms/log.go

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99
"errors"
1010
"io"
1111
"log/slog"
12-
"net/netip"
1312
"time"
1413

15-
"aead.dev/mtls"
1614
pb "github.com/minio/kms-go/kms/protobuf"
1715
)
1816

@@ -30,40 +28,35 @@ type StackFrame struct {
3028
Line int
3129
}
3230

33-
// LogRecord is a structure representing a KMS log event.
34-
type LogRecord struct {
35-
// The log level of the event.
36-
Level slog.Level
37-
38-
// The log message.
39-
Message string
31+
// MarshalPB converts the StackFrame into its protobuf representation.
32+
func (s *StackFrame) MarshalPB(v *pb.LogRecord_StackFrame) error {
33+
v.Function = s.Function
34+
v.File = s.File
35+
v.Line = uint32(s.Line)
36+
return nil
37+
}
4038

41-
// The time at which the event was produced.
42-
Time time.Time
39+
// UnmarshalPB initializes the StackFrame from its protobuf representation.
40+
func (s *StackFrame) UnmarshalPB(v *pb.LogRecord_StackFrame) error {
41+
s.Function = v.Function
42+
s.File = v.File
43+
s.Line = int(v.Line)
44+
return nil
45+
}
4346

44-
// The stack trace at the time the event was recorded.
45-
// Its first frame is the location at which this event
46-
// was produced and subsequent frames represent function
47-
// calls higher up the call stack.
47+
// LogRecord is a structure representing a KMS server log record.
48+
type LogRecord struct {
49+
Level slog.Level // The log level of the record.
50+
Message string // The log message.
51+
Time time.Time // The time at which the record was created.
52+
53+
// Trace is stack trace of function calls. Its first frame
54+
// is the location at which this record was created and
55+
// subsequent frames represent function calls higher up the
56+
// call stack.
4857
//
4958
// If empty, no stack trace has been captured.
5059
Trace []StackFrame
51-
52-
// If non-empty, HTTP method of the request that caused
53-
// this event.
54-
Method string
55-
56-
// If non-empty, URL path of the request that caused
57-
// this event.
58-
Path string
59-
60-
// If non-empty, identity of the request that caused
61-
// this event.
62-
Identity mtls.Identity
63-
64-
// If valid, IP address of the client sending the
65-
// request that caused this event.
66-
IP netip.Addr
6760
}
6861

6962
// MarshalPB converts the LogRecord into its protobuf representation.
@@ -74,61 +67,34 @@ func (r *LogRecord) MarshalPB(v *pb.LogRecord) error {
7467

7568
if len(r.Trace) > 0 {
7669
v.Trace = make([]*pb.LogRecord_StackFrame, 0, len(r.Trace))
77-
for _, t := range r.Trace {
70+
for _, frame := range r.Trace {
7871
v.Trace = append(v.Trace, &pb.LogRecord_StackFrame{
79-
Function: t.Function,
80-
File: t.File,
81-
Line: uint32(t.Line),
72+
Function: frame.Function,
73+
File: frame.File,
74+
Line: uint32(frame.Line),
8275
})
8376
}
8477
}
85-
if r.Method != "" || r.Path != "" || r.Identity != (mtls.Identity{}) || r.IP.IsValid() {
86-
v.Req = &pb.LogRecord_Request{
87-
Method: r.Method,
88-
Path: r.Path,
89-
Identity: r.Identity.String(),
90-
IP: r.IP.String(),
91-
}
92-
}
9378
return nil
9479
}
9580

9681
// UnmarshalPB initializes the LogRecord from its protobuf representation.
9782
func (r *LogRecord) UnmarshalPB(v *pb.LogRecord) error {
98-
var (
99-
ip netip.Addr
100-
id mtls.Identity
101-
)
102-
if v.Req != nil {
103-
var err error
104-
if ip, err = netip.ParseAddr(v.Req.IP); err != nil {
105-
return err
106-
}
107-
}
108-
if v.Req.Identity != "" {
109-
var err error
110-
if id, err = mtls.ParseIdentity(v.Req.Identity); err != nil {
111-
return err
112-
}
113-
}
114-
11583
r.Level = slog.Level(v.Level)
11684
r.Message = v.Message
11785
r.Time = v.Time.AsTime()
118-
119-
r.Trace = make([]StackFrame, 0, len(v.Trace))
120-
for _, t := range v.GetTrace() {
121-
r.Trace = append(r.Trace, StackFrame{
122-
Function: t.Function,
123-
File: t.File,
124-
Line: int(t.Line),
125-
})
86+
r.Trace = nil
87+
88+
if len(v.Trace) > 0 {
89+
r.Trace = make([]StackFrame, 0, len(v.Trace))
90+
for _, frame := range v.Trace {
91+
r.Trace = append(r.Trace, StackFrame{
92+
Function: frame.Function,
93+
File: frame.File,
94+
Line: int(frame.Line),
95+
})
96+
}
12697
}
127-
128-
r.Method = v.Req.GetMethod()
129-
r.Path = v.Req.GetPath()
130-
r.Identity = id
131-
r.IP = ip
13298
return nil
13399
}
134100

kms/protobuf/log.pb.go

Lines changed: 18 additions & 126 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kms/protobuf/log.proto

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ message LogRecord {
2222
uint32 Line = 3 [ json_name = "line" ];
2323
}
2424

25-
message Request {
26-
// HTTP method of the request that caused this event.
27-
string Method = 1 [ json_name = "method" ];
28-
29-
// URL path of the request that caused this event.
30-
string Path = 2 [ json_name = "path" ];
31-
32-
// Identity of the request that caused this event.
33-
string Identity = 3 [ json_name = "identity" ];
34-
35-
// IP address of the client sending the request that
36-
// caused this event.
37-
string IP = 4 [ json_name = "ip" ];
38-
}
39-
4025
// The log level of the event.
4126
sint32 Level = 1 [ json_name="level" ];
4227

@@ -53,7 +38,4 @@ message LogRecord {
5338
//
5439
// If empty, no stack trace has been captured.
5540
repeated StackFrame Trace = 4 [ json_name = "trace" ];
56-
57-
// HTTP request information
58-
Request Req = 5 [ json_name = "request" ];
5941
}

0 commit comments

Comments
 (0)