mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
[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:
parent
a63d55201f
commit
915b6b5436
|
@ -10,7 +10,7 @@ type Configuration struct {
|
||||||
// representing the permission to proceed with the operation.
|
// representing the permission to proceed with the operation.
|
||||||
JWTSecret string `mapstructure:"jwt_secret"`
|
JWTSecret string `mapstructure:"jwt_secret"`
|
||||||
DefaultRedirectionURL string `mapstructure:"default_redirection_url"`
|
DefaultRedirectionURL string `mapstructure:"default_redirection_url"`
|
||||||
GoogleAnalyticsTrackingID string `mapstructure:" google_analytics"`
|
GoogleAnalyticsTrackingID string `mapstructure:"google_analytics"`
|
||||||
|
|
||||||
AuthenticationBackend AuthenticationBackendConfiguration `mapstructure:"authentication_backend"`
|
AuthenticationBackend AuthenticationBackendConfiguration `mapstructure:"authentication_backend"`
|
||||||
Session SessionConfiguration `mapstructure:"session"`
|
Session SessionConfiguration `mapstructure:"session"`
|
||||||
|
@ -19,6 +19,6 @@ type Configuration struct {
|
||||||
DuoAPI *DuoAPIConfiguration `mapstructure:"duo_api"`
|
DuoAPI *DuoAPIConfiguration `mapstructure:"duo_api"`
|
||||||
AccessControl AccessControlConfiguration `mapstructure:"access_control"`
|
AccessControl AccessControlConfiguration `mapstructure:"access_control"`
|
||||||
Regulation *RegulationConfiguration `mapstructure:"regulation"`
|
Regulation *RegulationConfiguration `mapstructure:"regulation"`
|
||||||
Storage *StorageConfiguration `mapstructure:"storage"`
|
Storage StorageConfiguration `mapstructure:"storage"`
|
||||||
Notifier *NotifierConfiguration `mapstructure:"notifier"`
|
Notifier *NotifierConfiguration `mapstructure:"notifier"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,5 +53,5 @@ func Validate(configuration *schema.Configuration, validator *schema.StructValid
|
||||||
configuration.AccessControl.DefaultPolicy = "deny"
|
configuration.AccessControl.DefaultPolicy = "deny"
|
||||||
}
|
}
|
||||||
|
|
||||||
ValidateSQLStorage(configuration.Storage, validator)
|
ValidateStorage(configuration.Storage, validator)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,10 +21,8 @@ func newDefaultConfig() schema.Configuration {
|
||||||
Name: "authelia_session",
|
Name: "authelia_session",
|
||||||
Secret: "secret",
|
Secret: "secret",
|
||||||
}
|
}
|
||||||
config.Storage = &schema.StorageConfiguration{
|
config.Storage.Local = &schema.LocalStorageConfiguration{
|
||||||
Local: &schema.LocalStorageConfiguration{
|
|
||||||
Path: "abc",
|
Path: "abc",
|
||||||
},
|
|
||||||
}
|
}
|
||||||
config.Notifier = &schema.NotifierConfiguration{
|
config.Notifier = &schema.NotifierConfiguration{
|
||||||
FileSystem: &schema.FileSystemNotifierConfiguration{
|
FileSystem: &schema.FileSystemNotifierConfiguration{
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidateSQLStorage validates storage configuration.
|
// 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 {
|
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'"))
|
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) {
|
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"))
|
validator.Push(errors.New("Username and password must be provided"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
101
internal/configuration/validator/storage_test.go
Normal file
101
internal/configuration/validator/storage_test.go
Normal 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))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user