diff --git a/eventlog/log.go b/eventlog/log.go index d1d5798..814627c 100644 --- a/eventlog/log.go +++ b/eventlog/log.go @@ -1,6 +1,7 @@ package eventlog import ( + "bytes" "encoding/json" "errors" "fmt" @@ -27,6 +28,11 @@ var ( // sourceName is the human-readable application name populated at build time. // The ETW provider identity itself is stable and does not vary by build. sourceName = providerDisplayName + + // ProductVersion is the NVM for Windows version that wrote the event + // (same value as settings.ProductVersion). nvm.exe and sync.exe set this + // at startup so every structured Payload includes nvm_version for SIEM. + ProductVersion string ) type providerState struct { @@ -219,7 +225,7 @@ func (ev *EventLogger) writeStructured(descriptor eventDescriptor, eventName str return } - payloadJSON, err := marshalStructuredPayload(payload) + payloadJSON, err := marshalStructuredPayload(enrichStructuredPayload(payload)) if err != nil { return } @@ -476,6 +482,73 @@ func scalarFieldDescriptor(value *uint32) eventDataDescriptor { } } +const nvmVersionPayloadKey = "nvm_version" + +// enrichStructuredPayload adds nvm_version when ProductVersion is set and the +// caller did not already supply that key. Non-object payloads are left unchanged. +func enrichStructuredPayload(payload any) any { + ver := strings.TrimSpace(ProductVersion) + if ver == "" { + return payload + } + + switch value := payload.(type) { + case nil: + return map[string]any{nvmVersionPayloadKey: ver} + case map[string]any: + if _, exists := value[nvmVersionPayloadKey]; !exists { + value[nvmVersionPayloadKey] = ver + } + return value + case map[string]string: + out := make(map[string]any, len(value)+1) + for k, v := range value { + out[k] = v + } + if _, exists := out[nvmVersionPayloadKey]; !exists { + out[nvmVersionPayloadKey] = ver + } + return out + case json.RawMessage: + return injectNVMVersionJSON([]byte(value), ver) + case []byte: + if json.Valid(value) { + return injectNVMVersionJSON(value, ver) + } + return payload + case string: + trimmed := strings.TrimSpace(value) + if trimmed != "" && json.Valid([]byte(trimmed)) { + return injectNVMVersionJSON([]byte(trimmed), ver) + } + return payload + default: + encoded, err := json.Marshal(payload) + if err != nil || !json.Valid(encoded) { + return payload + } + return injectNVMVersionJSON(encoded, ver) + } +} + +func injectNVMVersionJSON(raw []byte, ver string) any { + trimmed := bytes.TrimSpace(raw) + if len(trimmed) == 0 || trimmed[0] != '{' { + return json.RawMessage(raw) + } + var obj map[string]any + if err := json.Unmarshal(trimmed, &obj); err != nil { + return json.RawMessage(raw) + } + if obj == nil { + obj = map[string]any{} + } + if _, exists := obj[nvmVersionPayloadKey]; !exists { + obj[nvmVersionPayloadKey] = ver + } + return obj +} + func marshalStructuredPayload(payload any) (string, error) { switch value := payload.(type) { case nil: diff --git a/eventlog/payload_enrich_test.go b/eventlog/payload_enrich_test.go new file mode 100644 index 0000000..235f776 --- /dev/null +++ b/eventlog/payload_enrich_test.go @@ -0,0 +1,81 @@ +package eventlog + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestEnrichStructuredPayloadAddsNVMVersion(t *testing.T) { + prev := ProductVersion + ProductVersion = "2.1.0-beta.9" + t.Cleanup(func() { ProductVersion = prev }) + + got := enrichStructuredPayload(map[string]any{"action": "upgrade"}) + m, ok := got.(map[string]any) + if !ok { + t.Fatalf("got %T", got) + } + if m["nvm_version"] != "2.1.0-beta.9" { + t.Fatalf("nvm_version = %v", m["nvm_version"]) + } + if m["action"] != "upgrade" { + t.Fatalf("action = %v", m["action"]) + } +} + +func TestEnrichStructuredPayloadPreservesCallerNVMVersion(t *testing.T) { + prev := ProductVersion + ProductVersion = "2.1.0-beta.9" + t.Cleanup(func() { ProductVersion = prev }) + + got := enrichStructuredPayload(map[string]any{"nvm_version": "9.9.9"}) + m := got.(map[string]any) + if m["nvm_version"] != "9.9.9" { + t.Fatalf("caller nvm_version overwritten: %v", m["nvm_version"]) + } +} + +func TestEnrichStructuredPayloadNilBecomesObject(t *testing.T) { + prev := ProductVersion + ProductVersion = "2.0.1" + t.Cleanup(func() { ProductVersion = prev }) + + got := enrichStructuredPayload(nil) + m, ok := got.(map[string]any) + if !ok || m["nvm_version"] != "2.0.1" { + t.Fatalf("nil enrich = %#v", got) + } +} + +func TestEnrichStructuredPayloadJSONObject(t *testing.T) { + prev := ProductVersion + ProductVersion = "2.1.0" + t.Cleanup(func() { ProductVersion = prev }) + + raw := json.RawMessage(`{"foo":"bar"}`) + got := enrichStructuredPayload(raw) + encoded, err := json.Marshal(got) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(encoded), `"nvm_version":"2.1.0"`) { + t.Fatalf("json enrich = %s", encoded) + } + if !strings.Contains(string(encoded), `"foo":"bar"`) { + t.Fatalf("lost foo: %s", encoded) + } +} + +func TestEnrichStructuredPayloadSkipsWhenUnset(t *testing.T) { + prev := ProductVersion + ProductVersion = "" + t.Cleanup(func() { ProductVersion = prev }) + + in := map[string]any{"a": 1} + got := enrichStructuredPayload(in) + m := got.(map[string]any) + if _, ok := m["nvm_version"]; ok { + t.Fatal("unexpected nvm_version when ProductVersion empty") + } +}