From 97006786dfed7947aaa96737201ef7e0e53dd952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= Date: Wed, 5 Aug 2020 09:05:31 +0200 Subject: [PATCH] IPA-EPN: enhance input validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enhance input validation: * make sure --from-nbdays and --to-nbdays are integer * make sure --from-nbdays < --to-nbdays Fixes: https://pagure.io/freeipa/issue/8444 Signed-off-by: François Cami Reviewed-By: Rob Crittenden Reviewed-By: Florence Blanc-Renaud --- ipaclient/install/ipa_epn.py | 28 +++++++++++++++++++++++++-- ipatests/test_integration/test_epn.py | 3 --- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ipaclient/install/ipa_epn.py b/ipaclient/install/ipa_epn.py index 82d7b3f57..88c926e88 100644 --- a/ipaclient/install/ipa_epn.py +++ b/ipaclient/install/ipa_epn.py @@ -246,9 +246,33 @@ class EPN(admintool.AdminTool): def validate_options(self): super(EPN, self).validate_options(needs_root=True) - if self.options.to_nbdays: + if self.options.to_nbdays is not None: + try: + if int(self.options.to_nbdays) < 0: + raise RuntimeError('Input is negative.') + except Exception as e: + self.option_parser.error( + "--to-nbdays must be a positive integer. " + "{error}".format(error=e) + ) self.options.dry_run = True - if self.options.from_nbdays and not self.options.to_nbdays: + if self.options.from_nbdays is not None: + try: + if int(self.options.from_nbdays) < 0: + raise RuntimeError('Input is negative.') + except Exception as e: + self.option_parser.error( + "--from-nbdays must be a positive integer. " + "{error}".format(error=e) + ) + if self.options.from_nbdays is not None and \ + self.options.to_nbdays is not None: + if int(self.options.from_nbdays) >= int(self.options.to_nbdays): + self.option_parser.error( + "--from-nbdays must be smaller than --to-nbdays." + ) + if self.options.from_nbdays is not None and \ + self.options.to_nbdays is None: self.option_parser.error( "You cannot specify --from-nbdays without --to-nbdays" ) diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py index e03521193..9cd880676 100644 --- a/ipatests/test_integration/test_epn.py +++ b/ipatests/test_integration/test_epn.py @@ -450,7 +450,6 @@ class TestEPN(IntegrationTest): in stderr_text_client assert rc > 0 - @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True) def test_EPN_nbdays_input_2(self): """alpha input""" @@ -461,7 +460,6 @@ class TestEPN(IntegrationTest): assert "error: --to-nbdays must be an integer." in stderr assert rc > 0 - @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True) def test_EPN_nbdays_input_3(self): """from_nbdays > to_nbdays""" @@ -473,7 +471,6 @@ class TestEPN(IntegrationTest): stderr assert rc > 0 - @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True) def test_EPN_nbdays_input_4(self): """decimal input"""