go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/rsyslog.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package resources 5 6 import ( 7 "errors" 8 "strings" 9 10 "go.mondoo.com/cnquery/checksums" 11 "go.mondoo.com/cnquery/llx" 12 "go.mondoo.com/cnquery/providers-sdk/v1/resources" 13 ) 14 15 const defaultRsyslogConf = "/etc/rsyslog.conf" 16 17 func (s *mqlRsyslogConf) id() (string, error) { 18 files := s.GetFiles() 19 if files.Error != nil { 20 return "", files.Error 21 } 22 23 checksum := checksums.New 24 for i := range files.Data { 25 path := files.Data[i].(*mqlFile).Path.Data 26 checksum = checksum.Add(path) 27 } 28 29 return checksum.String(), nil 30 } 31 32 func (s *mqlRsyslogConf) path() (string, error) { 33 return defaultRsyslogConf, nil 34 } 35 36 func (s *mqlRsyslogConf) files(path string) ([]interface{}, error) { 37 if !strings.HasSuffix(path, ".conf") { 38 return nil, errors.New("failed to initialize, path must end in `.conf` so we can find files in `.d` directory") 39 } 40 41 f, err := CreateResource(s.MqlRuntime, "file", map[string]*llx.RawData{ 42 "path": llx.StringData(path), 43 }) 44 if err != nil { 45 return nil, err 46 } 47 48 confD := path[0:len(path)-5] + ".d" 49 o, err := CreateResource(s.MqlRuntime, "files.find", map[string]*llx.RawData{ 50 "from": llx.StringData(confD), 51 "type": llx.StringData("file"), 52 }) 53 if err != nil { 54 return nil, err 55 } 56 57 list := o.(*mqlFilesFind).GetList() 58 if list.Error != nil { 59 return nil, list.Error 60 } 61 62 return append([]interface{}{f.(*mqlFile)}, list.Data...), nil 63 } 64 65 func (s *mqlRsyslogConf) content(files []interface{}) (string, error) { 66 var res strings.Builder 67 68 // TODO: this can be heavily improved once we do it right, since this is constantly 69 // re-registered as the file changes 70 for i := range files { 71 file := files[i].(*mqlFile) 72 content := file.GetContent() 73 if content.Error != nil { 74 if errors.Is(content.Error, resources.NotFoundError{}) { 75 continue 76 } 77 } 78 79 res.WriteString(content.Data) 80 res.WriteString("\n") 81 } 82 83 return res.String(), nil 84 } 85 86 func (s *mqlRsyslogConf) settings(content string) ([]interface{}, error) { 87 lines := strings.Split(content, "\n") 88 89 settings := []interface{}{} 90 var line string 91 for i := range lines { 92 line = lines[i] 93 if idx := strings.Index(line, "#"); idx >= 0 { 94 line = line[0:idx] 95 } 96 line = strings.Trim(line, " \t\r") 97 98 if line != "" { 99 settings = append(settings, line) 100 } 101 } 102 103 return settings, nil 104 }