Merge "Raise better error on file read problems"

This commit is contained in:
Jenkins 2016-02-19 13:57:10 +00:00 committed by Gerrit Code Review
commit d4ba619b6e
2 changed files with 45 additions and 6 deletions

View File

@ -51,8 +51,14 @@ class X509Csr(signature.SignatureMixin):
if encoding == 'pem':
try:
der_content = util.extract_pem(f.read())
except IOError:
raise X509CsrError("Could not read from source %s" % f)
except Exception:
raise X509CsrError("Data not in PEM format")
raise X509CsrError(
"Data source not readable or not in PEM format")
if not der_content:
raise X509CsrError("No PEM data found")
elif encoding == 'der':
der_content = f.read()
else:
@ -79,8 +85,11 @@ class X509Csr(signature.SignatureMixin):
:param path: Path to the file on disk
"""
with open(path, 'r') as f:
return X509Csr.from_open_file(f, encoding)
try:
with open(path, 'r') as f:
return X509Csr.from_open_file(f, encoding)
except IOError:
raise X509CsrError("Could not read file %s" % path)
def get_pubkey(self):
"""Get the public key from the CSR

View File

@ -97,15 +97,45 @@ class TestX509Csr(tests.DefaultRequestMixin, unittest.TestCase):
entries = name.get_entries_by_oid(x509_name.OID_countryName)
self.assertEqual(entries[0].get_value(), "UK")
def test_open_failure_throws(self):
open_name = 'anchor.X509.signing_request.open'
with mock.patch(open_name, create=True) as mock_open:
mock_open.side_effect = IOError(2, "No such file or direcory",
"some_path")
self.assertRaisesRegexp(x509_errors.X509Error,
"Could not read file",
signing_request.X509Csr.from_file,
"some_path")
def test_read_failure_throws(self):
f = mock.Mock()
f.read.side_effect = IOError(5, "Read failed")
self.assertRaisesRegexp(x509_errors.X509Error,
"Could not read from source",
signing_request.X509Csr.from_open_file,
f)
def test_bad_pem_throws(self):
bad_data = (
b"-----BEGIN SOMETHING-----\n"
b"++++++\n"
b"-----END SOMETHING-----\n"
)
csr = signing_request.X509Csr()
self.assertRaisesRegexp(x509_errors.X509Error, "not in PEM format",
csr.from_buffer,
bad_data)
def test_bad_data_throws(self):
bad_data = (
b"some bad data is "
b"EHRlc3RAYW5jaG9yLnRlc3QwTDANBgkqhkiG9w0BAQEFAAM7ADA4AjEA6m")
csr = signing_request.X509Csr()
self.assertRaises(x509_errors.X509Error,
csr.from_buffer,
bad_data)
self.assertRaisesRegexp(x509_errors.X509Error, "No PEM data found",
csr.from_buffer,
bad_data)
def test_get_subject_countryName(self):
name = self.csr.get_subject()