/* Copyright © 2023 Johannes Bülow This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */package cmd import ( "fmt" "log" "os" "path/filepath" "github.com/spf13/cobra" "github.com/spf13/viper" "git.jmbit.de/filegate/filegate/db" "git.jmbit.de/filegate/filegate/files" "git.jmbit.de/filegate/filegate/pods" "git.jmbit.de/filegate/filegate/utils" "git.jmbit.de/filegate/filegate/web" ) var cfgFile string var localfs string var production bool // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "filegate", Short: "Manage the Download and analsyis of suspicious files", Long: `A Web application that allows you to safely work with suspicious files and make their handling and processing easier for the IT team and all other staff`, Run: func(cmd *cobra.Command, args []string) { db.ConnectDB() // Check if local Filesystem should be used log.Print(localfs, viper.GetString("localfs")) // override any Filesystem declared via config file with the cli arg viper.Set("localfs", localfs) if viper.GetString("localfs") == "" { files.MinioConnect() files.MinioSetup() } pods.ConnectSocket() web.Router(production) utils.DropPrivileges() }, } // 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() { cobra.OnInitialize(initConfig) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, // will be global for your application. rootCmd.PersistentFlags(). StringVarP(&cfgFile, "config", "c", "", "config file (default is /etc/filegate.yaml)") rootCmd.PersistentFlags(). BoolVarP(&production, "production", "p", true, "Toggle production use") rootCmd.PersistentFlags(). StringVarP(&localfs, "localfs", "l", "", "Use this directory instead of S3") // Cobra also supports local flags, which will only run // when this action is called directly. } // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { executable, err := os.Executable() cobra.CheckErr(err) currentPath := filepath.Dir(executable) log.Println(currentPath) // Search config in local and /etc/filegate directory with name "config.yaml". viper.AddConfigPath(currentPath) // viper.AddConfigPath("/etc/filegate/") viper.SetConfigType("yaml") viper.SetConfigName("config") } // Webserver Config viper.SetDefault("web.trustedProxies", []string{"127.0.0.1"}) viper.SetDefault("web.address", "127.0.0.1:8080") SessionKey, err := utils.RandomString(64) if err != nil { log.Fatal("Could not create session secret") } viper.SetDefault("web.sessionKey", SessionKey) // Database Config viper.SetDefault("db.type", "sqlite") viper.SetDefault("db.host", "localhost") viper.SetDefault("db.user", "dbuser") viper.SetDefault("db.path", "./db.sqlite") viper.SetDefault("db.password", "dbpw") viper.SetDefault("db.port", 5432) viper.SetDefault("db.sslmode", "disable") // Minio (S3 Object Storage) viper.SetDefault("minio.accessKeyID", "MINIO_ACCESS_KEY") viper.SetDefault("minio.accessKeySecret", "MINIO_ACCESS_SECRET") viper.SetDefault("minio.hostname", "s3.example.com") viper.SetDefault("minio.port", "9000") viper.SetDefault("minio.sslmode", "true") viper.SetDefault("minio.bucket", "MINIO_BUCKET") viper.SetDefault("minio.location", "MINIO_LOCATION") // General Runtime configs viper.SetDefault("tempfiles", "/var/tmp/filegate/") viper.SetDefault("user", "filegate") viper.SetDefault("group", "filegate") viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } }