09_time 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Ensure that time constraints are properly rendered.
  2. -- Testcase --
  3. {%
  4. include("./root/usr/share/firewall4/main.uc", {
  5. getenv: function(varname) {
  6. switch (varname) {
  7. case 'ACTION':
  8. return 'print';
  9. }
  10. }
  11. })
  12. %}
  13. -- End --
  14. -- File uci/helpers.json --
  15. {}
  16. -- End --
  17. -- File uci/firewall.json --
  18. {
  19. "rule": [
  20. {
  21. ".description": "Check parsing a complete ISO datetime stamp",
  22. "name": "Time rule #1",
  23. "proto": "all",
  24. "start_date": "2022-05-30T21:51:23",
  25. "target": "ACCEPT"
  26. },
  27. {
  28. ".description": "Check parsing a datetime stamp without seconds",
  29. "name": "Time rule #2",
  30. "proto": "all",
  31. "start_date": "2022-05-30T21:51",
  32. "target": "ACCEPT"
  33. },
  34. {
  35. ".description": "Check parsing a datetime stamp without minutes and seconds",
  36. "name": "Time rule #3",
  37. "proto": "all",
  38. "start_date": "2022-05-30T21",
  39. "target": "ACCEPT"
  40. },
  41. {
  42. ".description": "Check parsing a datetime stamp without time",
  43. "name": "Time rule #4",
  44. "proto": "all",
  45. "start_date": "2022-05-30",
  46. "target": "ACCEPT"
  47. },
  48. {
  49. ".description": "Check parsing a datetime stamp without day and time",
  50. "name": "Time rule #5",
  51. "proto": "all",
  52. "start_date": "2022-05",
  53. "target": "ACCEPT"
  54. },
  55. {
  56. ".description": "Check parsing a datetime stamp without month, day and time",
  57. "name": "Time rule #6",
  58. "proto": "all",
  59. "start_date": "2022",
  60. "target": "ACCEPT"
  61. },
  62. {
  63. ".description": "Check parsing a complete timestamp",
  64. "name": "Time rule #7",
  65. "proto": "all",
  66. "start_time": "21:51:23",
  67. "target": "ACCEPT"
  68. },
  69. {
  70. ".description": "Check parsing a timestamp without seconds",
  71. "name": "Time rule #8",
  72. "proto": "all",
  73. "start_time": "21:51",
  74. "target": "ACCEPT"
  75. },
  76. {
  77. ".description": "Check parsing a timestamp without minutes and seconds",
  78. "name": "Time rule #9",
  79. "proto": "all",
  80. "start_time": "21",
  81. "target": "ACCEPT"
  82. },
  83. {
  84. ".description": "Check emitting datetime ranges",
  85. "name": "Time rule #10",
  86. "proto": "all",
  87. "start_date": "2022-05-30T21:51:23",
  88. "stop_date": "2022-06-01T23:51:23",
  89. "target": "ACCEPT"
  90. },
  91. {
  92. ".description": "Check emitting time ranges",
  93. "name": "Time rule #11",
  94. "proto": "all",
  95. "start_time": "21:51:23",
  96. "stop_time": "23:51:23",
  97. "target": "ACCEPT"
  98. },
  99. {
  100. ".description": "Check parsing weekdays",
  101. "name": "Time rule #12",
  102. "proto": "all",
  103. "weekdays": "Monday tuEsday wed SUN Th",
  104. "target": "ACCEPT"
  105. },
  106. ]
  107. }
  108. -- End --
  109. -- Expect stdout --
  110. table inet fw4
  111. flush table inet fw4
  112. table inet fw4 {
  113. #
  114. # Defines
  115. #
  116. #
  117. # User includes
  118. #
  119. include "/etc/nftables.d/*.nft"
  120. #
  121. # Filter rules
  122. #
  123. chain input {
  124. type filter hook input priority filter; policy drop;
  125. iif "lo" accept comment "!fw4: Accept traffic from loopback"
  126. ct state vmap { established : accept, related : accept } comment "!fw4: Handle inbound flows"
  127. }
  128. chain forward {
  129. type filter hook forward priority filter; policy drop;
  130. ct state vmap { established : accept, related : accept } comment "!fw4: Handle forwarded flows"
  131. }
  132. chain output {
  133. type filter hook output priority filter; policy drop;
  134. oif "lo" accept comment "!fw4: Accept traffic towards loopback"
  135. ct state vmap { established : accept, related : accept } comment "!fw4: Handle outbound flows"
  136. meta time >= "2022-05-30 21:51:23" counter accept comment "!fw4: Time rule #1"
  137. meta time >= "2022-05-30 21:51:00" counter accept comment "!fw4: Time rule #2"
  138. meta time >= "2022-05-30 21:00:00" counter accept comment "!fw4: Time rule #3"
  139. meta time >= "2022-05-30 00:00:00" counter accept comment "!fw4: Time rule #4"
  140. meta time >= "2022-05-01 00:00:00" counter accept comment "!fw4: Time rule #5"
  141. meta time >= "2022-01-01 00:00:00" counter accept comment "!fw4: Time rule #6"
  142. meta hour >= "21:51:23" counter accept comment "!fw4: Time rule #7"
  143. meta hour >= "21:51:00" counter accept comment "!fw4: Time rule #8"
  144. meta hour >= "21:00:00" counter accept comment "!fw4: Time rule #9"
  145. meta time "2022-05-30 21:51:23"-"2022-06-01 23:51:23" counter accept comment "!fw4: Time rule #10"
  146. meta hour "21:51:23"-"23:51:23" counter accept comment "!fw4: Time rule #11"
  147. meta day { "Monday", "Tuesday", "Wednesday", "Sunday", "Thursday" } counter accept comment "!fw4: Time rule #12"
  148. }
  149. chain prerouting {
  150. type filter hook prerouting priority filter; policy accept;
  151. }
  152. chain handle_reject {
  153. meta l4proto tcp reject with tcp reset comment "!fw4: Reject TCP traffic"
  154. reject with icmpx type port-unreachable comment "!fw4: Reject any other traffic"
  155. }
  156. #
  157. # NAT rules
  158. #
  159. chain dstnat {
  160. type nat hook prerouting priority dstnat; policy accept;
  161. }
  162. chain srcnat {
  163. type nat hook postrouting priority srcnat; policy accept;
  164. }
  165. #
  166. # Raw rules (notrack)
  167. #
  168. chain raw_prerouting {
  169. type filter hook prerouting priority raw; policy accept;
  170. }
  171. chain raw_output {
  172. type filter hook output priority raw; policy accept;
  173. }
  174. #
  175. # Mangle rules
  176. #
  177. chain mangle_prerouting {
  178. type filter hook prerouting priority mangle; policy accept;
  179. }
  180. chain mangle_postrouting {
  181. type filter hook postrouting priority mangle; policy accept;
  182. }
  183. chain mangle_input {
  184. type filter hook input priority mangle; policy accept;
  185. }
  186. chain mangle_output {
  187. type route hook output priority mangle; policy accept;
  188. }
  189. chain mangle_forward {
  190. type filter hook forward priority mangle; policy accept;
  191. }
  192. }
  193. -- End --