storj/satellite/console/validation.go

46 lines
1.2 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-29 16:23:44 +00:00
// See LICENSE for copying information.
package console
2018-11-29 16:23:44 +00:00
import (
"github.com/zeebo/errs"
)
const (
// PasswordMinimumLength is the minimum allowed length for user account passwords.
PasswordMinimumLength = 6
// PasswordMaximumLength is the maximum allowed length for user account passwords.
PasswordMaximumLength = 72
2018-11-29 16:23:44 +00:00
)
// ErrValidation validation related error class.
var ErrValidation = errs.Class("validation")
2018-11-29 16:23:44 +00:00
// ValidateNewPassword validates password for creation.
satellite/console: Don't lose ErrValiation error class There was a defined type (`validationErrors`) for gathering several validation errors and classify them with the `ErrValdiation errs.Class`. `errs.Combine` doesn't maintain the classes of the errors to combine, for example ``` var myClass errs.Class = "My error class" err1 := myClass.Wrap(erros.New("error 1")) err2 := myClass.Wrap(erros.New("error 2")) err3 := errors.New("error 3") combinedErr := errs.Combine(err1, err2, err3) myClass.Has(combinedErr) // It returns false // Even only passing errors with a class and with the same one for all // of them combinedErr := errs.Combine(err1, err2) myClass.Has(combinedErr) // It returns false ``` Hence `validationErrors` didn't return what we expected to return when calling its `Combine` method. This commit delete the type and it replaces by `errs.Group` when there are more than one error, and wrapping the `errs.Group.Err` returned error with `ErrValiation` error class. The bug caused the HTTP API server to return a 500 status code as you can seee in the following log message extracted from the satellite production logs: ``` code: 500 error: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address" errorVerbose: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address storj.io/storj/satellite/console.(*Service).CreateUser:593 storj.io/storj/satellite/console/consoleweb/consoleapi.(*Auth).Register:250 net/http.HandlerFunc.ServeHTTP:2047 storj.io/storj/private/web.(*RateLimiter).Limit.func1:90 net/http.HandlerFunc.ServeHTTP:2047 github.com/gorilla/mux.(*Router).ServeHTTP:210 storj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:464 net/http.HandlerFunc.ServeHTTP:2047 net/http.serverHandler.ServeHTTP:2879 net/http.(*conn).serve:1930" message: "There was an error processing your request" ``` The issues was that not being classified with `ErrValidation` class it was not picked by the correct switch branch of the `consoleapi.Auth.getStatusCode` method which is in the call chain to `consoleapi.Auth.Register` method when it calls `console.Service.CreateUser` and returns an error. These changes should return the appropriated HTTP status code (Bad Request) when `console.Service.CreateUser` returns a validation error. Returning the appropriated HTTP statsus code also makes not to show this as an error in the server logs because the Bad Request sttatus code gets logged with debug level. Change-Id: I869ea85788992ae0865c373860fbf93a40d2d387
2022-02-23 16:02:45 +00:00
// It returns an plain error (not wrapped in a errs.Class) if pass is invalid.
//
// Previously the limit was 128, however, bcrypt ignores any characters beyond 72,
// hence this checks new passwords -- old passwords might be longer.
func ValidateNewPassword(pass string) error {
if len(pass) < PasswordMinimumLength {
return errs.New(passwordTooShortErrMsg, PasswordMinimumLength)
}
if len(pass) > PasswordMaximumLength {
return errs.New(passwordTooLongErrMsg, PasswordMaximumLength)
}
satellite/console: Don't lose ErrValiation error class There was a defined type (`validationErrors`) for gathering several validation errors and classify them with the `ErrValdiation errs.Class`. `errs.Combine` doesn't maintain the classes of the errors to combine, for example ``` var myClass errs.Class = "My error class" err1 := myClass.Wrap(erros.New("error 1")) err2 := myClass.Wrap(erros.New("error 2")) err3 := errors.New("error 3") combinedErr := errs.Combine(err1, err2, err3) myClass.Has(combinedErr) // It returns false // Even only passing errors with a class and with the same one for all // of them combinedErr := errs.Combine(err1, err2) myClass.Has(combinedErr) // It returns false ``` Hence `validationErrors` didn't return what we expected to return when calling its `Combine` method. This commit delete the type and it replaces by `errs.Group` when there are more than one error, and wrapping the `errs.Group.Err` returned error with `ErrValiation` error class. The bug caused the HTTP API server to return a 500 status code as you can seee in the following log message extracted from the satellite production logs: ``` code: 500 error: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address" errorVerbose: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address storj.io/storj/satellite/console.(*Service).CreateUser:593 storj.io/storj/satellite/console/consoleweb/consoleapi.(*Auth).Register:250 net/http.HandlerFunc.ServeHTTP:2047 storj.io/storj/private/web.(*RateLimiter).Limit.func1:90 net/http.HandlerFunc.ServeHTTP:2047 github.com/gorilla/mux.(*Router).ServeHTTP:210 storj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:464 net/http.HandlerFunc.ServeHTTP:2047 net/http.serverHandler.ServeHTTP:2879 net/http.(*conn).serve:1930" message: "There was an error processing your request" ``` The issues was that not being classified with `ErrValidation` class it was not picked by the correct switch branch of the `consoleapi.Auth.getStatusCode` method which is in the call chain to `consoleapi.Auth.Register` method when it calls `console.Service.CreateUser` and returns an error. These changes should return the appropriated HTTP status code (Bad Request) when `console.Service.CreateUser` returns a validation error. Returning the appropriated HTTP statsus code also makes not to show this as an error in the server logs because the Bad Request sttatus code gets logged with debug level. Change-Id: I869ea85788992ae0865c373860fbf93a40d2d387
2022-02-23 16:02:45 +00:00
return nil
}
// ValidateFullName validates full name.
satellite/console: Don't lose ErrValiation error class There was a defined type (`validationErrors`) for gathering several validation errors and classify them with the `ErrValdiation errs.Class`. `errs.Combine` doesn't maintain the classes of the errors to combine, for example ``` var myClass errs.Class = "My error class" err1 := myClass.Wrap(erros.New("error 1")) err2 := myClass.Wrap(erros.New("error 2")) err3 := errors.New("error 3") combinedErr := errs.Combine(err1, err2, err3) myClass.Has(combinedErr) // It returns false // Even only passing errors with a class and with the same one for all // of them combinedErr := errs.Combine(err1, err2) myClass.Has(combinedErr) // It returns false ``` Hence `validationErrors` didn't return what we expected to return when calling its `Combine` method. This commit delete the type and it replaces by `errs.Group` when there are more than one error, and wrapping the `errs.Group.Err` returned error with `ErrValiation` error class. The bug caused the HTTP API server to return a 500 status code as you can seee in the following log message extracted from the satellite production logs: ``` code: 500 error: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address" errorVerbose: "console service: validation: full name can not be empty; validation: Your password needs at least 6 characters long; validation: mail: no address storj.io/storj/satellite/console.(*Service).CreateUser:593 storj.io/storj/satellite/console/consoleweb/consoleapi.(*Auth).Register:250 net/http.HandlerFunc.ServeHTTP:2047 storj.io/storj/private/web.(*RateLimiter).Limit.func1:90 net/http.HandlerFunc.ServeHTTP:2047 github.com/gorilla/mux.(*Router).ServeHTTP:210 storj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:464 net/http.HandlerFunc.ServeHTTP:2047 net/http.serverHandler.ServeHTTP:2879 net/http.(*conn).serve:1930" message: "There was an error processing your request" ``` The issues was that not being classified with `ErrValidation` class it was not picked by the correct switch branch of the `consoleapi.Auth.getStatusCode` method which is in the call chain to `consoleapi.Auth.Register` method when it calls `console.Service.CreateUser` and returns an error. These changes should return the appropriated HTTP status code (Bad Request) when `console.Service.CreateUser` returns a validation error. Returning the appropriated HTTP statsus code also makes not to show this as an error in the server logs because the Bad Request sttatus code gets logged with debug level. Change-Id: I869ea85788992ae0865c373860fbf93a40d2d387
2022-02-23 16:02:45 +00:00
// It returns an plain error (not wrapped in a errs.Class) if name is invalid.
func ValidateFullName(name string) error {
if name == "" {
return errs.New("full name can not be empty")
}
return nil
}