2021-08-03 09:55:21 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-09-17 09:53:59 +00:00
|
|
|
"strings"
|
2022-06-14 07:20:13 +00:00
|
|
|
"sync"
|
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"
|
|
|
|
|
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",
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
cmdWithConfigFlags(cmd, false, []string{})
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
cmd.AddCommand(
|
|
|
|
newBuildInfoCmd(),
|
|
|
|
NewCertificatesCmd(),
|
|
|
|
newCompletionCmd(),
|
|
|
|
NewHashPasswordCmd(),
|
|
|
|
NewRSACmd(),
|
2021-11-23 09:45:38 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
doStartupChecks(config, &providers)
|
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
runServers(config, providers, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runServers(config *schema.Configuration, providers middlewares.Providers, logger *logrus.Logger) {
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := startDefaultServer(config, providers)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := startMetricsServer(config, providers)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startDefaultServer(config *schema.Configuration, providers middlewares.Providers) (err error) {
|
|
|
|
svr, listener, err := server.CreateDefaultServer(*config, providers)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
if err = svr.Serve(listener); err != nil {
|
|
|
|
return fmt.Errorf("error occurred during default server operation: %w", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("error occurred during default server startup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startMetricsServer(config *schema.Configuration, providers middlewares.Providers) (err error) {
|
|
|
|
if providers.Metrics == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
svr, listener, err := server.CreateMetricsServer(config.Telemetry.Metrics)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
if err = svr.Serve(listener); err != nil {
|
|
|
|
return fmt.Errorf("error occurred during metrics server operation: %w", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("error occurred during metrics server startup: %w", err)
|
|
|
|
}
|
2022-04-04 23:57:47 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
return nil
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
func doStartupChecks(config *schema.Configuration, providers *middlewares.Providers) {
|
|
|
|
logger := logging.Logger()
|
|
|
|
|
|
|
|
var (
|
|
|
|
failures []string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2021-11-23 09:45:38 +00: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 09:53:59 +00: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)
|
|
|
|
|
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 {
|
|
|
|
logger.Fatalf("The following providers had fatal failures during startup: %s", strings.Join(failures, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|