-
Notifications
You must be signed in to change notification settings - Fork 43
/
io.py
323 lines (235 loc) · 9.21 KB
/
io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
from __future__ import absolute_import
import _awscrt
from awscrt import NativeResource, isinstance_str
from enum import IntEnum
import io
import threading
class LogLevel(IntEnum):
NoLogs = 0
Fatal = 1
Error = 2
Warn = 3
Info = 4
Debug = 5
Trace = 6
def init_logging(log_level, file_name):
"""
initialize a logger. log_level is type LogLevel, and file_name is of type str.
To write to stdout, or stderr, simply pass 'stdout' or 'stderr' as strings. Otherwise, a file path is assumed.
"""
assert log_level is not None
assert file_name is not None
_awscrt.init_logging(log_level, file_name)
def is_alpn_available():
return _awscrt.is_alpn_available()
class EventLoopGroup(NativeResource):
"""
Manages a collection of event-loops.
An event-loop is a thread for doing async work, such as I/O.
Classes that need to do async work will ask the EventLoopGroup for an event-loop to use.
"""
__slots__ = ('shutdown_event')
def __init__(self, num_threads=0):
"""
num_threads: Number of event-loops to create. Pass 0 to create one for each processor on the machine.
"""
super(EventLoopGroup, self).__init__()
shutdown_event = threading.Event()
def on_shutdown():
shutdown_event.set()
self.shutdown_event = shutdown_event
self._binding = _awscrt.event_loop_group_new(num_threads, on_shutdown)
class HostResolverBase(NativeResource):
__slots__ = ()
class DefaultHostResolver(HostResolverBase):
__slots__ = ()
def __init__(self, event_loop_group, max_hosts=16):
assert isinstance(event_loop_group, EventLoopGroup)
super(DefaultHostResolver, self).__init__()
self._binding = _awscrt.host_resolver_new_default(max_hosts, event_loop_group)
class ClientBootstrap(NativeResource):
__slots__ = ('shutdown_event')
def __init__(self, event_loop_group, host_resolver):
assert isinstance(event_loop_group, EventLoopGroup)
assert isinstance(host_resolver, HostResolverBase)
super(ClientBootstrap, self).__init__()
shutdown_event = threading.Event()
def on_shutdown():
shutdown_event.set()
self.shutdown_event = shutdown_event
self._binding = _awscrt.client_bootstrap_new(event_loop_group, host_resolver, on_shutdown)
def _read_binary_file(filepath):
with open(filepath, mode='rb') as fh:
contents = fh.read()
return contents
class SocketDomain(IntEnum):
IPv4 = 0
IPv6 = 1
Local = 2
class SocketType(IntEnum):
Stream = 0
DGram = 1
class SocketOptions(object):
__slots__ = (
'domain', 'type', 'connect_timeout_ms', 'keep_alive',
'keep_alive_timeout_secs', 'keep_alive_interval_secs', 'keep_alive_max_probes'
)
def __init__(self):
for slot in self.__slots__:
setattr(self, slot, None)
self.domain = SocketDomain.IPv6
self.type = SocketType.Stream
self.connect_timeout_ms = 5000
self.keep_alive = False
self.keep_alive_interval_secs = 0
self.keep_alive_timeout_secs = 0
self.keep_alive_max_probes = 0
class TlsVersion(IntEnum):
SSLv3 = 0
TLSv1 = 1
TLSv1_1 = 2
TLSv1_2 = 3
TLSv1_3 = 4
DEFAULT = 128
class TlsContextOptions(object):
__slots__ = (
'min_tls_ver', 'ca_dirpath', 'ca_buffer', 'alpn_list',
'certificate_buffer', 'private_key_buffer',
'pkcs12_filepath', 'pkcs12_password', 'verify_peer')
def __init__(self):
for slot in self.__slots__:
setattr(self, slot, None)
self.min_tls_ver = TlsVersion.DEFAULT
self.verify_peer = True
def override_default_trust_store_from_path(self, ca_dirpath, ca_filepath):
assert isinstance_str(ca_dirpath) or ca_dirpath is None
assert isinstance_str(ca_filepath) or ca_filepath is None
if ca_filepath:
ca_buffer = _read_binary_file(ca_filepath)
self.override_default_trust_store(ca_buffer)
self.ca_dirpath = ca_dirpath
def override_default_trust_store(self, rootca_buffer):
assert isinstance(rootca_buffer, bytes)
self.ca_buffer = rootca_buffer
@staticmethod
def create_client_with_mtls_from_path(cert_filepath, pk_filepath):
assert isinstance_str(cert_filepath)
assert isinstance_str(pk_filepath)
cert_buffer = _read_binary_file(cert_filepath)
key_buffer = _read_binary_file(pk_filepath)
return TlsContextOptions.create_client_with_mtls(cert_buffer, key_buffer)
@staticmethod
def create_client_with_mtls(cert_buffer, key_buffer):
assert isinstance(cert_buffer, bytes)
assert isinstance(key_buffer, bytes)
opt = TlsContextOptions()
opt.certificate_buffer = cert_buffer
opt.private_key_buffer = key_buffer
opt.verify_peer = True
return opt
@staticmethod
def create_client_with_mtls_pkcs12(pkcs12_filepath, pkcs12_password):
assert isinstance_str(pkcs12_filepath)
assert isinstance_str(pkcs12_password)
opt = TlsContextOptions()
opt.pkcs12_filepath = pkcs12_filepath
opt.pkcs12_password = pkcs12_password
opt.verify_peer = True
return opt
@staticmethod
def create_server_from_path(cert_filepath, pk_filepath):
assert isinstance_str(cert_filepath)
assert isinstance_str(pk_filepath)
cert_buffer = _read_binary_file(cert_filepath)
key_buffer = _read_binary_file(pk_filepath)
return TlsContextOptions.create_server(cert_buffer, key_buffer)
@staticmethod
def create_server(cert_buffer, key_buffer):
assert isinstance(cert_buffer, bytes)
assert isinstance(key_buffer, bytes)
opt = TlsContextOptions()
opt.certificate_buffer = cert_buffer
opt.private_key_buffer = key_buffer
opt.verify_peer = False
return opt
@staticmethod
def create_server_pkcs12(pkcs12_filepath, pkcs12_password):
assert isinstance_str(pkcs12_filepath)
assert isinstance_str(pkcs12_password)
opt = TlsContextOptions()
opt.pkcs12_filepath = pkcs12_filepath
opt.pkcs12_password = pkcs12_password
opt.verify_peer = False
return opt
def _alpn_list_to_str(alpn_list):
"""
Transform ['h2', 'http/1.1'] -> "h2;http/1.1"
None is returned if list is None or empty
"""
if alpn_list:
assert not isinstance_str(alpn_list)
return ';'.join(alpn_list)
return None
class ClientTlsContext(NativeResource):
__slots__ = ()
def __init__(self, options):
assert isinstance(options, TlsContextOptions)
super(ClientTlsContext, self).__init__()
self._binding = _awscrt.client_tls_ctx_new(
options.min_tls_ver.value,
options.ca_dirpath,
options.ca_buffer,
_alpn_list_to_str(options.alpn_list),
options.certificate_buffer,
options.private_key_buffer,
options.pkcs12_filepath,
options.pkcs12_password,
options.verify_peer
)
def new_connection_options(self):
return TlsConnectionOptions(self)
class TlsConnectionOptions(NativeResource):
__slots__ = ('tls_ctx')
def __init__(self, tls_ctx):
assert isinstance(tls_ctx, ClientTlsContext)
super(TlsConnectionOptions, self).__init__()
self.tls_ctx = tls_ctx
self._binding = _awscrt.tls_connections_options_new_from_ctx(tls_ctx)
def set_alpn_list(self, alpn_list):
_awscrt.tls_connection_options_set_alpn_list(self, _alpn_list_to_str(alpn_list))
def set_server_name(self, server_name):
_awscrt.tls_connection_options_set_server_name(self, server_name)
class InputStream(NativeResource):
"""InputStream allows awscrt native code to read from Python I/O classes"""
__slots__ = ()
# TODO: Implement IOBase interface so Python can read from this class as well.
def __init__(self, stream):
assert isinstance(stream, io.IOBase)
assert not isinstance(stream, InputStream)
super(InputStream, self).__init__()
self._binding = _awscrt.input_stream_new(stream)
@classmethod
def wrap(cls, stream, allow_none=False):
"""
If stream is already an awscrt.io.InputStream, return it.
Otherwise, return an awscrt.io.InputStream which wraps the stream.
"""
if isinstance(stream, InputStream):
return stream
if isinstance(stream, io.IOBase):
return cls(stream)
if stream is None and allow_none:
return None
raise TypeError('I/O stream type expected')