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