mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
Convert the global settings to correct type when reading them from file (#399)
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
parent
e680875ea1
commit
186dec7b26
2 changed files with 49 additions and 2 deletions
18
config.go
18
config.go
|
@ -38,6 +38,22 @@ func NewConfig() Config {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Config) keyToGlobalOptions(key string) GlobalOption {
|
||||||
|
return GlobalOption(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) convertGlobals() {
|
||||||
|
if globals, ok := c[Globals]; ok {
|
||||||
|
if settings, ok := globals.(map[string]interface{}); ok {
|
||||||
|
validGlobals := map[GlobalOption]string{}
|
||||||
|
for k, v := range settings {
|
||||||
|
validGlobals[c.keyToGlobalOptions(k)] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
c[Globals] = validGlobals
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ReadFrom implements the io.ReaderFrom interface. This
|
// ReadFrom implements the io.ReaderFrom interface. This
|
||||||
// should be used with io.Reader to load configuration from
|
// should be used with io.Reader to load configuration from
|
||||||
//file or from string etc.
|
//file or from string etc.
|
||||||
|
@ -49,6 +65,7 @@ func (c Config) ReadFrom(r io.Reader) (int64, error) {
|
||||||
if err = json.Unmarshal(data, &c); err != nil {
|
if err = json.Unmarshal(data, &c); err != nil {
|
||||||
return int64(len(data)), err
|
return int64(len(data)), err
|
||||||
}
|
}
|
||||||
|
c.convertGlobals()
|
||||||
return int64(len(data)), nil
|
return int64(len(data)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +104,6 @@ func (c Config) GetGlobal(option GlobalOption) (string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("no global config options found")
|
return "", fmt.Errorf("no global config options found")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetGlobal associates a value with a global configuration option
|
// SetGlobal associates a value with a global configuration option
|
||||||
|
|
|
@ -2,6 +2,7 @@ package gosec_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
@ -106,6 +107,36 @@ var _ = Describe("Configuration", func() {
|
||||||
Expect(err).Should(BeNil())
|
Expect(err).Should(BeNil())
|
||||||
Expect(enabled).Should(BeTrue())
|
Expect(enabled).Should(BeTrue())
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
|
It("should parse the global settings of type string from file", func() {
|
||||||
|
config := `
|
||||||
|
{
|
||||||
|
"global": {
|
||||||
|
"nosec": "enabled"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
cfg := gosec.NewConfig()
|
||||||
|
_, err := cfg.ReadFrom(strings.NewReader(config))
|
||||||
|
Expect(err).Should(BeNil())
|
||||||
|
|
||||||
|
value, err := cfg.GetGlobal(gosec.Nosec)
|
||||||
|
Expect(err).Should(BeNil())
|
||||||
|
Expect(value).Should(Equal("enabled"))
|
||||||
|
})
|
||||||
|
It("should parse the global settings of other types from file", func() {
|
||||||
|
config := `
|
||||||
|
{
|
||||||
|
"global": {
|
||||||
|
"nosec": true
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
cfg := gosec.NewConfig()
|
||||||
|
_, err := cfg.ReadFrom(strings.NewReader(config))
|
||||||
|
Expect(err).Should(BeNil())
|
||||||
|
|
||||||
|
value, err := cfg.GetGlobal(gosec.Nosec)
|
||||||
|
Expect(err).Should(BeNil())
|
||||||
|
Expect(value).Should(Equal("true"))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue