2023-12-24 17:09:08 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2024-01-28 15:21:38 +00:00
|
|
|
from typing import Tuple
|
2024-01-24 21:33:08 +00:00
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
DOCUMENTATION = r"""
|
2023-12-24 17:09:08 +00:00
|
|
|
---
|
2024-02-07 20:49:00 +00:00
|
|
|
module: ednz_cloud.hashistack.vault_init
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
short_description: Manages the initialization of HashiCorp Vault.
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
description:
|
|
|
|
- This module initializes HashiCorp Vault, ensuring that it is securely set up for use.
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
requirements:
|
|
|
|
- C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html))
|
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
options:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url:
|
|
|
|
description: The URL of the HashiCorp Vault API.
|
2023-12-24 17:09:08 +00:00
|
|
|
required: true
|
|
|
|
type: str
|
2024-01-24 21:33:08 +00:00
|
|
|
key_shares:
|
|
|
|
description:
|
|
|
|
- The number of key shares to split the master key into.
|
|
|
|
- Default is 5.
|
|
|
|
required: false
|
|
|
|
type: int
|
|
|
|
default: 5
|
|
|
|
key_threshold:
|
2023-12-24 17:09:08 +00:00
|
|
|
description:
|
2024-01-24 21:33:08 +00:00
|
|
|
- The number of key shares required to reconstruct the master key.
|
|
|
|
- Default is 3.
|
2023-12-24 17:09:08 +00:00
|
|
|
required: false
|
2024-01-24 21:33:08 +00:00
|
|
|
type: int
|
|
|
|
default: 3
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
author:
|
2024-02-07 20:49:00 +00:00
|
|
|
- Bertrand Lanson (@ednz_cloud)
|
2024-01-24 21:33:08 +00:00
|
|
|
"""
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
EXAMPLES = r"""
|
|
|
|
# Example: Initialize HashiCorp Vault with default settings
|
|
|
|
- name: Initialize HashiCorp Vault
|
2023-12-24 17:09:08 +00:00
|
|
|
my_namespace.my_collection.my_test:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url: https://vault.example.com
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
# Example: Initialize HashiCorp Vault with custom key shares and threshold
|
|
|
|
- name: Initialize HashiCorp Vault with custom settings
|
2023-12-24 17:09:08 +00:00
|
|
|
my_namespace.my_collection.my_test:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url: https://vault.example.com
|
|
|
|
key_shares: 7
|
|
|
|
key_threshold: 4
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = r"""
|
|
|
|
state:
|
2024-01-28 15:21:38 +00:00
|
|
|
description:
|
|
|
|
- Information about the state of HashiCorp Vault after initialization.
|
|
|
|
- This is a complex dictionary with the following keys:
|
|
|
|
- keys
|
|
|
|
- keys_base64
|
|
|
|
- root_token
|
|
|
|
- If the vault is already initialized, it will return a simple dict with a message stating it.
|
|
|
|
type: dict
|
2023-12-24 17:09:08 +00:00
|
|
|
returned: always
|
2024-01-28 15:21:38 +00:00
|
|
|
sample:
|
|
|
|
keys:
|
|
|
|
- wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
|
|
|
|
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
|
|
- yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
|
|
|
|
keys_base64:
|
|
|
|
- wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
|
|
|
|
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
|
|
- yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
|
|
|
|
root_token: hvs.zzzzzzzzzzzzzzzzzzzzzzzz
|
2024-01-24 21:33:08 +00:00
|
|
|
"""
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-12-28 21:38:26 +00:00
|
|
|
import traceback
|
2023-12-27 12:40:25 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import hvac
|
|
|
|
except ImportError:
|
|
|
|
HAS_HVAC = False
|
|
|
|
HVAC_IMPORT_ERROR = traceback.format_exc()
|
|
|
|
else:
|
|
|
|
HVAC_IMPORT_ERROR = None
|
|
|
|
HAS_HVAC = True
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
def initialize_vault(
|
|
|
|
api_url: str, key_shares: int, key_threshold: int
|
|
|
|
) -> Tuple[bool, dict]:
|
|
|
|
client = hvac.Client(url=api_url)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not client.sys.is_initialized():
|
|
|
|
return True, client.sys.initialize(key_shares, key_threshold)
|
|
|
|
else:
|
|
|
|
return False, {"message": "Vault is already initialized"}
|
|
|
|
except hvac.exceptions.VaultError as e:
|
|
|
|
raise hvac.exceptions.VaultError(f"Vault initialization failed: {str(e)}")
|
|
|
|
|
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url=dict(type="str", required=True),
|
|
|
|
key_shares=dict(type="int", required=False, default=5),
|
|
|
|
key_threshold=dict(type="int", required=False, default=3),
|
2023-12-24 17:09:08 +00:00
|
|
|
)
|
|
|
|
|
2024-01-24 22:54:58 +00:00
|
|
|
result = dict(changed=False, state="")
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
module = AnsibleModule(argument_spec=module_args, supports_check_mode=False)
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2023-12-27 19:30:39 +00:00
|
|
|
if not HAS_HVAC:
|
|
|
|
module.fail_json(
|
2024-01-24 21:33:08 +00:00
|
|
|
msg="Missing required library: hvac", exception=HVAC_IMPORT_ERROR
|
2023-12-27 19:30:39 +00:00
|
|
|
)
|
|
|
|
|
2023-12-28 21:38:26 +00:00
|
|
|
try:
|
2024-01-28 15:21:38 +00:00
|
|
|
vault_init_result, response_data = initialize_vault(
|
|
|
|
module.params["api_url"],
|
|
|
|
module.params["key_shares"],
|
|
|
|
module.params["key_threshold"],
|
|
|
|
)
|
|
|
|
|
|
|
|
result["changed"] = vault_init_result
|
|
|
|
result["state"] = response_data
|
2023-12-28 21:38:26 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
module.exit_json(**result)
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
except ValueError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-12-24 17:09:08 +00:00
|
|
|
main()
|