From 693112d57e0aaece5f6ec46100bedb818794dcef Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 28 Aug 2019 11:46:35 +0100 Subject: [PATCH] config: Fix generated passwords being stored as empty password - Fixes #3492 --- fs/config/config.go | 6 +++- fs/config/config_test.go | 73 ++++++++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/fs/config/config.go b/fs/config/config.go index 927167c21..676a9143f 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -89,6 +89,9 @@ var ( // For security reasons, the temp file is deleted once the configKey is successfully loaded. // This can be used to pass the configKey to a child process. PassConfigKeyForDaemonization = false + + // Password can be used to configure the random password generator + Password = random.Password ) func init() { @@ -854,6 +857,7 @@ func ChooseOption(o *fs.Option, name string) string { actions = append(actions, "nNo leave this optional password blank") } var password string + var err error switch i := Command(actions); i { case 'y': password = ChangePassword("the") @@ -861,7 +865,7 @@ func ChooseOption(o *fs.Option, name string) string { for { fmt.Printf("Password strength in bits.\n64 is just about memorable\n128 is secure\n1024 is the maximum\n") bits := ChooseNumber("Bits", 64, 1024) - password, err := random.Password(bits) + password, err = Password(bits) if err != nil { log.Fatalf("Failed to make password: %v", err) } diff --git a/fs/config/config_test.go b/fs/config/config_test.go index 76f39dc60..c662501a9 100644 --- a/fs/config/config_test.go +++ b/fs/config/config_test.go @@ -30,6 +30,7 @@ func testConfigFile(t *testing.T, configFileName string) func() { oldConfig := fs.Config oldConfigFile := configFile oldReadLine := ReadLine + oldPassword := Password os.Stdout = nil ConfigPath = path fs.Config = &fs.ConfigInfo{} @@ -63,6 +64,7 @@ func testConfigFile(t *testing.T, configFileName string) func() { os.Stdout = oldOsStdout ConfigPath = oldConfigPath ReadLine = oldReadLine + Password = oldPassword fs.Config = oldConfig configFile = oldConfigFile @@ -71,24 +73,28 @@ func testConfigFile(t *testing.T, configFileName string) func() { } } -func TestCRUD(t *testing.T) { - defer testConfigFile(t, "crud.conf")() - - // expect script for creating remote +// makeReadLine makes a simple readLine which returns a fixed list of +// strings +func makeReadLine(answers []string) func() string { i := 0 - ReadLine = func() string { - answers := []string{ - "config_test_remote", // type - "true", // bool value - "y", // type my own password - "secret", // password - "secret", // repeat - "y", // looks good, save - } + return func() string { i = i + 1 return answers[i-1] } +} +func TestCRUD(t *testing.T) { + defer testConfigFile(t, "crud.conf")() + + // script for creating remote + ReadLine = makeReadLine([]string{ + "config_test_remote", // type + "true", // bool value + "y", // type my own password + "secret", // password + "secret", // repeat + "y", // looks good, save + }) NewRemote("test") assert.Equal(t, []string{"test"}, configFile.GetSectionList()) @@ -97,7 +103,11 @@ func TestCRUD(t *testing.T) { assert.Equal(t, "secret", obscure.MustReveal(FileGet("test", "pass"))) // normal rename, test → asdf - ReadLine = func() string { return "asdf" } + ReadLine = makeReadLine([]string{ + "asdf", + "asdf", + "asdf", + }) RenameRemote("test") assert.Equal(t, []string{"asdf"}, configFile.GetSectionList()) @@ -118,6 +128,41 @@ func TestCRUD(t *testing.T) { assert.Equal(t, []string{}, configFile.GetSectionList()) } +func TestChooseOption(t *testing.T) { + defer testConfigFile(t, "crud.conf")() + + // script for creating remote + ReadLine = makeReadLine([]string{ + "config_test_remote", // type + "false", // bool value + "x", // bad choice + "g", // generate password + "1024", // very big + "y", // password OK + "y", // looks good, save + }) + Password = func(bits int) (string, error) { + assert.Equal(t, 1024, bits) + return "not very random password", nil + } + NewRemote("test") + + assert.Equal(t, "false", FileGet("test", "bool")) + assert.Equal(t, "not very random password", obscure.MustReveal(FileGet("test", "pass"))) + + // script for creating remote + ReadLine = makeReadLine([]string{ + "config_test_remote", // type + "true", // bool value + "n", // not required + "y", // looks good, save + }) + NewRemote("test") + + assert.Equal(t, "true", FileGet("test", "bool")) + assert.Equal(t, "", FileGet("test", "pass")) +} + func TestCreateUpatePasswordRemote(t *testing.T) { defer testConfigFile(t, "update.conf")()