URL: https://github.com/freeipa/freeipa/pull/115
Author: tomaskrizek
 Title: #115: Don't show traceback when ipa config file is not an absolute path
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/115/head:pr115
git checkout pr115
From 278f0133cfe40d322455a7ecc5ce24d780c16aae Mon Sep 17 00:00:00 2001
From: Tomas Krizek <tkri...@redhat.com>
Date: Tue, 27 Sep 2016 13:39:22 +0200
Subject: [PATCH] ipa: file path validation for config file option

Convert the config file attribute of ipa command to an absolute
path. Additionaly, check the file exists.

https://fedorahosted.org/freeipa/ticket/6114
---
 ipalib/plugable.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index af35f5b..27db8cb 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -494,6 +494,13 @@ def build_global_parser(self, parser=None, context=None):
         """
         Add global options to an optparse.OptionParser instance.
         """
+        def config_file_callback(option, opt, value, parser):
+            value = os.path.abspath(value)
+            if not os.path.isfile(value):
+                raise optparse.OptionValueError(
+                    "%s option '%s' is not a file" % (opt, value))
+            parser.values.conf = value
+
         if parser is None:
             parser = optparse.OptionParser(
                 add_help_option=False,
@@ -517,8 +524,9 @@ def build_global_parser(self, parser=None, context=None):
         parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
             help='Set environment variable KEY to VAL',
         )
-        parser.add_option('-c', dest='conf', metavar='FILE',
-            help='Load configuration from FILE',
+        parser.add_option('-c', dest='conf', metavar='FILE', action='callback',
+            callback=config_file_callback, type='string',
+            help='Load configuration from FILE. This must be an absolute path',
         )
         parser.add_option('-d', '--debug', action='store_true',
             help='Produce full debuging output',
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to