Skip to content

Commit c47af12

Browse files
arlimuschris-rock
andauthored
⭐ auditd.config (#5476)
* ⭐ auditd.config Signed-off-by: Dominik Richter <dominik.richter@gmail.com> * 🧹 fix typo --------- Signed-off-by: Dominik Richter <dominik.richter@gmail.com> Co-authored-by: Christoph Hartmann <chris@lollyrock.com>
1 parent 7365c9a commit c47af12

File tree

6 files changed

+326
-0
lines changed

6 files changed

+326
-0
lines changed

providers-sdk/v1/testutils/testdata/arch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,35 @@
10891089
}
10901090
}
10911091
},
1092+
{
1093+
"Resource": "file",
1094+
"ID": "/etc/audit/auditd.conf",
1095+
"Fields": {
1096+
"content": {
1097+
"type": "\u0007",
1098+
"value": "#\n# This file controls the configuration of the audit daemon\n#\n\nlocal_events = yes\nwrite_logs = yes\nLOG_FILE = /var/log/audit/AuDiT.log\nlog_group = root\nLOG_FORMAT = ENRICHED\nflush = INCREMENTAL_ASYNC\nfreq = 50\nmax_log_file = 8\nnum_logs = 5\npriority_boost = 4\nname_format = NONE\n##name = mydomain\nmax_log_file_action = ROTATE\nspace_left = 75\nspace_left_action = SYSLOG\nverify_email = yes\naction_mail_acct = root\nadmin_space_left = 50\nadmin_space_left_action = SUSPEND\ndisk_full_action = SUSPEND\ndisk_error_action = SUSPEND\nuse_libwrap = yes\n##tcp_listen_port = 60\ntcp_listen_queue = 5\ntcp_max_per_addr = 1\n##tcp_client_ports = 1024-65535\ntcp_client_max_idle = 0\ntransport = TCP\nkrb5_principal = auditd\n##krb5_key_file = /etc/audit/audit.key\ndistribute_network = no\nq_depth = 2000\noverflow_action = SYSLOG\nmax_restarts = 10\nplugin_dir = /etc/audit/plugins.d\nend_of_event_timeout = 2\n"
1099+
},
1100+
"exists": {
1101+
"type": "\u0004",
1102+
"value": true
1103+
},
1104+
"path": {
1105+
"type": "\u0007",
1106+
"value": "/etc/audit/auditd.conf"
1107+
},
1108+
"permissions": {
1109+
"type": "\u001bfile.permissions",
1110+
"value": {
1111+
"Name": "file.permissions",
1112+
"ID": "-rw-r--r--"
1113+
}
1114+
},
1115+
"size": {
1116+
"type": "\u0005",
1117+
"value": 882
1118+
}
1119+
}
1120+
},
10921121
{
10931122
"Resource": "user",
10941123
"ID": "user/0/root",

providers/os/resources/auditd.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// copyright: 2019, Dominik Richter and Christoph Hartmann
2+
// author: Dominik Richter
3+
// author: Christoph Hartmann
4+
5+
package resources
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
"slices"
11+
"strings"
12+
"sync"
13+
14+
"go.mondoo.com/cnquery/v11/llx"
15+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
16+
"go.mondoo.com/cnquery/v11/providers/os/resources/parsers"
17+
"go.mondoo.com/cnquery/v11/utils/multierr"
18+
)
19+
20+
type mqlAuditdConfigInternal struct {
21+
lock sync.Mutex
22+
}
23+
24+
func initAuditdConfig(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
25+
if x, ok := args["path"]; ok {
26+
path, ok := x.Value.(string)
27+
if !ok {
28+
return nil, nil, errors.New("wrong type for 'path' in auditd.config initialization, it must be a string")
29+
}
30+
31+
f, err := CreateResource(runtime, "file", map[string]*llx.RawData{
32+
"path": llx.StringData(path),
33+
})
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
args["file"] = llx.ResourceData(f, "file")
38+
39+
delete(args, "path")
40+
}
41+
42+
return args, nil, nil
43+
}
44+
45+
const defaultAuditdConfig = "/etc/audit/auditd.conf"
46+
47+
func (s *mqlAuditdConfig) id() (string, error) {
48+
file := s.GetFile()
49+
if file.Error != nil {
50+
return "", file.Error
51+
}
52+
53+
return file.Data.Path.Data, nil
54+
}
55+
56+
func (s *mqlAuditdConfig) file() (*mqlFile, error) {
57+
f, err := CreateResource(s.MqlRuntime, "file", map[string]*llx.RawData{
58+
"path": llx.StringData(defaultAuditdConfig),
59+
})
60+
if err != nil {
61+
return nil, err
62+
}
63+
return f.(*mqlFile), nil
64+
}
65+
66+
func (s *mqlAuditdConfig) parse(file *mqlFile) error {
67+
s.lock.Lock()
68+
defer s.lock.Unlock()
69+
70+
if file == nil {
71+
return errors.New("no base auditd config file to read")
72+
}
73+
74+
content := file.GetContent()
75+
if content.Error != nil {
76+
return content.Error
77+
}
78+
79+
ini := parsers.ParseIni(content.Data, "=")
80+
81+
res := make(map[string]any, len(ini.Fields))
82+
s.Params.Data = res
83+
s.Params.State = plugin.StateIsSet
84+
85+
if len(ini.Fields) == 0 {
86+
return nil
87+
}
88+
89+
root := ini.Fields[""]
90+
if root == nil {
91+
s.Params.Error = errors.New("failed to parse auditd config")
92+
return s.Params.Error
93+
}
94+
95+
fields, ok := root.(map[string]any)
96+
if !ok {
97+
s.Params.Error = errors.New("failed to parse auditd config (invalid data retrieved)")
98+
return s.Params.Error
99+
}
100+
101+
var errs multierr.Errors
102+
for k, v := range fields {
103+
key := strings.ToLower(k)
104+
if s, ok := v.(string); ok {
105+
if slices.Contains(auditdDowncaseKeywords, key) {
106+
res[key] = strings.ToLower(s)
107+
} else {
108+
res[key] = s
109+
}
110+
} else {
111+
errs.Add(fmt.Errorf("can't parse field '"+s+"', value is %+v", v))
112+
}
113+
}
114+
115+
s.Params.Error = errs.Deduplicate()
116+
return s.Params.Error
117+
}
118+
119+
func (s *mqlAuditdConfig) params(file *mqlFile) (map[string]any, error) {
120+
return nil, s.parse(file)
121+
}
122+
123+
var auditdDowncaseKeywords = []string{
124+
"local_events",
125+
"write_logs",
126+
"log_format",
127+
"flush",
128+
"max_log_file_action",
129+
"verify_email",
130+
"space_left_action",
131+
"admin_space_left_action",
132+
"disk_full_action",
133+
"disk_error_action",
134+
"use_libwrap",
135+
"enable_krb5",
136+
"overflow_action",
137+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package resources_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/testutils"
11+
)
12+
13+
func TestResource_AuditdConfig(t *testing.T) {
14+
x.TestSimpleErrors(t, []testutils.SimpleTest{
15+
{
16+
Code: "auditd.config('nopath').params",
17+
ResultIndex: 0,
18+
Expectation: "file 'nopath' not found",
19+
},
20+
})
21+
22+
t.Run("auditd file path", func(t *testing.T) {
23+
res := x.TestQuery(t, "auditd.config.file.path")
24+
assert.NotEmpty(t, res)
25+
assert.NoError(t, res[0].Data.Error)
26+
})
27+
28+
t.Run("auditd params", func(t *testing.T) {
29+
res := x.TestQuery(t, "auditd.config.params")
30+
assert.NotEmpty(t, res)
31+
assert.NoError(t, res[0].Data.Error)
32+
})
33+
34+
t.Run("auditd is downcasing relevant params", func(t *testing.T) {
35+
res := x.TestQuery(t, "auditd.config.params.log_format")
36+
assert.NotEmpty(t, res)
37+
assert.NoError(t, res[0].Data.Error)
38+
assert.Equal(t, "enriched", res[0].Data.Value)
39+
})
40+
41+
t.Run("auditd is NOT downcasing other params", func(t *testing.T) {
42+
res := x.TestQuery(t, "auditd.config.params.log_file")
43+
assert.NotEmpty(t, res)
44+
assert.NoError(t, res[0].Data.Error)
45+
assert.Equal(t, "/var/log/audit/AuDiT.log", res[0].Data.Value)
46+
})
47+
}

providers/os/resources/os.lr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@ private sshd.config.matchBlock @defaults("criteria") @context("file.context") {
722722
params map[string]string
723723
}
724724

725+
auditd.config {
726+
init(path? string)
727+
// File of this Auditd configuration
728+
file() file
729+
// Configuration values of this config
730+
params(file) map[string]string
731+
}
732+
725733
// Service on this system
726734
service @defaults("name running enabled type") {
727735
init(name string)

providers/os/resources/os.lr.go

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

providers/os/resources/os.lr.manifest.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ resources:
4545
vector: {}
4646
is_private: true
4747
min_mondoo_version: 5.15.0
48+
auditd.conf:
49+
fields:
50+
file: {}
51+
params: {}
52+
min_mondoo_version: 9.0.0
53+
auditd.config:
54+
fields:
55+
file: {}
56+
params: {}
57+
min_mondoo_version: 9.0.0
4858
auditpol:
4959
fields:
5060
list:

0 commit comments

Comments
 (0)