Don't check default store if custom are configured, try parse .browserpass.json (#29)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@@ -14,6 +15,26 @@ import (
|
|||||||
func configure(request *request) {
|
func configure(request *request) {
|
||||||
responseData := response.MakeConfigureResponse()
|
responseData := response.MakeConfigureResponse()
|
||||||
|
|
||||||
|
// User configured gpgPath in the browser, check if it is a valid binary to use
|
||||||
|
if request.Settings.GpgPath != "" {
|
||||||
|
err := validateGpgBinary(request.Settings.GpgPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf(
|
||||||
|
"The provided gpg binary path '%v' is invalid: %+v",
|
||||||
|
request.Settings.GpgPath, err,
|
||||||
|
)
|
||||||
|
response.SendErrorAndExit(
|
||||||
|
errors.CodeInvalidGpgPath,
|
||||||
|
&map[errors.Field]string{
|
||||||
|
errors.FieldMessage: "The provided gpg binary path is invalid",
|
||||||
|
errors.FieldAction: "configure",
|
||||||
|
errors.FieldError: err.Error(),
|
||||||
|
errors.FieldGpgPath: request.Settings.GpgPath,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check that each and every store in the settings exists and is accessible.
|
// Check that each and every store in the settings exists and is accessible.
|
||||||
// Then read the default configuration for these stores (if available).
|
// Then read the default configuration for these stores (if available).
|
||||||
for _, store := range request.Settings.Stores {
|
for _, store := range request.Settings.Stores {
|
||||||
@@ -39,15 +60,19 @@ func configure(request *request) {
|
|||||||
store.Path = normalizedStorePath
|
store.Path = normalizedStorePath
|
||||||
|
|
||||||
responseData.StoreSettings[store.ID], err = readDefaultSettings(store.Path)
|
responseData.StoreSettings[store.ID], err = readDefaultSettings(store.Path)
|
||||||
|
if err == nil {
|
||||||
|
var storeSettings StoreSettings
|
||||||
|
err = json.Unmarshal([]byte(responseData.StoreSettings[store.ID]), &storeSettings)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf(
|
log.Errorf(
|
||||||
"Unable to read the default settings of the user-configured password store '%+v' in its location: %+v",
|
"Unable to read .browserpass.json of the user-configured password store '%+v': %+v",
|
||||||
store, err,
|
store, err,
|
||||||
)
|
)
|
||||||
response.SendErrorAndExit(
|
response.SendErrorAndExit(
|
||||||
errors.CodeUnreadablePasswordStoreDefaultSettings,
|
errors.CodeUnreadablePasswordStoreDefaultSettings,
|
||||||
&map[errors.Field]string{
|
&map[errors.Field]string{
|
||||||
errors.FieldMessage: "Unable to read the default settings of the password store",
|
errors.FieldMessage: "Unable to read .browserpass.json of the password store",
|
||||||
errors.FieldAction: "configure",
|
errors.FieldAction: "configure",
|
||||||
errors.FieldError: err.Error(),
|
errors.FieldError: err.Error(),
|
||||||
errors.FieldStoreID: store.ID,
|
errors.FieldStoreID: store.ID,
|
||||||
@@ -59,11 +84,11 @@ func configure(request *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check whether a store in the default location exists and is accessible.
|
// Check whether a store in the default location exists and is accessible.
|
||||||
// If there is at least one store in the settings, it is expected that there might be no store in the default location => do not return errors.
|
// If there is at least one store in the settings, user will not use the default store => skip its validation.
|
||||||
// However, if there are no stores in the settings, user expects to use the default password store => return an error if it is not accessible.
|
// However, if there are no stores in the settings, user expects to use the default password store => return an error if it is not accessible.
|
||||||
|
if len(request.Settings.Stores) == 0 {
|
||||||
possibleDefaultStorePath, err := getDefaultPasswordStorePath()
|
possibleDefaultStorePath, err := getDefaultPasswordStorePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(request.Settings.Stores) == 0 {
|
|
||||||
log.Error("Unable to determine the location of the default password store: ", err)
|
log.Error("Unable to determine the location of the default password store: ", err)
|
||||||
response.SendErrorAndExit(
|
response.SendErrorAndExit(
|
||||||
errors.CodeUnknownDefaultPasswordStoreLocation,
|
errors.CodeUnknownDefaultPasswordStoreLocation,
|
||||||
@@ -73,11 +98,9 @@ func configure(request *request) {
|
|||||||
errors.FieldError: err.Error(),
|
errors.FieldError: err.Error(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
responseData.DefaultStore.Path, err = normalizePasswordStorePath(possibleDefaultStorePath)
|
responseData.DefaultStore.Path, err = normalizePasswordStorePath(possibleDefaultStorePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(request.Settings.Stores) == 0 {
|
|
||||||
log.Errorf(
|
log.Errorf(
|
||||||
"The default password store is not accessible at the location '%v': %+v",
|
"The default password store is not accessible at the location '%v': %+v",
|
||||||
possibleDefaultStorePath, err,
|
possibleDefaultStorePath, err,
|
||||||
@@ -93,19 +116,21 @@ func configure(request *request) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if responseData.DefaultStore.Path != "" {
|
|
||||||
responseData.DefaultStore.Settings, err = readDefaultSettings(responseData.DefaultStore.Path)
|
responseData.DefaultStore.Settings, err = readDefaultSettings(responseData.DefaultStore.Path)
|
||||||
|
if err == nil {
|
||||||
|
var storeSettings StoreSettings
|
||||||
|
err = json.Unmarshal([]byte(responseData.DefaultStore.Settings), &storeSettings)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf(
|
log.Errorf(
|
||||||
"Unable to read the default settings of the default password store in '%v': %+v",
|
"Unable to read .browserpass.json of the default password store in '%v': %+v",
|
||||||
responseData.DefaultStore.Path, err,
|
responseData.DefaultStore.Path, err,
|
||||||
)
|
)
|
||||||
response.SendErrorAndExit(
|
response.SendErrorAndExit(
|
||||||
errors.CodeUnreadableDefaultPasswordStoreDefaultSettings,
|
errors.CodeUnreadableDefaultPasswordStoreDefaultSettings,
|
||||||
&map[errors.Field]string{
|
&map[errors.Field]string{
|
||||||
errors.FieldMessage: "Unable to read the default settings of the default password store",
|
errors.FieldMessage: "Unable to read .browserpas.json of the default password store",
|
||||||
errors.FieldAction: "configure",
|
errors.FieldAction: "configure",
|
||||||
errors.FieldError: err.Error(),
|
errors.FieldError: err.Error(),
|
||||||
errors.FieldStorePath: responseData.DefaultStore.Path,
|
errors.FieldStorePath: responseData.DefaultStore.Path,
|
||||||
|
@@ -11,7 +11,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type storeSettings struct {
|
type StoreSettings struct {
|
||||||
GpgPath string `json:"gpgPath"`
|
GpgPath string `json:"gpgPath"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ type store struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Settings storeSettings `json:"settings"`
|
Settings StoreSettings `json:"settings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type settings struct {
|
type settings struct {
|
||||||
|
Reference in New Issue
Block a user