package cmd import ( "os" "time" "git.1in9.net/raider/wroofauth/internal/keystore" "git.1in9.net/raider/wroofauth/internal/logger" "github.com/lestrrat-go/jwx/jwk" "github.com/spf13/cobra" "github.com/spf13/viper" "go.uber.org/zap" ) var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "wroofauth", Short: "The auth system that might bite.", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./config.yaml)") viper.SetDefault("development", false) viper.BindEnv("development", "DEV") logger.StartLogger() viper.SetDefault("mongo.uri", "mongodb://localhost:27017") viper.SetDefault("mongo.database", "wroofauth") viper.SetDefault("mongo.collection.users", "users") viper.SetDefault("mongo.collection.clients", "clients") viper.SetDefault("mongo.collection.groups", "groups") viper.BindEnv("mongo.uri", "MONGO_URI", "MONGODB_URI") viper.BindEnv("mongo.database", "MONGO_DATABASE", "MONGODB_DATABASE") viper.SetDefault("redis.addr", "localhost:6379") viper.SetDefault("redis.username", "") viper.SetDefault("redis.password", "") viper.SetDefault("redis.db", 0) viper.BindEnv("redis.addr", "REDIS_ADDRESS", "REDIS_ADDR") viper.BindEnv("redis.password", "REDIS_PASSWORD", "REDIS_PASS") viper.BindEnv("redis.db", "REDIS_DB") /*viper.SetDefault("http.wyrd_url", "http://localhost:3001") viper.SetDefault("http.frontend_url", "http://localhost:3000") viper.BindEnv("http.wyrd_url", "WYRD_URL") viper.BindEnv("http.frontend_url", "WYRD_FRONTEND_URL")*/ viper.SetDefault("crypto.keys", jwk.NewSet()) viper.SetDefault("crypto.keyfile", nil) viper.SetDefault("crypto.use_key.frontend", "") viper.BindEnv("crypto.keyfile", "WROOF_KEYS") viper.SetDefault("totp.issuer", "WroofAuth") // Used for 2fa issuer value cobra.OnInitialize(loadConfig) } func loadConfig() { if etcdUrl, found := os.LookupEnv("ETCD_URL"); found { etcdPath, found := os.LookupEnv("ETCD_WROOF_CONFIG") if !found { etcdPath = "/config/wroofauth.json" } viper.SetConfigType("json") viper.AddRemoteProvider("etcd3", etcdUrl, etcdPath) err := viper.ReadRemoteConfig() if err != nil { logger.Sugar.Fatal(err) } go func() { for { time.Sleep(time.Second * 5) err := viper.WatchRemoteConfig() if err != nil { logger.Sugar.Fatal("unable to read remote config: %v", err) continue } } }() } if cfgFile != "" { viper.SetConfigFile(cfgFile) } else { viper.AddConfigPath(".") viper.SetConfigName("config") } err := viper.ReadInConfig() if err != nil { logger.Logger.Fatal("failed to load config", zap.Error(err)) } logger.StartLogger() // Restart Logger, as we may have changed our loglevel logger.Sugar.Info("Using config file:", viper.ConfigFileUsed()) keystore.LoadKeystore() }