Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't show traceback when ipa config file is not an absolute path #115

Closed
wants to merge 2 commits into from

Conversation

tkrizek
Copy link
Contributor

@tkrizek tkrizek commented Sep 26, 2016

When using the ipa command with the '-c' flag, the user provides
a configuration file. If this path is not absolute, an error
without a traceback should be displayed.

https://fedorahosted.org/freeipa/ticket/6114

@@ -518,7 +518,7 @@ def build_global_parser(self, parser=None, context=None):
help='Set environment variable KEY to VAL',
)
parser.add_option('-c', dest='conf', metavar='FILE',
help='Load configuration from FILE',
help='Load configuration from FILE. This must be an absolute path',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 check fails for this line, but it fits the code style of the surrounding code. IMO this should be fixed as a separate issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can't win this time :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should be validator for:

  • file exists
  • filename is absolute path

@MartinBasti MartinBasti self-assigned this Sep 27, 2016
Copy link
Contributor

@MartinBasti MartinBasti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to have absolute path? Can we move to next century and allow relative path? If yes please fix this as well.

self._merge_from_file(self.conf_default)
except ValueError as e:
root_logger.error(e)
sys.exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sys.exit() in modules ever

@@ -532,8 +533,12 @@ def _finalize_core(self, **defaults):

# Merge in context config file and then default config file:
if self.__d.get('mode', None) != 'dummy':
self._merge_from_file(self.conf)
self._merge_from_file(self.conf_default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave this failing with traceback, validation of input should happen in different place

@@ -518,7 +518,7 @@ def build_global_parser(self, parser=None, context=None):
help='Set environment variable KEY to VAL',
)
parser.add_option('-c', dest='conf', metavar='FILE',
help='Load configuration from FILE',
help='Load configuration from FILE. This must be an absolute path',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should be validator for:

  • file exists
  • filename is absolute path

@MartinBasti
Copy link
Contributor

NACK, please see my inline comments

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the callback you added creates abs path, so help should not mention 'absolute path'

Also help strings should be internationalized, I opened ticket for it https://fedorahosted.org/freeipa/ticket/6361

value = os.path.abspath(value)
if not os.path.isfile(value):
raise optparse.OptionValueError(
"%s option '%s' is not a file" % (opt, value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please use internationalized string here

  2. instead of path.isfile(), I would use ipautil.file_exists() to be consistent with other parts

  3. I don't like the error message, "-c option '/tmp/test.conf' is not a file" doesn't sound nice to me
    IMO "%(filename)s: file not found" is better

@MartinBasti
Copy link
Contributor

NACK, please see inline comments

@pspacek
Copy link
Contributor

pspacek commented Sep 27, 2016

Why the file must be absolute? I would rather remove this requirement and be done with it. open() the file and if it succeeds - use it. If it fails, print error returned from open.

Remove unnecessary check for absolute file paths for config file.

https://fedorahosted.org/freeipa/ticket/6114
@tkrizek
Copy link
Contributor Author

tkrizek commented Sep 27, 2016

I found no reason why the path should be absolute, so I removed that constraint.

The parser check to verify if file exists should remain, since non-existent files are otherwise ignored without any notice.

Copy link
Contributor

@MartinBasti MartinBasti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NACK, please fix translation string

def config_file_callback(option, opt, value, parser):
if not ipautil.file_exists(value):
raise optparse.OptionValueError(
_("%s: file not found") % value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be used this rather parser.error() instead of raising an exception? https://docs.python.org/2/library/optparse.html#how-optparse-handles-errors

@MartinBasti
Copy link
Contributor

nack, please see comments

Add a parser check to verify config file supplied to the ipa
command exists. Previously, invalid file paths would not results
in any error and would just silently proceed with default config.

https://fedorahosted.org/freeipa/ticket/6114
@MartinBasti MartinBasti added ack Pull Request approved, can be merged pushed Pull Request has already been pushed labels Oct 6, 2016
@MartinBasti MartinBasti closed this Oct 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ack Pull Request approved, can be merged pushed Pull Request has already been pushed
Projects
None yet
3 participants