sanitize.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package config
  2. import (
  3. "github.com/google/uuid"
  4. log "github.com/sirupsen/logrus"
  5. "strings"
  6. )
  7. // Sanitize will look for common configuration issues, and fix them. For example,
  8. // populating undefined fields - name -> title, etc.
  9. func (cfg *Config) Sanitize() {
  10. cfg.sanitizeLogLevel()
  11. // log.Infof("cfg %p", cfg)
  12. for idx := range cfg.Actions {
  13. cfg.Actions[idx].sanitize(cfg)
  14. }
  15. }
  16. func (cfg *Config) sanitizeLogLevel() {
  17. if logLevel, err := log.ParseLevel(cfg.LogLevel); err == nil {
  18. log.Info("Setting log level to ", logLevel)
  19. log.SetLevel(logLevel)
  20. }
  21. }
  22. func (action *Action) sanitize(cfg *Config) {
  23. if action.Timeout < 3 {
  24. action.Timeout = 3
  25. }
  26. action.ID = getActionID(action)
  27. action.Icon = lookupHTMLIcon(action.Icon, cfg.DefaultIconForActions)
  28. action.PopupOnStart = sanitizePopupOnStart(action.PopupOnStart, cfg)
  29. if action.MaxConcurrent < 1 {
  30. action.MaxConcurrent = 1
  31. }
  32. for idx := range action.Arguments {
  33. action.Arguments[idx].sanitize()
  34. }
  35. sanitizeAuthRequireGuestsToLogin(cfg)
  36. sanitizeLogHistoryPageSize(cfg)
  37. }
  38. func sanitizeAuthRequireGuestsToLogin(cfg *Config) {
  39. if cfg.AuthRequireGuestsToLogin {
  40. log.Infof("AuthRequireGuestsToLogin is enabled. All defaultPermissions will be set to false")
  41. cfg.DefaultPermissions.View = false
  42. cfg.DefaultPermissions.Exec = false
  43. cfg.DefaultPermissions.Logs = false
  44. }
  45. }
  46. func sanitizeLogHistoryPageSize(cfg *Config) {
  47. if cfg.LogHistoryPageSize < 10 {
  48. log.Warnf("LogsHistoryLimit is too low, setting it to 10")
  49. cfg.LogHistoryPageSize = 10
  50. } else if cfg.LogHistoryPageSize > 100 {
  51. log.Warnf("LogsHistoryLimit is high, you can do this, but expect browser lag.")
  52. }
  53. }
  54. func getActionID(action *Action) string {
  55. if action.ID == "" {
  56. return uuid.NewString()
  57. }
  58. if strings.Contains(action.ID, "{{") {
  59. log.Fatalf("Action IDs cannot contain variables")
  60. }
  61. return action.ID
  62. }
  63. //gocyclo:ignore
  64. func sanitizePopupOnStart(raw string, cfg *Config) string {
  65. switch raw {
  66. case "execution-dialog":
  67. return raw
  68. case "execution-dialog-output-html":
  69. return raw
  70. case "execution-dialog-stdout-only":
  71. return raw
  72. case "execution-button":
  73. return raw
  74. default:
  75. return cfg.DefaultPopupOnStart
  76. }
  77. }
  78. func (arg *ActionArgument) sanitize() {
  79. if arg.Title == "" {
  80. arg.Title = arg.Name
  81. }
  82. for idx, choice := range arg.Choices {
  83. if choice.Title == "" {
  84. arg.Choices[idx].Title = choice.Value
  85. }
  86. }
  87. arg.sanitizeNoType()
  88. // TODO Validate the default against the type checker, but this creates a
  89. // import loop
  90. }
  91. func (arg *ActionArgument) sanitizeNoType() {
  92. if len(arg.Choices) == 0 && arg.Type == "" {
  93. log.WithFields(log.Fields{
  94. "arg": arg.Name,
  95. }).Warn("Argument type isn't set, will default to 'ascii' but this may not be safe. You should set a type specifically.")
  96. arg.Type = "ascii"
  97. }
  98. }