Skip to content

Commit

Permalink
[Filebeat] Add timezone config option to decode_cef and syslog input (#…
Browse files Browse the repository at this point in the history
…27727)

CEF message that contain timestamps without a timezone were parsed as UTC. The time zone was not
configurable. This adds a `timezone` option to the decode_cef processor and cef module to allow the
time zone to be specified when a timestamp does not contain an offset or zone.

    CEF:0|Aruba Networks|ClearPass|6.8.7.120583|2002|RADIUS Accounting|1|rt=Aug 04 2021 11:31:15

Note that the CEF module receives messages using the syslog input. The syslog input does not have
a configurable time zone and always assumes timestamps without time zones are given in the machine's
local time zone. This change won't affect how the syslog envelop's time stamp is parsed by the module.

This also replaces the deprecated `import "4d63.com/tz"` with Go's relatively new built-in
`time/tzdata` package. The `timestamp` processor was updated.

While I was adding the a timezone config type I made the syslog input's timezone configurable too.

Fixes #27232

(cherry picked from commit b3497ca)
  • Loading branch information
andrewkroh authored and mergify-bot committed Sep 9, 2021
1 parent c0e590b commit 1c99541
Show file tree
Hide file tree
Showing 26 changed files with 284 additions and 164 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add support to merge registry updates in the filestream input across multiple ACKed batches in case of backpressure in the registry or disk. {pull}25976[25976]
- Add support to `decode_cef` for MAC addresses that do not contain separator characters. {issue}27050[27050] {pull}27109[27109]
- Add new `hmac` template function for httpjson input {pull}27168[27168]
- Add `timezone` config option to the `decode_cef` processor. {issue}27232[27232] {pull}27727[27727]
- Add `timezone` config option to the `syslog` input. {pull}27727[27727]
- Update `tags` and `threatintel.indicator.provider` fields in `threatintel.anomali` ingest pipeline {issue}24746[24746] {pull}27141[27141]
- Move AWS module and filesets to GA. {pull}27428[27428]
- update ecs.version to ECS 1.11.0. {pull}27107[27107]
Expand Down
92 changes: 0 additions & 92 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,6 @@ Third party libraries used by the Elastic Beats project:
================================================================================


--------------------------------------------------------------------------------
Dependency : 4d63.com/tz
Version: v1.1.1-0.20191124060701-6d37baae851b
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/4d63.com/[email protected]/LICENSE:

MIT License

Copyright (c) 2018 Leigh McCulloch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--------------------------------------------------------------------------------

zoneinfo.go generated from /lib/time/zoneinfo.zip from Go.

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : cloud.google.com/go
Version: v0.51.0
Expand Down Expand Up @@ -19439,35 +19376,6 @@ Contents of probable licence file $GOMODCACHE/k8s.io/[email protected]/LICENSE:
Indirect dependencies


--------------------------------------------------------------------------------
Dependency : 4d63.com/embedfiles
Version: v0.0.0-20190311033909-995e0740726f
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/4d63.com/[email protected]/LICENSE:

Copyright (c) 2017, Leigh McCulloch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : bazil.org/fuse
Version: v0.0.0-20160811212531-371fbbdaa898
Expand Down
18 changes: 14 additions & 4 deletions filebeat/docs/inputs/input-syslog.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<titleabbrev>Syslog</titleabbrev>
++++

The `syslog` input reads Syslog events as specified by RFC 3164 and RFC 5424, over TCP, UDP, or a Unix stream socket.
The `syslog` input reads Syslog events as specified by RFC 3164 and RFC 5424,
over TCP, UDP, or a Unix stream socket.

Example configurations:

Expand Down Expand Up @@ -40,12 +41,21 @@ Example configurations:

==== Configuration options

The `syslog` input configuration includes format, protocol specific options, and the
<<{beatname_lc}-input-{type}-common-options>> described later.
The `syslog` input configuration includes format, protocol specific options, and
the <<{beatname_lc}-input-{type}-common-options>> described later.

===== `format`

The syslog variant to use, `rfc3164` or `rfc5424`. To automatically detect the format from the log entries, set this option to `auto`. The default is `rfc3164`.
The syslog variant to use, `rfc3164` or `rfc5424`. To automatically detect the
format from the log entries, set this option to `auto`. The default is
`rfc3164`.

===== `timezone`

IANA time zone name (e.g. `America/New_York`) or fixed time offset (e.g.
`+0200`) to use when parsing syslog timestamps that do not contain a time zone.
`Local` may be specified to use the machine's local time zone. Defaults to
`Local`.

===== Protocol `udp`:

Expand Down
7 changes: 7 additions & 0 deletions filebeat/docs/modules/cef.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ A list of tags to include in events. Including `forwarded` indicates that the
events did not originate on this host and causes `host.name` to not be added to
events. Defaults to `[cef, forwarded]`.

*`var.timezone`*::

IANA time zone name (e.g. `America/New_York`) or fixed time offset (e.g.
`+0200`) to use when parsing times from the CEF message that do not contain a
time zone. `Local` may be specified to use the machine's local time zone.
Defaults to `UTC`.

[float]
==== Forcepoint NGFW Security Management Center

Expand Down
5 changes: 4 additions & 1 deletion filebeat/input/syslog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ import (
"github.com/elastic/beats/v7/filebeat/inputsource/udp"
"github.com/elastic/beats/v7/filebeat/inputsource/unix"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/cfgtype"
"github.com/elastic/beats/v7/libbeat/logp"
)

type config struct {
harvester.ForwarderConfig `config:",inline"`
Format syslogFormat `config:"format"`
Protocol common.ConfigNamespace `config:"protocol"`
Timezone *cfgtype.Timezone `config:"timezone"`
}

type syslogFormat int
Expand All @@ -59,7 +61,8 @@ var defaultConfig = config{
ForwarderConfig: harvester.ForwarderConfig{
Type: "syslog",
},
Format: syslogFormatRFC3164,
Format: syslogFormatRFC3164,
Timezone: cfgtype.MustNewTimezone("Local"),
}

type syslogTCP struct {
Expand Down
12 changes: 6 additions & 6 deletions filebeat/input/syslog/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ func GetCbByConfig(cfg config, forwarder *harvester.Forwarder, log *logp.Logger)

case syslogFormatRFC5424:
return func(data []byte, metadata inputsource.NetworkMetadata) {
ev := parseAndCreateEvent5424(data, metadata, time.Local, log)
ev := parseAndCreateEvent5424(data, metadata, cfg.Timezone.Location(), log)
forwarder.Send(ev)
}

case syslogFormatAuto:
return func(data []byte, metadata inputsource.NetworkMetadata) {
var ev beat.Event
if IsRFC5424Format(data) {
ev = parseAndCreateEvent5424(data, metadata, time.Local, log)
ev = parseAndCreateEvent5424(data, metadata, cfg.Timezone.Location(), log)
} else {
ev = parseAndCreateEvent3164(data, metadata, time.Local, log)
ev = parseAndCreateEvent3164(data, metadata, cfg.Timezone.Location(), log)
}
forwarder.Send(ev)
}
Expand All @@ -198,7 +198,7 @@ func GetCbByConfig(cfg config, forwarder *harvester.Forwarder, log *logp.Logger)
}

return func(data []byte, metadata inputsource.NetworkMetadata) {
ev := parseAndCreateEvent3164(data, metadata, time.Local, log)
ev := parseAndCreateEvent3164(data, metadata, cfg.Timezone.Location(), log)
forwarder.Send(ev)
}
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func parseAndCreateEvent3164(data []byte, metadata inputsource.NetworkMetadata,
"message": string(data),
})
}
return createEvent(ev, metadata, time.Local, log)
return createEvent(ev, metadata, timezone, log)
}

func parseAndCreateEvent5424(data []byte, metadata inputsource.NetworkMetadata, timezone *time.Location, log *logp.Logger) beat.Event {
Expand All @@ -299,7 +299,7 @@ func parseAndCreateEvent5424(data []byte, metadata inputsource.NetworkMetadata,
"message": string(data),
})
}
return createEvent(ev, metadata, time.Local, log)
return createEvent(ev, metadata, timezone, log)
}

func newBeatEvent(timestamp time.Time, metadata inputsource.NetworkMetadata, fields common.MapStr) beat.Event {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/elastic/beats/v7
go 1.16

require (
4d63.com/tz v1.1.1-0.20191124060701-6d37baae851b
cloud.google.com/go v0.51.0
cloud.google.com/go/bigquery v1.0.1
cloud.google.com/go/pubsub v1.0.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
4d63.com/embedfiles v0.0.0-20190311033909-995e0740726f h1:oyYjGRBNq1TxAIG8aHqtxlvqUfzdZf+MbcRb/oweNfY=
4d63.com/embedfiles v0.0.0-20190311033909-995e0740726f/go.mod h1:HxEsUxoVZyRxsZML/S6e2xAuieFMlGO0756ncWx1aXE=
4d63.com/tz v1.1.1-0.20191124060701-6d37baae851b h1:+TO4EgK74+Qo/ilRDiF2WpY09Jk9VSJSLe3wEn+dJBw=
4d63.com/tz v1.1.1-0.20191124060701-6d37baae851b/go.mod h1:SHGqVdL7hd2ZaX2T9uEiOZ/OFAUfCCLURdLPJsd8ZNs=
bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down
94 changes: 94 additions & 0 deletions libbeat/common/cfgtype/timezone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 cfgtype

import (
"encoding/json"
"time"

"github.com/pkg/errors"

// Embed the timezone database so this code works across platforms.
_ "time/tzdata"
)

var fixedOffsetFormats = []string{"-07", "-0700", "-07:00"}

// Timezone maps time instants to the zone in use at that time. Typically, the
// Timezone represents the collection of time offsets in use in a geographical
// area. For many Locations the time offset varies depending on whether daylight
// savings time is in use at the time instant.
type Timezone time.Location

// NewTimezone returns a new timezone.
func NewTimezone(tz string) (*Timezone, error) {
loc, err := loadLocation(tz)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse timezone %q", tz)
}
return (*Timezone)(loc), nil
}

// MustNewTimezone returns a new timezone. If tz is invalid it panics.
func MustNewTimezone(tz string) *Timezone {
timestamp, err := NewTimezone(tz)
if err != nil {
panic(err)
}
return timestamp
}

// Location returns a *time.Location. If timezone is nil it returns *time.UTC.
func (tz *Timezone) Location() *time.Location {
if tz == nil {
return time.UTC
}
return (*time.Location)(tz)
}

// MarshalJSON implements json.Marshaler interface.
func (tz *Timezone) MarshalJSON() ([]byte, error) {
if tz == nil {
return []byte("null"), nil
}
return json.Marshal(tz.Location().String())
}

// Unpack converts a time zone name or offset to Timezone. If using a fixed
// offset then the format must be [+-]HHMM (e.g +0800 or -0530).
func (tz *Timezone) Unpack(v string) error {
timezone, err := NewTimezone(v)
if err != nil {
return err
}
*tz = *timezone
return nil
}

func loadLocation(timezone string) (*time.Location, error) {
for _, format := range fixedOffsetFormats {
t, err := time.Parse(format, timezone)
if err == nil {
name, offset := t.Zone()
return time.FixedZone(name, offset), nil
}
}

// Handle IANA time zones.
return time.LoadLocation(timezone)
}
Loading

0 comments on commit 1c99541

Please sign in to comment.