From a24376c0a87bc8631f61edab8cd463a5c26aa494 Mon Sep 17 00:00:00 2001 From: Eoghan Glynn Date: Thu, 22 Mar 2012 16:54:26 +0000 Subject: Avoid leaking secrets into config logging. Implements bp cfg-password-options Allow options to be declared secret so that their value is obfuscated before logging. Change-Id: Ie2168d218b029d9c12fa5b48342cd5b17b2cc77a --- openstack/common/cfg.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'openstack/common') diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 1005b2f..7b729c3 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -207,6 +207,16 @@ as the leftover arguments, but will instead return:: ['cmd', '--debug', '/tmp/mything'] i.e. argument parsing is stopped at the first non-option argument. + +Options may be declared as secret so that their values are not leaked into +log files: + + opts = [ + cfg.StrOpt('s3_store_access_key', secret=True), + cfg.StrOpt('s3_store_secret_key', secret=True), + ... + ] + """ import collections @@ -399,8 +409,8 @@ class Opt(object): an string explaining how the options value is used """ - def __init__(self, name, dest=None, short=None, - default=None, metavar=None, help=None): + def __init__(self, name, dest=None, short=None, default=None, + metavar=None, help=None, secret=False): """Construct an Opt object. The only required parameter is the option's name. However, it is @@ -412,6 +422,7 @@ class Opt(object): :param default: the default value of the option :param metavar: the option argument to show in --help :param help: an explanation of how the option is used + :param secret: true iff the value should be obfuscated in log output """ self.name = name if dest is None: @@ -422,6 +433,7 @@ class Opt(object): self.default = default self.metavar = metavar self.help = help + self.secret = secret def _get_from_config_parser(self, cparser, section): """Retrieves the option value from a ConfigParser object. @@ -948,15 +960,22 @@ class ConfigOpts(collections.Mapping): logger.log(lvl, "config files: %s", self.config_file) logger.log(lvl, "=" * 80) + def _sanitize(opt, value): + """Obfuscate values of options declared secret""" + return value if not opt.secret else '*' * len(str(value)) + for opt_name in sorted(self._opts): - logger.log(lvl, "%-30s = %s", opt_name, getattr(self, opt_name)) + opt = self._get_opt_info(opt_name)['opt'] + logger.log(lvl, "%-30s = %s", opt_name, + _sanitize(opt, getattr(self, opt_name))) for group_name in self._groups: group_attr = self.GroupAttr(self, self._get_group(group_name)) for opt_name in sorted(self._groups[group_name]._opts): + opt = self._get_opt_info(opt_name, group_name)['opt'] logger.log(lvl, "%-30s = %s", "%s.%s" % (group_name, opt_name), - getattr(group_attr, opt_name)) + _sanitize(opt, getattr(group_attr, opt_name))) logger.log(lvl, "*" * 80) -- cgit