_validation.tpl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. {{- define "rsync.validation" -}}
  2. {{- $reservedParams := (list
  3. "port" "use chroot" "pid file"
  4. "max connections" "log file"
  5. ) -}}
  6. {{- range .Values.rsyncConfig.auxParams -}}
  7. {{- include "rsync.aux.validation" (dict "aux" .) -}}
  8. {{- if mustHas .param $reservedParams -}}
  9. {{- fail (printf "Rsync - Overriding parameter [%v] is not allowed." .param) -}}
  10. {{- end -}}
  11. {{- end -}}
  12. {{- if not .Values.rsyncModules -}}
  13. {{- fail "Rsync - At least one module must be configured." -}}
  14. {{- end -}}
  15. {{- end -}}
  16. {{- define "rsync.module.validation" -}}
  17. {{- $mod := .mod -}}
  18. {{- if not $mod.name -}}
  19. {{- fail "Rsync - [Module Name] is required." -}}
  20. {{- end -}}
  21. {{- if not (mustRegexMatch "^[a-zA-Z0-9]+([_-]*[a-zA-Z0-9]+)+$" $mod.name) -}}
  22. {{- $allow := "Can include [Letters (a-z, A-Z), Numbers (0,9), Underscore (_), Dash (-)]" -}}
  23. {{- $disallow := "But cannot start or end with [Underscore (_), Dash (-), Dot (.)]" -}}
  24. {{- fail (printf "Rsync - Module Name [%v] has invalid naming format. %v %v" $mod.name $allow $disallow) -}}
  25. {{- end -}}
  26. {{- if not $mod.hostPath -}}
  27. {{- fail (printf "Rsync - [Host Path] on module [%v] is required." $mod.name) -}}
  28. {{- end -}}
  29. {{- $modes := (list "RO" "RW" "WO") -}}
  30. {{- if not (mustHas $mod.accessMode $modes) -}}
  31. {{- fail (printf "Rsync - [Access Mode] must be one of [%v] on module [%v], but got [%v]." (join ", " $modes) $mod.name $mod.accessMode) -}}
  32. {{- end -}}
  33. {{- if kindIs "invalid" $mod.maxConnections -}}
  34. {{- fail (printf "Rsync - [Max Connections] on module [%v] is required." $mod.name) -}}
  35. {{- end -}}
  36. {{- if kindIs "invalid" $mod.uid -}}
  37. {{- fail (printf "Rsync - [User] on module [%v] is required." $mod.name) -}}
  38. {{- end -}}
  39. {{- if kindIs "invalid" $mod.gid -}}
  40. {{- fail (printf "Rsync - [Group] on module [%v] is required." $mod.name) -}}
  41. {{- end -}}
  42. {{- range $entry := $mod.hostsAllow -}}
  43. {{- if not $entry -}}
  44. {{- fail (printf "Rsync - Entry [%v] in [Hosts Allow] on module [%v] cannot be empty." $entry $mod.name) -}}
  45. {{- end -}}
  46. {{- end -}}
  47. {{- range $entry := $mod.hostsDeny -}}
  48. {{- if not $entry -}}
  49. {{- fail (printf "Rsync - Entry [%v] in [Hosts Deny] on module [%v] cannot be empty." $entry $mod.name) -}}
  50. {{- end -}}
  51. {{- end -}}
  52. {{- range $mod.auxParams -}}
  53. {{- include "rsync.aux.validation" (dict "aux" .) -}}
  54. {{- end -}}
  55. {{- end -}}
  56. {{- define "rsync.aux.validation" -}}
  57. {{- $aux := .aux -}}
  58. {{- if not $aux.param -}}
  59. {{- fail "Rsync - Parameter name is required." -}}
  60. {{- end -}}
  61. {{- if not $aux.value -}}
  62. {{- fail (printf "Rsync - Value on parameter [%v] is required." $aux.param) -}}
  63. {{- end -}}
  64. {{- end -}}