feat(vault): break things trying to fix vault unseal not returning anything on mulitple retries

This commit is contained in:
Bertrand Lanson 2024-01-24 23:54:58 +01:00
parent 3bb8eb8775
commit 8ce66d42a7
5 changed files with 41 additions and 25 deletions

View File

@ -61,17 +61,26 @@
ednxzu.hashistack.vault_unseal: ednxzu.hashistack.vault_unseal:
api_url: "{{ hashi_vault_configuration['api_addr'] }}" api_url: "{{ hashi_vault_configuration['api_addr'] }}"
key_shares: "{{ _vault_cluster_config['keys'] }}" key_shares: "{{ _vault_cluster_config['keys'] }}"
max_retries: "{{ (_vault_cluster_config['keys'] | length) - 1 }}" # max_retries: "{{ (_vault_cluster_config['keys'] | length) - 1 }}"
run_once: true run_once: true
delegate_to: "{{ groups['vault_servers'] | first }}" delegate_to: "{{ groups['vault_servers'] | first }}"
when: _vault_init_secret.changed when: _vault_init_secret.changed
register: _vault_unseal_secret
- name: "Print unseal status"
ansible.builtin.debug:
msg: "{{ _vault_unseal_secret }}"
- name: "Unseal all vault nodes" - name: "Unseal all vault nodes"
ednxzu.hashistack.vault_unseal: ednxzu.hashistack.vault_unseal:
api_url: "{{ hashi_vault_configuration['api_addr'] }}" api_url: "{{ hashi_vault_configuration['api_addr'] }}"
key_shares: "{{ _vault_cluster_config['keys'] }}" key_shares: "{{ _vault_cluster_config['keys'] }}"
max_retries: "{{ (_vault_cluster_config['keys'] | length) - 1 }}" # max_retries: "{{ (_vault_cluster_config['keys'] | length) - 1 }}"
retries: 5 retries: 5
delay: 5 delay: 5
register: _unseal_status register: _unseal_status
until: not _unseal_status.failed until: _unseal_status.changed
- name: "Print unseal status"
ansible.builtin.debug:
msg: "{{ _unseal_status }}"

View File

@ -150,6 +150,12 @@ vault_logging_configuration:
######################### #########################
extra_vault_container_volumes: [] extra_vault_container_volumes: []
#####################
# extra configuration
#####################
vault_extra_configuration: {}
############### ###############
# configuration # configuration
############### ###############

View File

@ -1,6 +1,6 @@
--- ---
# hashistack configuration merging for vault # hashistack configuration merging for vault
- name: "Merge listener configuration" - name: "Vault | Merge listener configuration"
ansible.builtin.set_fact: ansible.builtin.set_fact:
vault_listener_configuration: "{{ vault_listener_configuration: "{{
vault_listener_configuration | vault_listener_configuration |
@ -8,7 +8,7 @@
combine(vault_extra_listener_configuration | default({})) combine(vault_extra_listener_configuration | default({}))
}}" }}"
- name: "Merge service registration configuration" - name: "Vault | Merge service registration configuration"
vars: vars:
_config_to_merge: _config_to_merge:
service_registration: "{{ vault_service_registration_configuration }}" service_registration: "{{ vault_service_registration_configuration }}"
@ -19,7 +19,7 @@
}}" }}"
when: vault_enable_service_registration when: vault_enable_service_registration
- name: "Merge plugins configuration" - name: "Vault | Merge plugins configuration"
vars: vars:
_config_to_merge: _config_to_merge:
plugin_directory: "{{ vault_plugin_directory }}" plugin_directory: "{{ vault_plugin_directory }}"
@ -30,7 +30,7 @@
}}" }}"
when: vault_enable_plugins when: vault_enable_plugins
- name: "Merge logging configuration" - name: "Vault | Merge logging configuration"
vars: vars:
_config_to_merge: "{{ vault_logging_configuration }}" _config_to_merge: "{{ vault_logging_configuration }}"
ansible.builtin.set_fact: ansible.builtin.set_fact:
@ -39,3 +39,13 @@
combine(_config_to_merge) combine(_config_to_merge)
}}" }}"
when: vault_enable_log_to_file when: vault_enable_log_to_file
- name: "Vault | Merge extra configuration settings"
vars:
_config_to_merge: "{{ vault_extra_configuration }}"
ansible.builtin.set_fact:
hashi_vault_configuration: "{{
hashi_vault_configuration |
combine(_config_to_merge)
}}"
when: vault_extra_configuration is defined

View File

@ -6,7 +6,7 @@ __metaclass__ = type
DOCUMENTATION = r""" DOCUMENTATION = r"""
--- ---
module: my_test module: ednxzu.hashistack.vault_init
short_description: Manages the initialization of HashiCorp Vault. short_description: Manages the initialization of HashiCorp Vault.
@ -93,7 +93,7 @@ def run_module():
key_threshold=dict(type="int", required=False, default=3), key_threshold=dict(type="int", required=False, default=3),
) )
result = dict(changed=False, original_message="", state="") result = dict(changed=False, state="")
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

View File

@ -84,10 +84,9 @@ def run_module():
module_args = dict( module_args = dict(
api_url=dict(type="str", required=True), api_url=dict(type="str", required=True),
key_shares=dict(type="list", required=False, default=[]), key_shares=dict(type="list", required=False, default=[]),
max_retries=dict(type="int", required=False, default=3),
) )
result = dict(changed=False, original_message="", state=None) result = dict(changed=False, state="")
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
@ -108,24 +107,16 @@ def run_module():
# Unseal Vault # Unseal Vault
try: try:
retries = 0 key_shares = module.params["key_shares"]
vault_unseal_result = None vault_unseal_result = client.sys.submit_unseal_keys(key_shares)
while client.sys.is_sealed() and retries < module.params["max_retries"]: result["state"] = vault_unseal_result
key_share = module.params["key_shares"][
min(retries, len(module.params["key_shares"]) - 1)
]
vault_unseal_result = client.sys.submit_unseal_key(key_share)
retries += 1
except hvac.exceptions.VaultError as ve: except hvac.exceptions.VaultError as ve:
module.fail_json(msg=f"Vault unsealing failed: {ve}") module.fail_json(msg=f"Vault unsealing failed: {ve}")
# Check if the Vault is successfully unsealed
if client.sys.is_sealed(): if client.sys.is_sealed():
module.fail_json(msg="Vault unsealing failed: Maximum retries reached.") module.fail_json(msg="Vault unsealing failed.")
result["state"] = vault_unseal_result
result["changed"] = True result["changed"] = True
module.exit_json(**result) module.exit_json(**result)