diff --git a/config.template.yml b/config.template.yml index 55d157dd2..ed0b19e06 100644 --- a/config.template.yml +++ b/config.template.yml @@ -2,12 +2,11 @@ # Authelia configuration # ############################################################### -# The port to listen on +# The host and port to listen on +host: 0.0.0.0 port: 9091 -# Log level -# -# Level of verbosity for logs +# Level of verbosity for logs: info, debug, trace logs_level: debug # The secret used to generate JWT tokens when validating user identity by diff --git a/internal/configuration/schema/configuration.go b/internal/configuration/schema/configuration.go index 31e5119c4..93973faad 100644 --- a/internal/configuration/schema/configuration.go +++ b/internal/configuration/schema/configuration.go @@ -2,6 +2,7 @@ package schema // Configuration object extracted from YAML configuration file. type Configuration struct { + Host string `yaml:"host"` Port int `yaml:"port"` LogsLevel string `yaml:"logs_level"` JWTSecret string `yaml:"jwt_secret"` diff --git a/internal/configuration/validator/configuration.go b/internal/configuration/validator/configuration.go index 23428f2dc..0757580fa 100644 --- a/internal/configuration/validator/configuration.go +++ b/internal/configuration/validator/configuration.go @@ -11,6 +11,10 @@ var defaultLogsLevel = "info" // Validate and adapt the configuration read from file. func Validate(configuration *schema.Configuration, validator *schema.StructValidator) { + if configuration.Host == "" { + configuration.Host = "0.0.0.0" + } + if configuration.Port == 0 { configuration.Port = defaultPort } diff --git a/internal/configuration/validator/configuration_test.go b/internal/configuration/validator/configuration_test.go index 162bb89d9..c560d91b8 100644 --- a/internal/configuration/validator/configuration_test.go +++ b/internal/configuration/validator/configuration_test.go @@ -9,6 +9,7 @@ import ( func newDefaultConfig() schema.Configuration { config := schema.Configuration{} + config.Host = "127.0.0.1" config.Port = 9090 config.LogsLevel = "info" config.JWTSecret = "a_secret" @@ -49,6 +50,17 @@ func TestShouldValidateAndUpdatePort(t *testing.T) { assert.Equal(t, 8080, config.Port) } +func TestShouldValidateAndUpdateHost(t *testing.T) { + validator := schema.NewStructValidator() + config := newDefaultConfig() + config.Host = "" + + Validate(&config, validator) + + assert.Len(t, validator.Errors(), 0) + assert.Equal(t, "0.0.0.0", config.Host) +} + func TestShouldValidateAndUpdateLogsLevel(t *testing.T) { validator := schema.NewStructValidator() config := newDefaultConfig() diff --git a/internal/server/server.go b/internal/server/server.go index 7229a621c..a46ceac00 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -100,7 +100,7 @@ func StartServer(configuration schema.Configuration, providers middlewares.Provi ctx.SendFile(path.Join(publicDir, "index.html")) } - portPattern := fmt.Sprintf(":%d", configuration.Port) + portPattern := fmt.Sprintf("%s:%d", configuration.Host, configuration.Port) logging.Logger().Infof("Authelia is listening on %s", portPattern) logging.Logger().Fatal(fasthttp.ListenAndServe(portPattern,