2024-11-25 11:02:56 +01:00
|
|
|
"""
|
|
|
|
|
Silique (https://www.silique.fr)
|
|
|
|
|
Copyright (C) 2024
|
|
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-11-27 16:22:34 +01:00
|
|
|
from pathlib import Path
|
2024-11-25 11:02:56 +01:00
|
|
|
from ansible.parsing.vault import VaultLib, PromptVaultSecret
|
|
|
|
|
from ansible.module_utils._text import to_bytes
|
2024-11-27 16:22:34 +01:00
|
|
|
|
|
|
|
|
from rougail import RougailConfig
|
|
|
|
|
from rougail.user_data_file import RougailUserDataFile
|
2024-11-25 11:02:56 +01:00
|
|
|
|
|
|
|
|
from .i18n import _
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RougailUserDataAnsible(RougailUserDataFile):
|
2024-11-27 16:22:34 +01:00
|
|
|
"""Load Ansible data from encrypted file
|
|
|
|
|
"""
|
2024-11-25 11:02:56 +01:00
|
|
|
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 Exception(_("ansible is not set in step.user_data"))
|
|
|
|
|
self.rougailconfig = rougailconfig
|
|
|
|
|
self.filenames = self.rougailconfig["ansible.filename"]
|
|
|
|
|
self.secret = self.rougailconfig["ansible.secret"]
|
|
|
|
|
self.config = config
|
|
|
|
|
self.errors = []
|
|
|
|
|
self.warnings = []
|
|
|
|
|
|
|
|
|
|
def open(self, filename: str) -> dict:
|
2024-11-27 16:22:34 +01:00
|
|
|
"""Open file
|
|
|
|
|
"""
|
|
|
|
|
prompt = PromptVaultSecret(
|
|
|
|
|
to_bytes(self.secret), "default", ["Vault password: "]
|
|
|
|
|
)
|
|
|
|
|
vault = VaultLib([("default", prompt)])
|
|
|
|
|
with Path(filename).open("rb") as fh:
|
2024-11-25 11:02:56 +01:00
|
|
|
return self.yaml.load(vault.decrypt(fh.read()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RougailUserData = RougailUserDataAnsible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ("RougailUserDataAnsible",)
|