-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.go
More file actions
64 lines (59 loc) · 1.87 KB
/
payload.go
File metadata and controls
64 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package writ
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
// payloadEntry is a single record in the sidecar .payloads JSONL file.
// It stores the raw JSON of the input and/or output for one audited operation.
type payloadEntry struct {
AuditID string `json:"audit_id"`
Timestamp time.Time `json:"timestamp"`
EventType string `json:"event_type"`
Input json.RawMessage `json:"input,omitempty"`
Output json.RawMessage `json:"output,omitempty"`
}
// payloadWriter appends payloadEntry records to a sidecar JSONL file.
// Write failures are silent: the main chain is authoritative; the payloads
// file is supplementary and must not block normal operation.
type payloadWriter struct {
mu sync.Mutex
path string
}
func newPayloadWriter(auditPath string) (*payloadWriter, error) {
path := filepath.Clean(auditPath) + ".payloads"
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) //#nosec G304 -- construction-time path, same trust as AuditPath
if err != nil {
return nil, fmt.Errorf("writ: open payloads file: %w", err)
}
if err := f.Close(); err != nil {
return nil, fmt.Errorf("writ: close payloads file: %w", err)
}
fi, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("writ: stat payloads file: %w", err)
}
if fi.Mode().Perm() != 0o600 {
if err := os.Chmod(path, 0o600); err != nil {
return nil, fmt.Errorf("writ: payloads file has insecure permissions (%#o) and chmod failed: %w", fi.Mode().Perm(), err)
}
}
return &payloadWriter{path: path}, nil
}
func (pw *payloadWriter) write(entry payloadEntry) {
pw.mu.Lock()
defer pw.mu.Unlock()
f, err := os.OpenFile(pw.path, os.O_APPEND|os.O_WRONLY, 0o600) //#nosec G304
if err != nil {
return
}
defer func() { _ = f.Close() }()
line, err := json.Marshal(entry)
if err != nil {
return
}
_, _ = fmt.Fprintf(f, "%s\n", line)
}