all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 4003eff1e54c67c6e53198f5f362128569ce4f09 10514 bytes (raw)
name: gnu/tests/security.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 muradm <mail@muradm.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu tests security)
  #:use-module (guix gexp)
  #:use-module (gnu packages admin)
  #:use-module (gnu services)
  #:use-module (gnu services security)
  #:use-module (gnu services ssh)
  #:use-module (gnu system)
  #:use-module (gnu system vm)
  #:use-module (gnu tests)
  #:export (%test-fail2ban-basic
            %test-fail2ban-simple
            %test-fail2ban-extending))

\f
;;;
;;; fail2ban tests
;;;

(define (run-fail2ban-basic-test)

  (define os
    (marionette-operating-system
     (simple-operating-system
      (service fail2ban-service-type))
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm
    (virtual-machine
     (operating-system os)
     (port-forwardings '())))

  (define test
    (with-imported-modules '((gnu build marionette)
                             (guix build utils))
      #~(begin
          (use-modules (srfi srfi-64)
                       (gnu build marionette))

          (define marionette (make-marionette (list #$vm)))

          (define (wait-for-unix-socket-m socket)
            (wait-for-unix-socket socket marionette))

          (test-runner-current (system-test-runner #$output))
          (test-begin "fail2ban-basic-test")

          (test-assert "fail2ban running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'fail2ban))
             marionette))

          (test-assert "fail2ban socket ready"
            (wait-for-unix-socket-m
             "/var/run/fail2ban/fail2ban.sock"))

          (test-assert "fail2ban pid ready"
            (marionette-eval
             '(file-exists? "/var/run/fail2ban/fail2ban.pid")
             marionette))

          (test-assert "fail2ban log file"
            (marionette-eval
             '(file-exists? "/var/log/fail2ban.log")
             marionette))

          (test-end))))

  (gexp->derivation "fail2ban-basic-test" test))

(define %test-fail2ban-basic
  (system-test
   (name "fail2ban-basic")
   (description "Test basic fail2ban running capability.")
   (value (run-fail2ban-basic-test))))

(define %fail2ban-server-cmd
  (program-file
   "fail2ban-server-cmd"
   #~(begin
       (let ((cmd #$(file-append fail2ban "/bin/fail2ban-server")))
         (apply execl cmd cmd `("-p" "/var/run/fail2ban/fail2ban.pid"
                                "-s" "/var/run/fail2ban/fail2ban.sock"
                                ,@(cdr (program-arguments))))))))

(define (run-fail2ban-simple-test)

  (define os
    (marionette-operating-system
     (simple-operating-system
      (service
       fail2ban-service-type
       (fail2ban-configuration
        (jails
         (list
          (fail2ban-jail-configuration (name "sshd") (enabled #t)))))))
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm
    (virtual-machine
     (operating-system os)
     (port-forwardings '())))

  (define test
    (with-imported-modules '((gnu build marionette)
                             (guix build utils))
      #~(begin
          (use-modules (srfi srfi-64)
                       (ice-9 popen)
                       (ice-9 rdelim)
                       (rnrs io ports)
                       (gnu build marionette)
                       (guix build utils))

          (define marionette (make-marionette (list #$vm)))

          (define (wait-for-unix-socket-m socket)
            (wait-for-unix-socket socket marionette))

          (test-runner-current (system-test-runner #$output))
          (test-begin "fail2ban-simple-test")

          (test-assert "fail2ban running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'fail2ban))
             marionette))

          (test-assert "fail2ban socket ready"
            (wait-for-unix-socket-m
             "/var/run/fail2ban/fail2ban.sock"))

          (test-assert "fail2ban pid ready"
            (marionette-eval
             '(file-exists? "/var/run/fail2ban/fail2ban.pid")
             marionette))

          (test-assert "fail2ban log file"
            (marionette-eval
             '(file-exists? "/var/log/fail2ban.log")
             marionette))

          (test-equal "fail2ban sshd jail running"
            '("Status for the jail: sshd"
              "|- Filter"
              "|  |- Currently failed:\t0"
              "|  |- Total failed:\t0"
              "|  `- File list:\t/var/log/secure"
              "`- Actions"
              "   |- Currently banned:\t0"
              "   |- Total banned:\t0"
              "   `- Banned IP list:\t"
              "")
            (marionette-eval
             '(begin
                (use-modules (ice-9 rdelim) (ice-9 popen) (rnrs io ports))
                (let ((call-command
                       (lambda (cmd)
                         (let* ((err-cons (pipe))
                                (port (with-error-to-port (cdr err-cons)
                                        (lambda () (open-input-pipe cmd))))
                                (_ (setvbuf (car err-cons) 'block
                                            (* 1024 1024 16)))
                                (result (read-delimited "" port)))
                           (close-port (cdr err-cons))
                           (values result (read-delimited "" (car err-cons)))))))
                  (string-split
                   (call-command
                    (string-join (list #$%fail2ban-server-cmd "status" "sshd") " "))
                   #\newline)))
             marionette))

          (test-equal "fail2ban sshd jail running"
            0
            (marionette-eval
             '(status:exit-val (system* #$%fail2ban-server-cmd "status" "sshd"))
             marionette))

          (test-end))))

  (gexp->derivation "fail2ban-simple-test" test))

(define %test-fail2ban-simple
  (system-test
   (name "fail2ban-simple")
   (description "Test simple fail2ban running capability.")
   (value (run-fail2ban-simple-test))))

(define (run-fail2ban-extending-test)

  (define os
    (marionette-operating-system
     (simple-operating-system
      (service
       (fail2ban-jail-service
        openssh-service-type
        (fail2ban-jail-configuration
         (name "sshd") (enabled #t)))
       (openssh-configuration)))
     #:imported-modules '((gnu services herd)
                          (guix combinators))))

  (define vm
    (virtual-machine
     (operating-system os)
     (port-forwardings '())))

  (define test
    (with-imported-modules '((gnu build marionette)
                             (guix build utils))
      #~(begin
          (use-modules (srfi srfi-64)
                       (ice-9 popen)
                       (ice-9 rdelim)
                       (rnrs io ports)
                       (gnu build marionette)
                       (guix build utils))

          (define marionette (make-marionette (list #$vm)))

          (define (wait-for-unix-socket-m socket)
            (wait-for-unix-socket socket marionette))

          (test-runner-current (system-test-runner #$output))
          (test-begin "fail2ban-extending-test")

          (test-assert "sshd running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'ssh-daemon))
             marionette))

          (test-assert "fail2ban socket ready"
            (wait-for-unix-socket-m
             "/var/run/fail2ban/fail2ban.sock"))

          (test-assert "fail2ban pid ready"
            (marionette-eval
             '(file-exists? "/var/run/fail2ban/fail2ban.pid")
             marionette))

          (test-assert "fail2ban log file"
            (marionette-eval
             '(file-exists? "/var/log/fail2ban.log")
             marionette))

          (test-equal "fail2ban sshd jail running"
            '("Status for the jail: sshd"
              "|- Filter"
              "|  |- Currently failed:\t0"
              "|  |- Total failed:\t0"
              "|  `- File list:\t/var/log/secure"
              "`- Actions"
              "   |- Currently banned:\t0"
              "   |- Total banned:\t0"
              "   `- Banned IP list:\t"
              "")
            (marionette-eval
             '(begin
                (use-modules (ice-9 rdelim) (ice-9 popen) (rnrs io ports))
                (let ((call-command
                       (lambda (cmd)
                         (let* ((err-cons (pipe))
                                (port (with-error-to-port (cdr err-cons)
                                        (lambda () (open-input-pipe cmd))))
                                (_ (setvbuf (car err-cons) 'block
                                            (* 1024 1024 16)))
                                (result (read-delimited "" port)))
                           (close-port (cdr err-cons))
                           (values result (read-delimited "" (car err-cons)))))))
                  (string-split
                   (call-command
                    (string-join (list #$%fail2ban-server-cmd "status" "sshd") " "))
                   #\newline)))
             marionette))

          (test-equal "fail2ban sshd jail running"
            0
            (marionette-eval
             '(status:exit-val (system* #$%fail2ban-server-cmd "status" "sshd"))
             marionette))

          (test-end))))

  (gexp->derivation "fail2ban-extending-test" test))

(define %test-fail2ban-extending
  (system-test
   (name "fail2ban-extending")
   (description "Test extending fail2ban running capability.")
   (value (run-fail2ban-extending-test))))

debug log:

solving 4003eff1e5 ...
found 4003eff1e5 in https://yhetil.org/guix/20220822172607.31515-3-mail@muradm.net/

applying [1/1] https://yhetil.org/guix/20220822172607.31515-3-mail@muradm.net/
diff --git a/gnu/tests/security.scm b/gnu/tests/security.scm
new file mode 100644
index 0000000000..4003eff1e5

Checking patch gnu/tests/security.scm...
Applied patch gnu/tests/security.scm cleanly.

index at:
100644 4003eff1e54c67c6e53198f5f362128569ce4f09	gnu/tests/security.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.