Adapt validation for list requests on bundles and values

There are certain scenarios where fields of the Identifier are allowed
to be empty. If they are not empty they still have to match our
requirements for the contents of those fields (alphanumeric, -, _). This
is taken into acccount as well now.
This commit is contained in:
Benedikt Kulmann
2020-06-03 11:23:24 +02:00
parent 718ec8e061
commit ac98d4b85b
3 changed files with 13 additions and 35 deletions
+13 -8
View File
@@ -39,10 +39,10 @@ func validateGetSettingsBundle(req *proto.GetSettingsBundleRequest) error {
}
func validateListSettingsBundles(req *proto.ListSettingsBundlesRequest) error {
if err := validateBundleIdentifier(req.Identifier); err != nil {
return err
}
return nil
return validation.ValidateStruct(
req.Identifier,
validation.Field(&req.Identifier.Extension, validation.Match(regexForKeys)),
)
}
func validateSaveSettingsValue(req *proto.SaveSettingsValueRequest) error {
@@ -60,10 +60,15 @@ func validateGetSettingsValue(req *proto.GetSettingsValueRequest) error {
}
func validateListSettingsValues(req *proto.ListSettingsValuesRequest) error {
if err := validateValueIdentifier(req.Identifier); err != nil {
return err
}
return nil
return validation.ValidateStruct(
req.Identifier,
validation.Field(&req.Identifier.AccountUuid, is.UUID),
validation.Field(&req.Identifier.Extension, validation.Match(regexForKeys)),
validation.Field(&req.Identifier.Extension, validation.When(req.Identifier.BundleKey != "", validation.Required).Else(validation.Nil)),
validation.Field(&req.Identifier.BundleKey, validation.Match(regexForKeys)),
validation.Field(&req.Identifier.BundleKey, validation.When(req.Identifier.SettingKey != "", validation.Required).Else(validation.Nil)),
validation.Field(&req.Identifier.SettingKey, validation.Match(regexForKeys)),
)
}
func validateBundleIdentifier(identifier *proto.Identifier) error {
-12
View File
@@ -6,8 +6,6 @@ import (
"path"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"google.golang.org/grpc/codes"
gstatus "google.golang.org/grpc/status"
)
// ListBundles returns all bundles in the mountPath folder belonging to the given extension
@@ -52,11 +50,6 @@ func (s Store) ListBundles(identifier *proto.Identifier) ([]*proto.SettingsBundl
// ReadBundle tries to find a bundle by the given identifier within the mountPath.
// Extension and BundleKey within the identifier are required.
func (s Store) ReadBundle(identifier *proto.Identifier) (*proto.SettingsBundle, error) {
if len(identifier.Extension) < 1 || len(identifier.BundleKey) < 1 {
s.Logger.Error().Msg("extension and bundleKey cannot be empty")
return nil, gstatus.Error(codes.InvalidArgument, "Missing a required identifier attribute")
}
filePath := s.buildFilePathFromBundleArgs(identifier.Extension, identifier.BundleKey)
record := proto.SettingsBundle{}
if err := s.parseRecordFromFile(&record, filePath); err != nil {
@@ -70,11 +63,6 @@ func (s Store) ReadBundle(identifier *proto.Identifier) (*proto.SettingsBundle,
// WriteBundle writes the given record into a file within the mountPath
// Extension and BundleKey within the record identifier are required.
func (s Store) WriteBundle(record *proto.SettingsBundle) (*proto.SettingsBundle, error) {
if len(record.Identifier.Extension) < 1 || len(record.Identifier.BundleKey) < 1 {
s.Logger.Error().Msg("extension and bundleKey cannot be empty")
return nil, gstatus.Error(codes.InvalidArgument, "Missing a required identifier attribute")
}
filePath := s.buildFilePathFromBundle(record)
if err := s.writeRecordToFile(record, filePath); err != nil {
return nil, err
-15
View File
@@ -14,11 +14,6 @@ import (
// ReadValue tries to find a value by the given identifier attributes within the mountPath
// All identifier fields are required.
func (s Store) ReadValue(identifier *proto.Identifier) (*proto.SettingsValue, error) {
if len(identifier.AccountUuid) < 1 || len(identifier.Extension) < 1 || len(identifier.BundleKey) < 1 || len(identifier.SettingKey) < 1 {
s.Logger.Error().Msg("account-uuid, extension, bundle and setting are required")
return nil, gstatus.Errorf(codes.InvalidArgument, "Missing a required identifier attribute")
}
filePath := s.buildFilePathFromValueArgs(identifier.AccountUuid, identifier.Extension, identifier.BundleKey)
values, err := s.readValuesMapFromFile(filePath)
if err != nil {
@@ -34,11 +29,6 @@ func (s Store) ReadValue(identifier *proto.Identifier) (*proto.SettingsValue, er
// WriteValue writes the given SettingsValue into a file within the mountPath
// All identifier fields within the value are required.
func (s Store) WriteValue(value *proto.SettingsValue) (*proto.SettingsValue, error) {
if len(value.Identifier.AccountUuid) < 1 || len(value.Identifier.Extension) < 1 || len(value.Identifier.BundleKey) < 1 || len(value.Identifier.SettingKey) < 1 {
s.Logger.Error().Msg("all identifier keys are required")
return nil, gstatus.Errorf(codes.InvalidArgument, "Missing a required identifier attribute")
}
filePath := s.buildFilePathFromValue(value)
values, err := s.readValuesMapFromFile(filePath)
if err != nil {
@@ -54,11 +44,6 @@ func (s Store) WriteValue(value *proto.SettingsValue) (*proto.SettingsValue, err
// ListValues reads all values within the scope of the given identifier
// AccountUuid is required.
func (s Store) ListValues(identifier *proto.Identifier) ([]*proto.SettingsValue, error) {
if len(identifier.AccountUuid) < 1 {
s.Logger.Error().Msg("account-uuid is required")
return nil, gstatus.Errorf(codes.InvalidArgument, "Missing a required identifier attribute")
}
accountFolderPath := path.Join(s.mountPath, folderNameValues, identifier.AccountUuid)
var values []*proto.SettingsValue
if _, err := os.Stat(accountFolderPath); err != nil {