Skip to content

Commit

Permalink
fix(report): prioritize env vars over config.toml (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotakanbe committed Mar 9, 2021
1 parent 54e73c2 commit 5d47adb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
17 changes: 12 additions & 5 deletions config/azureconf.go
Expand Up @@ -20,18 +20,25 @@ type AzureConf struct {
Enabled bool `toml:"-" json:"-"`
}

const (
azureAccount = "AZURE_STORAGE_ACCOUNT"
azureKey = "AZURE_STORAGE_ACCESS_KEY"
)

// Validate configuration
func (c *AzureConf) Validate() (errs []error) {
if !c.Enabled {
return
}
if c.AccountName == "" {
c.AccountName = os.Getenv("AZURE_STORAGE_ACCOUNT")
}

if c.AccountKey == "" {
c.AccountKey = os.Getenv("AZURE_STORAGE_ACCESS_KEY")
// overwrite if env var is not empty
if os.Getenv(azureAccount) != "" {
c.AccountName = os.Getenv(azureAccount)
}
if os.Getenv(azureKey) != "" {
c.AccountKey = os.Getenv(azureKey)
}

if c.ContainerName == "" {
errs = append(errs, xerrors.Errorf("Azure storage container name is required"))
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Expand Up @@ -134,7 +134,7 @@ func (c Config) checkSSHKeyExist() (errs []error) {
}

// ValidateOnReport validates configuration
func (c Config) ValidateOnReport() bool {
func (c *Config) ValidateOnReport() bool {
errs := []error{}

if len(c.ResultsDir) != 0 {
Expand Down
21 changes: 7 additions & 14 deletions config/httpconf.go
Expand Up @@ -12,28 +12,21 @@ type HTTPConf struct {
Enabled bool `toml:"-" json:"-"`
}

const httpKey = "VULS_HTTP_URL"

// Validate validates configuration
func (c *HTTPConf) Validate() (errs []error) {
if !c.Enabled {
return nil
}

if _, err := govalidator.ValidateStruct(c); err != nil {
errs = append(errs, err)
}
return errs
}

const httpKey = "VULS_HTTP_URL"

// Init set options with the following priority.
// 1. Environment variable
// 2. config.toml
func (c *HTTPConf) Init(url string) {
// overwrite if env var is not empty
if os.Getenv(httpKey) != "" {
c.URL = os.Getenv(httpKey)
}
if url != "" {
c.URL = url

if _, err := govalidator.ValidateStruct(c); err != nil {
errs = append(errs, err)
}
return errs
}
4 changes: 2 additions & 2 deletions reporter/http.go
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"net/http"

c "github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
"golang.org/x/xerrors"
)
Expand All @@ -20,7 +20,7 @@ func (w HTTPRequestWriter) Write(rs ...models.ScanResult) (err error) {
if err := json.NewEncoder(b).Encode(r); err != nil {
return err
}
_, err = http.Post(c.Conf.HTTP.URL, "application/json; charset=utf-8", b)
_, err = http.Post(config.Conf.HTTP.URL, "application/json; charset=utf-8", b)
if err != nil {
return err
}
Expand Down
7 changes: 0 additions & 7 deletions subcmds/report.go
Expand Up @@ -39,8 +39,6 @@ type ReportCmd struct {
toS3 bool
toAzureBlob bool
toHTTP bool

toHTTPURL string
}

// Name return subcommand name
Expand Down Expand Up @@ -162,8 +160,6 @@ func (p *ReportCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.gzip, "gzip", false, "gzip compression")
f.BoolVar(&c.Conf.Pipe, "pipe", false, "Use args passed via PIPE")

f.StringVar(&p.toHTTPURL, "http", "", "-to-http http://vuls-report")

f.StringVar(&c.Conf.TrivyCacheDBDir, "trivy-cachedb-dir",
utils.DefaultCacheDir(), "/path/to/dir")
}
Expand All @@ -186,9 +182,6 @@ func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
c.Conf.Azure.Enabled = p.toAzureBlob
c.Conf.HTTP.Enabled = p.toHTTP

//TODO refactor
c.Conf.HTTP.Init(p.toHTTPURL)

if c.Conf.Diff {
c.Conf.DiffPlus, c.Conf.DiffMinus = true, true
}
Expand Down

0 comments on commit 5d47adb

Please sign in to comment.