From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id oBEWH+LWA2PVagEAbAwnHQ (envelope-from ) for ; Mon, 22 Aug 2022 21:20:02 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id yCkDH+LWA2N3gwEAauVa8A (envelope-from ) for ; Mon, 22 Aug 2022 21:20:02 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 0D53A239B7 for ; Mon, 22 Aug 2022 21:20:01 +0200 (CEST) Received: from localhost ([::1]:48650 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQCxs-0000T2-Uh for larch@yhetil.org; Mon, 22 Aug 2022 15:20:00 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1661196002; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=36k/5Yc9fF7MJvRIWo9d7Awv7Oo9ze8bgAPEug/JTCA=; b=hW80RON8kdKoHmu9xLOSqwPArWjyt6it3cZfjgr+5fVvY/NqmFZtNbueEOLaPdJabt7Lc3 yC4teNN2bl5LK1oV9duIZUnkeYATO8NK6pbSuYxoGMw93OxjykDyzHc7FMsVfeSASJeA0z dDM027MECQqOeconNblelebNJgAVpYM46PlQM9xWPOuZPNsnLUqqAhVX4OMGpRY2+kel6d aJ1I5OReaYMNrrBKrFoicEik49KUuf2iHJTDHlj+k2PUwwBIxnWEJxfNL9fNnMzjfEzecf +NmDPGTgwgYizo0/uP/5WXwMSrzopUNKu3hUK9uzYBuG+nYML/DjSrCI0wFXnA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1661196002; a=rsa-sha256; cv=none; b=JWqZT9lmR4CSDmLEPPPLLk5fY9pAjpbGBIU0h5lkAHyNkLOfEh2cM79AYpfY3hSsAcUY6p h6vBIdxB9/aQH5h1BySDLbstQcaMVfwhqES9HC1jl6frpgMIs1thGO8DQ4Squnwcjuho69 gvb/gG88ko5O9yr1RDG6LJPz2U0M+fn3uFMmpAgm7ULNWmRddt977jG8b86qibEpi4O/4x wb43Lo5oc9XYuSZiyph4LtgZi6NMUG879GquuqjFJTFV9RQhvPEk4V5B83JM6kgijyqtvC JwtyJ8b/juvLfyndU6AppTwIUqou9JxBgr800QyOXJ+C5IFnQvNddBSqED4eLA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=muradm.net header.s=mail header.b=DLGB+1x5; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 3.09 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=muradm.net header.s=mail header.b=DLGB+1x5; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 0D53A239B7 X-Spam-Score: 3.09 X-Migadu-Scanner: scn0.migadu.com X-TUID: YQTLAEQAcFs+ * 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)))) -- 2.37.1