2021-08-03 16:55:21 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2022-02-28 10:15:01 +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/configuration"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/validator"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2021-08-03 16:55:21 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// cmdWithConfigFlags is used for commands which require access to the configuration to add the flag to the command.
|
2022-02-28 10:15:01 +07:00
|
|
|
func cmdWithConfigFlags(cmd *cobra.Command, persistent bool, configs []string) {
|
|
|
|
if persistent {
|
|
|
|
cmd.PersistentFlags().StringSliceP("config", "c", configs, "configuration files to load")
|
|
|
|
} else {
|
|
|
|
cmd.Flags().StringSliceP("config", "c", configs, "configuration files to load")
|
|
|
|
}
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
var config *schema.Configuration
|
|
|
|
|
|
|
|
func newCmdWithConfigPreRun(ensureConfigExists, validateKeys, validateConfiguration bool) func(cmd *cobra.Command, args []string) {
|
|
|
|
return func(cmd *cobra.Command, _ []string) {
|
2022-02-28 10:15:01 +07:00
|
|
|
var (
|
|
|
|
logger *logrus.Logger
|
|
|
|
configs []string
|
|
|
|
err error
|
|
|
|
)
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
logger = logging.Logger()
|
|
|
|
|
|
|
|
if configs, err = cmd.Flags().GetStringSlice("config"); err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
logger.Fatalf("Error reading flags: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ensureConfigExists && len(configs) == 1 {
|
|
|
|
created, err := configuration.EnsureConfigurationExists(configs[0])
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if created {
|
|
|
|
logger.Warnf("Configuration did not exist so a default one has been generated at %s, you will need to configure this", configs[0])
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
var (
|
|
|
|
val *schema.StructValidator
|
|
|
|
)
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
config, val, err = loadConfig(configs, validateKeys, validateConfiguration)
|
2021-08-03 16:55:21 +07:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("Error occurred loading configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
warnings := val.Warnings()
|
|
|
|
if len(warnings) != 0 {
|
|
|
|
for _, warning := range warnings {
|
|
|
|
logger.Warnf("Configuration: %+v", warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := val.Errors()
|
|
|
|
if len(errs) != 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
logger.Errorf("Configuration: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Fatalf("Can't continue due to the errors loading the configuration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 10:15:01 +07:00
|
|
|
|
|
|
|
func loadConfig(configs []string, validateKeys, validateConfiguration bool) (c *schema.Configuration, val *schema.StructValidator, err error) {
|
|
|
|
var keys []string
|
|
|
|
|
|
|
|
val = schema.NewStructValidator()
|
|
|
|
|
|
|
|
if keys, c, err = configuration.Load(val,
|
|
|
|
configuration.NewDefaultSources(
|
|
|
|
configs,
|
|
|
|
configuration.DefaultEnvPrefix,
|
|
|
|
configuration.DefaultEnvDelimiter)...); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if validateKeys {
|
|
|
|
validator.ValidateKeys(keys, configuration.DefaultEnvPrefix, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if validateConfiguration {
|
|
|
|
validator.ValidateConfiguration(c, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, val, nil
|
|
|
|
}
|