Skip to content

Commit 1c0d558

Browse files
committed
fix(promtail): windows forward event crash
1 parent be047cb commit 1c0d558

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

‎clients/pkg/promtail/targets/windows/win_eventlog/event.go

+29-16
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,41 @@ package win_eventlog
3333
// More info on schema, if there will be need to add more:
3434
// https://docs.microsoft.com/en-us/windows/win32/wes/eventschema-elements
3535
type Event struct {
36-
Source Provider `xml:"System>Provider"`
37-
EventID int `xml:"System>EventID"`
38-
Version int `xml:"System>Version"`
39-
Level int `xml:"System>Level"`
40-
Task int `xml:"System>Task"`
41-
Opcode int `xml:"System>Opcode"`
42-
Keywords string `xml:"System>Keywords"`
43-
TimeCreated TimeCreated `xml:"System>TimeCreated"`
44-
EventRecordID int `xml:"System>EventRecordID"`
45-
Correlation Correlation `xml:"System>Correlation"`
46-
Execution Execution `xml:"System>Execution"`
47-
Channel string `xml:"System>Channel"`
48-
Computer string `xml:"System>Computer"`
49-
Security Security `xml:"System>Security"`
50-
UserData UserData `xml:"UserData"`
51-
EventData EventData `xml:"EventData"`
36+
Source Provider `xml:"System>Provider"`
37+
EventID int `xml:"System>EventID"`
38+
Version int `xml:"System>Version"`
39+
Level int `xml:"System>Level"`
40+
Task int `xml:"System>Task"`
41+
Opcode int `xml:"System>Opcode"`
42+
Keywords string `xml:"System>Keywords"`
43+
TimeCreated TimeCreated `xml:"System>TimeCreated"`
44+
EventRecordID int `xml:"System>EventRecordID"`
45+
Correlation Correlation `xml:"System>Correlation"`
46+
Execution Execution `xml:"System>Execution"`
47+
Channel string `xml:"System>Channel"`
48+
Computer string `xml:"System>Computer"`
49+
Security Security `xml:"System>Security"`
50+
UserData UserData `xml:"UserData"`
51+
EventData EventData `xml:"EventData"`
52+
RenderingInfo *RenderingInfo `xml:"RenderingInfo"`
5253
Message string
5354
LevelText string
5455
TaskText string
5556
OpcodeText string
5657
}
5758

59+
// RenderingInfo is provided for events forwarded by Windows Event Collector
60+
// see https://learn.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtformatmessage#parameters
61+
type RenderingInfo struct {
62+
Message string `xml:"Message"`
63+
Level string `xml:"Level"`
64+
Task string `xml:"Task"`
65+
Opcode string `xml:"Opcode"`
66+
Channel string `xml:"Channel"`
67+
Provider string `xml:"Provider"`
68+
Keywords []string `xml:"Keywords>Keyword"`
69+
}
70+
5871
// UserData Application-provided XML data
5972
type UserData struct {
6073
InnerXML []byte `xml:",innerxml"`

‎clients/pkg/promtail/targets/windows/win_eventlog/win_eventlog.go

+38
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,39 @@ func (w *EventFetcher) renderEvent(eventHandle EvtHandle, lang uint32) (Event, e
483483
return event, nil
484484
}
485485

486+
// Do resolve local messages the usual way, while using built-in information for events forwarded by WEC.
487+
// This is a safety measure as the underlying Windows-internal EvtFormatMessage might segfault in cases
488+
// where the publisher (i.e. the remote machine which forwared the event) is unavailable e.g. due to
489+
// a reboot. See https://github.com/influxdata/telegraf/issues/12328 for the full story.
490+
if event.RenderingInfo == nil {
491+
return w.renderLocalMessage(event, eventHandle, lang)
492+
}
493+
494+
// We got 'RenderInfo' elements, so try to apply them in the following function
495+
return w.renderRemoteMessage(event)
496+
}
497+
498+
func (w *EventFetcher) renderRemoteMessage(event Event) (Event, error) {
499+
// Populating text values from RenderingInfo part of the XML
500+
if len(event.RenderingInfo.Keywords) > 0 {
501+
event.Keywords = strings.Join(event.RenderingInfo.Keywords, ",")
502+
}
503+
if event.RenderingInfo.Message != "" {
504+
event.Message = event.RenderingInfo.Message
505+
}
506+
if event.RenderingInfo.Level != "" {
507+
event.LevelText = event.RenderingInfo.Level
508+
}
509+
if event.RenderingInfo.Task != "" {
510+
event.TaskText = event.RenderingInfo.Task
511+
}
512+
if event.RenderingInfo.Opcode != "" {
513+
event.OpcodeText = event.RenderingInfo.Opcode
514+
}
515+
return event, nil
516+
}
517+
518+
func (w *EventFetcher) renderLocalMessage(event Event, eventHandle EvtHandle, lang uint32) (Event, error) {
486519
publisherHandle, err := openPublisherMetadata(0, event.Source.Name, lang)
487520
if err != nil {
488521
return event, nil
@@ -525,6 +558,11 @@ func formatEventString(
525558
return "", err
526559
}
527560

561+
// Handle empty elements
562+
if bufferUsed < 1 {
563+
return "", nil
564+
}
565+
528566
bufferUsed *= 2
529567
buffer := make([]byte, bufferUsed)
530568
bufferUsed = 0

0 commit comments

Comments
 (0)