21de49
From 3e0e8c309c70a0d379b985189c23f1bacd62a96e Mon Sep 17 00:00:00 2001
21de49
From: Florence Blanc-Renaud <flo@redhat.com>
21de49
Date: Fri, 30 Nov 2018 15:46:25 +0100
21de49
Subject: [PATCH] ipatest: add test for ipa-pkinit-manage enable|disable
21de49
21de49
Add a test for ipa-pkinit-manage with the following scenario:
21de49
- install master with option --no-pkinit
21de49
- call ipa-pkinit-manage enable
21de49
- call ipa-pkinit-manage disable
21de49
- call ipa-pkinit-manage enable
21de49
21de49
At each step, check that the PKINIT cert is consistent with the
21de49
expectations: when pkinit is enabled, the cert is signed by IPA
21de49
CA and tracked by 'IPA' ca helper, but when pkinit is disabled,
21de49
the cert is self-signed and tracked by 'SelfSign' CA helper.
21de49
21de49
Related to https://pagure.io/freeipa/issue/7200
21de49
21de49
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
21de49
Reviewed-By: Christian Heimes <cheimes@redhat.com>
21de49
---
21de49
 .../test_integration/test_pkinit_manage.py    | 111 ++++++++++++++++++
21de49
 1 file changed, 111 insertions(+)
21de49
 create mode 100644 ipatests/test_integration/test_pkinit_manage.py
21de49
21de49
diff --git a/ipatests/test_integration/test_pkinit_manage.py b/ipatests/test_integration/test_pkinit_manage.py
21de49
new file mode 100644
21de49
index 0000000000000000000000000000000000000000..bc1d9e338cdf4e7a503b3c83ac12792894eecce2
21de49
--- /dev/null
21de49
+++ b/ipatests/test_integration/test_pkinit_manage.py
21de49
@@ -0,0 +1,111 @@
21de49
+#
21de49
+# Copyright (C) 2018  FreeIPA Contributors see COPYING for license
21de49
+#
21de49
+
21de49
+"""
21de49
+Module provides tests for the ipa-pkinit-manage command.
21de49
+"""
21de49
+
21de49
+from __future__ import absolute_import
21de49
+
21de49
+from ipalib import x509
21de49
+from ipaplatform.paths import paths
21de49
+from ipapython.dn import DN
21de49
+from ipatests.test_integration.base import IntegrationTest
21de49
+from ipatests.pytest_ipa.integration import tasks
21de49
+
21de49
+
21de49
+SELFSIGNED_CA_HELPER = 'SelfSign'
21de49
+IPA_CA_HELPER = 'IPA'
21de49
+PKINIT_STATUS_ENABLED = 'enabled'
21de49
+PKINIT_STATUS_DISABLED = 'disabled'
21de49
+
21de49
+
21de49
+def check_pkinit_status(host, status):
21de49
+    """Ensures that ipa-pkinit-manage status returns the expected state"""
21de49
+    result = host.run_command(['ipa-pkinit-manage', 'status'],
21de49
+                              raiseonerr=False)
21de49
+    assert result.returncode == 0
21de49
+    assert 'PKINIT is {}'.format(status) in result.stdout_text
21de49
+
21de49
+
21de49
+def check_pkinit_tracking(host, ca_helper):
21de49
+    """Ensures that the PKINIT cert is tracked by the expected helper"""
21de49
+    result = host.run_command(['getcert', 'list', '-f', paths.KDC_CERT],
21de49
+                              raiseonerr=False)
21de49
+    assert result.returncode == 0
21de49
+    # Make sure that only one request exists
21de49
+    assert result.stdout_text.count('Request ID') == 1
21de49
+    # Make sure that the right CA helper is used to track the cert
21de49
+    assert 'CA: {}'.format(ca_helper) in result.stdout_text
21de49
+
21de49
+
21de49
+def check_pkinit_cert_issuer(host, issuer):
21de49
+    """Ensures that the PKINIT cert is signed by the expected issuer"""
21de49
+    data = host.get_file_contents(paths.KDC_CERT)
21de49
+    pkinit_cert = x509.load_pem_x509_certificate(data)
21de49
+    # Make sure that the issuer is the expected one
21de49
+    assert DN(pkinit_cert.issuer) == DN(issuer)
21de49
+
21de49
+
21de49
+def check_pkinit(host, enabled=True):
21de49
+    """Checks that PKINIT is configured as expected
21de49
+
21de49
+    If enabled:
21de49
+    ipa-pkinit-manage status must return 'PKINIT is enabled'
21de49
+    the certificate must be tracked by IPA CA helper
21de49
+    the certificate must be signed by IPA CA
21de49
+    If disabled:
21de49
+    ipa-pkinit-manage status must return 'PKINIT is disabled'
21de49
+    the certificate must be tracked by SelfSign CA helper
21de49
+    the certificate must be self-signed
21de49
+    """
21de49
+    if enabled:
21de49
+        # When pkinit is enabled:
21de49
+        # cert is tracked by IPA CA helper
21de49
+        # cert is signed by IPA CA
21de49
+        check_pkinit_status(host, PKINIT_STATUS_ENABLED)
21de49
+        check_pkinit_tracking(host, IPA_CA_HELPER)
21de49
+        check_pkinit_cert_issuer(
21de49
+            host,
21de49
+            'CN=Certificate Authority,O={}'.format(host.domain.realm))
21de49
+    else:
21de49
+        # When pkinit is disabled
21de49
+        # cert is tracked by 'SelfSign' CA helper
21de49
+        # cert is self-signed
21de49
+        check_pkinit_status(host, PKINIT_STATUS_DISABLED)
21de49
+        check_pkinit_tracking(host, SELFSIGNED_CA_HELPER)
21de49
+        check_pkinit_cert_issuer(
21de49
+            host,
21de49
+            'CN={},O={}'.format(host.hostname, host.domain.realm))
21de49
+
21de49
+
21de49
+class TestPkinitManage(IntegrationTest):
21de49
+    """Tests the ipa-pkinit-manage command.
21de49
+
21de49
+    ipa-pkinit-manage can be used to enable, disable or check
21de49
+    the status of PKINIT.
21de49
+    When pkinit is enabled, the kerberos server is using a certificate
21de49
+    signed either externally or by IPA CA. In the latter case, certmonger
21de49
+    is tracking the cert with IPA helper.
21de49
+    When pkinit is disabled, the kerberos server is using a self-signed
21de49
+    certificate that is tracked by certmonger with the SelfSigned helper.
21de49
+    """
21de49
+
21de49
+    @classmethod
21de49
+    def install(cls, mh):
21de49
+        # Install the master with PKINIT disabled
21de49
+        tasks.install_master(cls.master, extra_args=['--no-pkinit'])
21de49
+        check_pkinit(cls.master, enabled=False)
21de49
+
21de49
+    def test_pkinit_enable(self):
21de49
+        self.master.run_command(['ipa-pkinit-manage', 'enable'])
21de49
+        check_pkinit(self.master, enabled=True)
21de49
+
21de49
+    def test_pkinit_disable(self):
21de49
+        self.master.run_command(['ipa-pkinit-manage', 'disable'])
21de49
+        check_pkinit(self.master, enabled=False)
21de49
+
21de49
+    def test_pkinit_reenable(self):
21de49
+        self.master.run_command(['ipa-pkinit-manage', 'enable'])
21de49
+        check_pkinit(self.master, enabled=True)
21de49
-- 
21de49
2.17.2
21de49