From patchwork Mon Aug 22 17:26:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: muradm X-Patchwork-Id: 41871 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id BD09B27BBEA; Mon, 22 Aug 2022 20:20:31 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id E74B627BBE9 for ; Mon, 22 Aug 2022 20:20:30 +0100 (BST) Received: from localhost ([::1]:48650 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQCy3-0000T2-N6 for patchwork@mira.cbaines.net; Mon, 22 Aug 2022 15:20:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33934) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQBCZ-0007sS-50 for guix-patches@gnu.org; Mon, 22 Aug 2022 13:27:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:51896) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oQBCY-0001sA-SW for guix-patches@gnu.org; Mon, 22 Aug 2022 13:27:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oQBCY-00077s-No for guix-patches@gnu.org; Mon, 22 Aug 2022 13:27:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56608] [PATCH v2 2/2] gnu: tests: Add fail2ban tests. Resent-From: muradm Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 22 Aug 2022 17:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56608 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56608@debbugs.gnu.org, Maxim Cournoyer Received: via spool by 56608-submit@debbugs.gnu.org id=B56608.166118918627335 (code B ref 56608); Mon, 22 Aug 2022 17:27:02 +0000 Received: (at 56608) by debbugs.gnu.org; 22 Aug 2022 17:26:26 +0000 Received: from localhost ([127.0.0.1]:41642 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oQBBx-00076n-TY for submit@debbugs.gnu.org; Mon, 22 Aug 2022 13:26:26 -0400 Received: from nomad-cl1.staging.muradm.net ([139.162.159.157]:48166 helo=nomad-cl1.muradm.net) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oQBBn-000769-Ha for 56608@debbugs.gnu.org; Mon, 22 Aug 2022 13:26:16 -0400 Received: from localhost ([127.0.0.1]:58074) by nomad-cl1.muradm.net with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1oQBAt-0006U8-1Q; Mon, 22 Aug 2022 17:25:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=muradm.net; s=mail; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=8GvMZx8eJA6O0wm5nsULpGla+hWqq1CY4pDpZ+kzMZg=; b=DLGB+1x5WlYldeTsG7+ARSA8kU +jQ1b5OvARbr7lWlgYz0pFC6dxIiHNop32zecEVSGNvWdDJsYHscncDrlGsBZL6MrfE+6WstKRGW3 eFOAij4frCkkc+FfMwZg0jN4Ht0lKOQWXi+qxOfxQtVWMf1c59FmPExhLbFVSKKCE4uF2hdyCYpQp 3vW2e4LomuZCEco2gjrQ540bfpcDjDIBWisPogj8C7ftaPZxV9Bega1ENfTY39eSkcVOTfno8jBzq oyPeODAKaxe7ytwcgnrA4Ocim5mycJL1dix+PnsHnsvS1kX7QmYoPugtLXvFkP/iOLMr0JGDFMHWd lAiS/ip1F32FXKD24q0//fanlujdEI8gmM6P3Pv0NmwLq0WtY0BanK6unpRkfKKJQTEeSSAeAIqHJ 9S/XBVONczRBGfgC9jkyMt0Kj98DHiQm9911CeFwTKPA7VhMsnUfTodysYvhU41okxYKziQNbfgcd gipnSHzqT3S5oMAwWo5JqoSL; Received: from muradm by localhost with local (Exim 4.96) (envelope-from ) id 1oQBBh-0008Cb-1L; Mon, 22 Aug 2022 20:26:09 +0300 From: muradm Date: Mon, 22 Aug 2022 20:26:07 +0300 Message-Id: <20220822172607.31515-3-mail@muradm.net> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220822172607.31515-1-mail@muradm.net> References: <87edxxqpg3.fsf@gmail.com> <20220822172607.31515-1-mail@muradm.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * gnu/tests/security.scm: New module. * gnu/local.mk: Add new security module. --- gnu/local.mk | 1 + gnu/tests/security.scm | 314 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 gnu/tests/security.scm diff --git a/gnu/local.mk b/gnu/local.mk index acd41797b9..31569033bc 100644 --- a/gnu/local.mk +++ b/gnu/local.mk @@ -758,6 +758,7 @@ GNU_SYSTEM_MODULES = \ %D%/tests/package-management.scm \ %D%/tests/reconfigure.scm \ %D%/tests/rsync.scm \ + %D%/tests/security.scm \ %D%/tests/security-token.scm \ %D%/tests/singularity.scm \ %D%/tests/ssh.scm \ diff --git a/gnu/tests/security.scm b/gnu/tests/security.scm new file mode 100644 index 0000000000..4003eff1e5 --- /dev/null +++ b/gnu/tests/security.scm @@ -0,0 +1,314 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2022 muradm +;;; +;;; 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 . + +(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)) + + +;;; +;;; 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))))