From d826d901e6258a9e26aac26678fce9c9e5c03ab7 Mon Sep 17 00:00:00 2001 From: Austin Lucas Lake <53884490+austinlucaslake@users.noreply.github.com> Date: Mon, 6 May 2024 11:37:34 -0700 Subject: [PATCH] consolidated functions and added parameter to force new key generation --- plugins/modules/gpg_keypair.py | 244 ++++++++++++++++----------------- 1 file changed, 119 insertions(+), 125 deletions(-) diff --git a/plugins/modules/gpg_keypair.py b/plugins/modules/gpg_keypair.py index 23d2a39f..7d9143c4 100644 --- a/plugins/modules/gpg_keypair.py +++ b/plugins/modules/gpg_keypair.py @@ -130,6 +130,17 @@ options: type: list elements: str default: [] + force: + description: + - If O(force=True), key generation is executed using the module's options, even a matching key is found. + - This parameter does not override V(check_mode). + - This parameter is ignored if O(state=absent). + type: bool + default: False +notes: + - If a user-id is provided, the module's options are matched against all keys with said user-id. + - If a fingerprint is provided but no user-id is provided, the module's options are matched against the fingerprint(s). + - If neither a fingerprint or user-id is provided, the module's options are matched against all keys. ''' EXAMPLES = ''' @@ -199,6 +210,35 @@ def all_permutations(arr): for i in range(len(arr)))) +def key_type_from_algo(algo): + if algo == 1: + return 'RSA' + elif algo == 16: + return 'ELG' + elif algo == 17: + return 'DSA' + elif algo == 18: + return 'ECDH' + elif algo == 19: + return 'ECDSA' + elif algo == 22: + return 'EDDSA' + + +def expand_usages(usages): + usages = list(usages) + for i in range(len(usages)): + if usages[i] == 'c': + usages[i] = 'cert' + elif usages[i] == 's': + usages[i] = 'sign' + elif usages[i] == 'a': + usages[i] = 'auth' + elif usages[i] == 'e': + usages[i] = 'encrypt' + return usages + + def validate_key(module, key_type, key_length, key_curve, key_usage, key_name='primary key'): if key_type == 'EDDSA': if key_curve and key_curve != 'ed25519': @@ -237,7 +277,83 @@ def validate_key(module, key_type, key_length, key_curve, key_usage, key_name='p pass -def validate_params(module, params): +def delete_keypair(module, matching_keys, check_mode): + if matching_keys: + module.run_command( + ['--dry-run' if check_mode else '', '--batch', '--yes', '--delete-secret-and-public-key'] + matching_keys, + executable=get_bin_path('gpg') + ) + return dict(changed=True, fingerprints=matching_keys) + return dict(changed=False, fingerprints=[]) + + +def generate_keypair(module, params, matching_keys, check_mode): + if matching_keys and not params['force']: + return dict(changed=False, fingerprints=matching_keys) + + parameters = '''<