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