Skip to content

Commit

Permalink
config: Fix generated passwords being stored as empty password - Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Aug 28, 2019
1 parent 0edbc95 commit 693112d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
6 changes: 5 additions & 1 deletion fs/config/config.go
Expand Up @@ -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() {
Expand Down Expand Up @@ -854,14 +857,15 @@ 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")
case 'g':
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)
}
Expand Down
73 changes: 59 additions & 14 deletions fs/config/config_test.go
Expand Up @@ -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{}
Expand Down Expand Up @@ -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

Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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")()

Expand Down

0 comments on commit 693112d

Please sign in to comment.