2015-12-07 21:38:50 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-12-10 21:46:43 +00:00
|
|
|
package config
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2016-02-05 21:22:17 +00:00
|
|
|
"github.com/fernet/fernet-go"
|
2015-12-07 21:38:50 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2016-03-09 19:50:08 +00:00
|
|
|
// File represents a YAML configuration file that namespaces all Clair
|
|
|
|
// configuration under the top-level "clair" key.
|
|
|
|
type File struct {
|
|
|
|
Clair Config `yaml:"clair"`
|
|
|
|
}
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
// Config is the global configuration for an instance of Clair.
|
|
|
|
type Config struct {
|
|
|
|
Database *DatabaseConfig
|
|
|
|
Updater *UpdaterConfig
|
|
|
|
Notifier *NotifierConfig
|
|
|
|
API *APIConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseConfig is the configuration used to specify how Clair connects
|
|
|
|
// to a database.
|
|
|
|
type DatabaseConfig struct {
|
2015-12-28 20:03:29 +00:00
|
|
|
Source string
|
|
|
|
CacheSize int
|
2015-12-07 21:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdaterConfig is the configuration for the Updater service.
|
|
|
|
type UpdaterConfig struct {
|
|
|
|
Interval time.Duration
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:36:06 +00:00
|
|
|
// NotifierConfig is the configuration for the Notifier service and its registered notifiers.
|
2015-12-07 21:38:50 +00:00
|
|
|
type NotifierConfig struct {
|
2016-02-05 21:22:17 +00:00
|
|
|
Attempts int
|
|
|
|
RenotifyInterval time.Duration
|
|
|
|
Params map[string]interface{} `yaml:",inline"`
|
2015-12-07 21:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// APIConfig is the configuration for the API service.
|
|
|
|
type APIConfig struct {
|
|
|
|
Port int
|
|
|
|
HealthPort int
|
|
|
|
Timeout time.Duration
|
2016-02-05 21:22:17 +00:00
|
|
|
PaginationKey string
|
2015-12-07 21:38:50 +00:00
|
|
|
CertFile, KeyFile, CAFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig is a configuration that can be used as a fallback value.
|
2016-03-17 18:52:41 +00:00
|
|
|
func DefaultConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Database: &DatabaseConfig{
|
|
|
|
CacheSize: 16384,
|
|
|
|
},
|
|
|
|
Updater: &UpdaterConfig{
|
|
|
|
Interval: 1 * time.Hour,
|
|
|
|
},
|
|
|
|
API: &APIConfig{
|
|
|
|
Port: 6060,
|
|
|
|
HealthPort: 6061,
|
|
|
|
Timeout: 900 * time.Second,
|
|
|
|
},
|
|
|
|
Notifier: &NotifierConfig{
|
|
|
|
Attempts: 5,
|
|
|
|
RenotifyInterval: 2 * time.Hour,
|
|
|
|
},
|
|
|
|
}
|
2015-12-07 21:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load is a shortcut to open a file, read it, and generate a Config.
|
|
|
|
// It supports relative and absolute paths. Given "", it returns DefaultConfig.
|
|
|
|
func Load(path string) (config *Config, err error) {
|
2016-03-17 18:52:41 +00:00
|
|
|
var cfgFile File
|
|
|
|
cfgFile.Clair = DefaultConfig()
|
2015-12-07 21:38:50 +00:00
|
|
|
if path == "" {
|
2016-03-18 20:06:53 +00:00
|
|
|
return &cfgFile.Clair, nil
|
2015-12-07 21:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(os.ExpandEnv(path))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
d, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-09 19:50:08 +00:00
|
|
|
err = yaml.Unmarshal(d, &cfgFile)
|
2016-02-05 21:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-03-09 19:50:08 +00:00
|
|
|
config = &cfgFile.Clair
|
2016-02-05 21:22:17 +00:00
|
|
|
|
2016-03-09 19:50:08 +00:00
|
|
|
// Generate a pagination key if none is provided.
|
2016-02-05 21:22:17 +00:00
|
|
|
if config.API.PaginationKey == "" {
|
|
|
|
var key fernet.Key
|
|
|
|
if err = key.Generate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config.API.PaginationKey = key.Encode()
|
|
|
|
} else {
|
|
|
|
_, err = fernet.DecodeKey(config.API.PaginationKey)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
return
|
|
|
|
}
|