forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Filebeat] Add timezone config option to decode_cef and syslog input (e…
…lastic#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 elastic#27232
- Loading branch information
1 parent
1806da0
commit 12f05fc
Showing
26 changed files
with
284 additions
and
164 deletions.
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
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
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,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) | ||
} |
Oops, something went wrong.