From ee8f20b20340420367e9bdf637b5f00907f12236 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 29 Sep 2021 11:38:03 -0700 Subject: [PATCH] make initV1Directory publically accessible and usable --- storage/v1/profile_store.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/storage/v1/profile_store.go b/storage/v1/profile_store.go index 65cdb44..762c3c0 100644 --- a/storage/v1/profile_store.go +++ b/storage/v1/profile_store.go @@ -39,15 +39,25 @@ func (ps *ProfileStoreV1) CheckPassword(checkpass string) bool { return oldkey == ps.key } -func initV1Directory(directory, password string) ([32]byte, [128]byte, error) { +// InitV1Directory generates a key and salt from a password, writes a SALT and VERSION file and returns the key and salt +func InitV1Directory(directory, password string) ([32]byte, [128]byte, error) { + os.Mkdir(directory, 0700) + key, salt, err := CreateKeySalt(password) if err != nil { log.Errorf("Could not create key for profile store from password: %v\n", err) return [32]byte{}, [128]byte{}, err } - ioutil.WriteFile(path.Join(directory, versionFile), []byte(version), 0600) - ioutil.WriteFile(path.Join(directory, saltFile), salt[:], 0600) + if err = ioutil.WriteFile(path.Join(directory, versionFile), []byte(version), 0600); err != nil { + log.Errorf("Could not write version file: %v", err) + return [32]byte{}, [128]byte{}, err + } + + if err = ioutil.WriteFile(path.Join(directory, saltFile), salt[:], 0600); err != nil { + log.Errorf("Could not write salt file: %v", err) + return [32]byte{}, [128]byte{}, err + } return key, salt, nil } @@ -55,9 +65,7 @@ func initV1Directory(directory, password string) ([32]byte, [128]byte, error) { // CreateProfileWriterStore creates a profile store backed by a filestore listening for events and saving them // directory should be $appDir/profiles/$rand func CreateProfileWriterStore(eventManager event.Manager, directory, password string, profile *model.Profile) *ProfileStoreV1 { - os.Mkdir(directory, 0700) - - key, salt, err := initV1Directory(directory, password) + key, salt, err := InitV1Directory(directory, password) if err != nil { return nil } @@ -136,7 +144,7 @@ func ReadProfile(directory string, key [32]byte, salt [128]byte) (*model.Profile // UpgradeV0Profile takes a profile (presumably from a V0 store) and creates and writes a V1 store func UpgradeV0Profile(profile *model.Profile, directory, password string) error { - key, salt, err := initV1Directory(directory, password) + key, salt, err := InitV1Directory(directory, password) if err != nil { return err }