config: not properly loaded error
When a wrong config file was used, it result in a panic. Adding some check condition to validate the Unmarshaled configuration before using it fixes #134
This commit is contained in:
parent
8d5a330acf
commit
34e99466a6
@ -19,6 +19,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
cerrors "github.com/coreos/clair/utils/errors"
|
||||
"github.com/fernet/fernet-go"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
@ -111,7 +112,10 @@ func Load(path string) (config *Config, err error) {
|
||||
return
|
||||
}
|
||||
config = &cfgFile.Clair
|
||||
|
||||
if config.API == nil || config.Database == nil || config.Notifier == nil || config.Updater == nil {
|
||||
err = cerrors.ErrConfigNotLoaded
|
||||
return
|
||||
}
|
||||
// Generate a pagination key if none is provided.
|
||||
if config.API.PaginationKey == "" {
|
||||
var key fernet.Key
|
||||
|
41
config/config_test.go
Normal file
41
config/config_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
cerrors "github.com/coreos/clair/utils/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const wrongConfig = `
|
||||
dummyKey:
|
||||
wrong:true
|
||||
`
|
||||
|
||||
func TestLoadWrongConfiguration(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "clair-config")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
|
||||
if _, err := tmpfile.Write([]byte(wrongConfig)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = Load(tmpfile.Name())
|
||||
|
||||
assert.EqualError(t, err, cerrors.ErrConfigNotLoaded.Error())
|
||||
}
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
_, err := Load("../config.example.yaml")
|
||||
assert.NoError(t, err)
|
||||
}
|
@ -29,6 +29,9 @@ var (
|
||||
|
||||
// ErrCouldNotParse is returned when a fetcher fails to parse the update data.
|
||||
ErrCouldNotParse = errors.New("updater/fetchers: could not parse")
|
||||
|
||||
//ErrConfigNotLoaded is returned when the configuration file is not loaded properly
|
||||
ErrConfigNotLoaded = errors.New("could not load configuration properly")
|
||||
)
|
||||
|
||||
// ErrBadRequest occurs when a method has been passed an inappropriate argument.
|
||||
|
Loading…
Reference in New Issue
Block a user