Skip to content

Commit

Permalink
replace keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Jun 26, 2016
1 parent a8e124b commit aae18f1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
11 changes: 2 additions & 9 deletions lib/creds/cred_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,19 @@ def create_plan(existing_users=None, proposed_users=None):
# TODO: Add 'interactive' option
def execute_plan(plan=None):
for task in plan:
print('TASK')
print(task)
state = task['comparison_result']['state']
result = task['comparison_result']
print('RESULT')
print(result)
if state == 'missing':
command = generate_add_user_command(task.get('proposed_user'))
print('COMMAND FOR NEW: {0}'.format(command))

command_output = execute_command(command)
print('COMMAND OUTPUT = {0}'.format(command_output))
if task['proposed_user'].public_keys:
write_authorized_keys(task['proposed_user'])
return command_output
elif state == 'existing' and result:
command = generate_modify_user_command(task=task)
print('COMMAND FOR EXISTING: {0}'.format(command))
command_output = execute_command(command)
print(command_output)
if task['proposed_user'].public_keys:
write_authorized_keys(task['proposed_user'])
return command_output
else:
print('Skipping')
2 changes: 0 additions & 2 deletions lib/creds/ssh/public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

class PublicKey(object):
def __init__(self, raw=None, b64encoded=None):
print(raw)
print(b64encoded)
if not any((raw, b64encoded)):
raise AttributeError('Key not provided')
self._raw = raw
Expand Down
16 changes: 5 additions & 11 deletions lib/creds/user_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,20 @@ def compare_user(passed_user=None, user_list=None):
# if passed_user.public_keys and (not returned[0].public_keys == passed_user.public_keys):
existing_keys = returned[0].public_keys
passed_keys = passed_user.public_keys
replace_keys = True
replace_keys = False
# Check if existing and passed keys exist, and if so, compare
if all((existing_keys, passed_keys)) and len(existing_keys) == len(passed_user.public_keys):
# Compare each key, and if any differences, replace
existing = set(key.raw for key in existing_keys)
replacement = set( key.raw for key in passed_keys )
if set.difference(existing, replacement):
replace_keys = True
# If not both existing and passed keys set, then
# If not existing keys but keys passed set, then
if passed_keys and not existing_keys:
replace_keys = True

if replace_keys:
comparison_result['public_keys_action'] = 'modify'
comparison_result['current_public_keys_value'] = existing_keys
comparison_result['replacement_public_keys_value'] = passed_keys
print()
print('-------')
print(existing_keys)
print(passed_keys)
print('-------')

# comparison_result['shell_action'] = 'modify'
# comparison_result['current_shell_value'] = returned[0].shell
# comparison_result['replacement_shell_value'] = passed_user.shell
return dict(state='existing', result=comparison_result, existing_user=returned)
8 changes: 7 additions & 1 deletion tests/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ def test_update_existing_user():
create_test_user()
current_users = Users.from_passwd()
provided_users = list()
provided_users.append(User(name='testuserx1234', uid=59999, gecos='test user gecos update'))
raw_public_key = PUBLIC_KEYS[0].get('raw')
public_key = PublicKey(raw=raw_public_key)
provided_users.append(User(name='testuserx1234', uid=59999, gecos='test user gecos update', public_keys=[public_key]))
plan = create_plan(existing_users=current_users, proposed_users=provided_users)
assert plan[0]['comparison_result']['state'] == 'existing'
execute_plan(plan)
current_users = Users.from_passwd()
new_user = current_users.describe_users(users_filter=dict(name='testuserx1234'))
assert new_user[0].public_keys[0].raw == raw_public_key
delete_test_user_and_group()


Expand Down
2 changes: 1 addition & 1 deletion tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_user_instance_creation_and_write():
shell = '/bin/false'
public_key = PublicKey(raw=PUBLIC_KEYS[0]['raw'])
# print(public_key)
test_user = User(name=name, uid=uid, gid=gid, gecos=gecos, home_dir=home_dir, shell=shell, public_keys=[public_key])
# test_user = User(name=name, uid=uid, gid=gid, gecos=gecos, home_dir=home_dir, shell=shell, public_keys=[public_key])
# users = Users(input_list=[test_user])
# print()
# print(users)

0 comments on commit aae18f1

Please sign in to comment.