6d47df
From 940755e37b06ea95c32abd056277da19fb05ed3e Mon Sep 17 00:00:00 2001
6d47df
From: Florence Blanc-Renaud <flo@redhat.com>
6d47df
Date: Dec 06 2018 10:40:02 +0000
6d47df
Subject: ipatest: add test for ipa-pkinit-manage enable|disable
6d47df
6d47df
6d47df
Add a test for ipa-pkinit-manage with the following scenario:
6d47df
- install master with option --no-pkinit
6d47df
- call ipa-pkinit-manage enable
6d47df
- call ipa-pkinit-manage disable
6d47df
- call ipa-pkinit-manage enable
6d47df
6d47df
At each step, check that the PKINIT cert is consistent with the
6d47df
expectations: when pkinit is enabled, the cert is signed by IPA
6d47df
CA and tracked by 'IPA' ca helper, but when pkinit is disabled,
6d47df
the cert is self-signed and tracked by 'SelfSign' CA helper.
6d47df
6d47df
The new test is added in the nightly definitons.
6d47df
6d47df
Related to https://pagure.io/freeipa/issue/7200
6d47df
6d47df
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
6d47df
Reviewed-By: Christian Heimes <cheimes@redhat.com>
6d47df
6d47df
---
6d47df
6d47df
#diff --git a/ipatests/prci_definitions/nightly_f28.yaml b/ipatests/prci_definitions/nightly_f28.yaml
6d47df
#index ae8cacc..8462c14 100644
6d47df
#--- a/ipatests/prci_definitions/nightly_f28.yaml
6d47df
#+++ b/ipatests/prci_definitions/nightly_f28.yaml
6d47df
#@@ -639,3 +639,15 @@ jobs:
6d47df
#         template: *ci-master-f28
6d47df
#         timeout: 16000
6d47df
#         topology: *ipaserver
6d47df
#+
6d47df
#+  fedora-28/test_pkinit_manage:
6d47df
#+    requires: [fedora-28/build]
6d47df
#+    priority: 50
6d47df
#+    job:
6d47df
#+      class: RunPytest
6d47df
#+      args:
6d47df
#+        build_url: '{fedora-28/build_url}'
6d47df
#+        test_suite: test_integration/test_pkinit_manage.py
6d47df
#+        template: *ci-master-f28
6d47df
#+        timeout: 3600
6d47df
#+        topology: *master_1repl
6d47df
diff --git a/ipatests/prci_definitions/nightly_master.yaml b/ipatests/prci_definitions/nightly_master.yaml
6d47df
index 66921b6..3f2b346 100644
6d47df
--- a/ipatests/prci_definitions/nightly_master.yaml
6d47df
+++ b/ipatests/prci_definitions/nightly_master.yaml
6d47df
@@ -639,3 +639,15 @@ jobs:
6d47df
         template: *ci-master-f29
6d47df
         timeout: 16000
6d47df
         topology: *ipaserver
6d47df
+
6d47df
+  fedora-29/test_pkinit_manage:
6d47df
+    requires: [fedora-29/build]
6d47df
+    priority: 50
6d47df
+    job:
6d47df
+      class: RunPytest
6d47df
+      args:
6d47df
+        build_url: '{fedora-29/build_url}'
6d47df
+        test_suite: test_integration/test_pkinit_manage.py
6d47df
+        template: *ci-master-f29
6d47df
+        timeout: 3600
6d47df
+        topology: *master_1repl
6d47df
diff --git a/ipatests/prci_definitions/nightly_rawhide.yaml b/ipatests/prci_definitions/nightly_rawhide.yaml
6d47df
index 24c26be..bdc34d2 100644
6d47df
--- a/ipatests/prci_definitions/nightly_rawhide.yaml
6d47df
+++ b/ipatests/prci_definitions/nightly_rawhide.yaml
6d47df
@@ -627,3 +627,15 @@ jobs:
6d47df
         template: *ci-master-frawhide
6d47df
         timeout: 7200
6d47df
         topology: *ipaserver
6d47df
+
6d47df
+  fedora-rawhide/test_pkinit_manage:
6d47df
+    requires: [fedora-rawhide/build]
6d47df
+    priority: 50
6d47df
+    job:
6d47df
+      class: RunPytest
6d47df
+      args:
6d47df
+        build_url: '{fedora-rawhide/build_url}'
6d47df
+        test_suite: test_integration/test_pkinit_manage.py
6d47df
+        template: *ci-master-frawhide
6d47df
+        timeout: 3600
6d47df
+        topology: *master_1repl
6d47df
diff --git a/ipatests/test_integration/test_pkinit_manage.py b/ipatests/test_integration/test_pkinit_manage.py
6d47df
new file mode 100644
6d47df
index 0000000..bc1d9e3
6d47df
--- /dev/null
6d47df
+++ b/ipatests/test_integration/test_pkinit_manage.py
6d47df
@@ -0,0 +1,111 @@
6d47df
+#
6d47df
+# Copyright (C) 2018  FreeIPA Contributors see COPYING for license
6d47df
+#
6d47df
+
6d47df
+"""
6d47df
+Module provides tests for the ipa-pkinit-manage command.
6d47df
+"""
6d47df
+
6d47df
+from __future__ import absolute_import
6d47df
+
6d47df
+from ipalib import x509
6d47df
+from ipaplatform.paths import paths
6d47df
+from ipapython.dn import DN
6d47df
+from ipatests.test_integration.base import IntegrationTest
6d47df
+from ipatests.pytest_ipa.integration import tasks
6d47df
+
6d47df
+
6d47df
+SELFSIGNED_CA_HELPER = 'SelfSign'
6d47df
+IPA_CA_HELPER = 'IPA'
6d47df
+PKINIT_STATUS_ENABLED = 'enabled'
6d47df
+PKINIT_STATUS_DISABLED = 'disabled'
6d47df
+
6d47df
+
6d47df
+def check_pkinit_status(host, status):
6d47df
+    """Ensures that ipa-pkinit-manage status returns the expected state"""
6d47df
+    result = host.run_command(['ipa-pkinit-manage', 'status'],
6d47df
+                              raiseonerr=False)
6d47df
+    assert result.returncode == 0
6d47df
+    assert 'PKINIT is {}'.format(status) in result.stdout_text
6d47df
+
6d47df
+
6d47df
+def check_pkinit_tracking(host, ca_helper):
6d47df
+    """Ensures that the PKINIT cert is tracked by the expected helper"""
6d47df
+    result = host.run_command(['getcert', 'list', '-f', paths.KDC_CERT],
6d47df
+                              raiseonerr=False)
6d47df
+    assert result.returncode == 0
6d47df
+    # Make sure that only one request exists
6d47df
+    assert result.stdout_text.count('Request ID') == 1
6d47df
+    # Make sure that the right CA helper is used to track the cert
6d47df
+    assert 'CA: {}'.format(ca_helper) in result.stdout_text
6d47df
+
6d47df
+
6d47df
+def check_pkinit_cert_issuer(host, issuer):
6d47df
+    """Ensures that the PKINIT cert is signed by the expected issuer"""
6d47df
+    data = host.get_file_contents(paths.KDC_CERT)
6d47df
+    pkinit_cert = x509.load_pem_x509_certificate(data)
6d47df
+    # Make sure that the issuer is the expected one
6d47df
+    assert DN(pkinit_cert.issuer) == DN(issuer)
6d47df
+
6d47df
+
6d47df
+def check_pkinit(host, enabled=True):
6d47df
+    """Checks that PKINIT is configured as expected
6d47df
+
6d47df
+    If enabled:
6d47df
+    ipa-pkinit-manage status must return 'PKINIT is enabled'
6d47df
+    the certificate must be tracked by IPA CA helper
6d47df
+    the certificate must be signed by IPA CA
6d47df
+    If disabled:
6d47df
+    ipa-pkinit-manage status must return 'PKINIT is disabled'
6d47df
+    the certificate must be tracked by SelfSign CA helper
6d47df
+    the certificate must be self-signed
6d47df
+    """
6d47df
+    if enabled:
6d47df
+        # When pkinit is enabled:
6d47df
+        # cert is tracked by IPA CA helper
6d47df
+        # cert is signed by IPA CA
6d47df
+        check_pkinit_status(host, PKINIT_STATUS_ENABLED)
6d47df
+        check_pkinit_tracking(host, IPA_CA_HELPER)
6d47df
+        check_pkinit_cert_issuer(
6d47df
+            host,
6d47df
+            'CN=Certificate Authority,O={}'.format(host.domain.realm))
6d47df
+    else:
6d47df
+        # When pkinit is disabled
6d47df
+        # cert is tracked by 'SelfSign' CA helper
6d47df
+        # cert is self-signed
6d47df
+        check_pkinit_status(host, PKINIT_STATUS_DISABLED)
6d47df
+        check_pkinit_tracking(host, SELFSIGNED_CA_HELPER)
6d47df
+        check_pkinit_cert_issuer(
6d47df
+            host,
6d47df
+            'CN={},O={}'.format(host.hostname, host.domain.realm))
6d47df
+
6d47df
+
6d47df
+class TestPkinitManage(IntegrationTest):
6d47df
+    """Tests the ipa-pkinit-manage command.
6d47df
+
6d47df
+    ipa-pkinit-manage can be used to enable, disable or check
6d47df
+    the status of PKINIT.
6d47df
+    When pkinit is enabled, the kerberos server is using a certificate
6d47df
+    signed either externally or by IPA CA. In the latter case, certmonger
6d47df
+    is tracking the cert with IPA helper.
6d47df
+    When pkinit is disabled, the kerberos server is using a self-signed
6d47df
+    certificate that is tracked by certmonger with the SelfSigned helper.
6d47df
+    """
6d47df
+
6d47df
+    @classmethod
6d47df
+    def install(cls, mh):
6d47df
+        # Install the master with PKINIT disabled
6d47df
+        tasks.install_master(cls.master, extra_args=['--no-pkinit'])
6d47df
+        check_pkinit(cls.master, enabled=False)
6d47df
+
6d47df
+    def test_pkinit_enable(self):
6d47df
+        self.master.run_command(['ipa-pkinit-manage', 'enable'])
6d47df
+        check_pkinit(self.master, enabled=True)
6d47df
+
6d47df
+    def test_pkinit_disable(self):
6d47df
+        self.master.run_command(['ipa-pkinit-manage', 'disable'])
6d47df
+        check_pkinit(self.master, enabled=False)
6d47df
+
6d47df
+    def test_pkinit_reenable(self):
6d47df
+        self.master.run_command(['ipa-pkinit-manage', 'enable'])
6d47df
+        check_pkinit(self.master, enabled=True)
6d47df
6d47df
From ffa04a1862be198b9e1a5f6205d1ae0909ac5a4d Mon Sep 17 00:00:00 2001
6d47df
From: Florence Blanc-Renaud <flo@redhat.com>
6d47df
Date: Dec 06 2018 10:40:02 +0000
6d47df
Subject: PKINIT: fix ipa-pkinit-manage enable|disable
6d47df
6d47df
6d47df
The command ipa-pkinit-manage enable|disable is reporting
6d47df
success even though the PKINIT cert is not re-issued.
6d47df
The command triggers the request of a new certificate
6d47df
(signed by IPA CA when state=enable, selfsigned when disabled),
6d47df
but as the cert file is still present, certmonger does not create
6d47df
a new request and the existing certificate is kept.
6d47df
6d47df
The fix consists in deleting the cert and key file before calling
6d47df
certmonger to request a new cert.
6d47df
6d47df
There was also an issue in the is_pkinit_enabled() function:
6d47df
if no tracking request was found for the PKINIT cert,
6d47df
is_pkinit_enabled() was returning True while it should not.
6d47df
6d47df
Fixes https://pagure.io/freeipa/issue/7200
6d47df
6d47df
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
6d47df
Reviewed-By: Christian Heimes <cheimes@redhat.com>
6d47df
6d47df
---
6d47df
6d47df
diff --git a/ipaserver/install/ipa_pkinit_manage.py b/ipaserver/install/ipa_pkinit_manage.py
6d47df
index 4a79bba..86bd1ba 100644
6d47df
--- a/ipaserver/install/ipa_pkinit_manage.py
6d47df
+++ b/ipaserver/install/ipa_pkinit_manage.py
6d47df
@@ -72,6 +72,8 @@ class PKINITManage(AdminTool):
6d47df
                 if ca_enabled:
6d47df
                     logger.warning(
6d47df
                         "Failed to stop tracking certificates: %s", e)
6d47df
+            # remove the cert and key
6d47df
+            krb.delete_pkinit_cert()
6d47df
 
6d47df
             krb.enable_ssl()
6d47df
 
6d47df
diff --git a/ipaserver/install/krbinstance.py b/ipaserver/install/krbinstance.py
6d47df
index 4ead1c5..850946a 100644
6d47df
--- a/ipaserver/install/krbinstance.py
6d47df
+++ b/ipaserver/install/krbinstance.py
6d47df
@@ -77,7 +77,7 @@ def is_pkinit_enabled():
6d47df
     if os.path.exists(paths.KDC_CERT):
6d47df
         pkinit_request_ca = get_pkinit_request_ca()
6d47df
 
6d47df
-        if pkinit_request_ca != "SelfSign":
6d47df
+        if pkinit_request_ca and pkinit_request_ca != "SelfSign":
6d47df
             return True
6d47df
 
6d47df
     return False
6d47df
@@ -602,6 +602,10 @@ class KrbInstance(service.Service):
6d47df
     def stop_tracking_certs(self):
6d47df
         certmonger.stop_tracking(certfile=paths.KDC_CERT)
6d47df
 
6d47df
+    def delete_pkinit_cert(self):
6d47df
+        installutils.remove_file(paths.KDC_CERT)
6d47df
+        installutils.remove_file(paths.KDC_KEY)
6d47df
+
6d47df
     def uninstall(self):
6d47df
         if self.is_configured():
6d47df
             self.print_msg("Unconfiguring %s" % self.service_name)
6d47df
@@ -627,8 +631,7 @@ class KrbInstance(service.Service):
6d47df
         # stop tracking and remove certificates
6d47df
         self.stop_tracking_certs()
6d47df
         installutils.remove_file(paths.CACERT_PEM)
6d47df
-        installutils.remove_file(paths.KDC_CERT)
6d47df
-        installutils.remove_file(paths.KDC_KEY)
6d47df
+        self.delete_pkinit_cert()
6d47df
 
6d47df
         if running:
6d47df
             self.restart()
6d47df