Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auditbeat/module/auditd: add ignore_errors config option #36851

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ is collected by it.

*Auditbeat*

- Add `ignore_errors` option to audit module. {issue}15768[15768] {pull}36851[36851]

*Filebeat*

Expand Down
3 changes: 3 additions & 0 deletions auditbeat/docs/modules/auditd.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ loaded after the rules declared in `audit_rules` are loaded. Wildcards are
supported and will expand in lexicographical order. The format is the same as
that of the `audit_rules` field.

*`ignore_errors`*:: This setting allows errors during rule loading and parsing
to be ignored, but logged as warnings.

*`backpressure_strategy`*:: Specifies the strategy that {beatname_uc} uses to
prevent backpressure from propagating to the kernel and impacting audited
processes.
Expand Down
3 changes: 3 additions & 0 deletions auditbeat/module/auditd/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ loaded after the rules declared in `audit_rules` are loaded. Wildcards are
supported and will expand in lexicographical order. The format is the same as
that of the `audit_rules` field.

*`ignore_errors`*:: This setting allows errors during rule loading and parsing
to be ignored, but logged as warnings.

*`backpressure_strategy`*:: Specifies the strategy that {beatname_uc} uses to
prevent backpressure from propagating to the kernel and impacting audited
processes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

//go:build unix

package auditd

import (
Expand All @@ -30,6 +32,7 @@ import (

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/go-libaudit/v2/rule"
"github.com/elastic/go-libaudit/v2/rule/flags"
)
Expand All @@ -46,6 +49,7 @@ type Config struct {
RuleFiles []string `config:"audit_rule_files"` // List of rule files.
SocketType string `config:"socket_type"` // Socket type to use with the kernel (unicast or multicast).
Immutable bool `config:"immutable"` // Sets kernel audit config immutable.
IgnoreErrors bool `config:"ignore_errors"` // Ignore errors when reading and parsing rules, equivalent to auditctl -i.

// Tuning options (advanced, use with care)
ReassemblerMaxInFlight uint32 `config:"reassembler.max_in_flight"`
Expand Down Expand Up @@ -120,11 +124,19 @@ func (c Config) rules() []auditRule {
}

func (c *Config) loadRules() error {
var log *logp.Logger
if c.IgnoreErrors {
log = logp.NewLogger(moduleName)
}

var paths []string
for _, pattern := range c.RuleFiles {
absPattern, err := filepath.Abs(pattern)
if err != nil {
return fmt.Errorf("unable to get the absolute path for %s: %w", pattern, err)
if log == nil {
return fmt.Errorf("unable to get the absolute path for %s: %w", pattern, err)
}
log.Warnf("unable to get the absolute path for %s: %v", pattern, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just out of curiousity, why %v here for the error? I was under the impression %w was intended for printing errors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%w is only for use with https://pkg.go.dev/fmt#Errorf when wrapping errors.

}
files, err := filepath.Glob(absPattern)
if err != nil {
Expand All @@ -136,7 +148,7 @@ func (c *Config) loadRules() error {

knownRules := ruleSet{}

rules, err := readRules(bytes.NewBufferString(c.RulesBlob), "(audit_rules at auditbeat.yml)", knownRules)
rules, err := readRules(bytes.NewBufferString(c.RulesBlob), "(audit_rules at auditbeat.yml)", knownRules, log)
if err != nil {
return err
}
Expand All @@ -145,9 +157,13 @@ func (c *Config) loadRules() error {
for _, filename := range paths {
fHandle, err := os.Open(filename)
if err != nil {
return fmt.Errorf("unable to open rule file '%s': %w", filename, err)
if log == nil {
return fmt.Errorf("unable to open rule file '%s': %w", filename, err)
}
log.Warnf("unable to open rule file '%s': %v", filename, err)
continue
}
rules, err = readRules(fHandle, filename, knownRules)
rules, err = readRules(fHandle, filename, knownRules, log)
if err != nil {
return err
}
Expand All @@ -170,7 +186,11 @@ func (c Config) failureMode() (uint32, error) {
}
}

func readRules(reader io.Reader, source string, knownRules ruleSet) (rules []auditRule, err error) {
// readRules reads the audit rules from reader, adding them to knownRules. If
// log is nil, errors will result in an empty rules set being returned. Otherwise
// errors will be logged as warnings and any successfully parsed rules will be
// returned.
func readRules(reader io.Reader, source string, knownRules ruleSet, log *logp.Logger) (rules []auditRule, err error) {
var errs multierror.Errors

s := bufio.NewScanner(reader)
Expand Down Expand Up @@ -207,8 +227,11 @@ func readRules(reader io.Reader, source string, knownRules ruleSet) (rules []aud
rules = append(rules, rule)
}

if len(errs) > 0 {
return nil, fmt.Errorf("failed loading rules: %w", errs.Err())
if len(errs) != 0 {
if log == nil {
return nil, fmt.Errorf("failed loading rules: %w", errs.Err())
}
log.Warnf("errors loading rules: %v", errs.Err())
}
return rules, nil
}
254 changes: 0 additions & 254 deletions auditbeat/module/auditd/config_linux_test.go

This file was deleted.