summaryrefslogtreecommitdiff
path: root/keystoneclient/common
diff options
context:
space:
mode:
authorDirk Mueller <dirk@dmllr.de>2013-06-20 18:49:26 +0200
committerDirk Mueller <dirk@dmllr.de>2013-11-28 22:53:43 +0100
commit07a7c3102c684baa8b364f419c9e9b6c18360f5e (patch)
tree5df869b90bbd22631e4ede91500318021865e8c7 /keystoneclient/common
parent30ab23854c3411a0a4f03e5640b0dd596214e2e1 (diff)
downloadpython-keystoneclient-07a7c3102c684baa8b364f419c9e9b6c18360f5e.tar.gz
Add workaround for OSError raised by Popen.communicate()
Python 2.6 can raise OSError when too much data is written to STDIN and the process died prematurely. In the case of keystoneclient this happens during the first cms_verify() call of a process. The calling logic expects a useful error message in order to refetch the CA or singing CERT, which is missing in the case of an OSError. So just fake it instead. Add basic unit tests to cover all of the public methods from keystone.common.cms, raising test coverage to 77%. Add unit test for this specific bug (test_cms_verify_token_no_oserror). Closes-Bug: LP Bug#1235252 Change-Id: I6e650ab9494c605b4e41c78c87a9505e09d5fc29
Diffstat (limited to 'keystoneclient/common')
-rw-r--r--keystoneclient/common/cms.py51
1 files changed, 47 insertions, 4 deletions
diff --git a/keystoneclient/common/cms.py b/keystoneclient/common/cms.py
index da04029..0e6a5b7 100644
--- a/keystoneclient/common/cms.py
+++ b/keystoneclient/common/cms.py
@@ -21,6 +21,7 @@ If set_subprocess() is not called, this module will pick Python's subprocess
or eventlet.green.subprocess based on if os module is patched by eventlet.
"""
+import errno
import hashlib
import logging
@@ -57,6 +58,46 @@ def set_subprocess(_subprocess=None):
subprocess = _subprocess
+def _check_files_accessible(files):
+ err = None
+ try:
+ for try_file in files:
+ with open(try_file, 'r'):
+ pass
+ except IOError as e:
+ # Catching IOError means there is an issue with
+ # the given file.
+ err = ('Hit OSError in _process_communicate_handle_oserror()\n'
+ 'Likely due to %s: %s') % (try_file, e.strerror)
+
+ return err
+
+
+def _process_communicate_handle_oserror(process, text, files):
+ """Wrapper around process.communicate that checks for OSError."""
+
+ try:
+ output, err = process.communicate(text)
+ except OSError as e:
+ if e.errno != errno.EPIPE:
+ raise
+ # OSError with EPIPE only occurs with Python 2.6.x/old 2.7.x
+ # http://bugs.python.org/issue10963
+
+ # The quick exit is typically caused by the openssl command not being
+ # able to read an input file, so check ourselves if can't read a file.
+ err = _check_files_accessible(files)
+ if process.stderr:
+ err += process.stderr.read()
+
+ output = ""
+ retcode = -1
+ else:
+ retcode = process.poll()
+
+ return output, err, retcode
+
+
def cms_verify(formatted, signing_cert_file_name, ca_file_name):
"""Verifies the signature of the contents IAW CMS syntax.
@@ -73,8 +114,8 @@ def cms_verify(formatted, signing_cert_file_name, ca_file_name):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
- output, err = process.communicate(formatted)
- retcode = process.poll()
+ output, err, retcode = _process_communicate_handle_oserror(
+ process, formatted, (signing_cert_file_name, ca_file_name))
# Do not log errors, as some happen in the positive thread
# instead, catch them in the calling code and log them there.
@@ -184,8 +225,10 @@ def cms_sign_text(text, signing_cert_file_name, signing_key_file_name):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
- output, err = process.communicate(text)
- retcode = process.poll()
+
+ output, err, retcode = _process_communicate_handle_oserror(
+ process, text, (signing_cert_file_name, signing_key_file_name))
+
if retcode or "Error" in err:
LOG.error('Signing error: %s' % err)
raise subprocess.CalledProcessError(retcode, "openssl")