rougail-user-data-ansible/src/rougail/user_data_ansible/__init__.py
2025-04-09 21:26:42 +02:00

71 lines
2.4 KiB
Python

"""
Silique (https://www.silique.fr)
Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from pathlib import Path
from ansible.parsing.vault import VaultLib, PromptVaultSecret
from ansible.module_utils._text import to_bytes
from rougail import RougailConfig
from rougail.user_data_yaml import RougailUserDataYaml
from rougail.error import ExtentionError
from .i18n import _
from .__version__ import __version__
class RougailUserDataAnsible(RougailUserDataYaml):
"""Load Ansible data from encrypted file
"""
def __init__(
self,
config,
*,
rougailconfig=None,
) -> None:
if rougailconfig is None:
rougailconfig = RougailConfig
user_data = rougailconfig["step.user_data"]
if "ansible" not in user_data:
user_data.append("ansible")
rougailconfig["step.user_data"] = user_data
user_data = rougailconfig["step.user_data"]
if "ansible" not in user_data:
raise ExtentionError(_("ansible is not set in step.user_data"))
self.rougailconfig = rougailconfig
self.filenames = self.rougailconfig["ansible.filename"]
self.secret = self.rougailconfig["ansible.secret"]
self.file_with_secrets = self.rougailconfig["ansible.file_with_secrets"]
self.config = config
self.errors = []
self.warnings = []
def open(self, filename: str) -> dict:
"""Open file
"""
prompt = PromptVaultSecret(
to_bytes(self.secret), "default", ["Vault password: "]
)
vault = VaultLib([("default", prompt)])
with Path(filename).open("rb") as fh:
return self.yaml.load(vault.decrypt(fh.read()))
RougailUserData = RougailUserDataAnsible
__all__ = ("RougailUserDataAnsible",)