arguments.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package executor
  2. import (
  3. config "github.com/OliveTin/OliveTin/internal/config"
  4. sv "github.com/OliveTin/OliveTin/internal/stringvariables"
  5. log "github.com/sirupsen/logrus"
  6. "errors"
  7. "net/url"
  8. "regexp"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. typecheckRegex = map[string]string{
  14. "very_dangerous_raw_string": "",
  15. "int": "^[\\d]+$",
  16. "ascii": "^[a-zA-Z0-9]+$",
  17. "ascii_identifier": "^[a-zA-Z0-9\\-\\.\\_]+$",
  18. "ascii_sentence": "^[a-zA-Z0-9 \\,\\.]+$",
  19. }
  20. )
  21. func parseActionArguments(rawShellCommand string, values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
  22. log.WithFields(log.Fields{
  23. "actionTitle": actionTitle,
  24. "cmd": rawShellCommand,
  25. }).Infof("Action parse args - Before")
  26. r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
  27. matches := r.FindAllStringSubmatch(rawShellCommand, -1)
  28. for _, match := range matches {
  29. argValue, argProvided := values[match[1]]
  30. if !argProvided {
  31. log.Infof("%v", values)
  32. return "", errors.New("Required arg not provided: " + match[1])
  33. }
  34. err := typecheckActionArgument(match[1], argValue, action)
  35. if err != nil {
  36. return "", err
  37. }
  38. log.WithFields(log.Fields{
  39. "name": match[1],
  40. "value": argValue,
  41. }).Debugf("Arg assigned")
  42. rawShellCommand = strings.ReplaceAll(rawShellCommand, match[0], argValue)
  43. }
  44. rawShellCommand = sv.ReplaceEntityVars(entityPrefix, rawShellCommand)
  45. log.WithFields(log.Fields{
  46. "actionTitle": actionTitle,
  47. "cmd": rawShellCommand,
  48. }).Infof("Action parse args - After")
  49. return rawShellCommand, nil
  50. }
  51. func typecheckActionArgument(name string, value string, action *config.Action) error {
  52. arg := action.FindArg(name)
  53. if arg == nil {
  54. return errors.New("Action arg not defined: " + name)
  55. }
  56. if len(arg.Choices) > 0 {
  57. return typecheckChoice(value, arg)
  58. }
  59. return TypeSafetyCheck(name, value, arg.Type)
  60. }
  61. func typecheckChoice(value string, arg *config.ActionArgument) error {
  62. for _, choice := range arg.Choices {
  63. if value == choice.Value {
  64. return nil
  65. }
  66. }
  67. return errors.New("argument value is not one of the predefined choices")
  68. }
  69. // TypeSafetyCheck checks argument values match a specific type. The types are
  70. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  71. // characters.
  72. func TypeSafetyCheck(name string, value string, argumentType string) error {
  73. if argumentType == "url" {
  74. return typeSafetyCheckUrl(name, value)
  75. }
  76. if argumentType == "datetime" {
  77. _, err := time.Parse("2006-01-02T15:04:05", value)
  78. if err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. return typeSafetyCheckRegex(name, value, argumentType)
  84. }
  85. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  86. pattern, found := typecheckRegex[argumentType]
  87. if !found {
  88. return errors.New("argument type not implemented " + argumentType)
  89. }
  90. matches, _ := regexp.MatchString(pattern, value)
  91. if !matches {
  92. log.WithFields(log.Fields{
  93. "name": name,
  94. "value": value,
  95. "type": argumentType,
  96. }).Warn("Arg type check safety failure")
  97. return errors.New("invalid argument, doesn't match " + argumentType)
  98. }
  99. return nil
  100. }
  101. func typeSafetyCheckUrl(name string, value string) error {
  102. _, err := url.ParseRequestURI(value)
  103. return err
  104. }