tls.py (1635:97afbb6c5a15) tls.py (1654:fc7d0578e124)
1import os
2import ssl
3import subprocess
4
5from conftest import option
6from unit.applications.proto import TestApplicationProto
7
8
9class TestApplicationTLS(TestApplicationProto):
10 def setup_method(self):
1import os
2import ssl
3import subprocess
4
5from conftest import option
6from unit.applications.proto import TestApplicationProto
7
8
9class TestApplicationTLS(TestApplicationProto):
10 def setup_method(self):
11 super().setup_method()
12
13 self.context = ssl.create_default_context()
14 self.context.check_hostname = False
15 self.context.verify_mode = ssl.CERT_NONE
16
17 def certificate(self, name='default', load=True):
18 self.openssl_conf()
19
20 subprocess.call(
21 [
22 'openssl',
23 'req',
24 '-x509',
25 '-new',
26 '-subj', '/CN=' + name + '/',
11 self.context = ssl.create_default_context()
12 self.context.check_hostname = False
13 self.context.verify_mode = ssl.CERT_NONE
14
15 def certificate(self, name='default', load=True):
16 self.openssl_conf()
17
18 subprocess.call(
19 [
20 'openssl',
21 'req',
22 '-x509',
23 '-new',
24 '-subj', '/CN=' + name + '/',
27 '-config', self.temp_dir + '/openssl.conf',
28 '-out', self.temp_dir + '/' + name + '.crt',
29 '-keyout', self.temp_dir + '/' + name + '.key',
25 '-config', option.temp_dir + '/openssl.conf',
26 '-out', option.temp_dir + '/' + name + '.crt',
27 '-keyout', option.temp_dir + '/' + name + '.key',
30 ],
31 stderr=subprocess.STDOUT,
32 )
33
34 if load:
35 self.certificate_load(name)
36
37 def certificate_load(self, crt, key=None):
38 if key is None:
39 key = crt
40
28 ],
29 stderr=subprocess.STDOUT,
30 )
31
32 if load:
33 self.certificate_load(name)
34
35 def certificate_load(self, crt, key=None):
36 if key is None:
37 key = crt
38
41 key_path = self.temp_dir + '/' + key + '.key'
42 crt_path = self.temp_dir + '/' + crt + '.crt'
39 key_path = option.temp_dir + '/' + key + '.key'
40 crt_path = option.temp_dir + '/' + crt + '.crt'
43
44 with open(key_path, 'rb') as k, open(crt_path, 'rb') as c:
45 return self.conf(k.read() + c.read(), '/certificates/' + crt)
46
47 def get_ssl(self, **kwargs):
48 return self.get(wrapper=self.context.wrap_socket, **kwargs)
49
50 def post_ssl(self, **kwargs):

--- 10 unchanged lines hidden (view full) ---

61 ssl_version = ssl.PROTOCOL_TLSv1_2
62
63 else:
64 ssl_version = ssl.PROTOCOL_TLSv1_1
65
66 return ssl.get_server_certificate(addr, ssl_version=ssl_version)
67
68 def openssl_conf(self):
41
42 with open(key_path, 'rb') as k, open(crt_path, 'rb') as c:
43 return self.conf(k.read() + c.read(), '/certificates/' + crt)
44
45 def get_ssl(self, **kwargs):
46 return self.get(wrapper=self.context.wrap_socket, **kwargs)
47
48 def post_ssl(self, **kwargs):

--- 10 unchanged lines hidden (view full) ---

59 ssl_version = ssl.PROTOCOL_TLSv1_2
60
61 else:
62 ssl_version = ssl.PROTOCOL_TLSv1_1
63
64 return ssl.get_server_certificate(addr, ssl_version=ssl_version)
65
66 def openssl_conf(self):
69 conf_path = self.temp_dir + '/openssl.conf'
67 conf_path = option.temp_dir + '/openssl.conf'
70
71 if os.path.exists(conf_path):
72 return
73
74 with open(conf_path, 'w') as f:
75 f.write(
76 """[ req ]
77default_bits = 2048

--- 25 unchanged lines hidden ---
68
69 if os.path.exists(conf_path):
70 return
71
72 with open(conf_path, 'w') as f:
73 f.write(
74 """[ req ]
75default_bits = 2048

--- 25 unchanged lines hidden ---