2021-08-03 16:55:21 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-09-17 16:53:59 +07:00
|
|
|
"strings"
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-03 16:55:21 +07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2021-11-23 16:45:38 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/notification"
|
2021-09-17 11:44:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/ntp"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/oidc"
|
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/v4/internal/server"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-08-03 16:55:21 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRootCmd returns a new Root Cmd.
|
|
|
|
func NewRootCmd() (cmd *cobra.Command) {
|
|
|
|
version := utils.Version()
|
|
|
|
|
|
|
|
cmd = &cobra.Command{
|
|
|
|
Use: "authelia",
|
|
|
|
Example: cmdAutheliaExample,
|
|
|
|
Short: fmt.Sprintf("authelia %s", version),
|
|
|
|
Long: fmt.Sprintf(fmtAutheliaLong, version),
|
|
|
|
Version: version,
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
PreRun: newCmdWithConfigPreRun(true, true, true),
|
|
|
|
Run: cmdRootRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdWithConfigFlags(cmd)
|
|
|
|
|
|
|
|
cmd.AddCommand(
|
|
|
|
newBuildInfoCmd(),
|
|
|
|
NewCertificatesCmd(),
|
|
|
|
newCompletionCmd(),
|
|
|
|
NewHashPasswordCmd(),
|
|
|
|
NewRSACmd(),
|
2021-11-23 16:45:38 +07:00
|
|
|
NewStorageCmd(),
|
2021-08-03 16:55:21 +07:00
|
|
|
newValidateConfigCmd(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRootRun(_ *cobra.Command, _ []string) {
|
|
|
|
logger := logging.Logger()
|
|
|
|
|
|
|
|
logger.Infof("Authelia %s is starting", utils.Version())
|
|
|
|
|
|
|
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
|
|
|
logger.Info("===> Authelia is running in development mode. <===")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := logging.InitializeLogger(config.Log, true); err != nil {
|
|
|
|
logger.Fatalf("Cannot initialize logger: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
providers, warnings, errors := getProviders(config)
|
|
|
|
if len(warnings) != 0 {
|
|
|
|
for _, err := range warnings {
|
|
|
|
logger.Warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) != 0 {
|
|
|
|
for _, err := range errors {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Fatalf("Errors occurred provisioning providers.")
|
|
|
|
}
|
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
doStartupChecks(config, &providers)
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
server.Start(*config, providers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProviders(config *schema.Configuration) (providers middlewares.Providers, warnings []error, errors []error) {
|
2021-09-17 16:53:59 +07:00
|
|
|
// TODO: Adjust this so the CertPool can be used like a provider.
|
2021-08-03 16:55:21 +07:00
|
|
|
autheliaCertPool, warnings, errors := utils.NewX509CertPool(config.CertificatesDirectory)
|
|
|
|
if len(warnings) != 0 || len(errors) != 0 {
|
|
|
|
return providers, warnings, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
var storageProvider storage.Provider
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.Storage.PostgreSQL != nil:
|
|
|
|
storageProvider = storage.NewPostgreSQLProvider(*config.Storage.PostgreSQL)
|
|
|
|
case config.Storage.MySQL != nil:
|
|
|
|
storageProvider = storage.NewMySQLProvider(*config.Storage.MySQL)
|
|
|
|
case config.Storage.Local != nil:
|
|
|
|
storageProvider = storage.NewSQLiteProvider(config.Storage.Local.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
userProvider authentication.UserProvider
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.AuthenticationBackend.File != nil:
|
|
|
|
userProvider = authentication.NewFileUserProvider(config.AuthenticationBackend.File)
|
|
|
|
case config.AuthenticationBackend.LDAP != nil:
|
2021-09-17 16:53:59 +07:00
|
|
|
userProvider = authentication.NewLDAPUserProvider(config.AuthenticationBackend, autheliaCertPool)
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
var notifier notification.Notifier
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.Notifier.SMTP != nil:
|
2021-08-10 07:52:41 +07:00
|
|
|
notifier = notification.NewSMTPNotifier(config.Notifier.SMTP, autheliaCertPool)
|
2021-08-03 16:55:21 +07:00
|
|
|
case config.Notifier.FileSystem != nil:
|
|
|
|
notifier = notification.NewFileNotifier(*config.Notifier.FileSystem)
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:44:35 +07:00
|
|
|
var ntpProvider *ntp.Provider
|
|
|
|
if config.NTP != nil {
|
|
|
|
ntpProvider = ntp.NewProvider(config.NTP)
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
clock := utils.RealClock{}
|
|
|
|
authorizer := authorization.NewAuthorizer(config)
|
|
|
|
sessionProvider := session.NewProvider(config.Session, autheliaCertPool)
|
|
|
|
regulator := regulation.NewRegulator(config.Regulation, storageProvider, clock)
|
|
|
|
|
|
|
|
oidcProvider, err := oidc.NewOpenIDConnectProvider(config.IdentityProviders.OIDC)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return middlewares.Providers{
|
|
|
|
Authorizer: authorizer,
|
|
|
|
UserProvider: userProvider,
|
|
|
|
Regulator: regulator,
|
|
|
|
OpenIDConnect: oidcProvider,
|
|
|
|
StorageProvider: storageProvider,
|
2021-09-17 11:44:35 +07:00
|
|
|
NTP: ntpProvider,
|
2021-08-03 16:55:21 +07:00
|
|
|
Notifier: notifier,
|
|
|
|
SessionProvider: sessionProvider,
|
|
|
|
}, warnings, errors
|
|
|
|
}
|
2021-09-17 16:53:59 +07:00
|
|
|
|
|
|
|
func doStartupChecks(config *schema.Configuration, providers *middlewares.Providers) {
|
|
|
|
logger := logging.Logger()
|
|
|
|
|
|
|
|
var (
|
|
|
|
failures []string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
if err = doStartupCheck(logger, "storage", providers.StorageProvider, false); err != nil {
|
|
|
|
logger.Errorf("Failure running the storage provider startup check: %+v", err)
|
|
|
|
|
|
|
|
failures = append(failures, "storage")
|
|
|
|
}
|
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
if err = doStartupCheck(logger, "user", providers.UserProvider, false); err != nil {
|
|
|
|
logger.Errorf("Failure running the user provider startup check: %+v", err)
|
|
|
|
|
|
|
|
failures = append(failures, "user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = doStartupCheck(logger, "notification", providers.Notifier, config.Notifier.DisableStartupCheck); err != nil {
|
|
|
|
logger.Errorf("Failure running the notification provider startup check: %+v", err)
|
|
|
|
|
|
|
|
failures = append(failures, "notification")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.NTP.DisableStartupCheck && !providers.Authorizer.IsSecondFactorEnabled() {
|
|
|
|
logger.Debug("The NTP startup check was skipped due to there being no configured 2FA access control rules")
|
|
|
|
} else if err = doStartupCheck(logger, "ntp", providers.NTP, config.NTP.DisableStartupCheck); err != nil {
|
|
|
|
logger.Errorf("Failure running the user provider startup check: %+v", err)
|
|
|
|
|
|
|
|
failures = append(failures, "ntp")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(failures) != 0 {
|
|
|
|
logger.Fatalf("The following providers had fatal failures during startup: %s", strings.Join(failures, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
func doStartupCheck(logger *logrus.Logger, name string, provider models.StartupCheck, disabled bool) (err error) {
|
2021-09-17 16:53:59 +07:00
|
|
|
if disabled {
|
|
|
|
logger.Debugf("%s provider: startup check skipped as it is disabled", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if provider == nil {
|
|
|
|
return fmt.Errorf("unrecognized provider or it is not configured properly")
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
if err = provider.StartupCheck(); err != nil {
|
2021-09-17 16:53:59 +07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|