[FIX] Prevent crash when storage config is nil (#623)

* Prevent crash when storage config is nil.

* Fix google analytics configuration.

Fixes #622.
This commit is contained in:
Clément Michaud 2020-02-06 03:53:02 +01:00 committed by GitHub
parent a63d55201f
commit 915b6b5436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 9 deletions

View File

@ -10,7 +10,7 @@ type Configuration struct {
// representing the permission to proceed with the operation.
JWTSecret string `mapstructure:"jwt_secret"`
DefaultRedirectionURL string `mapstructure:"default_redirection_url"`
GoogleAnalyticsTrackingID string `mapstructure:" google_analytics"`
GoogleAnalyticsTrackingID string `mapstructure:"google_analytics"`
AuthenticationBackend AuthenticationBackendConfiguration `mapstructure:"authentication_backend"`
Session SessionConfiguration `mapstructure:"session"`
@ -19,6 +19,6 @@ type Configuration struct {
DuoAPI *DuoAPIConfiguration `mapstructure:"duo_api"`
AccessControl AccessControlConfiguration `mapstructure:"access_control"`
Regulation *RegulationConfiguration `mapstructure:"regulation"`
Storage *StorageConfiguration `mapstructure:"storage"`
Storage StorageConfiguration `mapstructure:"storage"`
Notifier *NotifierConfiguration `mapstructure:"notifier"`
}

View File

@ -53,5 +53,5 @@ func Validate(configuration *schema.Configuration, validator *schema.StructValid
configuration.AccessControl.DefaultPolicy = "deny"
}
ValidateSQLStorage(configuration.Storage, validator)
ValidateStorage(configuration.Storage, validator)
}

View File

@ -21,10 +21,8 @@ func newDefaultConfig() schema.Configuration {
Name: "authelia_session",
Secret: "secret",
}
config.Storage = &schema.StorageConfiguration{
Local: &schema.LocalStorageConfiguration{
config.Storage.Local = &schema.LocalStorageConfiguration{
Path: "abc",
},
}
config.Notifier = &schema.NotifierConfiguration{
FileSystem: &schema.FileSystemNotifierConfiguration{

View File

@ -7,7 +7,7 @@ import (
)
// ValidateSQLStorage validates storage configuration.
func ValidateSQLStorage(configuration *schema.StorageConfiguration, validator *schema.StructValidator) {
func ValidateStorage(configuration schema.StorageConfiguration, validator *schema.StructValidator) {
if configuration.Local == nil && configuration.MySQL == nil && configuration.PostgreSQL == nil {
validator.Push(errors.New("A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'"))
}
@ -22,7 +22,7 @@ func ValidateSQLStorage(configuration *schema.StorageConfiguration, validator *s
}
func validateSQLConfiguration(configuration *schema.SQLStorageConfiguration, validator *schema.StructValidator) {
if configuration.Password != "" && configuration.Username == "" {
if configuration.Password == "" || configuration.Username == "" {
validator.Push(errors.New("Username and password must be provided"))
}

View File

@ -0,0 +1,101 @@
package validator
import (
"testing"
"github.com/authelia/authelia/internal/configuration/schema"
"github.com/stretchr/testify/suite"
)
type StorageSuite struct {
suite.Suite
configuration schema.StorageConfiguration
}
func (s *StorageSuite) SetupTest() {
s.configuration.Local = &schema.LocalStorageConfiguration{
Path: "/this/is/a/path",
}
}
func (s *StorageSuite) TestShouldValidateOneStorageIsConfigured() {
validator := schema.NewStructValidator()
s.configuration.Local = nil
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 1)
s.Assert().EqualError(validator.Errors()[0], "A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'")
}
func (s *StorageSuite) TestShouldValidateLocalPathIsProvided() {
validator := schema.NewStructValidator()
s.configuration.Local.Path = ""
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 1)
s.Assert().EqualError(validator.Errors()[0], "A file path must be provided with key 'path'")
validator = schema.NewStructValidator()
s.configuration.Local.Path = "/myapth"
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 0)
}
func (s *StorageSuite) TestShouldValidateSQLUsernamePasswordAndDatabaseAreProvided() {
validator := schema.NewStructValidator()
s.configuration.MySQL = &schema.MySQLStorageConfiguration{}
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 2)
s.Assert().EqualError(validator.Errors()[0], "Username and password must be provided")
s.Assert().EqualError(validator.Errors()[1], "A database must be provided")
validator = schema.NewStructValidator()
s.configuration.MySQL = &schema.MySQLStorageConfiguration{
SQLStorageConfiguration: schema.SQLStorageConfiguration{
Username: "myuser",
Password: "pass",
Database: "database",
},
}
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 0)
}
func (s *StorageSuite) TestShouldValidatePostgresSSLModeIsDisableByDefault() {
validator := schema.NewStructValidator()
s.configuration.PostgreSQL = &schema.PostgreSQLStorageConfiguration{
SQLStorageConfiguration: schema.SQLStorageConfiguration{
Username: "myuser",
Password: "pass",
Database: "database",
},
}
ValidateStorage(s.configuration, validator)
s.Assert().Equal("disable", s.configuration.PostgreSQL.SSLMode)
}
func (s *StorageSuite) TestShouldValidatePostgresSSLModeMustBeValid() {
validator := schema.NewStructValidator()
s.configuration.PostgreSQL = &schema.PostgreSQLStorageConfiguration{
SQLStorageConfiguration: schema.SQLStorageConfiguration{
Username: "myuser",
Password: "pass",
Database: "database",
},
SSLMode: "unknown",
}
ValidateStorage(s.configuration, validator)
s.Require().Len(validator.Errors(), 1)
s.Assert().EqualError(validator.Errors()[0], "SSL mode must be 'disable', 'require', 'verify-ca' or 'verify-full'")
}
func TestShouldRunStorageSuite(t *testing.T) {
suite.Run(t, new(StorageSuite))
}