Merge pull request 'make initV1Directory publically accessible and usable' (#388) from StroeUse into master
continuous-integration/drone/push Build is pending Details

Reviewed-on: #388
This commit is contained in:
Sarah Jamie Lewis 2021-09-30 19:34:37 +00:00
commit a6d4eaf10e
1 changed files with 15 additions and 7 deletions

View File

@ -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
}