pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0215-smart-card-advises-use-a-wrapper-around-Bash-for-loo.patch

483b06
From 4e6992f985ebfb6e6c3fb4a6fa7a2959d84ca243 Mon Sep 17 00:00:00 2001
483b06
From: Martin Babinsky <mbabinsk@redhat.com>
483b06
Date: Thu, 22 Jun 2017 15:30:41 +0200
483b06
Subject: [PATCH] smart card advises: use a wrapper around Bash `for` loops
483b06
483b06
Replace the raw `command` calls constructing the for loops in some
483b06
methods by a wrapper hiding this detail.
483b06
483b06
https://pagure.io/freeipa/issue/7036
483b06
483b06
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
483b06
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
483b06
---
483b06
 ipaserver/advise/base.py                    | 23 +++++++++++++++++++++++
483b06
 ipaserver/advise/plugins/smart_card_auth.py | 26 +++++++-------------------
483b06
 2 files changed, 30 insertions(+), 19 deletions(-)
483b06
483b06
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
483b06
index 581478fb75bc4f50b6bffe2e4cf9b51de46fa095..be7274417042fca521039b56af60831563f6952b 100644
483b06
--- a/ipaserver/advise/base.py
483b06
+++ b/ipaserver/advise/base.py
483b06
@@ -201,6 +201,24 @@ class UnbranchedIfStatement(IfBranch):
483b06
         self.advice_output.command('fi')
483b06
 
483b06
 
483b06
+class ForLoop(CompoundStatement):
483b06
+    """
483b06
+    Wrapper around the for loop
483b06
+    """
483b06
+    def __init__(self, advice_output, loop_variable, iterable):
483b06
+        super(ForLoop, self).__init__(advice_output)
483b06
+        self.loop_variable = loop_variable
483b06
+        self.iterable = iterable
483b06
+
483b06
+    def begin_statement(self):
483b06
+        self.advice_output.command(
483b06
+            'for {} in {}'.format(self.loop_variable, self.iterable))
483b06
+        self.advice_output.command('do')
483b06
+
483b06
+    def end_statement(self):
483b06
+        self.advice_output.command('done')
483b06
+
483b06
+
483b06
 class _AdviceOutput(object):
483b06
 
483b06
     def __init__(self):
483b06
@@ -334,6 +352,11 @@ class _AdviceOutput(object):
483b06
         with self._compound_statement(ElseIfBranch, predicate):
483b06
             yield
483b06
 
483b06
+    @contextmanager
483b06
+    def for_loop(self, loop_variable, iterable):
483b06
+        with self._compound_statement(ForLoop, loop_variable, iterable):
483b06
+            yield
483b06
+
483b06
 
483b06
 class Advice(Plugin):
483b06
     """
483b06
diff --git a/ipaserver/advise/plugins/smart_card_auth.py b/ipaserver/advise/plugins/smart_card_auth.py
483b06
index 2dc9ddb25ce41a8c85aab827a92a1143784d9457..3ff94be1e8b108668989602b1b406a39d23ff501 100644
483b06
--- a/ipaserver/advise/plugins/smart_card_auth.py
483b06
+++ b/ipaserver/advise/plugins/smart_card_auth.py
483b06
@@ -40,48 +40,36 @@ class common_smart_card_auth_config(Advice):
483b06
             ['You need to provide one or more paths to the PEM files '
483b06
              'containing CAs signing the Smart Cards']
483b06
         )
483b06
-        self.log.command(
483b06
-            "for {} in ${}".format(
483b06
-                single_ca_path_variable, ca_paths_variable))
483b06
-        self.log.command("do")
483b06
-        with self.log.indented_block():
483b06
+        with self.log.for_loop(single_ca_path_variable,
483b06
+                               '${}'.format(ca_paths_variable)):
483b06
             self.log.exit_on_predicate(
483b06
                 '[ ! -f "${}" ]'.format(single_ca_path_variable),
483b06
                 ['Invalid CA certificate filename: ${}'.format(
483b06
                     single_ca_path_variable),
483b06
                  'Please check that the path exists and is a valid file']
483b06
             )
483b06
-        self.log.command("done")
483b06
 
483b06
     def upload_smartcard_ca_certificates_to_systemwide_db(self):
483b06
-        self.log.command(
483b06
-            "for {} in ${}".format(
483b06
+        with self.log.for_loop(
483b06
                 self.single_ca_cert_variable_name,
483b06
-                self.smart_card_ca_certs_variable_name))
483b06
-        self.log.command("do")
483b06
-        with self.log.indented_block():
483b06
+                '${}'.format(self.smart_card_ca_certs_variable_name)):
483b06
             self.log.command(
483b06
                 'certutil -d {} -A -i ${} -n "Smart Card CA $(uuidgen)" '
483b06
                 '-t CT,C,C'.format(
483b06
                     self.systemwide_nssdb, self.single_ca_cert_variable_name
483b06
-                ),
483b06
+                )
483b06
             )
483b06
-        self.log.command("done")
483b06
 
483b06
     def install_smart_card_signing_ca_certs(self):
483b06
-        self.log.command(
483b06
-            "for {} in ${}".format(
483b06
+        with self.log.for_loop(
483b06
                 self.single_ca_cert_variable_name,
483b06
-                self.smart_card_ca_certs_variable_name))
483b06
-        self.log.command("do")
483b06
-        with self.log.indented_block():
483b06
+                '${}'.format(self.smart_card_ca_certs_variable_name)):
483b06
             self.log.exit_on_failed_command(
483b06
                 'ipa-cacert-manage install ${} -t CT,C,C'.format(
483b06
                     self.single_ca_cert_variable_name
483b06
                 ),
483b06
                 ['Failed to install external CA certificate to IPA']
483b06
             )
483b06
-        self.log.command("done")
483b06
 
483b06
     def update_ipa_ca_certificate_store(self):
483b06
         self.log.exit_on_failed_command(
483b06
-- 
483b06
2.9.4
483b06