| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import errno |
| 14 |
import os |
| 15 |
import subprocess |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import testresources |
| 19 |
from testtools import matchers |
| 20 |
|
| 21 |
from keystoneclient.common import cms |
| 22 |
from keystoneclient import exceptions |
| 23 |
from keystoneclient.tests.unit import client_fixtures |
| 24 |
from keystoneclient.tests.unit import utils |
| 25 |
|
| 26 |
|
| 27 |
class CMSTest(utils.TestCase, testresources.ResourcedTestCase): |
| 28 |
"""Unit tests for the keystoneclient.common.cms module.""" |
| 29 |
|
| 30 |
resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)] |
| 31 |
|
| 32 |
def __init__(self, *args, **kwargs): |
| 33 |
super(CMSTest, self).__init__(*args, **kwargs) |
| 34 |
process = subprocess.Popen(['openssl', 'version'], |
| 35 |
stdout=subprocess.PIPE) |
| 36 |
out, err = process.communicate() |
| 37 |
|
| 38 |
openssl_version = out.split()[1] |
| 39 |
|
| 40 |
if err or openssl_version.startswith(b'0'): |
| 41 |
raise Exception('Your version of OpenSSL is not supported. ' |
| 42 |
'You will need to update it to 1.0 or later.') |
| 43 |
|
| 44 |
def _raise_OSError(*args): |
| 45 |
e = OSError() |
| 46 |
e.errno = errno.EPIPE |
| 47 |
raise e |
| 48 |
|
| 49 |
def test_cms_verify(self): |
| 50 |
self.assertRaises(exceptions.CertificateConfigError, |
| 51 |
cms.cms_verify, |
| 52 |
'data', |
| 53 |
'no_exist_cert_file', |
| 54 |
'no_exist_ca_file') |
| 55 |
|
| 56 |
def test_token_tocms_to_token(self): |
| 57 |
with open(os.path.join(client_fixtures.CMSDIR, |
| 58 |
'auth_token_scoped.pem')) as f: |
| 59 |
AUTH_TOKEN_SCOPED_CMS = f.read() |
| 60 |
|
| 61 |
self.assertEqual(cms.token_to_cms(self.examples.SIGNED_TOKEN_SCOPED), |
| 62 |
AUTH_TOKEN_SCOPED_CMS) |
| 63 |
|
| 64 |
tok = cms.cms_to_token(cms.token_to_cms( |
| 65 |
self.examples.SIGNED_TOKEN_SCOPED)) |
| 66 |
self.assertEqual(tok, self.examples.SIGNED_TOKEN_SCOPED) |
| 67 |
|
| 68 |
def test_asn1_token(self): |
| 69 |
self.assertTrue(cms.is_asn1_token(self.examples.SIGNED_TOKEN_SCOPED)) |
| 70 |
self.assertFalse(cms.is_asn1_token('FOOBAR')) |
| 71 |
|
| 72 |
def test_cms_sign_token_no_files(self): |
| 73 |
self.assertRaises(subprocess.CalledProcessError, |
| 74 |
cms.cms_sign_token, |
| 75 |
self.examples.TOKEN_SCOPED_DATA, |
| 76 |
'/no/such/file', '/no/such/key') |
| 77 |
|
| 78 |
def test_cms_sign_token_no_files_pkiz(self): |
| 79 |
self.assertRaises(subprocess.CalledProcessError, |
| 80 |
cms.pkiz_sign, |
| 81 |
self.examples.TOKEN_SCOPED_DATA, |
| 82 |
'/no/such/file', '/no/such/key') |
| 83 |
|
| 84 |
def test_cms_sign_token_success(self): |
| 85 |
self.assertTrue( |
| 86 |
cms.pkiz_sign(self.examples.TOKEN_SCOPED_DATA, |
| 87 |
self.examples.SIGNING_CERT_FILE, |
| 88 |
self.examples.SIGNING_KEY_FILE)) |
| 89 |
|
| 90 |
def test_cms_verify_token_no_files(self): |
| 91 |
self.assertRaises(exceptions.CertificateConfigError, |
| 92 |
cms.cms_verify, |
| 93 |
self.examples.SIGNED_TOKEN_SCOPED, |
| 94 |
'/no/such/file', '/no/such/key') |
| 95 |
|
| 96 |
def test_cms_verify_token_no_oserror(self): |
| 97 |
with mock.patch('subprocess.Popen.communicate', |
| 98 |
new=self._raise_OSError): |
| 99 |
try: |
| 100 |
cms.cms_verify("x", '/no/such/file', '/no/such/key') |
| 101 |
except exceptions.CertificateConfigError as e: |
| 102 |
self.assertIn('/no/such/file', e.output) |
| 103 |
self.assertIn('Hit OSError ', e.output) |
| 104 |
else: |
| 105 |
self.fail('Expected exceptions.CertificateConfigError') |
| 106 |
|
| 107 |
def test_cms_verify_token_scoped(self): |
| 108 |
cms_content = cms.token_to_cms(self.examples.SIGNED_TOKEN_SCOPED) |
| 109 |
self.assertTrue(cms.cms_verify(cms_content, |
| 110 |
self.examples.SIGNING_CERT_FILE, |
| 111 |
self.examples.SIGNING_CA_FILE)) |
| 112 |
|
| 113 |
def test_cms_verify_token_scoped_expired(self): |
| 114 |
cms_content = cms.token_to_cms( |
| 115 |
self.examples.SIGNED_TOKEN_SCOPED_EXPIRED) |
| 116 |
self.assertTrue(cms.cms_verify(cms_content, |
| 117 |
self.examples.SIGNING_CERT_FILE, |
| 118 |
self.examples.SIGNING_CA_FILE)) |
| 119 |
|
| 120 |
def test_cms_verify_token_unscoped(self): |
| 121 |
cms_content = cms.token_to_cms(self.examples.SIGNED_TOKEN_UNSCOPED) |
| 122 |
self.assertTrue(cms.cms_verify(cms_content, |
| 123 |
self.examples.SIGNING_CERT_FILE, |
| 124 |
self.examples.SIGNING_CA_FILE)) |
| 125 |
|
| 126 |
def test_cms_verify_token_v3_scoped(self): |
| 127 |
cms_content = cms.token_to_cms(self.examples.SIGNED_v3_TOKEN_SCOPED) |
| 128 |
self.assertTrue(cms.cms_verify(cms_content, |
| 129 |
self.examples.SIGNING_CERT_FILE, |
| 130 |
self.examples.SIGNING_CA_FILE)) |
| 131 |
|
| 132 |
def test_cms_hash_token_no_token_id(self): |
| 133 |
token_id = None |
| 134 |
self.assertThat(cms.cms_hash_token(token_id), matchers.Is(None)) |
| 135 |
|
| 136 |
def test_cms_hash_token_not_pki(self): |
| 137 |
"""If the token_id is not a PKI token then it returns the token_id.""" |
| 138 |
token = 'something' |
| 139 |
self.assertFalse(cms.is_asn1_token(token)) |
| 140 |
self.assertThat(cms.cms_hash_token(token), matchers.Is(token)) |
| 141 |
|
| 142 |
def test_cms_hash_token_default_md5(self): |
| 143 |
"""The default hash method is md5.""" |
| 144 |
token = self.examples.SIGNED_TOKEN_SCOPED |
| 145 |
token_id_default = cms.cms_hash_token(token) |
| 146 |
token_id_md5 = cms.cms_hash_token(token, mode='md5') |
| 147 |
self.assertThat(token_id_default, matchers.Equals(token_id_md5)) |
| 148 |
|
| 149 |
self.assertThat(token_id_default, matchers.HasLength(32)) |
| 150 |
|
| 151 |
def test_cms_hash_token_sha256(self): |
| 152 |
"""Can also hash with sha256.""" |
| 153 |
token = self.examples.SIGNED_TOKEN_SCOPED |
| 154 |
token_id = cms.cms_hash_token(token, mode='sha256') |
| 155 |
|
| 156 |
self.assertThat(token_id, matchers.HasLength(64)) |
| 157 |
|
| 158 |
@mock.patch('keystoneclient.common.cms._check_files_accessible') |
| 159 |
def test_process_communicate_handle_oserror_epipe(self, files_acc_mock): |
| 160 |
process_mock = mock.Mock() |
| 161 |
process_mock.communicate = self._raise_OSError |
| 162 |
process_mock.stderr = mock.Mock() |
| 163 |
process_mock.stderr.read = mock.Mock(return_value='proc stderr') |
| 164 |
files_acc_mock.return_value = 1, ('file_path', 'fileerror') |
| 165 |
output, err, retcode = cms._process_communicate_handle_oserror( |
| 166 |
process_mock, '', []) |
| 167 |
|
| 168 |
self.assertEqual((output, retcode), ('', 1)) |
| 169 |
self.assertIn('file_path', err) |
| 170 |
self.assertIn('fileerror', err) |
| 171 |
self.assertIn('proc stderr', err) |
| 172 |
|
| 173 |
@mock.patch('keystoneclient.common.cms._check_files_accessible') |
| 174 |
def test_process_communicate_handle_oserror_epipe_files_ok( |
| 175 |
self, files_acc_mock): |
| 176 |
process_mock = mock.Mock() |
| 177 |
process_mock.communicate = self._raise_OSError |
| 178 |
process_mock.stderr = mock.Mock() |
| 179 |
process_mock.stderr.read = mock.Mock(return_value='proc stderr') |
| 180 |
files_acc_mock.return_value = -1, None |
| 181 |
output, err, retcode = cms._process_communicate_handle_oserror( |
| 182 |
process_mock, '', []) |
| 183 |
|
| 184 |
self.assertEqual((output, retcode), ('', -1)) |
| 185 |
self.assertIn('proc stderr', err) |
| 186 |
|
| 187 |
def test_process_communicate_handle_oserror_no_exception(self): |
| 188 |
process_mock = mock.Mock() |
| 189 |
process_mock.communicate.return_value = 'out', 'err' |
| 190 |
process_mock.poll.return_value = 0 |
| 191 |
|
| 192 |
output, err, retcode = cms._process_communicate_handle_oserror( |
| 193 |
process_mock, '', []) |
| 194 |
|
| 195 |
self.assertEqual(output, 'out') |
| 196 |
self.assertEqual(err, 'err') |
| 197 |
self.assertEqual(retcode, 0) |
| 198 |
|
| 199 |
|
| 200 |
def load_tests(loader, tests, pattern): |
| 201 |
return testresources.OptimisingTestSuite(tests) |