2021-08-03 09:55:21 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2022-08-08 21:50:12 +00:00
|
|
|
"context"
|
2021-08-03 09:55:21 +00:00
|
|
|
"fmt"
|
2022-08-08 21:50:12 +00:00
|
|
|
"net"
|
2021-08-03 09:55:21 +00:00
|
|
|
"os"
|
2022-08-08 21:50:12 +00:00
|
|
|
"os/signal"
|
2021-09-17 09:53:59 +00:00
|
|
|
"strings"
|
2022-08-08 21:50:12 +00:00
|
|
|
"syscall"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-03 09:55:21 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-08-08 21:50:12 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2022-03-06 05:47:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/server"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRootCmd returns a new Root Cmd.
|
|
|
|
func NewRootCmd() (cmd *cobra.Command) {
|
|
|
|
version := utils.Version()
|
|
|
|
|
|
|
|
cmd = &cobra.Command{
|
|
|
|
Use: "authelia",
|
2022-06-14 12:40:00 +00:00
|
|
|
Short: fmt.Sprintf(fmtCmdAutheliaShort, version),
|
|
|
|
Long: fmt.Sprintf(fmtCmdAutheliaLong, version),
|
2021-08-03 09:55:21 +00:00
|
|
|
Example: cmdAutheliaExample,
|
|
|
|
Version: version,
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
PreRun: newCmdWithConfigPreRun(true, true, true),
|
|
|
|
Run: cmdRootRun,
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
cmdWithConfigFlags(cmd, false, []string{})
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
cmd.AddCommand(
|
|
|
|
newBuildInfoCmd(),
|
2022-06-27 08:27:57 +00:00
|
|
|
newCryptoCmd(),
|
2022-06-14 12:40:00 +00:00
|
|
|
newHashPasswordCmd(),
|
|
|
|
newStorageCmd(),
|
2021-08-03 09:55:21 +00:00
|
|
|
newValidateConfigCmd(),
|
2022-02-28 03:15:01 +00:00
|
|
|
newAccessControlCommand(),
|
2021-08-03 09:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
providers, warnings, errors := getProviders()
|
2021-08-03 09:55:21 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
doStartupChecks(config, &providers, logger)
|
2021-09-17 09:53:59 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
runServers(config, providers, logger)
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
func runServers(config *schema.Configuration, providers middlewares.Providers, log *logrus.Logger) {
|
|
|
|
ctx := context.Background()
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
defer signal.Stop(quit)
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mainServer, metricsServer *fasthttp.Server
|
|
|
|
mainListener, metricsListener net.Listener
|
|
|
|
)
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
g.Go(func() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
|
|
|
log.WithError(recoverErr(rec)).Errorf("Critical error in server caught (recovered)")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if mainServer, mainListener, err = server.CreateDefaultServer(*config, providers); err != nil {
|
|
|
|
return err
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
if err = mainServer.Serve(mainListener); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
g.Go(func() (err error) {
|
|
|
|
if providers.Metrics == nil {
|
|
|
|
return nil
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
|
|
|
log.WithError(recoverErr(rec)).Errorf("Critical error in metrics server caught (recovered)")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if metricsServer, metricsListener, err = server.CreateMetricsServer(config.Telemetry.Metrics); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = metricsServer.Serve(metricsListener); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
|
|
|
return nil
|
2022-08-08 21:50:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
break
|
|
|
|
case <-ctx.Done():
|
|
|
|
break
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
log.Infof("Shutting down")
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = mainServer.Shutdown(); err != nil {
|
|
|
|
log.WithError(err).Errorf("Error occurred shutting down the server")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metricsServer != nil {
|
|
|
|
if err = metricsServer.Shutdown(); err != nil {
|
|
|
|
log.WithError(err).Errorf("Error occurred shutting down the metrics server")
|
2022-06-14 07:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-04 23:57:47 +00:00
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
if err = g.Wait(); err != nil {
|
|
|
|
log.WithError(err).Errorf("Error occurred waiting for shutdown")
|
|
|
|
}
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
func doStartupChecks(config *schema.Configuration, providers *middlewares.Providers, log *logrus.Logger) {
|
2021-09-17 09:53:59 +00:00
|
|
|
var (
|
|
|
|
failures []string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
if err = doStartupCheck(log, "storage", providers.StorageProvider, false); err != nil {
|
|
|
|
log.Errorf("Failure running the storage provider startup check: %+v", err)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
failures = append(failures, "storage")
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
if err = doStartupCheck(log, "user", providers.UserProvider, false); err != nil {
|
|
|
|
log.Errorf("Failure running the user provider startup check: %+v", err)
|
2021-09-17 09:53:59 +00:00
|
|
|
|
|
|
|
failures = append(failures, "user")
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:50:12 +00:00
|
|
|
if err = doStartupCheck(log, "notification", providers.Notifier, config.Notifier.DisableStartupCheck); err != nil {
|
|
|
|
log.Errorf("Failure running the notification provider startup check: %+v", err)
|
2021-09-17 09:53:59 +00:00
|
|
|
|
|
|
|
failures = append(failures, "notification")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.NTP.DisableStartupCheck && !providers.Authorizer.IsSecondFactorEnabled() {
|
2022-08-08 21:50:12 +00:00
|
|
|
log.Debug("The NTP startup check was skipped due to there being no configured 2FA access control rules")
|
|
|
|
} else if err = doStartupCheck(log, "ntp", providers.NTP, config.NTP.DisableStartupCheck); err != nil {
|
|
|
|
log.Errorf("Failure running the ntp provider startup check: %+v", err)
|
2021-09-17 09:53:59 +00:00
|
|
|
|
2022-02-03 03:04:24 +00:00
|
|
|
if !config.NTP.DisableFailure {
|
|
|
|
failures = append(failures, "ntp")
|
|
|
|
}
|
2021-09-17 09:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(failures) != 0 {
|
2022-08-08 21:50:12 +00:00
|
|
|
log.Fatalf("The following providers had fatal failures during startup: %s", strings.Join(failures, ", "))
|
2021-09-17 09:53:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
func doStartupCheck(logger *logrus.Logger, name string, provider model.StartupCheck, disabled bool) error {
|
2021-09-17 09:53:59 +00: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")
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:07:53 +00:00
|
|
|
return provider.StartupCheck()
|
2021-09-17 09:53:59 +00:00
|
|
|
}
|