483b06
From b98b21aaa709ccd91369e89a836f64c06c4593e8 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Thu, 27 Apr 2017 09:33:25 +0200
483b06
Subject: [PATCH] certdb: add named trust flag constants
483b06
483b06
Add named constants for common trust flag combinations.
483b06
483b06
Use the named constants instead of trust flags strings in the code.
483b06
483b06
https://pagure.io/freeipa/issue/6831
483b06
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 install/restart_scripts/restart_httpd      |  3 ++-
483b06
 install/tools/ipa-replica-conncheck        |  4 +++-
483b06
 ipaclient/install/client.py                |  9 ++++++---
483b06
 ipapython/certdb.py                        |  9 +++++++--
483b06
 ipaserver/install/ca.py                    |  2 +-
483b06
 ipaserver/install/certs.py                 |  5 +++--
483b06
 ipaserver/install/dsinstance.py            |  5 +++--
483b06
 ipaserver/install/httpinstance.py          |  5 +++--
483b06
 ipaserver/install/ipa_cacert_manage.py     | 16 +++++++++++-----
483b06
 ipaserver/install/plugins/upload_cacrt.py  |  2 +-
483b06
 ipaserver/install/server/replicainstall.py |  3 ++-
483b06
 ipaserver/install/server/upgrade.py        |  4 ++--
483b06
 12 files changed, 44 insertions(+), 23 deletions(-)
483b06
483b06
diff --git a/install/restart_scripts/restart_httpd b/install/restart_scripts/restart_httpd
483b06
index b661b82b896b109c3859ac82c2d84ab27b839f72..cd7f12024ea3cab16e9c664687cd854e666c9570 100644
483b06
--- a/install/restart_scripts/restart_httpd
483b06
+++ b/install/restart_scripts/restart_httpd
483b06
@@ -24,6 +24,7 @@ import traceback
483b06
 from ipalib import api
483b06
 from ipaplatform import services
483b06
 from ipaplatform.paths import paths
483b06
+from ipapython.certdb import TRUSTED_PEER_TRUST_FLAGS
483b06
 from ipaserver.install import certs, installutils
483b06
 
483b06
 
483b06
@@ -36,7 +37,7 @@ def _main():
483b06
     nickname = installutils.get_directive(paths.HTTPD_NSS_CONF, "NSSNickname")
483b06
 
483b06
     # Add trust flag which set certificate trusted for SSL connections.
483b06
-    db.trust_root_cert(nickname, "P,,")
483b06
+    db.trust_root_cert(nickname, TRUSTED_PEER_TRUST_FLAGS)
483b06
 
483b06
     syslog.syslog(syslog.LOG_NOTICE, 'certmonger restarted httpd')
483b06
 
483b06
diff --git a/install/tools/ipa-replica-conncheck b/install/tools/ipa-replica-conncheck
483b06
index fdbd4f32d9fa4a625cca3614e13e71d00f58e57e..528242268f9992e903781b76a379039d533853c0 100755
483b06
--- a/install/tools/ipa-replica-conncheck
483b06
+++ b/install/tools/ipa-replica-conncheck
483b06
@@ -549,7 +549,9 @@ def main():
483b06
                             data = ca_cert.public_bytes(
483b06
                                 serialization.Encoding.DER)
483b06
                             nss_db.add_cert(
483b06
-                                data, str(DN(ca_cert.subject)), 'C,,')
483b06
+                                data,
483b06
+                                str(DN(ca_cert.subject)),
483b06
+                                certdb.EXTERNAL_CA_TRUST_FLAGS)
483b06
 
483b06
                     api.bootstrap(context='client',
483b06
                                   confdir=paths.ETC_IPA,
483b06
diff --git a/ipaclient/install/client.py b/ipaclient/install/client.py
483b06
index abca692fd61be4a9f35a1398fb2af4b1d9e8689b..e78be904dd6bad491d9f3c1bb1e1410bc1779d45 100644
483b06
--- a/ipaclient/install/client.py
483b06
+++ b/ipaclient/install/client.py
483b06
@@ -2318,8 +2318,9 @@ def update_ipa_nssdb():
483b06
     if not os.path.exists(os.path.join(ipa_db.secdir, 'cert8.db')):
483b06
         create_ipa_nssdb()
483b06
 
483b06
-    for nickname, trust_flags in (('IPA CA', 'CT,C,C'),
483b06
-                                  ('External CA cert', 'C,,')):
483b06
+    for nickname, trust_flags in (
483b06
+            ('IPA CA', certdb.IPA_CA_TRUST_FLAGS),
483b06
+            ('External CA cert', certdb.EXTERNAL_CA_TRUST_FLAGS)):
483b06
         try:
483b06
             cert = sys_db.get_cert(nickname)
483b06
         except RuntimeError:
483b06
@@ -2680,7 +2681,9 @@ def _install(options):
483b06
             tmp_db.create_db()
483b06
 
483b06
             for i, cert in enumerate(ca_certs):
483b06
-                tmp_db.add_cert(cert, 'CA certificate %d' % (i + 1), 'C,,')
483b06
+                tmp_db.add_cert(cert,
483b06
+                                'CA certificate %d' % (i + 1),
483b06
+                                certdb.EXTERNAL_CA_TRUST_FLAGS)
483b06
         except CalledProcessError:
483b06
             raise ScriptError(
483b06
                 "Failed to add CA to temporary NSS database.",
483b06
diff --git a/ipapython/certdb.py b/ipapython/certdb.py
483b06
index ea73ec139df9013b860df447fcffd9038cf7c8f2..44c7bf3197c198295035742e6db48527d76e85a6 100644
483b06
--- a/ipapython/certdb.py
483b06
+++ b/ipapython/certdb.py
483b06
@@ -52,6 +52,11 @@ CA_NICKNAME_FMT = "%s IPA CA"
483b06
 
483b06
 NSS_FILES = ("cert8.db", "key3.db", "secmod.db", "pwdfile.txt")
483b06
 
483b06
+EMPTY_TRUST_FLAGS = ',,'
483b06
+IPA_CA_TRUST_FLAGS = 'CT,C,C'
483b06
+EXTERNAL_CA_TRUST_FLAGS = 'C,,'
483b06
+TRUSTED_PEER_TRUST_FLAGS = 'P,,'
483b06
+
483b06
 
483b06
 def get_ca_nickname(realm, format=CA_NICKNAME_FMT):
483b06
     return format % realm
483b06
@@ -441,7 +446,7 @@ class NSSDatabase(object):
483b06
             cert = x509.load_certificate(cert_pem)
483b06
             nickname = str(DN(cert.subject))
483b06
             data = cert.public_bytes(serialization.Encoding.DER)
483b06
-            self.add_cert(data, nickname, ',,')
483b06
+            self.add_cert(data, nickname, EMPTY_TRUST_FLAGS)
483b06
 
483b06
         if extracted_key:
483b06
             in_file = ipautil.write_tmp_file(
483b06
@@ -473,7 +478,7 @@ class NSSDatabase(object):
483b06
                 root_nickname)
483b06
         else:
483b06
             if trust_flags is None:
483b06
-                trust_flags = 'C,,'
483b06
+                trust_flags = EXTERNAL_CA_TRUST_FLAGS
483b06
             try:
483b06
                 self.run_certutil(["-M", "-n", root_nickname,
483b06
                                    "-t", trust_flags])
483b06
diff --git a/ipaserver/install/ca.py b/ipaserver/install/ca.py
483b06
index 8ee0fda23411563c70b7db5f39f43c2869c108b5..52cb20f1cb3612394544a6a41f10e9e939bc0657 100644
483b06
--- a/ipaserver/install/ca.py
483b06
+++ b/ipaserver/install/ca.py
483b06
@@ -320,7 +320,7 @@ def install_step_1(standalone, replica_config, options):
483b06
             realm_name, nssdir=dirname, subject_base=subject_base)
483b06
         cacert = cadb.get_cert_from_db('caSigningCert cert-pki-ca', pem=False)
483b06
         nickname = certdb.get_ca_nickname(realm_name)
483b06
-        trust_flags = 'CT,C,C'
483b06
+        trust_flags = certdb.IPA_CA_TRUST_FLAGS
483b06
         dsdb.add_cert(cacert, nickname, trust_flags)
483b06
         certstore.put_ca_cert_nss(api.Backend.ldap2, api.env.basedn,
483b06
                                   cacert, nickname, trust_flags,
483b06
diff --git a/ipaserver/install/certs.py b/ipaserver/install/certs.py
483b06
index 89e57134f24c505d669057eefffb7862b3b8179a..f87e00eb5e9c14ed30d39ef9f6e86b6f24bb1c61 100644
483b06
--- a/ipaserver/install/certs.py
483b06
+++ b/ipaserver/install/certs.py
483b06
@@ -37,6 +37,7 @@ from ipalib.install import certmonger, sysrestore
483b06
 from ipapython.ipa_log_manager import root_logger
483b06
 from ipapython import dogtag
483b06
 from ipapython import ipautil
483b06
+from ipapython.certdb import EMPTY_TRUST_FLAGS, IPA_CA_TRUST_FLAGS
483b06
 from ipapython.certdb import get_ca_nickname, find_cert_from_txt, NSSDatabase
483b06
 from ipapython.dn import DN
483b06
 from ipalib import pkcs10, x509, api
483b06
@@ -597,7 +598,7 @@ class CertDB(object):
483b06
         # a new certificate database.
483b06
         self.create_passwd_file()
483b06
         self.create_certdbs()
483b06
-        self.load_cacert(cacert_fname, 'CT,C,C')
483b06
+        self.load_cacert(cacert_fname, IPA_CA_TRUST_FLAGS)
483b06
 
483b06
     def create_from_pkcs12(self, pkcs12_fname, pkcs12_passwd, passwd=None,
483b06
                            ca_file=None, trust_flags=None):
483b06
@@ -643,7 +644,7 @@ class CertDB(object):
483b06
                     cert, st = find_cert_from_txt(certs, st)
483b06
                 except RuntimeError:
483b06
                     break
483b06
-                self.add_cert(cert, 'CA %s' % num, ',,', pem=True)
483b06
+                self.add_cert(cert, 'CA %s' % num, EMPTY_TRUST_FLAGS, pem=True)
483b06
                 num += 1
483b06
 
483b06
         # We only handle one server cert
483b06
diff --git a/ipaserver/install/dsinstance.py b/ipaserver/install/dsinstance.py
483b06
index 403fe8489fdd9e0dbf40dd4df3794b51185d45b9..0db0368fa4b48495718afd779291ce164d1687c8 100644
483b06
--- a/ipaserver/install/dsinstance.py
483b06
+++ b/ipaserver/install/dsinstance.py
483b06
@@ -32,6 +32,7 @@ import fnmatch
483b06
 import ldap
483b06
 
483b06
 from ipalib.install import certmonger, certstore
483b06
+from ipapython.certdb import IPA_CA_TRUST_FLAGS, EXTERNAL_CA_TRUST_FLAGS
483b06
 from ipapython.ipa_log_manager import root_logger
483b06
 from ipapython import ipautil, ipaldap
483b06
 from ipapython import dogtag
483b06
@@ -766,7 +767,7 @@ class DsInstance(service.Service):
483b06
         )
483b06
         if self.pkcs12_info:
483b06
             if self.ca_is_configured:
483b06
-                trust_flags = 'CT,C,C'
483b06
+                trust_flags = IPA_CA_TRUST_FLAGS
483b06
             else:
483b06
                 trust_flags = None
483b06
             dsdb.create_from_pkcs12(self.pkcs12_info[0], self.pkcs12_info[1],
483b06
@@ -1065,7 +1066,7 @@ class DsInstance(service.Service):
483b06
         certdb.cacert_name = cacert_name
483b06
         status = True
483b06
         try:
483b06
-            certdb.load_cacert(cacert_fname, 'C,,')
483b06
+            certdb.load_cacert(cacert_fname, EXTERNAL_CA_TRUST_FLAGS)
483b06
         except ipautil.CalledProcessError as e:
483b06
             root_logger.critical("Error importing CA cert file named [%s]: %s" %
483b06
                                          (cacert_fname, str(e)))
483b06
diff --git a/ipaserver/install/httpinstance.py b/ipaserver/install/httpinstance.py
483b06
index ab688a85f157b1886842a91bb7d22f9ea99e3615..a6aeb21edc73783ff9a3f9b526409ea525aa66dd 100644
483b06
--- a/ipaserver/install/httpinstance.py
483b06
+++ b/ipaserver/install/httpinstance.py
483b06
@@ -32,6 +32,7 @@ import six
483b06
 from augeas import Augeas
483b06
 
483b06
 from ipalib.install import certmonger
483b06
+from ipapython.certdb import IPA_CA_TRUST_FLAGS, TRUSTED_PEER_TRUST_FLAGS
483b06
 from ipaserver.install import service
483b06
 from ipaserver.install import certs
483b06
 from ipaserver.install import installutils
483b06
@@ -381,7 +382,7 @@ class HTTPInstance(service.Service):
483b06
 
483b06
         if self.pkcs12_info:
483b06
             if self.ca_is_configured:
483b06
-                trust_flags = 'CT,C,C'
483b06
+                trust_flags = IPA_CA_TRUST_FLAGS
483b06
             else:
483b06
                 trust_flags = None
483b06
             db.init_from_pkcs12(self.pkcs12_info[0], self.pkcs12_info[1],
483b06
@@ -403,7 +404,7 @@ class HTTPInstance(service.Service):
483b06
             self.__set_mod_nss_nickname(nickname)
483b06
             self.add_cert_to_service()
483b06
 
483b06
-            db.trust_root_cert(nickname, "P,,")
483b06
+            db.trust_root_cert(nickname, TRUSTED_PEER_TRUST_FLAGS)
483b06
 
483b06
         else:
483b06
             if not self.promote:
483b06
diff --git a/ipaserver/install/ipa_cacert_manage.py b/ipaserver/install/ipa_cacert_manage.py
483b06
index 3b732e4dcbb5c9b4dfbb9e3608bc7d7afd3e10c2..88b40d45e10281d272882d21e06f5d53cf5a701d 100644
483b06
--- a/ipaserver/install/ipa_cacert_manage.py
483b06
+++ b/ipaserver/install/ipa_cacert_manage.py
483b06
@@ -26,6 +26,7 @@ import gssapi
483b06
 
483b06
 from ipalib.install import certmonger, certstore
483b06
 from ipapython import admintool, ipautil
483b06
+from ipapython.certdb import EMPTY_TRUST_FLAGS, EXTERNAL_CA_TRUST_FLAGS
483b06
 from ipapython.dn import DN
483b06
 from ipaplatform.paths import paths
483b06
 from ipalib import api, errors, x509
483b06
@@ -242,10 +243,10 @@ class CACertManage(admintool.AdminTool):
483b06
 
483b06
         with certs.NSSDatabase() as tmpdb:
483b06
             tmpdb.create_db()
483b06
-            tmpdb.add_cert(old_cert_der, 'IPA CA', 'C,,')
483b06
+            tmpdb.add_cert(old_cert_der, 'IPA CA', EXTERNAL_CA_TRUST_FLAGS)
483b06
 
483b06
             try:
483b06
-                tmpdb.add_cert(new_cert_der, 'IPA CA', 'C,,')
483b06
+                tmpdb.add_cert(new_cert_der, 'IPA CA', EXTERNAL_CA_TRUST_FLAGS)
483b06
             except ipautil.CalledProcessError as e:
483b06
                 raise admintool.ScriptError(
483b06
                     "Not compatible with the current CA certificate: %s" % e)
483b06
@@ -253,7 +254,8 @@ class CACertManage(admintool.AdminTool):
483b06
             ca_certs = x509.load_certificate_list_from_file(ca_file.name)
483b06
             for ca_cert in ca_certs:
483b06
                 data = ca_cert.public_bytes(serialization.Encoding.DER)
483b06
-                tmpdb.add_cert(data, str(DN(ca_cert.subject)), 'C,,')
483b06
+                tmpdb.add_cert(
483b06
+                    data, str(DN(ca_cert.subject)), EXTERNAL_CA_TRUST_FLAGS)
483b06
 
483b06
             try:
483b06
                 tmpdb.verify_ca_cert_validity('IPA CA')
483b06
@@ -270,7 +272,11 @@ class CACertManage(admintool.AdminTool):
483b06
                 except RuntimeError:
483b06
                     break
483b06
                 certstore.put_ca_cert_nss(
483b06
-                    conn, api.env.basedn, ca_cert, nickname, ',,')
483b06
+                    conn,
483b06
+                    api.env.basedn,
483b06
+                    ca_cert,
483b06
+                    nickname,
483b06
+                    EMPTY_TRUST_FLAGS)
483b06
 
483b06
         dn = DN(('cn', self.cert_nickname), ('cn', 'ca_renewal'),
483b06
                 ('cn', 'ipa'), ('cn', 'etc'), api.env.basedn)
483b06
@@ -343,7 +349,7 @@ class CACertManage(admintool.AdminTool):
483b06
 
483b06
         with certs.NSSDatabase() as tmpdb:
483b06
             tmpdb.create_db()
483b06
-            tmpdb.add_cert(cert, nickname, 'C,,')
483b06
+            tmpdb.add_cert(cert, nickname, EXTERNAL_CA_TRUST_FLAGS)
483b06
             for ca_cert, ca_nickname, ca_trust_flags in ca_certs:
483b06
                 tmpdb.add_cert(ca_cert, ca_nickname, ca_trust_flags)
483b06
 
483b06
diff --git a/ipaserver/install/plugins/upload_cacrt.py b/ipaserver/install/plugins/upload_cacrt.py
483b06
index 425ea63976ec92a6d69492d90a1e970e528c4a26..7d294ff971bd109e5fbb3570bfff0198f24b68d3 100644
483b06
--- a/ipaserver/install/plugins/upload_cacrt.py
483b06
+++ b/ipaserver/install/plugins/upload_cacrt.py
483b06
@@ -55,7 +55,7 @@ class update_upload_cacrt(Updater):
483b06
             if 'u' in trust_flags:
483b06
                 continue
483b06
             if nickname == ca_nickname and ca_enabled:
483b06
-                trust_flags = 'CT,C,C'
483b06
+                trust_flags = certdb.IPA_CA_TRUST_FLAGS
483b06
             cert = db.get_cert_from_db(nickname, pem=False)
483b06
             trust, _ca, eku = certstore.trust_flags_to_key_policy(trust_flags)
483b06
 
483b06
diff --git a/ipaserver/install/server/replicainstall.py b/ipaserver/install/server/replicainstall.py
483b06
index aa8e67f60b8abe591d55a907c409b584c74d4541..5e78e6faf51ded2fe7634f230c66aa15ae84bad4 100644
483b06
--- a/ipaserver/install/server/replicainstall.py
483b06
+++ b/ipaserver/install/server/replicainstall.py
483b06
@@ -23,6 +23,7 @@ import ipaclient.install.ntpconf
483b06
 from ipalib.install import certstore, sysrestore
483b06
 from ipalib.install.kinit import kinit_keytab
483b06
 from ipapython import ipaldap, ipautil
483b06
+from ipapython.certdb import IPA_CA_TRUST_FLAGS
483b06
 from ipapython.dn import DN
483b06
 from ipapython.ipa_log_manager import root_logger
483b06
 from ipapython.admintool import ScriptError
483b06
@@ -737,7 +738,7 @@ def install_check(installer):
483b06
                                   nssdir=tmp_db_dir,
483b06
                                   subject_base=config.subject_base)
483b06
             if ca_enabled:
483b06
-                trust_flags = 'CT,C,C'
483b06
+                trust_flags = IPA_CA_TRUST_FLAGS
483b06
             else:
483b06
                 trust_flags = None
483b06
             tmp_db.create_from_pkcs12(pkcs12_info[0], pkcs12_info[1],
483b06
diff --git a/ipaserver/install/server/upgrade.py b/ipaserver/install/server/upgrade.py
483b06
index 5e5c83731d3d3415deb61271baa7865c62f60336..73a4f1108a56a766cdbbcb93d7050482a8264a75 100644
483b06
--- a/ipaserver/install/server/upgrade.py
483b06
+++ b/ipaserver/install/server/upgrade.py
483b06
@@ -1389,7 +1389,7 @@ def fix_trust_flags():
483b06
     nickname = certdb.get_ca_nickname(api.env.realm)
483b06
     cert = db.get_cert_from_db(nickname)
483b06
     if cert:
483b06
-        db.trust_root_cert(nickname, 'CT,C,C')
483b06
+        db.trust_root_cert(nickname, certdb.IPA_CA_TRUST_FLAGS)
483b06
 
483b06
     sysupgrade.set_upgrade_state('http', 'fix_trust_flags', True)
483b06
 
483b06
@@ -1407,7 +1407,7 @@ def fix_server_cert_trust_flags():
483b06
     sc_nickname = installutils.get_directive(paths.HTTPD_NSS_CONF,
483b06
                                              "NSSNickname")
483b06
     # Add trust flag which set certificate trusted for SSL connections.
483b06
-    db.trust_root_cert(sc_nickname, "P,,")
483b06
+    db.trust_root_cert(sc_nickname, certdb.TRUSTED_PEER_TRUST_FLAGS)
483b06
 
483b06
     sysupgrade.set_upgrade_state('http', 'fix_serv_cert_trust_flags', True)
483b06
 
483b06
-- 
483b06
2.9.4
483b06