config_show.go 6.5 KB

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