-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[winlogbeat] Add handling for missing event data types in the experim…
…ental API (#41418) (#41512) * [winlogbeat] Add handling for missing event data types in the experimental API (#40684) * Check type assertion result before using the result * Update CHANGELOG.next.asciidoc * Use sync.OnceValue to lazy init ansi decoder * Move cached encoder out of function * Add comment to clarify default cp * Update strings_windows.go (cherry picked from commit 36c3d5d) Co-authored-by: Marc Guasch <marc-gr@users.noreply.github.com>
- Loading branch information
1 parent
e861780
commit b768b39
Showing
7 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package sys | ||
|
||
import ( | ||
"sync" | ||
|
||
"golang.org/x/sys/windows" | ||
"golang.org/x/text/encoding" | ||
"golang.org/x/text/encoding/charmap" | ||
) | ||
|
||
var getCachedANSIDecoder = sync.OnceValue(initANSIDecoder) | ||
|
||
func initANSIDecoder() *encoding.Decoder { | ||
ansiCP := windows.GetACP() | ||
for _, enc := range charmap.All { | ||
cm, ok := enc.(*charmap.Charmap) | ||
if !ok { | ||
continue | ||
} | ||
cmID, _ := cm.ID() | ||
if uint32(cmID) != ansiCP { | ||
continue | ||
} | ||
return cm.NewDecoder() | ||
} | ||
// This should never be reached. | ||
// If the ANSI Code Page is not found, we will default to | ||
// Windows1252 Code Page, which is default for ANSI in | ||
// many regions and corresponds to Western European languages. | ||
return charmap.Windows1252.NewDecoder() | ||
} | ||
|
||
func ANSIBytesToString(enc []byte) (string, error) { | ||
out, err := getCachedANSIDecoder().Bytes(enc) | ||
return string(out), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package sys | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var ( | ||
modkernel = windows.NewLazySystemDLL("Kernel32.dll") | ||
|
||
procSystemTimeToFileTime = modkernel.NewProc("SystemTimeToFileTime") | ||
) | ||
|
||
func SystemTimeToFileTime(systemTime *windows.Systemtime, fileTime *windows.Filetime) error { | ||
r1, _, err := syscall.SyscallN(procSystemTimeToFileTime.Addr(), uintptr(unsafe.Pointer(systemTime)), uintptr(unsafe.Pointer(fileTime))) | ||
if r1 == 0 { | ||
return fmt.Errorf("error converting system time to file time: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package sys | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func TestSystemTimeToFileTime(t *testing.T) { | ||
ts := time.Date( | ||
2024, time.Month(9), 3, | ||
0, 0, 0, 0, time.UTC).UnixNano() | ||
st := windows.Systemtime{ | ||
Year: 2024, | ||
Month: 9, | ||
Day: 3, | ||
} | ||
var ft windows.Filetime | ||
if err := SystemTimeToFileTime(&st, &ft); err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, ts, ft.Nanoseconds()) | ||
} |