mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Don't remove comment from public key, and improve error message for duplicate keys (github_key) (#22282)
* improve error message for duplicate public keys * don't consider self when searching for duplicates * don't strip comment from key
This commit is contained in:
parent
e0d0446183
commit
e5ff8e3407
1 changed files with 16 additions and 10 deletions
|
@ -132,12 +132,12 @@ class GitHubSession(object):
|
||||||
|
|
||||||
def get_all_keys(session):
|
def get_all_keys(session):
|
||||||
url = API_BASE + '/user/keys'
|
url = API_BASE + '/user/keys'
|
||||||
|
result = []
|
||||||
while url:
|
while url:
|
||||||
r = session.request('GET', url)
|
r = session.request('GET', url)
|
||||||
for key in r.json():
|
result.extend(r.json())
|
||||||
yield key
|
|
||||||
|
|
||||||
url = r.links().get('next')
|
url = r.links().get('next')
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def create_key(session, name, pubkey, check_mode):
|
def create_key(session, name, pubkey, check_mode):
|
||||||
|
@ -176,11 +176,20 @@ def ensure_key_absent(session, name, check_mode):
|
||||||
'deleted_keys': to_delete}
|
'deleted_keys': to_delete}
|
||||||
|
|
||||||
|
|
||||||
def ensure_key_present(session, name, pubkey, force, check_mode):
|
def ensure_key_present(module, session, name, pubkey, force, check_mode):
|
||||||
matching_keys = [k for k in get_all_keys(session) if k['title'] == name]
|
all_keys = get_all_keys(session)
|
||||||
|
matching_keys = [k for k in all_keys if k['title'] == name]
|
||||||
deleted_keys = []
|
deleted_keys = []
|
||||||
|
|
||||||
if matching_keys and force and matching_keys[0]['key'] != pubkey:
|
new_signature = pubkey.split(' ')[1]
|
||||||
|
for key in all_keys:
|
||||||
|
existing_signature = key['key'].split(' ')[1]
|
||||||
|
if new_signature == existing_signature and key['title'] != name:
|
||||||
|
module.fail_json(msg=(
|
||||||
|
"another key with the same content is already registered "
|
||||||
|
"under the name |{}|").format(key['title']))
|
||||||
|
|
||||||
|
if matching_keys and force and matching_keys[0]['key'].split(' ')[1] != new_signature:
|
||||||
delete_keys(session, matching_keys, check_mode=check_mode)
|
delete_keys(session, matching_keys, check_mode=check_mode)
|
||||||
(deleted_keys, matching_keys) = (matching_keys, [])
|
(deleted_keys, matching_keys) = (matching_keys, [])
|
||||||
|
|
||||||
|
@ -221,15 +230,12 @@ def main():
|
||||||
# Keys consist of a protocol, the key data, and an optional comment.
|
# Keys consist of a protocol, the key data, and an optional comment.
|
||||||
if len(pubkey_parts) < 2:
|
if len(pubkey_parts) < 2:
|
||||||
module.fail_json(msg='"pubkey" parameter has an invalid format')
|
module.fail_json(msg='"pubkey" parameter has an invalid format')
|
||||||
|
|
||||||
# Strip out comment so we can compare to the keys GitHub returns.
|
|
||||||
pubkey = ' '.join(pubkey_parts[:2])
|
|
||||||
elif state == 'present':
|
elif state == 'present':
|
||||||
module.fail_json(msg='"pubkey" is required when state=present')
|
module.fail_json(msg='"pubkey" is required when state=present')
|
||||||
|
|
||||||
session = GitHubSession(module, token)
|
session = GitHubSession(module, token)
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
result = ensure_key_present(session, name, pubkey, force=force,
|
result = ensure_key_present(module, session, name, pubkey, force=force,
|
||||||
check_mode=module.check_mode)
|
check_mode=module.check_mode)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
result = ensure_key_absent(session, name, check_mode=module.check_mode)
|
result = ensure_key_absent(session, name, check_mode=module.check_mode)
|
||||||
|
|
Loading…
Add table
Reference in a new issue