+ config: upgrade from v4 to v5

This commit is contained in:
Simon Zolin 2019-09-13 14:19:10 +03:00
parent d2a5a550f7
commit a71521a658
1 changed files with 56 additions and 1 deletions

View File

@ -7,10 +7,11 @@ import (
"github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"golang.org/x/crypto/bcrypt"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
const currentSchemaVersion = 4 // used for upgrading from old configs to new config const currentSchemaVersion = 5 // used for upgrading from old configs to new config
// Performs necessary upgrade operations if needed // Performs necessary upgrade operations if needed
func upgradeConfig() error { func upgradeConfig() error {
@ -75,6 +76,12 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err
if err != nil { if err != nil {
return err return err
} }
fallthrough
case 4:
err := upgradeSchema4to5(diskConfig)
if err != nil {
return err
}
default: default:
err := fmt.Errorf("configuration file contains unknown schema_version, abort") err := fmt.Errorf("configuration file contains unknown schema_version, abort")
log.Println(err) log.Println(err)
@ -213,3 +220,51 @@ func upgradeSchema3to4(diskConfig *map[string]interface{}) error {
return nil return nil
} }
// Replace "auth_name", "auth_pass" string settings with an array:
// users:
// - name: "..."
// password: "..."
// ...
func upgradeSchema4to5(diskConfig *map[string]interface{}) error {
log.Printf("%s(): called", _Func())
(*diskConfig)["schema_version"] = 5
name, ok := (*diskConfig)["auth_name"]
if !ok {
return nil
}
nameStr, ok := name.(string)
if !ok {
log.Fatal("Please use double quotes in your user name in \"auth_name\" and restart AdGuardHome")
return nil
}
pass, ok := (*diskConfig)["auth_pass"]
if !ok {
return nil
}
passStr, ok := pass.(string)
if !ok {
log.Fatal("Please use double quotes in your password in \"auth_pass\" and restart AdGuardHome")
return nil
}
if len(nameStr) == 0 {
return nil
}
hash, err := bcrypt.GenerateFromPassword([]byte(passStr), bcrypt.DefaultCost)
if err != nil {
log.Fatalf("Can't use password \"%s\": bcrypt.GenerateFromPassword: %s", passStr, err)
return nil
}
u := User{
Name: nameStr,
PasswordHash: string(hash),
}
users := []User{u}
(*diskConfig)["users"] = users
return nil
}