From cb7b0edc5b9f2ef7dda80a69e63fdad034e3e600 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Wed, 28 Feb 2018 15:31:58 -0500 Subject: Fix checks for configuration keys in the DB Currently, we will error if the DB is configured with the default containers/storage config, and then opened by a libpod which has explicitly set the defaults. This is due to us using an empty config by default (to tell c/storage to use its defaults). This patch changes our handling so that unset storage config (using the default) and explicitly setting the defaults are both compatible. Signed-off-by: Matthew Heon Closes: #423 Approved by: baude --- libpod/boltdb_state_internal.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'libpod/boltdb_state_internal.go') diff --git a/libpod/boltdb_state_internal.go b/libpod/boltdb_state_internal.go index 033dbe8bd..9c05dcc3a 100644 --- a/libpod/boltdb_state_internal.go +++ b/libpod/boltdb_state_internal.go @@ -64,28 +64,31 @@ func checkRuntimeConfig(db *bolt.DB, runtime *Runtime) error { } if err := validateDBAgainstConfig(configBkt, "static dir", - runtime.config.StaticDir, staticDir); err != nil { + runtime.config.StaticDir, staticDir, ""); err != nil { return err } if err := validateDBAgainstConfig(configBkt, "tmp dir", - runtime.config.TmpDir, tmpDir); err != nil { + runtime.config.TmpDir, tmpDir, ""); err != nil { return err } if err := validateDBAgainstConfig(configBkt, "run root", - runtime.config.StorageConfig.RunRoot, runRoot); err != nil { + runtime.config.StorageConfig.RunRoot, runRoot, + storage.DefaultStoreOptions.RunRoot); err != nil { return err } if err := validateDBAgainstConfig(configBkt, "graph root", - runtime.config.StorageConfig.GraphRoot, graphRoot); err != nil { + runtime.config.StorageConfig.GraphRoot, graphRoot, + storage.DefaultStoreOptions.GraphRoot); err != nil { return err } return validateDBAgainstConfig(configBkt, "graph driver name", runtime.config.StorageConfig.GraphDriverName, - graphDriverName) + graphDriverName, + storage.DefaultStoreOptions.GraphDriverName) }) return err @@ -93,14 +96,36 @@ func checkRuntimeConfig(db *bolt.DB, runtime *Runtime) error { // Validate a configuration entry in the DB against current runtime config // If the given configuration key does not exist it will be created -func validateDBAgainstConfig(bucket *bolt.Bucket, fieldName, runtimeValue string, keyName []byte) error { +// If the given runtimeValue or value retrieved from the database are the empty +// string and defaultValue is not, defaultValue will be checked instead. This +// ensures that we will not fail on configuration changes in configured c/storage. +func validateDBAgainstConfig(bucket *bolt.Bucket, fieldName, runtimeValue string, keyName []byte, defaultValue string) error { keyBytes := bucket.Get(keyName) if keyBytes == nil { - if err := bucket.Put(keyName, []byte(runtimeValue)); err != nil { + dbValue := []byte(runtimeValue) + if runtimeValue == "" && defaultValue != "" { + dbValue = []byte(defaultValue) + } + + if err := bucket.Put(keyName, dbValue); err != nil { return errors.Wrapf(err, "error updating %s in DB runtime config", fieldName) } } else { if runtimeValue != string(keyBytes) { + // If runtimeValue is the empty string, check against + // the default + if runtimeValue == "" && defaultValue != "" && + string(keyBytes) == defaultValue { + return nil + } + + // If DB value is the empty string, check that the + // runtime value is the default + if string(keyBytes) == "" && defaultValue != "" && + runtimeValue == defaultValue { + return nil + } + return errors.Wrapf(ErrDBBadConfig, "database %s %s does not match our %s %s", fieldName, string(keyBytes), fieldName, runtimeValue) } -- cgit v1.2.3-54-g00ecf