...

Source file src/github.com/sassoftware/relic/cmdline/auditor/config.go

Documentation: github.com/sassoftware/relic/cmdline/auditor

     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  	"github.com/sassoftware/relic/config"
    29  	"gopkg.in/yaml.v2"
    30  )
    31  
    32  type AuditConfig struct {
    33  	ConfigDir   string
    34  	DatabaseURI string
    35  	LogFile     string
    36  	GraylogURL  string
    37  }
    38  
    39  var auditConfig AuditConfig
    40  
    41  func readConfig() error {
    42  	if argConfigFile == "" {
    43  		return errors.New("--config is required")
    44  	}
    45  	blob, err := ioutil.ReadFile(argConfigFile)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	if err := yaml.Unmarshal(blob, &auditConfig); err != nil {
    50  		return err
    51  	}
    52  	if auditConfig.ConfigDir == "" {
    53  		return errors.New("ConfigDir must be set in configuration file")
    54  	}
    55  	return nil
    56  }
    57  
    58  func getServerConfs() ([]*config.Config, error) {
    59  	dir, err := os.Open(auditConfig.ConfigDir)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	defer dir.Close()
    64  	names, err := dir.Readdirnames(-1)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	sort.Strings(names)
    69  	var confs []*config.Config
    70  	for _, name := range names {
    71  		if strings.HasSuffix(name, ".yml") {
    72  			cpath := filepath.Join(auditConfig.ConfigDir, name)
    73  			cfg, err := config.ReadFile(cpath)
    74  			if err != nil {
    75  				return nil, fmt.Errorf("%s: %s", cpath, err)
    76  			}
    77  			if cfg.Amqp == nil || cfg.Amqp.URL == "" {
    78  				return nil, fmt.Errorf("%s has no amqp server", cpath)
    79  			}
    80  			confs = append(confs, cfg)
    81  		}
    82  	}
    83  	return confs, nil
    84  }
    85  

View as plain text