github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/config_show.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "text/template" 8 9 "github.com/antonmedv/expr" 10 "github.com/sanity-io/litter" 11 log "github.com/sirupsen/logrus" 12 "github.com/spf13/cobra" 13 "gopkg.in/yaml.v2" 14 15 "github.com/crowdsecurity/crowdsec/pkg/csconfig" 16 "github.com/crowdsecurity/crowdsec/pkg/exprhelpers" 17 ) 18 19 func showConfigKey(key string) error { 20 type Env struct { 21 Config *csconfig.Config 22 } 23 24 opts := []expr.Option{} 25 opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...) 26 opts = append(opts, expr.Env(Env{})) 27 28 program, err := expr.Compile(key, opts...) 29 if err != nil { 30 return err 31 } 32 33 output, err := expr.Run(program, Env{Config: csConfig}) 34 if err != nil { 35 return err 36 } 37 38 switch csConfig.Cscli.Output { 39 case "human", "raw": 40 // Don't use litter for strings, it adds quotes 41 // that we didn't have before 42 switch output.(type) { 43 case string: 44 fmt.Println(output) 45 default: 46 litter.Dump(output) 47 } 48 case "json": 49 data, err := json.MarshalIndent(output, "", " ") 50 if err != nil { 51 return fmt.Errorf("failed to marshal configuration: %w", err) 52 } 53 54 fmt.Printf("%s\n", string(data)) 55 } 56 57 return nil 58 } 59 60 var configShowTemplate = `Global: 61 62 {{- if .ConfigPaths }} 63 - Configuration Folder : {{.ConfigPaths.ConfigDir}} 64 - Data Folder : {{.ConfigPaths.DataDir}} 65 - Hub Folder : {{.ConfigPaths.HubDir}} 66 - Simulation File : {{.ConfigPaths.SimulationFilePath}} 67 {{- end }} 68 69 {{- if .Common }} 70 - Log Folder : {{.Common.LogDir}} 71 - Log level : {{.Common.LogLevel}} 72 - Log Media : {{.Common.LogMedia}} 73 {{- end }} 74 75 {{- if .Crowdsec }} 76 Crowdsec{{if and .Crowdsec.Enable (not (ValueBool .Crowdsec.Enable))}} (disabled){{end}}: 77 - Acquisition File : {{.Crowdsec.AcquisitionFilePath}} 78 - Parsers routines : {{.Crowdsec.ParserRoutinesCount}} 79 {{- if .Crowdsec.AcquisitionDirPath }} 80 - Acquisition Folder : {{.Crowdsec.AcquisitionDirPath}} 81 {{- end }} 82 {{- end }} 83 84 {{- if .Cscli }} 85 cscli: 86 - Output : {{.Cscli.Output}} 87 - Hub Branch : {{.Cscli.HubBranch}} 88 {{- end }} 89 90 {{- if .API }} 91 {{- if .API.Client }} 92 API Client: 93 {{- if .API.Client.Credentials }} 94 - URL : {{.API.Client.Credentials.URL}} 95 - Login : {{.API.Client.Credentials.Login}} 96 {{- end }} 97 - Credentials File : {{.API.Client.CredentialsFilePath}} 98 {{- end }} 99 100 {{- if .API.Server }} 101 Local API Server{{if and .API.Server.Enable (not (ValueBool .API.Server.Enable))}} (disabled){{end}}: 102 - Listen URL : {{.API.Server.ListenURI}} 103 - Listen Socket : {{.API.Server.ListenSocket}} 104 - Profile File : {{.API.Server.ProfilesPath}} 105 106 {{- if .API.Server.TLS }} 107 {{- if .API.Server.TLS.CertFilePath }} 108 - Cert File : {{.API.Server.TLS.CertFilePath}} 109 {{- end }} 110 111 {{- if .API.Server.TLS.KeyFilePath }} 112 - Key File : {{.API.Server.TLS.KeyFilePath}} 113 {{- end }} 114 115 {{- if .API.Server.TLS.CACertPath }} 116 - CA Cert : {{.API.Server.TLS.CACertPath}} 117 {{- end }} 118 119 {{- if .API.Server.TLS.CRLPath }} 120 - CRL : {{.API.Server.TLS.CRLPath}} 121 {{- end }} 122 123 {{- if .API.Server.TLS.CacheExpiration }} 124 - Cache Expiration : {{.API.Server.TLS.CacheExpiration}} 125 {{- end }} 126 127 {{- if .API.Server.TLS.ClientVerification }} 128 - Client Verification : {{.API.Server.TLS.ClientVerification}} 129 {{- end }} 130 131 {{- if .API.Server.TLS.AllowedAgentsOU }} 132 {{- range .API.Server.TLS.AllowedAgentsOU }} 133 - Allowed Agents OU : {{.}} 134 {{- end }} 135 {{- end }} 136 137 {{- if .API.Server.TLS.AllowedBouncersOU }} 138 {{- range .API.Server.TLS.AllowedBouncersOU }} 139 - Allowed Bouncers OU : {{.}} 140 {{- end }} 141 {{- end }} 142 {{- end }} 143 144 - Trusted IPs: 145 {{- range .API.Server.TrustedIPs }} 146 - {{.}} 147 {{- end }} 148 149 {{- if and .API.Server.OnlineClient .API.Server.OnlineClient.Credentials }} 150 Central API: 151 - URL : {{.API.Server.OnlineClient.Credentials.URL}} 152 - Login : {{.API.Server.OnlineClient.Credentials.Login}} 153 - Credentials File : {{.API.Server.OnlineClient.CredentialsFilePath}} 154 {{- end }} 155 {{- end }} 156 {{- end }} 157 158 {{- if .DbConfig }} 159 - Database: 160 - Type : {{.DbConfig.Type}} 161 {{- if eq .DbConfig.Type "sqlite" }} 162 - Path : {{.DbConfig.DbPath}} 163 {{- else}} 164 - Host : {{.DbConfig.Host}} 165 - Port : {{.DbConfig.Port}} 166 - User : {{.DbConfig.User}} 167 - DB Name : {{.DbConfig.DbName}} 168 {{- end }} 169 {{- if .DbConfig.MaxOpenConns }} 170 - Max Open Conns : {{.DbConfig.MaxOpenConns}} 171 {{- end }} 172 {{- if ne .DbConfig.DecisionBulkSize 0 }} 173 - Decision Bulk Size : {{.DbConfig.DecisionBulkSize}} 174 {{- end }} 175 {{- if .DbConfig.Flush }} 176 {{- if .DbConfig.Flush.MaxAge }} 177 - Flush age : {{.DbConfig.Flush.MaxAge}} 178 {{- end }} 179 {{- if .DbConfig.Flush.MaxItems }} 180 - Flush size : {{.DbConfig.Flush.MaxItems}} 181 {{- end }} 182 {{- end }} 183 {{- end }} 184 ` 185 186 func (cli *cliConfig) show(key string) error { 187 cfg := cli.cfg() 188 189 if err := cfg.LoadAPIClient(); err != nil { 190 log.Errorf("failed to load API client configuration: %s", err) 191 // don't return, we can still show the configuration 192 } 193 194 if key != "" { 195 return showConfigKey(key) 196 } 197 198 switch cfg.Cscli.Output { 199 case "human": 200 // The tests on .Enable look funny because the option has a true default which has 201 // not been set yet (we don't really load the LAPI) and go templates don't dereference 202 // pointers in boolean tests. Prefix notation is the cherry on top. 203 funcs := template.FuncMap{ 204 // can't use generics here 205 "ValueBool": func(b *bool) bool { return b != nil && *b }, 206 } 207 208 tmp, err := template.New("config").Funcs(funcs).Parse(configShowTemplate) 209 if err != nil { 210 return err 211 } 212 213 err = tmp.Execute(os.Stdout, cfg) 214 if err != nil { 215 return err 216 } 217 case "json": 218 data, err := json.MarshalIndent(cfg, "", " ") 219 if err != nil { 220 return fmt.Errorf("failed to marshal configuration: %w", err) 221 } 222 223 fmt.Printf("%s\n", string(data)) 224 case "raw": 225 data, err := yaml.Marshal(cfg) 226 if err != nil { 227 return fmt.Errorf("failed to marshal configuration: %w", err) 228 } 229 230 fmt.Printf("%s\n", string(data)) 231 } 232 233 return nil 234 } 235 236 func (cli *cliConfig) newShowCmd() *cobra.Command { 237 var key string 238 239 cmd := &cobra.Command{ 240 Use: "show", 241 Short: "Displays current config", 242 Long: `Displays the current cli configuration.`, 243 Args: cobra.ExactArgs(0), 244 DisableAutoGenTag: true, 245 RunE: func(_ *cobra.Command, _ []string) error { 246 return cli.show(key) 247 }, 248 } 249 250 flags := cmd.Flags() 251 flags.StringVarP(&key, "key", "", "", "Display only this value (Config.API.Server.ListenURI)") 252 253 return cmd 254 }