|
| 1 | +/* |
| 2 | +Copyright © 2024 Juliano Martinez <juliano@martinez.io> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/spf13/viper" |
| 25 | +) |
| 26 | + |
| 27 | +var logger *slog.Logger |
| 28 | + |
| 29 | +func init() { |
| 30 | + logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 31 | +} |
| 32 | + |
| 33 | +var cfgFile string |
| 34 | + |
| 35 | +// rootCmd represents the base command when called without any subcommands |
| 36 | +var rootCmd = &cobra.Command{ |
| 37 | + Use: "vault-audit-filter", |
| 38 | + Short: "A brief description of your application", |
| 39 | + Long: `A longer description that spans multiple lines and likely contains |
| 40 | +examples and usage of using your application. For example: |
| 41 | +
|
| 42 | +Cobra is a CLI library for Go that empowers applications. |
| 43 | +This application is a tool to generate the needed files |
| 44 | +to quickly create a Cobra application.`, |
| 45 | + // Uncomment the following line if your bare application |
| 46 | + // has an action associated with it: |
| 47 | + // Run: func(cmd *cobra.Command, args []string) { }, |
| 48 | +} |
| 49 | + |
| 50 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 51 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 52 | +func Execute() { |
| 53 | + err := rootCmd.Execute() |
| 54 | + if err != nil { |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func init() { |
| 60 | + cobra.OnInitialize(initConfig) |
| 61 | + |
| 62 | + // Here you will define your flags and configuration settings. |
| 63 | + // Cobra supports persistent flags, which, if defined here, |
| 64 | + // will be global for your application. |
| 65 | + |
| 66 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.vault-audit-filter.yaml)") |
| 67 | + rootCmd.PersistentFlags().String("vault.address", "http://127.0.0.1:8200", "Vault source address") |
| 68 | + rootCmd.PersistentFlags().String("vault.token", "", "Vault source token") |
| 69 | + rootCmd.PersistentFlags().String("vault.audit_path", "/vault-audit-filter", "Vault audit path") |
| 70 | + rootCmd.PersistentFlags().String("vault.audit_address", "127.0.0.1:1269", "Courier audit device address to receive the audit") |
| 71 | + rootCmd.PersistentFlags().String("vault.audit_description", "Courier audit device", "Vault audit description") |
| 72 | + |
| 73 | + // Cobra also supports local flags, which will only run |
| 74 | + // when this action is called directly. |
| 75 | + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 76 | +} |
| 77 | + |
| 78 | +// initConfig reads in config file and ENV variables if set. |
| 79 | +func initConfig() { |
| 80 | + if cfgFile != "" { |
| 81 | + // Use config file from the flag. |
| 82 | + viper.SetConfigFile(cfgFile) |
| 83 | + } else { |
| 84 | + // Find home directory. |
| 85 | + home, err := os.UserHomeDir() |
| 86 | + cobra.CheckErr(err) |
| 87 | + |
| 88 | + // Search config in home directory with name ".vault-audit-filter" (without extension). |
| 89 | + viper.AddConfigPath(home) |
| 90 | + viper.SetConfigType("yaml") |
| 91 | + viper.SetConfigName(".vault-audit-filter") |
| 92 | + } |
| 93 | + |
| 94 | + viper.BindPFlag("vault.address", rootCmd.PersistentFlags().Lookup("vault.address")) |
| 95 | + viper.BindPFlag("vault.token", rootCmd.PersistentFlags().Lookup("vault.token")) |
| 96 | + viper.BindPFlag("vault.audit_path", rootCmd.PersistentFlags().Lookup("vault.audit_path")) |
| 97 | + viper.BindPFlag("vault.audit_address", rootCmd.PersistentFlags().Lookup("vault.audit_address")) |
| 98 | + viper.BindPFlag("vault.audit_description", rootCmd.PersistentFlags().Lookup("vault.audit_description")) |
| 99 | + |
| 100 | + viper.AutomaticEnv() // read in environment variables that match |
| 101 | + |
| 102 | + // If a config file is found, read it in. |
| 103 | + if err := viper.ReadInConfig(); err == nil { |
| 104 | + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) |
| 105 | + } |
| 106 | + |
| 107 | + if viper.GetString("vault.token") == "" { |
| 108 | + logger.Error("vault.token is required") |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + if !viper.IsSet("rule_groups") || len(viper.GetStringSlice("rule_groups")) == 0 { |
| 113 | + logger.Info("No rules defined in configuration; all audit logs will be printed") |
| 114 | + } |
| 115 | +} |
0 commit comments