2018-07-19 17:42:25 +01:00
|
|
|
package gosec
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2017-12-14 00:04:22 +00:00
|
|
|
const (
|
|
|
|
// Globals are applicable to all rules and used for general
|
2018-07-19 17:42:25 +01:00
|
|
|
// configuration settings for gosec.
|
2017-12-14 00:04:22 +00:00
|
|
|
Globals = "global"
|
|
|
|
)
|
|
|
|
|
2019-01-14 11:37:40 +00:00
|
|
|
// GlobalOption defines the name of the global options
|
|
|
|
type GlobalOption string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Nosec global option for #nosec directive
|
|
|
|
Nosec GlobalOption = "nosec"
|
|
|
|
// Audit global option which indicates that gosec runs in audit mode
|
|
|
|
Audit GlobalOption = "audit"
|
2019-09-04 09:20:43 +01:00
|
|
|
// NoSecAlternative global option alternative for #nosec directive
|
|
|
|
NoSecAlternative GlobalOption = "#nosec"
|
2019-01-14 11:37:40 +00:00
|
|
|
)
|
|
|
|
|
2017-04-28 22:46:26 +01:00
|
|
|
// Config is used to provide configuration and customization to each of the rules.
|
|
|
|
type Config map[string]interface{}
|
|
|
|
|
|
|
|
// NewConfig initializes a new configuration instance. The configuration data then
|
|
|
|
// needs to be loaded via c.ReadFrom(strings.NewReader("config data"))
|
|
|
|
// or from a *os.File.
|
|
|
|
func NewConfig() Config {
|
2017-07-19 22:17:00 +01:00
|
|
|
cfg := make(Config)
|
2019-01-14 11:37:40 +00:00
|
|
|
cfg[Globals] = make(map[GlobalOption]string)
|
2017-07-19 22:17:00 +01:00
|
|
|
return cfg
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the io.ReaderFrom interface. This
|
|
|
|
// should be used with io.Reader to load configuration from
|
|
|
|
//file or from string etc.
|
|
|
|
func (c Config) ReadFrom(r io.Reader) (int64, error) {
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return int64(len(data)), err
|
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
if err = json.Unmarshal(data, &c); err != nil {
|
2017-04-28 22:46:26 +01:00
|
|
|
return int64(len(data)), err
|
|
|
|
}
|
|
|
|
return int64(len(data)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements the io.WriteTo interface. This should
|
|
|
|
// be used to save or print out the configuration information.
|
|
|
|
func (c Config) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return int64(len(data)), err
|
|
|
|
}
|
|
|
|
return io.Copy(w, bytes.NewReader(data))
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// Get returns the configuration section for the supplied key
|
|
|
|
func (c Config) Get(section string) (interface{}, error) {
|
|
|
|
settings, found := c[section]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("Section %s not in configuration", section)
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
return settings, nil
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// Set section in the configuration to specified value
|
|
|
|
func (c Config) Set(section string, value interface{}) {
|
|
|
|
c[section] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGlobal returns value associated with global configuration option
|
2019-01-14 11:37:40 +00:00
|
|
|
func (c Config) GetGlobal(option GlobalOption) (string, error) {
|
2017-12-14 00:04:22 +00:00
|
|
|
if globals, ok := c[Globals]; ok {
|
2019-01-14 11:37:40 +00:00
|
|
|
if settings, ok := globals.(map[GlobalOption]string); ok {
|
2017-07-19 22:17:00 +01:00
|
|
|
if value, ok := settings[option]; ok {
|
|
|
|
return value, nil
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
return "", fmt.Errorf("global setting for %s not found", option)
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
return "", fmt.Errorf("no global config options found")
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-11 13:45:31 +01:00
|
|
|
// SetGlobal associates a value with a global configuration option
|
2019-01-14 11:37:40 +00:00
|
|
|
func (c Config) SetGlobal(option GlobalOption, value string) {
|
2017-12-14 00:04:22 +00:00
|
|
|
if globals, ok := c[Globals]; ok {
|
2019-01-14 11:37:40 +00:00
|
|
|
if settings, ok := globals.(map[GlobalOption]string); ok {
|
2017-07-19 22:17:00 +01:00
|
|
|
settings[option] = value
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2019-01-14 11:37:40 +00:00
|
|
|
|
|
|
|
// IsGlobalEnabled checks if a global option is enabled
|
|
|
|
func (c Config) IsGlobalEnabled(option GlobalOption) (bool, error) {
|
|
|
|
value, err := c.GetGlobal(option)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return (value == "true" || value == "enabled"), nil
|
|
|
|
}
|