config_show.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. - Hub Folder : {{.Cscli.HubDir}}
  73. {{- end }}
  74. {{- if .API }}
  75. {{- if .API.Client }}
  76. API Client:
  77. {{- if .API.Client.Credentials }}
  78. - URL : {{.API.Client.Credentials.URL}}
  79. - Login : {{.API.Client.Credentials.Login}}
  80. {{- end }}
  81. - Credentials File : {{.API.Client.CredentialsFilePath}}
  82. {{- end }}
  83. {{- if .API.Server }}
  84. Local API Server{{if and .API.Server.Enable (not (ValueBool .API.Server.Enable))}} (disabled){{end}}:
  85. - Listen URL : {{.API.Server.ListenURI}}
  86. - Profile File : {{.API.Server.ProfilesPath}}
  87. {{- if .API.Server.TLS }}
  88. {{- if .API.Server.TLS.CertFilePath }}
  89. - Cert File : {{.API.Server.TLS.CertFilePath}}
  90. {{- end }}
  91. {{- if .API.Server.TLS.KeyFilePath }}
  92. - Key File : {{.API.Server.TLS.KeyFilePath}}
  93. {{- end }}
  94. {{- if .API.Server.TLS.CACertPath }}
  95. - CA Cert : {{.API.Server.TLS.CACertPath}}
  96. {{- end }}
  97. {{- if .API.Server.TLS.CRLPath }}
  98. - CRL : {{.API.Server.TLS.CRLPath}}
  99. {{- end }}
  100. {{- if .API.Server.TLS.CacheExpiration }}
  101. - Cache Expiration : {{.API.Server.TLS.CacheExpiration}}
  102. {{- end }}
  103. {{- if .API.Server.TLS.ClientVerification }}
  104. - Client Verification : {{.API.Server.TLS.ClientVerification}}
  105. {{- end }}
  106. {{- if .API.Server.TLS.AllowedAgentsOU }}
  107. {{- range .API.Server.TLS.AllowedAgentsOU }}
  108. - Allowed Agents OU : {{.}}
  109. {{- end }}
  110. {{- end }}
  111. {{- if .API.Server.TLS.AllowedBouncersOU }}
  112. {{- range .API.Server.TLS.AllowedBouncersOU }}
  113. - Allowed Bouncers OU : {{.}}
  114. {{- end }}
  115. {{- end }}
  116. {{- end }}
  117. - Trusted IPs:
  118. {{- range .API.Server.TrustedIPs }}
  119. - {{.}}
  120. {{- end }}
  121. {{- if and .API.Server.OnlineClient .API.Server.OnlineClient.Credentials }}
  122. Central API:
  123. - URL : {{.API.Server.OnlineClient.Credentials.URL}}
  124. - Login : {{.API.Server.OnlineClient.Credentials.Login}}
  125. - Credentials File : {{.API.Server.OnlineClient.CredentialsFilePath}}
  126. {{- end }}
  127. {{- end }}
  128. {{- end }}
  129. {{- if .DbConfig }}
  130. - Database:
  131. - Type : {{.DbConfig.Type}}
  132. {{- if eq .DbConfig.Type "sqlite" }}
  133. - Path : {{.DbConfig.DbPath}}
  134. {{- else}}
  135. - Host : {{.DbConfig.Host}}
  136. - Port : {{.DbConfig.Port}}
  137. - User : {{.DbConfig.User}}
  138. - DB Name : {{.DbConfig.DbName}}
  139. {{- end }}
  140. {{- if .DbConfig.MaxOpenConns }}
  141. - Max Open Conns : {{.DbConfig.MaxOpenConns}}
  142. {{- end }}
  143. {{- if ne .DbConfig.DecisionBulkSize 0 }}
  144. - Decision Bulk Size : {{.DbConfig.DecisionBulkSize}}
  145. {{- end }}
  146. {{- if .DbConfig.Flush }}
  147. {{- if .DbConfig.Flush.MaxAge }}
  148. - Flush age : {{.DbConfig.Flush.MaxAge}}
  149. {{- end }}
  150. {{- if .DbConfig.Flush.MaxItems }}
  151. - Flush size : {{.DbConfig.Flush.MaxItems}}
  152. {{- end }}
  153. {{- end }}
  154. {{- end }}
  155. `
  156. func runConfigShow(cmd *cobra.Command, args []string) error {
  157. flags := cmd.Flags()
  158. if err := csConfig.LoadAPIClient(); err != nil {
  159. log.Errorf("failed to load API client configuration: %s", err)
  160. // don't return, we can still show the configuration
  161. }
  162. key, err := flags.GetString("key")
  163. if err != nil {
  164. return err
  165. }
  166. if key != "" {
  167. return showConfigKey(key)
  168. }
  169. switch csConfig.Cscli.Output {
  170. case "human":
  171. // The tests on .Enable look funny because the option has a true default which has
  172. // not been set yet (we don't really load the LAPI) and go templates don't dereference
  173. // pointers in boolean tests. Prefix notation is the cherry on top.
  174. funcs := template.FuncMap{
  175. // can't use generics here
  176. "ValueBool": func(b *bool) bool { return b!=nil && *b },
  177. }
  178. tmp, err := template.New("config").Funcs(funcs).Parse(configShowTemplate)
  179. if err != nil {
  180. return err
  181. }
  182. err = tmp.Execute(os.Stdout, csConfig)
  183. if err != nil {
  184. return err
  185. }
  186. case "json":
  187. data, err := json.MarshalIndent(csConfig, "", " ")
  188. if err != nil {
  189. return fmt.Errorf("failed to marshal configuration: %w", err)
  190. }
  191. fmt.Printf("%s\n", string(data))
  192. case "raw":
  193. data, err := yaml.Marshal(csConfig)
  194. if err != nil {
  195. return fmt.Errorf("failed to marshal configuration: %w", err)
  196. }
  197. fmt.Printf("%s\n", string(data))
  198. }
  199. return nil
  200. }
  201. func NewConfigShowCmd() *cobra.Command {
  202. cmdConfigShow := &cobra.Command{
  203. Use: "show",
  204. Short: "Displays current config",
  205. Long: `Displays the current cli configuration.`,
  206. Args: cobra.ExactArgs(0),
  207. DisableAutoGenTag: true,
  208. RunE: runConfigShow,
  209. }
  210. flags := cmdConfigShow.Flags()
  211. flags.StringP("key", "", "", "Display only this value (Config.API.Server.ListenURI)")
  212. return cmdConfigShow
  213. }