summaryrefslogtreecommitdiff
path: root/morphlib/util.py
diff options
context:
space:
mode:
authorSam Thursfield <sam@afuera.me.uk>2014-08-29 10:20:13 +0000
committerSam Thursfield <sam@afuera.me.uk>2014-09-19 14:32:56 +0000
commit359248a35948d2060dba97ef7073c155e3b9c1bb (patch)
treef3794295153601b58a12c0576a12e3a89b0f6063 /morphlib/util.py
parenta32de7934cce79dda2b8dc20be1c5ec94109869e (diff)
downloadmorph-359248a35948d2060dba97ef7073c155e3b9c1bb.tar.gz
Don't log environment variables with 'PASSWORD' in their name.
This involved rewriting the util.log_dict_diff() function. It has been renamed to log_environment_changes() to better reflect its purpose. It no longer logs both the old and new values in the event of an environment variable changing. It now just logs the new value. This makes the code simpler and seems like it should not be a big problem. Some projects recommend passing credentials through the environment. OpenStack does this, for example, see: <http://docs.openstack.org/user-guide/content/cli_openrc.html> It's unlikely that users would be happy about applications saving these passwords in log files all over their system. I do not recommend ever storing valuable passwords in the environment.
Diffstat (limited to 'morphlib/util.py')
-rw-r--r--morphlib/util.py39
1 files changed, 21 insertions, 18 deletions
diff --git a/morphlib/util.py b/morphlib/util.py
index 36ab4e21..0d4e25dc 100644
--- a/morphlib/util.py
+++ b/morphlib/util.py
@@ -207,24 +207,27 @@ def new_repo_caches(app): # pragma: no cover
return lrc, rrc
-
-def log_dict_diff(app, cur, pre): # pragma: no cover
- '''Log the differences between two dicts to debug log'''
- dictA = cur
- dictB = pre
- for key in dictA.keys():
- if key not in dictB:
- app.status(msg="New environment: %(key)s = %(value)s",
- key=key, value=dictA[key], chatty=True)
- elif dictA[key] != dictB[key]:
- app.status(msg="Environment changed: \
- %(key)s = %(valA)s to %(key)s = %(valB)s",
- key=key, valA=dictA[key], valB=dictB[key], chatty=True)
- for key in dictB.keys():
- if key not in dictA:
- app.status(msg="Environment removed: %(key)s = %(value)s",
- key=key, value=dictB[key], chatty=True)
-
+def env_variable_is_password(key): # pragma: no cover
+ return 'PASSWORD' in key
+
+def log_environment_changes(app, current_env, previous_env): # pragma: no cover
+ '''Log the differences between two environments to debug log.'''
+ def log_event(key, value, event):
+ if env_variable_is_password(key):
+ value_msg = '(value hidden)'
+ else:
+ value_msg = '= "%s"' % value
+ msg = '%s environment variable %s %s' % (event, key, value_msg)
+ app.status(msg=msg, chatty=True)
+
+ for key in current_env.keys():
+ if key not in previous_env:
+ log_event(key, current_env[key], 'new')
+ elif current_env[key] != previous_env[key]:
+ log_event(key, current_env[key], 'changed')
+ for key in previous_env.keys():
+ if key not in current_env:
+ log_event(key, previous_env[key], 'unset')
# This acquired from rdiff-backup which is GPLv2+ and a patch from 2011
# which has not yet been merged, combined with a tad of tidying from us.