refactor(gpg_secretstore): fallible python imports for modules and plugins

This commit is contained in:
Jan Christian Grünhage 2022-07-12 10:38:50 +02:00
parent 761e12344f
commit a87df0120b
No known key found for this signature in database
GPG key ID: EEC1170CE56FA2ED

View file

@ -1,7 +1,7 @@
#!/usr/bin/python
# coding: utf-8
# (c) 2021, Famedly GmbH
# (c) 2021-2022, Famedly GmbH
# GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl-3.0.txt)
from __future__ import absolute_import, division, print_function
@ -12,6 +12,7 @@ DOCUMENTATION = """
name: gpg_secretstore
author:
- Jadyn Emma Jäger (@jadyndev)
- Jan Christian Grünhage (@jcgruenhage)
short_description: read passwords that are compatible with passwordstore.org's pass utility
description:
- Enables Ansible to read passwords/secrets from the passwordstore.org pass utility.
@ -64,20 +65,27 @@ _raw:
"""
from ansible.plugins.lookup import LookupBase
from ansible_collections.famedly.base.plugins.module_utils.gpg_utils import SecretStore
# Check if all required libs can loaded
try:
import gnupg
HAS_LIB = True
except ImportError:
raise ModuleNotFoundError("Library PGPy not found!")
from ansible.module_utils.six import raise_from
from ansible.module_utils.basic import missing_required_lib
from ansible.errors import AnsibleError
from ansible_collections.famedly.base.plugins.module_utils.gpg_utils import (
SecretStore,
check_secretstore_import_errors,
)
class LookupModule(LookupBase):
def run(self, terms: dict, variables, **kwargs):
errors = []
traceback = []
for lib, exception in check_secretstore_import_errors().items():
errors.append(missing_required_lib(lib))
traceback.append(exception)
if errors:
raise_from(
AnsibleError("\n".join(errors)),
"\n".join(traceback),
)
if len(terms) == 1:
data_type = "plain"
else: