4
0

log_arguments.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package executor
  2. import (
  3. "strings"
  4. config "github.com/OliveTin/OliveTin/internal/config"
  5. "github.com/OliveTin/OliveTin/internal/entities"
  6. "github.com/OliveTin/OliveTin/internal/tpl"
  7. )
  8. func argumentTypeStorableInLog(argType string) bool {
  9. switch argType {
  10. case "password", "very_dangerous_raw_string":
  11. return false
  12. default:
  13. return true
  14. }
  15. }
  16. func storableArgumentNames(action *config.Action) map[string]struct{} {
  17. if action == nil {
  18. return nil
  19. }
  20. names := make(map[string]struct{}, len(action.Arguments))
  21. for i := range action.Arguments {
  22. arg := &action.Arguments[i]
  23. if !argumentTypeStorableInLog(arg.Type) {
  24. continue
  25. }
  26. names[arg.Name] = struct{}{}
  27. }
  28. return names
  29. }
  30. func storableArgumentNamesFromRequest(req *ExecutionRequest) map[string]struct{} {
  31. if req == nil || req.Binding == nil || req.Binding.Action == nil {
  32. return nil
  33. }
  34. return storableArgumentNames(req.Binding.Action)
  35. }
  36. func isStorableArgumentName(name string, allowedNames map[string]struct{}) bool {
  37. if strings.HasPrefix(name, config.ReservedArgumentNamePrefix) {
  38. return false
  39. }
  40. _, ok := allowedNames[name]
  41. return ok
  42. }
  43. func collectStorableArguments(args map[string]string, allowedNames map[string]struct{}) map[string]string {
  44. result := make(map[string]string)
  45. for name, value := range args {
  46. if isStorableArgumentName(name, allowedNames) {
  47. result[name] = value
  48. }
  49. }
  50. return result
  51. }
  52. func filterStorableArguments(args map[string]string, allowedNames map[string]struct{}) map[string]string {
  53. if len(args) == 0 {
  54. return nil
  55. }
  56. result := collectStorableArguments(args, allowedNames)
  57. if len(result) == 0 {
  58. return nil
  59. }
  60. return result
  61. }
  62. func storableArgumentsFromRequest(req *ExecutionRequest) map[string]string {
  63. allowedNames := storableArgumentNamesFromRequest(req)
  64. if len(allowedNames) == 0 {
  65. return nil
  66. }
  67. return filterStorableArguments(req.Arguments, allowedNames)
  68. }
  69. func copyStorableArgumentsToLogEntry(req *ExecutionRequest) {
  70. args := storableArgumentsFromRequest(req)
  71. if args == nil || req.logEntry == nil {
  72. return
  73. }
  74. req.mutateLogEntry(func(entry *InternalLogEntry) {
  75. entry.Arguments = args
  76. })
  77. }
  78. func restartArgumentMissingFromStored(arg *config.ActionArgument, entity *entities.Entity, storedArgs map[string]string) bool {
  79. if !argumentTypeStorableInLog(arg.Type) {
  80. return true
  81. }
  82. if !restartArgumentRequired(arg, entity) {
  83. return false
  84. }
  85. if storedArgs == nil {
  86. return true
  87. }
  88. _, ok := storedArgs[arg.Name]
  89. return !ok
  90. }
  91. func RestartArgumentsIncomplete(action *config.Action, entity *entities.Entity, storedArgs map[string]string) bool {
  92. if action == nil {
  93. return false
  94. }
  95. for i := range action.Arguments {
  96. if restartArgumentMissingFromStored(&action.Arguments[i], entity, storedArgs) {
  97. return true
  98. }
  99. }
  100. return false
  101. }
  102. func restartArgumentRequired(arg *config.ActionArgument, entity *entities.Entity) bool {
  103. if argumentSkipsValidation(arg) {
  104. return false
  105. }
  106. defaultValue := arg.Default
  107. if defaultValue != "" {
  108. defaultValue = tpl.ParseTemplateOfActionBeforeExec(defaultValue, entity)
  109. }
  110. return defaultValue == ""
  111. }