diff --git a/config.go b/config.go index 261c306..2ffe4ec 100644 --- a/config.go +++ b/config.go @@ -1,209 +1,38 @@ -package config +package sirenaConfig import ( - "fmt" "io/ioutil" - "log" "os" - "path/filepath" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/imdario/mergo" - yaml "gopkg.in/yaml.v2" ) -// Config is a main Sirena agent config -type Config struct { - LogLevel string `yaml:"log_level,omitempty"` - Addr string `yaml:"addr,omitempty"` - TrackXML bool `yaml:"track_xml,omitempty"` - SirenaClientID string `yaml:"sirena_client_id,omitempty"` - SirenaHost string `yaml:"sirena_host,omitempty"` - SirenaPort string `yaml:"sirena_port,omitempty"` - ClientPublicKey string `yaml:"client_public_key,omitempty"` - ClientPrivateKey string `yaml:"client_private_key,omitempty"` - ClientPrivateKeyPassword string `yaml:"client_private_key_password,omitempty"` - ServerPublicKey string `yaml:"server_public_key,omitempty"` - RedisHost string `yaml:"redis_host,omitempty"` - RedisPort string `yaml:"redis_port,omitempty"` - RedisPassword string `yaml:"redis_password,omitempty"` - RedisDB int `yaml:"redis_db,omitempty"` - SirenaProxyURL string `yaml:"sirena_proxy_url,omitempty"` - PubSubConfig PubSubConfig `yaml:"pubsub,omitempty"` - FormatsConfig FormatsConfig `yaml:"formats,omitempty"` - SentryEnable bool `yaml:"sentry_enable,omitempty"` - SentryDSN string `yaml:"sentry_dsn,omitempty"` -} - -// PubSubConfig is a config for external pub/sub logging -type PubSubConfig struct { - Host string `yaml:"host"` - ProjectID string `yaml:"project_id"` - Topic string `yaml:"topic"` - Subscription string `yaml:"subscription"` - Key string `yaml:"key"` - Timeout time.Duration `yaml:"timeout"` -} - -// formatsConfig is a config with date/time formats for external pub/sub logging -type FormatsConfig struct { - Time string `yaml:"time"` - Date string `yaml:"date"` - XmlDate string `yaml:"xml_date"` -} - -// CNFG is a Config singletone -var CNFG *Config - -func init() { - CNFG = loadConfig() -} - -// Get returns config -func Get() *Config { - return CNFG -} - -// getEnv return env variable or default value provided -func getEnv(name, defaultVal string) string { - val := os.Getenv(name) - if val != "" { - return val - } - return defaultVal -} - -// loadConfig loads config from YAML files -func loadConfig() *Config { - configPath := getEnv("CONFIG_PATH", "config") - stage := getEnv("STAGE", "development") - - fmt.Printf("Config path: %s\n", configPath) - fmt.Printf("Stage: %s\n", stage) - - yamlFileList := []string{} - err := filepath.Walk(configPath, func(path string, f os.FileInfo, err error) error { - if err != nil { - fmt.Printf("Error accessing a path %q: %v\n", configPath, err) - return nil - } - if f.IsDir() { - return nil - } - if filepath.Ext(path) != ".yaml" { - return nil - } - fmt.Printf("Found YAML file %s\n", path) - yamlFileList = append(yamlFileList, path) - return nil - }) - if err != nil { - log.Fatal(err) - } - - loadedConfigs := map[string]Config{} - for _, yamlFilePath := range yamlFileList { - fmt.Printf("Processing YAML file %s\n", yamlFilePath) - yamlFileBytes, err := ioutil.ReadFile(yamlFilePath) - fmt.Printf("%s contents:\n%s\n", yamlFilePath, yamlFileBytes) - if err != nil { - log.Fatal(err) - } - fileConfig := map[string]Config{} - err = yaml.Unmarshal(yamlFileBytes, fileConfig) - if err != nil { - log.Fatal(err) - } - mergo.Merge(&loadedConfigs, fileConfig) - } - - fmt.Println("Loaded configs:") - spew.Dump(loadedConfigs) - - _, stageExists := loadedConfigs[stage] - defaultConfig, defaultExists := loadedConfigs["defaults"] - if !stageExists { - fmt.Printf("Stage %s doesn't exist. Using default config", stage) - if !defaultExists { - panic(`No "defaults" config found`) - } - return &defaultConfig - } - CONFIG := defaultConfig - mergo.Merge(&CONFIG, loadedConfigs[stage], mergo.WithOverride) - - return &CONFIG +type SirenaConfig struct { + ClientID string `yaml:"client_id,omitempty"` + Host string `yaml:"host,omitempty"` + Port string `yaml:"port,omitempty"` + ClientPublicKey string `yaml:"client_public_key,omitempty"` + ClientPrivateKey string `yaml:"client_private_key,omitempty"` + ClientPrivateKeyPassword string `yaml:"client_private_key_password,omitempty"` + ServerPublicKey string `yaml:"server_public_key,omitempty"` + KeysPath string `yaml:"key_path"` } // GetSirenaAddr return sirena address to connect client to -func (config *Config) GetSirenaAddr() string { +func (config *SirenaConfig) GetSirenaAddr() string { if config == nil { return "" } - if config.SirenaPort == "" { - return config.SirenaHost + if config.Port == "" { + return config.Host } - return config.SirenaHost + ":" + config.SirenaPort -} - -// KeyDirs is a list of directories to search Sirena key files in -var KeyDirs = []string{ - os.Getenv("GOPATH"), - binaryDir() + "/keys", - pwdDir() + "/sirena-agent-go/keys", - pwdDir() + "/sirena-proxy/keys", + return config.Host + ":" + config.Port } // GetKeyFile returns contents of key file -func (config *Config) GetKeyFile(keyFile string) ([]byte, error) { - for _, keyDir := range KeyDirs { - exists, err := pathExists(keyDir + "/" + keyFile) - if err != nil { - log.Print(err) - } - if !exists { - continue - } - return ioutil.ReadFile(keyDir + "/" + keyFile) - } - return nil, fmt.Errorf("No key file %s found", keyFile) -} - -// binaryDir returns path where binary was run from -func binaryDir() string { - ex, err := os.Executable() - if err != nil { - return "" - } - return filepath.Dir(ex) -} - -// pwdDir returns pwd dir -func pwdDir() string { - ex, err := os.Getwd() - if err != nil { - return "" - } - return filepath.Dir(ex) -} - -// pathExists checks if file or dir exist -func pathExists(path string) (bool, error) { - _, err := os.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil +func (config *SirenaConfig) GetKeyFile(keyFile string) ([]byte, error) { + KeyPath := config.KeysPath + "/" + keyFile + if _, err := os.Stat(KeyPath); os.IsNotExist(err) { + return nil, err } - return true, err -} - -func GetPubSubConfig() PubSubConfig { - return CNFG.PubSubConfig -} -func GetFormatDateTime() string { - return CNFG.FormatsConfig.Time + return ioutil.ReadFile(KeyPath) } diff --git a/config.yaml b/config.yaml deleted file mode 100644 index 0114064..0000000 --- a/config.yaml +++ /dev/null @@ -1,19 +0,0 @@ -defaults: - addr: localhost:8080 - sirena_proxy_url: https://sirena-proxy-dev.tmc24.teamcsrv.com/proxy - redis_host: 127.0.0.1 - redis_port: 6379 - redis_password: - redis_db: - pubsub: - host: "localhost:8085" # local pubsub - project_id: "tmconsulting24" - topic: "provider-logs-dev" - subscription: "provider-logs-receiver-dev" - key: "" - timeout: 15 # connection timeout in seconds - formats: - time: "2006-01-02T15:04:05" - date: "2006-01-02" - sentry_enable: true - sentry_dsn: https://7b00887b8296433ab7154351f96de934:2282c9518e334de489f167bb3bc10d05@sentry.io/1364864 diff --git a/configuration/defaults/sirena.yaml b/configuration/defaults/sirena.yaml new file mode 100644 index 0000000..09c4484 --- /dev/null +++ b/configuration/defaults/sirena.yaml @@ -0,0 +1,10 @@ +defaults: + sirena: + client_id: "" + host: "" + port: "" + key_path: "keys" + client_public_key: Client.Public.key.pem + client_private_key: Client.Private.key.pem + server_public_key: Server.Public.key.pem + client_private_key_password: "" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6d10b90 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/tmconsulting/sirena-config + +go 1.12