security.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 muradm <mail@muradm.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests security)
  19. #:use-module (guix gexp)
  20. #:use-module (gnu packages admin)
  21. #:use-module (gnu services)
  22. #:use-module (gnu services security)
  23. #:use-module (gnu services ssh)
  24. #:use-module (gnu system)
  25. #:use-module (gnu system vm)
  26. #:use-module (gnu tests)
  27. #:export (%test-fail2ban-basic
  28. %test-fail2ban-extension
  29. %test-fail2ban-simple))
  30. ;;;
  31. ;;; fail2ban tests
  32. ;;;
  33. (define-syntax-rule (fail2ban-test test-name test-os tests-more ...)
  34. (lambda ()
  35. (define os
  36. (marionette-operating-system
  37. test-os
  38. #:imported-modules '((gnu services herd))))
  39. (define vm
  40. (virtual-machine
  41. (operating-system os)
  42. (port-forwardings '())))
  43. (define test
  44. (with-imported-modules '((gnu build marionette)
  45. (guix build utils))
  46. #~(begin
  47. (use-modules (srfi srfi-64)
  48. (gnu build marionette))
  49. (define marionette (make-marionette (list #$vm)))
  50. (test-runner-current (system-test-runner #$output))
  51. (test-begin test-name)
  52. (test-assert "fail2ban running"
  53. (marionette-eval
  54. '(begin
  55. (use-modules (gnu services herd))
  56. (start-service 'fail2ban))
  57. marionette))
  58. (test-assert "fail2ban socket ready"
  59. (wait-for-unix-socket
  60. "/var/run/fail2ban/fail2ban.sock" marionette))
  61. (test-assert "fail2ban running after restart"
  62. (marionette-eval
  63. '(begin
  64. (use-modules (gnu services herd))
  65. (restart-service 'fail2ban))
  66. marionette))
  67. (test-assert "fail2ban socket ready after restart"
  68. (wait-for-unix-socket
  69. "/var/run/fail2ban/fail2ban.sock" marionette))
  70. (test-assert "fail2ban pid ready"
  71. (marionette-eval
  72. '(file-exists? "/var/run/fail2ban/fail2ban.pid")
  73. marionette))
  74. (test-assert "fail2ban log file"
  75. (marionette-eval
  76. '(file-exists? "/var/log/fail2ban.log")
  77. marionette))
  78. tests-more ...
  79. (test-end))))
  80. (gexp->derivation test-name test)))
  81. (define run-fail2ban-basic-test
  82. (fail2ban-test
  83. "fail2ban-basic-test"
  84. (simple-operating-system
  85. (service fail2ban-service-type))))
  86. (define %test-fail2ban-basic
  87. (system-test
  88. (name "fail2ban-basic")
  89. (description "Test basic fail2ban running capability.")
  90. (value (run-fail2ban-basic-test))))
  91. (define %fail2ban-server-cmd
  92. (program-file
  93. "fail2ban-server-cmd"
  94. #~(begin
  95. (let ((cmd #$(file-append fail2ban "/bin/fail2ban-server")))
  96. (apply execl cmd cmd `("-p" "/var/run/fail2ban/fail2ban.pid"
  97. "-s" "/var/run/fail2ban/fail2ban.sock"
  98. ,@(cdr (program-arguments))))))))
  99. (define run-fail2ban-simple-test
  100. (fail2ban-test
  101. "fail2ban-basic-test"
  102. (simple-operating-system
  103. (service fail2ban-service-type (fail2ban-configuration
  104. (jails (list (fail2ban-jail-configuration
  105. (name "sshd")))))))
  106. (test-equal "fail2ban sshd jail running status output"
  107. '("Status for the jail: sshd"
  108. "|- Filter"
  109. "| |- Currently failed:\t0"
  110. "| |- Total failed:\t0"
  111. "| `- File list:\t/var/log/secure"
  112. "`- Actions"
  113. " |- Currently banned:\t0"
  114. " |- Total banned:\t0"
  115. " `- Banned IP list:\t"
  116. "")
  117. (marionette-eval
  118. '(begin
  119. (use-modules (ice-9 rdelim) (ice-9 popen) (rnrs io ports))
  120. (let ((call-command
  121. (lambda (cmd)
  122. (let* ((err-cons (pipe))
  123. (port (with-error-to-port (cdr err-cons)
  124. (lambda () (open-input-pipe cmd))))
  125. (_ (setvbuf (car err-cons) 'block
  126. (* 1024 1024 16)))
  127. (result (read-delimited "" port)))
  128. (close-port (cdr err-cons))
  129. (values result (read-delimited "" (car err-cons)))))))
  130. (string-split
  131. (call-command
  132. (string-join (list #$%fail2ban-server-cmd "status" "sshd") " "))
  133. #\newline)))
  134. marionette))
  135. (test-equal "fail2ban sshd jail running exit code"
  136. 0
  137. (marionette-eval
  138. '(status:exit-val (system* #$%fail2ban-server-cmd "status" "sshd"))
  139. marionette))))
  140. (define %test-fail2ban-simple
  141. (system-test
  142. (name "fail2ban-simple")
  143. (description "Test simple fail2ban running capability.")
  144. (value (run-fail2ban-simple-test))))
  145. (define run-fail2ban-extension-test
  146. (fail2ban-test
  147. "fail2ban-extension-test"
  148. (simple-operating-system
  149. (service (fail2ban-jail-service openssh-service-type (fail2ban-jail-configuration
  150. (name "sshd") (enabled? #t)))
  151. (openssh-configuration)))
  152. (test-equal "fail2ban sshd jail running status output"
  153. '("Status for the jail: sshd"
  154. "|- Filter"
  155. "| |- Currently failed:\t0"
  156. "| |- Total failed:\t0"
  157. "| `- File list:\t/var/log/secure"
  158. "`- Actions"
  159. " |- Currently banned:\t0"
  160. " |- Total banned:\t0"
  161. " `- Banned IP list:\t"
  162. "")
  163. (marionette-eval
  164. '(begin
  165. (use-modules (ice-9 rdelim) (ice-9 popen) (rnrs io ports))
  166. (let ((call-command
  167. (lambda (cmd)
  168. (let* ((err-cons (pipe))
  169. (port (with-error-to-port (cdr err-cons)
  170. (lambda () (open-input-pipe cmd))))
  171. (_ (setvbuf (car err-cons) 'block
  172. (* 1024 1024 16)))
  173. (result (read-delimited "" port)))
  174. (close-port (cdr err-cons))
  175. (values result (read-delimited "" (car err-cons)))))))
  176. (string-split
  177. (call-command
  178. (string-join (list #$%fail2ban-server-cmd "status" "sshd") " "))
  179. #\newline)))
  180. marionette))
  181. (test-equal "fail2ban sshd jail running exit code"
  182. 0
  183. (marionette-eval
  184. '(status:exit-val (system* #$%fail2ban-server-cmd "status" "sshd"))
  185. marionette))))
  186. (define %test-fail2ban-extension
  187. (system-test
  188. (name "fail2ban-extension")
  189. (description "Test extension fail2ban running capability.")
  190. (value (run-fail2ban-extension-test))))