Skip to content

Commit

Permalink
BUG: Properly Update Keyring Config Data
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Zaccardi authored Dec 19, 2016
2 parents df7e89d + 5ba59d5 commit 7f1908f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
18 changes: 13 additions & 5 deletions keg/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class KeyringError(Exception):
pass


class MissingValueError(Exception):

def __init__(self, key):
self.key = key

super(Exception, self).__init__("No value for {} found in key "
"ring".format(key))


class Manager(object):

# regex that matches "${*}$" where * = any printable ASCII character
Expand Down Expand Up @@ -81,8 +90,7 @@ def pattern_to_password(self, value, service_name=None):

stored_value = keyring.get_password(service, clean)
if not stored_value:
raise KeyError(
'Unable to find password for {}'.format(clean))
raise MissingValueError(clean)

value = value.replace(raw, stored_value, 1)

Expand All @@ -95,13 +103,13 @@ def substitute(self, data):
def key_and_value(key, value):
try:
password = self.pattern_to_password(value)
except KeyError:
except MissingValueError as err:
# Backwards comp... Ask for the value if we can't find it.
#
# TODO: Remove the need to ask for a password in this method
# instead the substitute should work on known values and setting
# values should be a different function
password = self.ask_and_create(key)
password = self.ask_and_create(err.key)

return key, password

Expand All @@ -112,7 +120,7 @@ def key_and_value(key, value):
# Mutate the data passed in for backwards comp.
# TODO: Remove this mutation and just return the data allowing the
# caller to decide what to do with it.
data = subbed
data.update(subbed)
return subbed

def delete(self, key):
Expand Down
5 changes: 3 additions & 2 deletions keg/tests/test_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ def test_substitute_finds_all_values_for_sub(self):
'multi_string': '${multi_s_one}$:${multi_s_two}$',
'multi_byte': b'${multi_b_one}$:${multi_b_two}$',
}
data = self.manager.substitute(data)
returned = self.manager.substitute(data)

assert self.manager.sub_keys_seen == {'string', 'bytes', 'multi_s_one',
'multi_s_two', 'multi_b_one',
'multi_b_two'}
assert data['multi_byte'] == b'multi_b_one:multi_b_two'
assert returned['multi_byte'] == b'multi_b_one:multi_b_two'
assert data == returned

def test_ask_and_create(self):
self.manager.secure_entry = lambda msg: 'password'
Expand Down

0 comments on commit 7f1908f

Please sign in to comment.