Skip to content

Commit

Permalink
fix Erroneous badger path when using start with relative rootdir and …
Browse files Browse the repository at this point in the history
…TLS certificates pathing
  • Loading branch information
orpheuslummis committed Apr 11, 2023
1 parent 04deae0 commit afbd42d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
21 changes: 17 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func DefaultConfig() *Config {
// Use on a Config struct already loaded with default values from DefaultConfig().
// To be executed once at the beginning of the program.
func (cfg *Config) LoadWithRootdir(withRootdir bool) error {
var err error

// Use default logging configuration here, so that
// we can log errors in a consistent way even in the case of early failure.
defaultLogCfg := defaultLogConfig()
Expand All @@ -112,6 +114,12 @@ func (cfg *Config) LoadWithRootdir(withRootdir bool) error {
return err
}

// using absolute rootdir for robustness.
cfg.Rootdir, err = filepath.Abs(cfg.Rootdir)
if err != nil {
return err
}

if withRootdir {
cfg.v.AddConfigPath(cfg.Rootdir)
if err := cfg.v.ReadInConfig(); err != nil {
Expand Down Expand Up @@ -180,10 +188,16 @@ func (cfg *Config) validate() error {
}

func (cfg *Config) paramsPreprocessing() error {
// We prefer using absolute paths.
// We prefer using absolute paths, relative to the rootdir.
if !filepath.IsAbs(cfg.v.GetString("datastore.badger.path")) {
cfg.v.Set("datastore.badger.path", filepath.Join(cfg.Rootdir, cfg.v.GetString("datastore.badger.path")))
}
if !filepath.IsAbs(cfg.v.GetString("api.privkeypath")) {
cfg.v.Set("api.privkeypath", filepath.Join(cfg.Rootdir, cfg.v.GetString("api.privkeypath")))
}
if !filepath.IsAbs(cfg.v.GetString("api.pubkeypath")) {
cfg.v.Set("api.pubkeypath", filepath.Join(cfg.Rootdir, cfg.v.GetString("api.pubkeypath")))
}

// log.logger configuration as a string
logloggerAsStringSlice := cfg.v.GetStringSlice("log.logger")
Expand Down Expand Up @@ -269,12 +283,11 @@ type APIConfig struct {
}

func defaultAPIConfig() *APIConfig {
rootDir := DefaultRootDir()
return &APIConfig{
Address: "localhost:9181",
TLS: false,
PubKeyPath: filepath.Join(rootDir, "certs/server.key"),
PrivKeyPath: filepath.Join(rootDir, "certs/server.crt"),
PubKeyPath: "certs/server.key",
PrivKeyPath: "certs/server.crt",
Email: DefaultAPIEmail,
}
}
Expand Down
8 changes: 3 additions & 5 deletions config/configfile_yaml.gotmpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# DefraDB configuration (YAML)

# NOTE: Paths below are relative to the DefraDB directory unless otherwise stated.
# By default, the DefraDB directory is "$HOME/.defradb", but
# can be changed via the $DEFRA_ROOTDIR env variable or --rootdir CLI flag.
# The default DefraDB directory is "$HOME/.defradb". It can be changed via the --rootdir CLI flag.
# Relative paths are interpreted as being rooted in the DefraDB directory.

datastore:
# Store can be badger | memory
# badger: fast pure Go key-value store optimized for SSDs (https://github.com/dgraph-io/badger)
# memory: in-memory version of badger
store: {{ .Datastore.Store }}
badger:
# The path to the database data file(s), relative paths here will be converted to absolute file paths
# on database start.
# The path to the database data file(s).
path: {{ .Datastore.Badger.Path }}
# Maximum file size of the value log files. The in-memory file size will be 2*valuelogfilesize.
# Human friendly units can be used (ex: 500MB).
Expand Down

0 comments on commit afbd42d

Please sign in to comment.