Skip to content

Commit e01be7e

Browse files
authored
ClientTlsContext tests and rename-o-rama (#68)
The arguments to these functions confused me so I renamed them.
1 parent 0902a35 commit e01be7e

File tree

8 files changed

+131
-61
lines changed

8 files changed

+131
-61
lines changed

awscrt/io.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ class TlsVersion(IntEnum):
129129

130130
class TlsContextOptions(object):
131131
__slots__ = (
132-
'min_tls_ver', 'ca_path', 'ca_buffer', 'alpn_list',
132+
'min_tls_ver', 'ca_dirpath', 'ca_buffer', 'alpn_list',
133133
'certificate_buffer', 'private_key_buffer',
134-
'pkcs12_path', 'pkcs12_password', 'verify_peer')
134+
'pkcs12_filepath', 'pkcs12_password', 'verify_peer')
135135

136136
def __init__(self):
137137

@@ -141,31 +141,30 @@ def __init__(self):
141141
self.min_tls_ver = TlsVersion.DEFAULT
142142
self.verify_peer = True
143143

144-
def override_default_trust_store_from_path(self, ca_path, ca_file):
144+
def override_default_trust_store_from_path(self, ca_dirpath, ca_filepath):
145145

146-
assert isinstance_str(ca_path) or ca_path is None
147-
assert isinstance_str(ca_file) or ca_file is None
146+
assert isinstance_str(ca_dirpath) or ca_dirpath is None
147+
assert isinstance_str(ca_filepath) or ca_filepath is None
148148

149-
ca_buffer = None
150-
if ca_file:
151-
ca_buffer = _read_binary_file(ca_file)
149+
if ca_filepath:
150+
ca_buffer = _read_binary_file(ca_filepath)
151+
self.override_default_trust_store(ca_buffer)
152152

153-
self.ca_path = ca_path
154-
self.override_default_trust_store(ca_buffer)
153+
self.ca_dirpath = ca_dirpath
155154

156155
def override_default_trust_store(self, rootca_buffer):
157156
assert isinstance(rootca_buffer, bytes)
158157

159158
self.ca_buffer = rootca_buffer
160159

161160
@staticmethod
162-
def create_client_with_mtls_from_path(cert_path, pk_path):
161+
def create_client_with_mtls_from_path(cert_filepath, pk_filepath):
163162

164-
assert isinstance_str(cert_path)
165-
assert isinstance_str(pk_path)
163+
assert isinstance_str(cert_filepath)
164+
assert isinstance_str(pk_filepath)
166165

167-
cert_buffer = _read_binary_file(cert_path)
168-
key_buffer = _read_binary_file(pk_path)
166+
cert_buffer = _read_binary_file(cert_filepath)
167+
key_buffer = _read_binary_file(pk_filepath)
169168

170169
return TlsContextOptions.create_client_with_mtls(cert_buffer, key_buffer)
171170

@@ -182,25 +181,25 @@ def create_client_with_mtls(cert_buffer, key_buffer):
182181
return opt
183182

184183
@staticmethod
185-
def create_client_with_mtls_pkcs12(pkcs12_path, pkcs12_password):
184+
def create_client_with_mtls_pkcs12(pkcs12_filepath, pkcs12_password):
186185

187-
assert isinstance_str(pkcs12_path)
186+
assert isinstance_str(pkcs12_filepath)
188187
assert isinstance_str(pkcs12_password)
189188

190189
opt = TlsContextOptions()
191-
opt.pkcs12_path = pkcs12_path
190+
opt.pkcs12_filepath = pkcs12_filepath
192191
opt.pkcs12_password = pkcs12_password
193192
opt.verify_peer = True
194193
return opt
195194

196195
@staticmethod
197-
def create_server_from_path(cert_path, pk_path):
196+
def create_server_from_path(cert_filepath, pk_filepath):
198197

199-
assert isinstance_str(cert_path)
200-
assert isinstance_str(pk_path)
198+
assert isinstance_str(cert_filepath)
199+
assert isinstance_str(pk_filepath)
201200

202-
cert_buffer = _read_binary_file(cert_path)
203-
key_buffer = _read_binary_file(pk_path)
201+
cert_buffer = _read_binary_file(cert_filepath)
202+
key_buffer = _read_binary_file(pk_filepath)
204203

205204
return TlsContextOptions.create_server(cert_buffer, key_buffer)
206205

@@ -216,13 +215,13 @@ def create_server(cert_buffer, key_buffer):
216215
return opt
217216

218217
@staticmethod
219-
def create_server_pkcs12(pkcs12_path, pkcs12_password):
218+
def create_server_pkcs12(pkcs12_filepath, pkcs12_password):
220219

221-
assert isinstance_str(pkcs12_path)
220+
assert isinstance_str(pkcs12_filepath)
222221
assert isinstance_str(pkcs12_password)
223222

224223
opt = TlsContextOptions()
225-
opt.pkcs12_path = pkcs12_path
224+
opt.pkcs12_filepath = pkcs12_filepath
226225
opt.pkcs12_password = pkcs12_password
227226
opt.verify_peer = False
228227
return opt
@@ -237,12 +236,12 @@ def __init__(self, options):
237236
super(ClientTlsContext, self).__init__()
238237
self._binding = _awscrt.client_tls_ctx_new(
239238
options.min_tls_ver.value,
240-
options.ca_path,
239+
options.ca_dirpath,
241240
options.ca_buffer,
242241
options.alpn_list,
243242
options.certificate_buffer,
244243
options.private_key_buffer,
245-
options.pkcs12_path,
244+
options.pkcs12_filepath,
246245
options.pkcs12_password,
247246
options.verify_peer
248247
)

source/io.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,30 +369,30 @@ PyObject *aws_py_client_tls_ctx_new(PyObject *self, PyObject *args) {
369369
struct aws_allocator *allocator = aws_py_get_allocator();
370370

371371
int min_tls_version;
372-
const char *ca_path;
372+
const char *ca_dirpath;
373373
const char *ca_buffer;
374374
Py_ssize_t ca_buffer_len;
375375
const char *alpn_list;
376376
const char *certificate_buffer;
377377
Py_ssize_t certificate_buffer_len;
378378
const char *private_key_buffer;
379379
Py_ssize_t private_key_buffer_len;
380-
const char *pkcs12_path;
380+
const char *pkcs12_filepath;
381381
const char *pkcs12_password;
382382
uint8_t verify_peer;
383383
if (!PyArg_ParseTuple(
384384
args,
385385
"bzz#zz#z#zzb",
386386
&min_tls_version,
387-
&ca_path,
387+
&ca_dirpath,
388388
&ca_buffer,
389389
&ca_buffer_len,
390390
&alpn_list,
391391
&certificate_buffer,
392392
&certificate_buffer_len,
393393
&private_key_buffer,
394394
&private_key_buffer_len,
395-
&pkcs12_path,
395+
&pkcs12_filepath,
396396
&pkcs12_password,
397397
&verify_peer)) {
398398
return NULL;
@@ -415,8 +415,8 @@ PyObject *aws_py_client_tls_ctx_new(PyObject *self, PyObject *args) {
415415

416416
ctx_options.minimum_tls_version = min_tls_version;
417417

418-
if (ca_path) {
419-
if (aws_tls_ctx_options_override_default_trust_store_from_path(&ctx_options, ca_path, NULL)) {
418+
if (ca_dirpath) {
419+
if (aws_tls_ctx_options_override_default_trust_store_from_path(&ctx_options, ca_dirpath, NULL)) {
420420
PyErr_SetAwsLastError();
421421
goto ctx_options_failure;
422422
}
@@ -438,9 +438,10 @@ PyObject *aws_py_client_tls_ctx_new(PyObject *self, PyObject *args) {
438438
}
439439

440440
#ifdef __APPLE__
441-
if (pkcs12_path && pkcs12_password) {
441+
if (pkcs12_filepath && pkcs12_password) {
442442
struct aws_byte_cursor password = aws_byte_cursor_from_c_str(pkcs12_password);
443-
if (aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(&ctx_options, allocator, pkcs12_path, &password)) {
443+
if (aws_tls_ctx_options_init_client_mtls_pkcs12_from_path(
444+
&ctx_options, allocator, pkcs12_filepath, &password)) {
444445
PyErr_SetAwsLastError();
445446
goto ctx_options_failure;
446447
}

test/files/short.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/resources/unittests.crt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDzjCCArYCCQCoztOER4pOkzANBgkqhkiG9w0BAQsFADCBqDELMAkGA1UEBhMC
3+
VVMxEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1NlYXR0bGUxIDAeBgNV
4+
BAoMF0FtYXpvbiBXZWIgU2VydmljZXMgSW5jMRswGQYDVQQLDBJBV1MgU0RLcyBh
5+
bmQgVG9vbHMxEjAQBgNVBAMMCWxvY2FsaG9zdDEfMB0GCSqGSIb3DQEJARYQaGVu
6+
c29AYW1hem9uLmNvbTAeFw0xNzA5MDEwMjE2MThaFw00NTAxMTcwMjE2MThaMIGo
7+
MQswCQYDVQQGEwJVUzETMBEGA1UECAwKV2FzaGluZ3RvbjEQMA4GA1UEBwwHU2Vh
8+
dHRsZTEgMB4GA1UECgwXQW1hem9uIFdlYiBTZXJ2aWNlcyBJbmMxGzAZBgNVBAsM
9+
EkFXUyBTREtzIGFuZCBUb29sczESMBAGA1UEAwwJbG9jYWxob3N0MR8wHQYJKoZI
10+
hvcNAQkBFhBoZW5zb0BhbWF6b24uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
11+
MIIBCgKCAQEA12pXSPgORAMlQtYRbxuz/Ocaoran3C2Fjyjhu0vucSEZSwxDJp75
12+
TBQEMafSpSEKAQLeDt7xuDRDYn52V4UE6cF+xTWhtzsf7mhN/lHaDPcvR2ASPAEk
13+
zkil8KCLY4e6tTxSwQ97splNuEZ099HoJYTTLFaReIfd1D3zZ1EYcSw8w+GZ2SxE
14+
UfYUSL2CFmIYSkQjnlsJCIpCoGgDiBAPbIUJO3KWBDX0JgGDbx3Wf3jXG/Y6T63L
15+
PsO+AS20RCvcEF0F/rlDINzI5EAHO1TOEd9fKOu+JAK06Pw1m77BgOrE7FtvIG7k
16+
YNVuOEPeInOHkOuqryDisB1PwiyPNIbqdQIDAQABMA0GCSqGSIb3DQEBCwUAA4IB
17+
AQDL3vA0QeYb+XE8pUm3lxwso4zf0lwYi8Fni23ThqlvTNrP0glaWNu28aa03F5r
18+
Jc80acRjySG8q/gqwMMLOE+xqLgTzAHLYDnX2BZdaeIJWdgQP/YrWACrnYlVJ4kZ
19+
fi3QiBU0b5OgQdwX0csr6NQ7fv5i9EiNdPf+Ll1gxQj0Q0AaJzb4+TUL4dHZV3L6
20+
RRRK5KpTI3I+5A3vLSYSgwlVT+qB4J6+Z7O9SZX8s0xnm569tECbRnDDYv3E90SU
21+
QMN6Rzsr2crUzQSMq2hQTnrpFvRX52Yw7Dkz4SgkP3Q4xzvITPgA8REgHd4eDgrz
22+
36J362qmeHxjl/+KLxv/Vr4b
23+
-----END CERTIFICATE-----

test/resources/unittests.csr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-----BEGIN CERTIFICATE REQUEST-----
2+
MIIC7jCCAdYCAQAwgagxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApXYXNoaW5ndG9u
3+
MRAwDgYDVQQHDAdTZWF0dGxlMSAwHgYDVQQKDBdBbWF6b24gV2ViIFNlcnZpY2Vz
4+
IEluYzEbMBkGA1UECwwSQVdTIFNES3MgYW5kIFRvb2xzMRIwEAYDVQQDDAlsb2Nh
5+
bGhvc3QxHzAdBgkqhkiG9w0BCQEWEGhlbnNvQGFtYXpvbi5jb20wggEiMA0GCSqG
6+
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDXaldI+A5EAyVC1hFvG7P85xqitqfcLYWP
7+
KOG7S+5xIRlLDEMmnvlMFAQxp9KlIQoBAt4O3vG4NENifnZXhQTpwX7FNaG3Ox/u
8+
aE3+UdoM9y9HYBI8ASTOSKXwoItjh7q1PFLBD3uymU24RnT30eglhNMsVpF4h93U
9+
PfNnURhxLDzD4ZnZLERR9hRIvYIWYhhKRCOeWwkIikKgaAOIEA9shQk7cpYENfQm
10+
AYNvHdZ/eNcb9jpPrcs+w74BLbREK9wQXQX+uUMg3MjkQAc7VM4R318o674kArTo
11+
/DWbvsGA6sTsW28gbuRg1W44Q94ic4eQ66qvIOKwHU/CLI80hup1AgMBAAGgADAN
12+
BgkqhkiG9w0BAQsFAAOCAQEASvXJuyYUTu58xtT2kAE9J8OQdyfYvAiTskN7JNHq
13+
Y3Dcj1IsHfpsthbop5sgoXFYnPDBHQjd5c8KD8SgzSwjReeGQFUUsdG8uB6w2dhM
14+
Kbqiz/Ny0AD+HyuVN3PqavbGJqcIetabskAxX5TP943WYveaJcz8D7+6B97S2Vk5
15+
5o8oiOeNOPqn2UC8erHjkw1kf1Rl4wa2jbmcrUjvsM9DiekYdbpAr+3xgoRDj03J
16+
bvTv4p2RQyO8hHaRX0zlpidXyfOEAjMz6ZqhItE++pxSbZ0aNfzm/c12CsTqB4ZA
17+
nGgVk67n/vmEPtobZGhIZoDGBsh1HJyApNX+JEP/av56gg==
18+
-----END CERTIFICATE REQUEST-----

test/resources/unittests.key

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 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hlND/jiBjBHfsC13oUOVz8TrAzxDKAzbGLcOT2trgxMFbR8Ez+jur
18+
1yQbPnoKZrB7SopAkcVqZv4ks0LLu+BLfEFXYy8CgYEApN8xXDgoRVnCqQpN53iM
19+
n/c0iqjOXkTIb/jIksAdv3AAjaayP2JaOXul7RL2fJeshYiw684vbb/RNK6jJDlM
20+
sH0Pt6t3tZmB2bC1KFfh7+BMdjg/p63LC6PAasa3GanObh67YADPOfoghCsOcgzd
21+
6brt56fRDdHgE2P75ER/zm8CgYEArAxx6bepT3syIWiYww3itYBJofS26zP9++Zs
22+
T9rX5hT5IbMo5vwIJqO0+mDVrwQfu9Wc7vnwjhm+pEy4qfPW6Hn7SNppxnY6itZo
23+
J4/azOIeaM92B5h3Pv0gxBFK8YyjO8beXurx+79ENuOtfFxd8knOe/Mplcnpurjt
24+
SeVJuG8CgYBxEYouOM9UuZlblXQXfudTWWf+x5CEWxyJgKaktHEh3iees1gB7ZPb
25+
OewLa8AYVjqbNgS/r/aUFjpBbCov8ICxcy86SuGda10LDFX83sbyMm8XhktfyC3L
26+
54irVW5mNUDcA8s9+DloeTlUlJIr8J/RADC9rpqHLaZzcdvpIMhVsw==
27+
-----END RSA PRIVATE KEY-----

test/resources/unittests.p12

2.52 KB
Binary file not shown.

test/test_io.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# permissions and limitations under the License.
1313

1414
from __future__ import absolute_import
15-
from awscrt.io import ClientBootstrap, DefaultHostResolver, EventLoopGroup
15+
from awscrt.io import ClientBootstrap, ClientTlsContext, DefaultHostResolver, EventLoopGroup, TlsContextOptions
1616
from test import NativeResourceTest
1717
import unittest
1818

@@ -42,5 +42,31 @@ def test_init(self):
4242
bootstrap = ClientBootstrap(event_loop_group, host_resolver)
4343

4444

45+
class ClientTlsContextTest(NativeResourceTest):
46+
def test_init_defaults(self):
47+
opt = TlsContextOptions()
48+
ctx = ClientTlsContext(opt)
49+
50+
def test_with_mtls_from_path(self):
51+
opt = TlsContextOptions.create_client_with_mtls_from_path(
52+
'test/resources/unittests.crt', 'test/resources/unittests.key')
53+
ctx = ClientTlsContext(opt)
54+
55+
def test_with_mtls_pkcs12(self):
56+
opt = TlsContextOptions.create_client_with_mtls_pkcs12(
57+
'test/resources/unittests.p12', '1234')
58+
ctx = ClientTlsContext(opt)
59+
60+
def test_override_default_trust_store_dir(self):
61+
opt = TlsContextOptions()
62+
opt.override_default_trust_store_from_path('test/resources', None)
63+
ctx = ClientTlsContext(opt)
64+
65+
def test_override_default_trust_store_file(self):
66+
opt = TlsContextOptions()
67+
opt.override_default_trust_store_from_path(None, 'test/resources/unittests.crt')
68+
ctx = ClientTlsContext(opt)
69+
70+
4571
if __name__ == '__main__':
4672
unittest.main()

0 commit comments

Comments
 (0)