Blame SOURCES/ansible-freeipa-0.1.12-Add-suppport-for-changing-password-of-symmetric-vaults_rhbz#1839197.patch

01db47
From 78b635ae78346fdfb298dd0d0c82ae1ff34b754a Mon Sep 17 00:00:00 2001
01db47
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
01db47
Date: Tue, 23 Jun 2020 17:53:47 -0300
01db47
Subject: [PATCH] Add suppport for changing password of symmetric vaults.
01db47
01db47
Allows changing passwords of symmetric waults, using a new variable
01db47
`new_password` (or the file-base version, `new_password_file`). The
01db47
old password must be passed using the `password` or `password_file`
01db47
variables that also received new aliases `old_password` and
01db47
`old_password_file`, respectively.
01db47
01db47
Tests were modyfied to reflect the changes.
01db47
---
01db47
 README-vault.md                               |  23 +++-
01db47
 .../vault/change-password-symmetric-vault.yml |   2 +-
01db47
 plugins/modules/ipavault.py                   | 129 +++++++++++++++---
01db47
 tests/vault/test_vault_symmetric.yml          |  64 +++++++++
01db47
 4 files changed, 194 insertions(+), 24 deletions(-)
01db47
01db47
diff --git a/README-vault.md b/README-vault.md
01db47
index c7ae6916..fa1d3e11 100644
01db47
--- a/README-vault.md
01db47
+++ b/README-vault.md
01db47
@@ -165,6 +165,22 @@ Example playbook to make sure vault data is absent in a symmetric vault:
01db47
       state: absent
01db47
 ```
01db47
 
01db47
+Example playbook to change the password of a symmetric:
01db47
+
01db47
+```yaml
01db47
+---
01db47
+- name: Playbook to handle vaults
01db47
+  hosts: ipaserver
01db47
+  become: true
01db47
+
01db47
+  tasks:
01db47
+  - ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: symvault
01db47
+      old_password: SomeVAULTpassword
01db47
+      new_password: SomeNEWpassword
01db47
+```
01db47
+
01db47
 Example playbook to make sure vault is absent:
01db47
 
01db47
 ```yaml
01db47
@@ -197,8 +213,11 @@ Variable | Description | Required
01db47
 `name` \| `cn` | The list of vault name strings. | yes
01db47
 `description` | The vault description string. | no
01db47
 `nomembers` | Suppress processing of membership attributes. (bool) | no
01db47
-`password ` \| `vault_password` \| `ipavaultpassword` | Vault password. | no
01db47
-`public_key ` \| `vault_public_key` \| `ipavaultpublickey` | Base64 encoded vault public key. | no
01db47
+`password` \| `vault_password` \| `ipavaultpassword` \| `old_password`| Vault password. | no
01db47
+`password_file` \| `vault_password_file` \| `old_password_file`| File containing Base64 encoded Vault password. | no
01db47
+`new_password` | Vault new password. | no
01db47
+`new_password_file` | File containing Base64 encoded new Vault password. | no
01db47
+`public_key ` \| `vault_public_key` \| `old_password_file` | Base64 encoded vault public key. | no
01db47
 `public_key_file` \| `vault_public_key_file` | Path to file with public key. | no
01db47
 `private_key `\| `vault_private_key` | Base64 encoded vault private key. Used only to retrieve data. | no
01db47
 `private_key_file` \| `vault_private_key_file` | Path to file with private key. Used only to retrieve data. | no
01db47
diff --git a/playbooks/vault/change-password-symmetric-vault.yml b/playbooks/vault/change-password-symmetric-vault.yml
01db47
index 3871f45d..396a79f6 100644
01db47
--- a/playbooks/vault/change-password-symmetric-vault.yml
01db47
+++ b/playbooks/vault/change-password-symmetric-vault.yml
01db47
@@ -10,7 +10,7 @@
01db47
       ipaadmin_password: SomeADMINpassword
01db47
       name: symvault
01db47
       password: SomeVAULTpassword
01db47
-  - name: Change vault passord.
01db47
+  - name: Change vault password.
01db47
     ipavault:
01db47
       ipaadmin_password: SomeADMINpassword
01db47
       name: symvault
01db47
diff --git a/plugins/modules/ipavault.py b/plugins/modules/ipavault.py
01db47
index ad5dd413..46c6fcdb 100644
01db47
--- a/plugins/modules/ipavault.py
01db47
+++ b/plugins/modules/ipavault.py
01db47
@@ -69,12 +69,20 @@
01db47
     description: password to be used on symmetric vault.
01db47
     required: false
01db47
     type: string
01db47
-    aliases: ["ipavaultpassword", "vault_password"]
01db47
+    aliases: ["ipavaultpassword", "vault_password", "old_password"]
01db47
   password_file:
01db47
     description: file with password to be used on symmetric vault.
01db47
     required: false
01db47
     type: string
01db47
-    aliases: ["vault_password_file"]
01db47
+    aliases: ["vault_password_file", "old_password_file"]
01db47
+  new_password:
01db47
+    description: new password to be used on symmetric vault.
01db47
+    required: false
01db47
+    type: string
01db47
+  new_password_file:
01db47
+    description: file with new password to be used on symmetric vault.
01db47
+    required: false
01db47
+    type: string
01db47
   salt:
01db47
     description: Vault salt.
01db47
     required: false
01db47
@@ -235,7 +243,15 @@
01db47
     state: retrieved
01db47
   register: result
01db47
 - debug:
01db47
-    msg: "{{ result.data | b64decode }}"
01db47
+    msg: "{{ result.data }}"
01db47
+
01db47
+# Change password of a symmetric vault
01db47
+- ipavault:
01db47
+    ipaadmin_password: SomeADMINpassword
01db47
+    name: symvault
01db47
+    username: admin
01db47
+    old_password: SomeVAULTpassword
01db47
+    new_password: SomeNEWpassword
01db47
 
01db47
 # Ensure vault symvault is absent
01db47
 - ipavault:
01db47
@@ -416,18 +432,29 @@ def check_parameters(module, state, action, description, username, service,
01db47
                      shared, users, groups, services, owners, ownergroups,
01db47
                      ownerservices, vault_type, salt, password, password_file,
01db47
                      public_key, public_key_file, private_key,
01db47
-                     private_key_file, vault_data, datafile_in, datafile_out):
01db47
+                     private_key_file, vault_data, datafile_in, datafile_out,
01db47
+                     new_password, new_password_file):
01db47
     invalid = []
01db47
     if state == "present":
01db47
         invalid = ['private_key', 'private_key_file', 'datafile_out']
01db47
 
01db47
+        if all([password, password_file]) \
01db47
+           or all([new_password, new_password_file]):
01db47
+            module.fail_json(msg="Password specified multiple times.")
01db47
+
01db47
+        if any([new_password, new_password_file]) \
01db47
+           and not any([password, password_file]):
01db47
+            module.fail_json(
01db47
+                msg="Either `password` or `password_file` must be provided to "
01db47
+                    "change symmetric vault password.")
01db47
+
01db47
         if action == "member":
01db47
             invalid.extend(['description'])
01db47
 
01db47
     elif state == "absent":
01db47
         invalid = ['description', 'salt', 'vault_type', 'private_key',
01db47
                    'private_key_file', 'datafile_in', 'datafile_out',
01db47
-                   'vault_data']
01db47
+                   'vault_data', 'new_password', 'new_password_file']
01db47
 
01db47
         if action == "vault":
01db47
             invalid.extend(['users', 'groups', 'services', 'owners',
01db47
@@ -437,7 +464,7 @@ def check_parameters(module, state, action, description, username, service,
01db47
     elif state == "retrieved":
01db47
         invalid = ['description', 'salt', 'datafile_in', 'users', 'groups',
01db47
                    'owners', 'ownergroups', 'public_key', 'public_key_file',
01db47
-                   'vault_data']
01db47
+                   'vault_data', 'new_password', 'new_password_file']
01db47
         if action == 'member':
01db47
             module.fail_json(
01db47
                 msg="State `retrieved` do not support action `member`.")
01db47
@@ -458,11 +485,17 @@ def check_parameters(module, state, action, description, username, service,
01db47
 def check_encryption_params(module, state, action, vault_type, salt,
01db47
                             password, password_file, public_key,
01db47
                             public_key_file, private_key, private_key_file,
01db47
-                            vault_data, datafile_in, datafile_out, res_find):
01db47
+                            vault_data, datafile_in, datafile_out,
01db47
+                            new_password, new_password_file, res_find):
01db47
     vault_type_invalid = []
01db47
+
01db47
+    if res_find is not None:
01db47
+        vault_type = res_find['ipavaulttype']
01db47
+
01db47
     if vault_type == "standard":
01db47
         vault_type_invalid = ['public_key', 'public_key_file', 'password',
01db47
-                              'password_file', 'salt']
01db47
+                              'password_file', 'salt', 'new_password',
01db47
+                              'new_password_file']
01db47
 
01db47
     if vault_type is None or vault_type == "symmetric":
01db47
         vault_type_invalid = ['public_key', 'public_key_file',
01db47
@@ -473,8 +506,14 @@ def check_encryption_params(module, state, action, vault_type, salt,
01db47
                 msg="Symmetric vault requires password or password_file "
01db47
                     "to store data or change `salt`.")
01db47
 
01db47
+        if any([new_password, new_password_file]) and res_find is None:
01db47
+            module.fail_json(
01db47
+                msg="Cannot modify password of inexistent vault.")
01db47
+
01db47
     if vault_type == "asymmetric":
01db47
-        vault_type_invalid = ['password', 'password_file']
01db47
+        vault_type_invalid = [
01db47
+            'password', 'password_file', 'new_password', 'new_password_file'
01db47
+        ]
01db47
         if not any([public_key, public_key_file]) and res_find is None:
01db47
             module.fail_json(
01db47
                 msg="Assymmetric vault requires public_key "
01db47
@@ -487,6 +526,43 @@ def check_encryption_params(module, state, action, vault_type, salt,
01db47
                 (param, vault_type or 'symmetric'))
01db47
 
01db47
 
01db47
+def change_password(module, res_find, password, password_file, new_password,
01db47
+                    new_password_file):
01db47
+    """
01db47
+    Change the password of a symmetric vault.
01db47
+
01db47
+    To change the password of a vault, it is needed to retrieve the stored
01db47
+    data with the current password, and store the data again, with the new
01db47
+    password, forcing it to override the old one.
01db47
+    """
01db47
+    # verify parameters.
01db47
+    if not any([new_password, new_password_file]):
01db47
+        return []
01db47
+    if res_find["ipavaulttype"][0] != "symmetric":
01db47
+        module.fail_json(msg="Cannot change password of `%s` vault."
01db47
+                             % res_find["ipavaulttype"])
01db47
+
01db47
+    # prepare arguments to retrieve data.
01db47
+    name = res_find["cn"][0]
01db47
+    args = {}
01db47
+    if password:
01db47
+        args["password"] = password
01db47
+    if password_file:
01db47
+        args["password"] = password_file
01db47
+    # retrieve current stored data
01db47
+    result = api_command(module, 'vault_retrieve', name, args)
01db47
+    args['data'] = result['result']['data']
01db47
+
01db47
+    # modify arguments to store data with new password.
01db47
+    if password:
01db47
+        args["password"] = new_password
01db47
+    if password_file:
01db47
+        args["password"] = new_password_file
01db47
+    args["override_password"] = True
01db47
+    # return the command to store data with the new password.
01db47
+    return [(name, "vault_archive", args)]
01db47
+
01db47
+
01db47
 def main():
01db47
     ansible_module = AnsibleModule(
01db47
         argument_spec=dict(
01db47
@@ -533,10 +609,18 @@ def main():
01db47
             datafile_out=dict(type="str", required=False, default=None,
01db47
                               aliases=['out']),
01db47
             vault_password=dict(type="str", required=False, default=None,
01db47
-                                aliases=['ipavaultpassword', 'password'],
01db47
-                                no_log=True),
01db47
+                                no_log=True,
01db47
+                                aliases=['ipavaultpassword', 'password',
01db47
+                                         "old_password"]),
01db47
             vault_password_file=dict(type="str", required=False, default=None,
01db47
-                                     no_log=False, aliases=['password_file']),
01db47
+                                     no_log=False,
01db47
+                                     aliases=[
01db47
+                                        'password_file', "old_password_file"
01db47
+                                     ]),
01db47
+            new_password=dict(type="str", required=False, default=None,
01db47
+                              no_log=True),
01db47
+            new_password_file=dict(type="str", required=False, default=None,
01db47
+                                   no_log=False),
01db47
             # state
01db47
             action=dict(type="str", default="vault",
01db47
                         choices=["vault", "data", "member"]),
01db47
@@ -546,6 +630,7 @@ def main():
01db47
         supports_check_mode=True,
01db47
         mutually_exclusive=[['username', 'service', 'shared'],
01db47
                             ['datafile_in', 'vault_data'],
01db47
+                            ['new_password', 'new_password_file'],
01db47
                             ['vault_password', 'vault_password_file'],
01db47
                             ['vault_public_key', 'vault_public_key_file']],
01db47
     )
01db47
@@ -576,6 +661,8 @@ def main():
01db47
     salt = module_params_get(ansible_module, "vault_salt")
01db47
     password = module_params_get(ansible_module, "vault_password")
01db47
     password_file = module_params_get(ansible_module, "vault_password_file")
01db47
+    new_password = module_params_get(ansible_module, "new_password")
01db47
+    new_password_file = module_params_get(ansible_module, "new_password_file")
01db47
     public_key = module_params_get(ansible_module, "vault_public_key")
01db47
     public_key_file = module_params_get(ansible_module,
01db47
                                         "vault_public_key_file")
01db47
@@ -614,7 +701,8 @@ def main():
01db47
                      service, shared, users, groups, services, owners,
01db47
                      ownergroups, ownerservices, vault_type, salt, password,
01db47
                      password_file, public_key, public_key_file, private_key,
01db47
-                     private_key_file, vault_data, datafile_in, datafile_out)
01db47
+                     private_key_file, vault_data, datafile_in, datafile_out,
01db47
+                     new_password, new_password_file)
01db47
     # Init
01db47
 
01db47
     changed = False
01db47
@@ -660,7 +748,7 @@ def main():
01db47
                     ansible_module, state, action, vault_type, salt, password,
01db47
                     password_file, public_key, public_key_file, private_key,
01db47
                     private_key_file, vault_data, datafile_in, datafile_out,
01db47
-                    res_find)
01db47
+                    new_password, new_password_file, res_find)
01db47
 
01db47
                 # Found the vault
01db47
                 if action == "vault":
01db47
@@ -721,7 +809,6 @@ def main():
01db47
                     owner_add_args = gen_member_args(
01db47
                         args, owner_add, ownergroups_add, ownerservice_add)
01db47
                     if owner_add_args is not None:
01db47
-                        # ansible_module.warn("OWNER ADD: %s" % owner_add_args)
01db47
                         commands.append(
01db47
                             [name, 'vault_add_owner', owner_add_args])
01db47
 
01db47
@@ -729,7 +816,6 @@ def main():
01db47
                     owner_del_args = gen_member_args(
01db47
                         args, owner_del, ownergroups_del, ownerservice_del)
01db47
                     if owner_del_args is not None:
01db47
-                        # ansible_module.warn("OWNER DEL: %s" % owner_del_args)
01db47
                         commands.append(
01db47
                             [name, 'vault_remove_owner', owner_del_args])
01db47
 
01db47
@@ -758,19 +844,22 @@ def main():
01db47
                 if any([vault_data, datafile_in]):
01db47
                     commands.append([name, "vault_archive", pwdargs])
01db47
 
01db47
+                cmds = change_password(
01db47
+                    ansible_module, res_find, password, password_file,
01db47
+                    new_password, new_password_file)
01db47
+                commands.extend(cmds)
01db47
+
01db47
             elif state == "retrieved":
01db47
                 if res_find is None:
01db47
                     ansible_module.fail_json(
01db47
                         msg="Vault `%s` not found to retrieve data." % name)
01db47
 
01db47
-                vault_type = res_find['cn']
01db47
-
01db47
                 # verify data encription args
01db47
                 check_encryption_params(
01db47
                     ansible_module, state, action, vault_type, salt, password,
01db47
                     password_file, public_key, public_key_file, private_key,
01db47
                     private_key_file, vault_data, datafile_in, datafile_out,
01db47
-                    res_find)
01db47
+                    new_password, new_password_file, res_find)
01db47
 
01db47
                 pwdargs = data_storage_args(
01db47
                     args, vault_data, password, password_file, private_key,
01db47
@@ -813,7 +902,6 @@ def main():
01db47
         errors = []
01db47
         for name, command, args in commands:
01db47
             try:
01db47
-                # ansible_module.warn("RUN: %s %s %s" % (command, name, args))
01db47
                 result = api_command(ansible_module, command, name, args)
01db47
 
01db47
                 if command == 'vault_archive':
01db47
@@ -829,7 +917,6 @@ def main():
01db47
                         raise Exception("No data retrieved.")
01db47
                     changed = False
01db47
                 else:
01db47
-                    # ansible_module.warn("RESULT: %s" % (result))
01db47
                     if "completed" in result:
01db47
                         if result["completed"] > 0:
01db47
                             changed = True
01db47
diff --git a/tests/vault/test_vault_symmetric.yml b/tests/vault/test_vault_symmetric.yml
01db47
index c9429f4f..a6072d88 100644
01db47
--- a/tests/vault/test_vault_symmetric.yml
01db47
+++ b/tests/vault/test_vault_symmetric.yml
01db47
@@ -178,6 +178,61 @@
01db47
     register: result
01db47
     failed_when: result.data != 'Hello World.' or result.changed
01db47
 
01db47
+  - name: Change vault password.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: symvault
01db47
+      password: SomeVAULTpassword
01db47
+      new_password: SomeNEWpassword
01db47
+    register: result
01db47
+    failed_when: not result.changed
01db47
+
01db47
+  - name: Retrieve data from symmetric vault, with wrong password.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: symvault
01db47
+      password: SomeVAULTpassword
01db47
+      state: retrieved
01db47
+    register: result
01db47
+    failed_when: not result.failed or "Invalid credentials" not in result.msg
01db47
+
01db47
+  - name: Change vault password, with wrong `old_password`.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: symvault
01db47
+      password: SomeVAULTpassword
01db47
+      new_password: SomeNEWpassword
01db47
+    register: result
01db47
+    failed_when: not result.failed or "Invalid credentials" not in result.msg
01db47
+
01db47
+  - name: Retrieve data from symmetric vault, with new password.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: symvault
01db47
+      password: SomeNEWpassword
01db47
+      state: retrieved
01db47
+    register: result
01db47
+    failed_when: result.data != 'Hello World.' or result.changed
01db47
+
01db47
+  - name: Try to add vault with multiple passwords.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: inexistentvault
01db47
+      password: SomeVAULTpassword
01db47
+      password_file: "{{ ansible_env.HOME }}/password.txt"
01db47
+    register: result
01db47
+    failed_when: not result.failed or "parameters are mutually exclusive" not in result.msg
01db47
+
01db47
+  - name: Try to add vault with multiple new passwords.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: inexistentvault
01db47
+      password: SomeVAULTpassword
01db47
+      new_password: SomeVAULTpassword
01db47
+      new_password_file: "{{ ansible_env.HOME }}/password.txt"
01db47
+    register: result
01db47
+    failed_when: not result.failed or "parameters are mutually exclusive" not in result.msg
01db47
+
01db47
   - name: Ensure symmetric vault is absent
01db47
     ipavault:
01db47
       ipaadmin_password: SomeADMINpassword
01db47
@@ -194,5 +249,14 @@
01db47
     register: result
01db47
     failed_when: result.changed
01db47
 
01db47
+  - name: Try to change password of inexistent vault.
01db47
+    ipavault:
01db47
+      ipaadmin_password: SomeADMINpassword
01db47
+      name: inexistentvault
01db47
+      password: SomeVAULTpassword
01db47
+      new_password: SomeNEWpassword
01db47
+    register: result
01db47
+    failed_when: not result.failed or "Cannot modify password of inexistent vault" not in result.msg
01db47
+
01db47
   - name: Cleanup testing environment.
01db47
     import_tasks: env_cleanup.yml