| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package auditor |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "io/ioutil" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "sort" |
| 26 | "strings" |
| 27 | |
| 28 | "gopkg.in/yaml.v3" |
| 29 | |
| 30 | "github.com/sassoftware/relic/v7/config" |
| 31 | ) |
| 32 | |
| 33 | type AuditConfig struct { |
| 34 | ConfigDir string |
| 35 | DatabaseURI string |
| 36 | LogFile string |
| 37 | GraylogURL string |
| 38 | } |
| 39 | |
| 40 | var auditConfig AuditConfig |
| 41 | |
| 42 | func readConfig() error { |
| 43 | if argConfigFile == "" { |
| 44 | return errors.New("--config is required") |
| 45 | } |
| 46 | blob, err := ioutil.ReadFile(argConfigFile) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | if err := yaml.Unmarshal(blob, &auditConfig); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | if auditConfig.ConfigDir == "" { |
| 54 | return errors.New("ConfigDir must be set in configuration file") |
| 55 | } |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | func getServerConfs() ([]*config.Config, error) { |
| 60 | dir, err := os.Open(auditConfig.ConfigDir) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | defer dir.Close() |
| 65 | names, err := dir.Readdirnames(-1) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | sort.Strings(names) |
| 70 | var confs []*config.Config |
| 71 | for _, name := range names { |
| 72 | if strings.HasSuffix(name, ".yml") { |
| 73 | cpath := filepath.Join(auditConfig.ConfigDir, name) |
| 74 | cfg, err := config.ReadFile(cpath) |
| 75 | if err != nil { |
| 76 | return nil, fmt.Errorf("%s: %w", cpath, err) |
| 77 | } |
| 78 | if cfg.Amqp == nil || cfg.Amqp.URL == "" { |
| 79 | return nil, fmt.Errorf("%s has no amqp server", cpath) |
| 80 | } |
| 81 | confs = append(confs, cfg) |
| 82 | } |
| 83 | } |
| 84 | return confs, nil |
| 85 | } |