123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package main
- import (
- "encoding/json"
- "fmt"
- "os"
- "text/template"
- "github.com/antonmedv/expr"
- log "github.com/sirupsen/logrus"
- "github.com/spf13/cobra"
- "gopkg.in/yaml.v2"
- "github.com/crowdsecurity/crowdsec/pkg/csconfig"
- "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
- )
- func showConfigKey(key string) error {
- type Env struct {
- Config *csconfig.Config
- }
- opts := []expr.Option{}
- opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...)
- opts = append(opts, expr.Env(Env{}))
- program, err := expr.Compile(key, opts...)
- if err != nil {
- return err
- }
- output, err := expr.Run(program, Env{Config: csConfig})
- if err != nil {
- return err
- }
- switch csConfig.Cscli.Output {
- case "human", "raw":
- switch output.(type) {
- case string:
- fmt.Printf("%s\n", output)
- case int:
- fmt.Printf("%d\n", output)
- default:
- fmt.Printf("%v\n", output)
- }
- case "json":
- data, err := json.MarshalIndent(output, "", " ")
- if err != nil {
- return fmt.Errorf("failed to marshal configuration: %w", err)
- }
- fmt.Printf("%s\n", string(data))
- }
- return nil
- }
- var configShowTemplate = `Global:
- {{- if .ConfigPaths }}
- - Configuration Folder : {{.ConfigPaths.ConfigDir}}
- - Data Folder : {{.ConfigPaths.DataDir}}
- - Hub Folder : {{.ConfigPaths.HubDir}}
- - Simulation File : {{.ConfigPaths.SimulationFilePath}}
- {{- end }}
- {{- if .Common }}
- - Log Folder : {{.Common.LogDir}}
- - Log level : {{.Common.LogLevel}}
- - Log Media : {{.Common.LogMedia}}
- {{- end }}
- {{- if .Crowdsec }}
- Crowdsec{{if and .Crowdsec.Enable (not (ValueBool .Crowdsec.Enable))}} (disabled){{end}}:
- - Acquisition File : {{.Crowdsec.AcquisitionFilePath}}
- - Parsers routines : {{.Crowdsec.ParserRoutinesCount}}
- {{- if .Crowdsec.AcquisitionDirPath }}
- - Acquisition Folder : {{.Crowdsec.AcquisitionDirPath}}
- {{- end }}
- {{- end }}
- {{- if .Cscli }}
- cscli:
- - Output : {{.Cscli.Output}}
- - Hub Branch : {{.Cscli.HubBranch}}
- {{- end }}
- {{- if .API }}
- {{- if .API.Client }}
- API Client:
- {{- if .API.Client.Credentials }}
- - URL : {{.API.Client.Credentials.URL}}
- - Login : {{.API.Client.Credentials.Login}}
- {{- end }}
- - Credentials File : {{.API.Client.CredentialsFilePath}}
- {{- end }}
- {{- if .API.Server }}
- Local API Server{{if and .API.Server.Enable (not (ValueBool .API.Server.Enable))}} (disabled){{end}}:
- - Listen URL : {{.API.Server.ListenURI}}
- - Profile File : {{.API.Server.ProfilesPath}}
- {{- if .API.Server.TLS }}
- {{- if .API.Server.TLS.CertFilePath }}
- - Cert File : {{.API.Server.TLS.CertFilePath}}
- {{- end }}
- {{- if .API.Server.TLS.KeyFilePath }}
- - Key File : {{.API.Server.TLS.KeyFilePath}}
- {{- end }}
- {{- if .API.Server.TLS.CACertPath }}
- - CA Cert : {{.API.Server.TLS.CACertPath}}
- {{- end }}
- {{- if .API.Server.TLS.CRLPath }}
- - CRL : {{.API.Server.TLS.CRLPath}}
- {{- end }}
- {{- if .API.Server.TLS.CacheExpiration }}
- - Cache Expiration : {{.API.Server.TLS.CacheExpiration}}
- {{- end }}
- {{- if .API.Server.TLS.ClientVerification }}
- - Client Verification : {{.API.Server.TLS.ClientVerification}}
- {{- end }}
- {{- if .API.Server.TLS.AllowedAgentsOU }}
- {{- range .API.Server.TLS.AllowedAgentsOU }}
- - Allowed Agents OU : {{.}}
- {{- end }}
- {{- end }}
- {{- if .API.Server.TLS.AllowedBouncersOU }}
- {{- range .API.Server.TLS.AllowedBouncersOU }}
- - Allowed Bouncers OU : {{.}}
- {{- end }}
- {{- end }}
- {{- end }}
- - Trusted IPs:
- {{- range .API.Server.TrustedIPs }}
- - {{.}}
- {{- end }}
- {{- if and .API.Server.OnlineClient .API.Server.OnlineClient.Credentials }}
- Central API:
- - URL : {{.API.Server.OnlineClient.Credentials.URL}}
- - Login : {{.API.Server.OnlineClient.Credentials.Login}}
- - Credentials File : {{.API.Server.OnlineClient.CredentialsFilePath}}
- {{- end }}
- {{- end }}
- {{- end }}
- {{- if .DbConfig }}
- - Database:
- - Type : {{.DbConfig.Type}}
- {{- if eq .DbConfig.Type "sqlite" }}
- - Path : {{.DbConfig.DbPath}}
- {{- else}}
- - Host : {{.DbConfig.Host}}
- - Port : {{.DbConfig.Port}}
- - User : {{.DbConfig.User}}
- - DB Name : {{.DbConfig.DbName}}
- {{- end }}
- {{- if .DbConfig.MaxOpenConns }}
- - Max Open Conns : {{.DbConfig.MaxOpenConns}}
- {{- end }}
- {{- if ne .DbConfig.DecisionBulkSize 0 }}
- - Decision Bulk Size : {{.DbConfig.DecisionBulkSize}}
- {{- end }}
- {{- if .DbConfig.Flush }}
- {{- if .DbConfig.Flush.MaxAge }}
- - Flush age : {{.DbConfig.Flush.MaxAge}}
- {{- end }}
- {{- if .DbConfig.Flush.MaxItems }}
- - Flush size : {{.DbConfig.Flush.MaxItems}}
- {{- end }}
- {{- end }}
- {{- end }}
- `
- func runConfigShow(cmd *cobra.Command, args []string) error {
- flags := cmd.Flags()
- if err := csConfig.LoadAPIClient(); err != nil {
- log.Errorf("failed to load API client configuration: %s", err)
- // don't return, we can still show the configuration
- }
- key, err := flags.GetString("key")
- if err != nil {
- return err
- }
- if key != "" {
- return showConfigKey(key)
- }
- switch csConfig.Cscli.Output {
- case "human":
- // The tests on .Enable look funny because the option has a true default which has
- // not been set yet (we don't really load the LAPI) and go templates don't dereference
- // pointers in boolean tests. Prefix notation is the cherry on top.
- funcs := template.FuncMap{
- // can't use generics here
- "ValueBool": func(b *bool) bool { return b!=nil && *b },
- }
- tmp, err := template.New("config").Funcs(funcs).Parse(configShowTemplate)
- if err != nil {
- return err
- }
- err = tmp.Execute(os.Stdout, csConfig)
- if err != nil {
- return err
- }
- case "json":
- data, err := json.MarshalIndent(csConfig, "", " ")
- if err != nil {
- return fmt.Errorf("failed to marshal configuration: %w", err)
- }
- fmt.Printf("%s\n", string(data))
- case "raw":
- data, err := yaml.Marshal(csConfig)
- if err != nil {
- return fmt.Errorf("failed to marshal configuration: %w", err)
- }
- fmt.Printf("%s\n", string(data))
- }
- return nil
- }
- func NewConfigShowCmd() *cobra.Command {
- cmdConfigShow := &cobra.Command{
- Use: "show",
- Short: "Displays current config",
- Long: `Displays the current cli configuration.`,
- Args: cobra.ExactArgs(0),
- DisableAutoGenTag: true,
- RunE: runConfigShow,
- }
- flags := cmdConfigShow.Flags()
- flags.StringP("key", "", "", "Display only this value (Config.API.Server.ListenURI)")
- return cmdConfigShow
- }
|