483b06
From a0ea8706fddb0459982c2ae276679cea6b0a812e Mon Sep 17 00:00:00 2001
483b06
From: Florence Blanc-Renaud <flo@redhat.com>
483b06
Date: Thu, 27 Apr 2017 18:20:06 +0200
483b06
Subject: [PATCH] vault: piped input for ipa vault-add fails
483b06
483b06
An exception is raised when using echo "Secret123\n" | ipa vault-add myvault
483b06
483b06
This happens because the code is using (string).decode(sys.stdin.encoding)
483b06
and sys.stdin.encoding is None when the input is read from a pipe.
483b06
The fix is using the prompt_password method defined by Backend.textui,
483b06
which gracefully handles this issue.
483b06
483b06
https://pagure.io/freeipa/issue/6907
483b06
483b06
Reviewed-By: Christian Heimes <cheimes@redhat.com>
483b06
Reviewed-By: Abhijeet Kasurde <akasurde@redhat.com>
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
---
483b06
 ipaclient/plugins/vault.py | 37 ++++++++-----------------------------
483b06
 1 file changed, 8 insertions(+), 29 deletions(-)
483b06
483b06
diff --git a/ipaclient/plugins/vault.py b/ipaclient/plugins/vault.py
483b06
index 3fb4900d9cf90e6902c40e1c3d8cfdafec2e28b8..f21dc4dbb6579c0f92ae9ab94d76a6396b26b233 100644
483b06
--- a/ipaclient/plugins/vault.py
483b06
+++ b/ipaclient/plugins/vault.py
483b06
@@ -21,11 +21,9 @@ from __future__ import print_function
483b06
 
483b06
 import base64
483b06
 import errno
483b06
-import getpass
483b06
 import io
483b06
 import json
483b06
 import os
483b06
-import sys
483b06
 import tempfile
483b06
 
483b06
 from cryptography.fernet import Fernet, InvalidToken
483b06
@@ -84,29 +82,6 @@ register = Registry()
483b06
 MAX_VAULT_DATA_SIZE = 2**20  # = 1 MB
483b06
 
483b06
 
483b06
-def get_new_password():
483b06
-    """
483b06
-    Gets new password from user and verify it.
483b06
-    """
483b06
-    while True:
483b06
-        password = getpass.getpass('New password: ').decode(
483b06
-            sys.stdin.encoding)
483b06
-        password2 = getpass.getpass('Verify password: ').decode(
483b06
-            sys.stdin.encoding)
483b06
-
483b06
-        if password == password2:
483b06
-            return password
483b06
-
483b06
-        print('  ** Passwords do not match! **')
483b06
-
483b06
-
483b06
-def get_existing_password():
483b06
-    """
483b06
-    Gets existing password from user.
483b06
-    """
483b06
-    return getpass.getpass('Password: ').decode(sys.stdin.encoding)
483b06
-
483b06
-
483b06
 def generate_symmetric_key(password, salt):
483b06
     """
483b06
     Generates symmetric key from password and salt.
483b06
@@ -304,7 +279,8 @@ class vault_add(Local):
483b06
                 password = password.rstrip('\n')
483b06
 
483b06
             else:
483b06
-                password = get_new_password()
483b06
+                password = self.api.Backend.textui.prompt_password(
483b06
+                    'New password')
483b06
 
483b06
             # generate vault salt
483b06
             options['ipavaultsalt'] = os.urandom(16)
483b06
@@ -887,9 +863,11 @@ class vault_archive(ModVaultData):
483b06
 
483b06
             else:
483b06
                 if override_password:
483b06
-                    password = get_new_password()
483b06
+                    password = self.api.Backend.textui.prompt_password(
483b06
+                        'New password')
483b06
                 else:
483b06
-                    password = get_existing_password()
483b06
+                    password = self.api.Backend.textui.prompt_password(
483b06
+                        'Password', confirm=False)
483b06
 
483b06
             if not override_password:
483b06
                 # verify password by retrieving existing data
483b06
@@ -1112,7 +1090,8 @@ class vault_retrieve(ModVaultData):
483b06
                 password = password.rstrip('\n')
483b06
 
483b06
             else:
483b06
-                password = get_existing_password()
483b06
+                password = self.api.Backend.textui.prompt_password(
483b06
+                    'Password', confirm=False)
483b06
 
483b06
             # generate encryption key from password
483b06
             encryption_key = generate_symmetric_key(password, salt)
483b06
-- 
483b06
2.12.2
483b06