Skip to content

Commit e16ace1

Browse files
authored
KIKIMR-18071 support TLS over GRPC in ydb-dstool (#4565)
1 parent d7b2c12 commit e16ace1

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

ydb/apps/dstool/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ user@host:~/github$ git clone https://github.com/ydb-platform/ydb.git
2727

2828
Follow the steps described at https://grpc.io/docs/languages/python/quickstart.
2929

30+
Typical command to install the `grpc_tools` package:
31+
32+
```bash
33+
pip3 install grpcio-tools 'protobuf<5.0.0,>=3.13.0'
34+
```
35+
3036
## Compile proto files for python
3137

3238
```bash

ydb/apps/dstool/lib/common.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self):
5757
self.quiet = None
5858
self.http_timeout = None
5959
self.cafile = None
60+
self.cadata = None
6061
self.insecure = None
6162
self.http = None
6263

@@ -73,6 +74,14 @@ def get_protocol_host_port(self, endpoint):
7374
else:
7475
return protocol, endpoint, self.mon_port
7576

77+
def get_cafile_data(self):
78+
if self.cafile is None:
79+
return None
80+
if self.cadata is None:
81+
with open(self.cafile, 'rb') as f:
82+
self.cadata = f.read()
83+
return self.cadata
84+
7685
def get_netloc(self, host, port):
7786
netloc = '%s:%d' % (host, port)
7887
if netloc in name_cache:
@@ -317,10 +326,12 @@ def invoke_grpc(func, *params, explicit_host=None, host=None):
317326
options = [
318327
('grpc.max_receive_message_length', 256 << 20), # 256 MiB
319328
]
320-
with grpc.insecure_channel('%s:%d' % (host, connection_params.grpc_port), options) as channel:
321-
if connection_params.verbose:
322-
p = ', '.join('<<< %s >>>' % text_format.MessageToString(param, as_one_line=True) for param in params)
323-
print('INFO: issuing %s(%s) @%s:%d' % (func, p, host, connection_params.grpc_port), file=sys.stderr)
329+
if connection_params.verbose:
330+
p = ', '.join('<<< %s >>>' % text_format.MessageToString(param, as_one_line=True) for param in params)
331+
print('INFO: issuing %s(%s) @%s:%d protocol %s' % (func, p, host, connection_params.grpc_port,
332+
connection_params.mon_protocol), file=sys.stderr)
333+
334+
def work(channel):
324335
try:
325336
stub = kikimr_grpc.TGRpcServerStub(channel)
326337
res = getattr(stub, func)(*params)
@@ -332,6 +343,16 @@ def invoke_grpc(func, *params, explicit_host=None, host=None):
332343
print('ERROR: exception %s' % e, file=sys.stderr)
333344
raise ConnectionError("Can't connect to specified addresses by gRPC protocol")
334345

346+
hostport = '%s:%d' % (host, connection_params.grpc_port)
347+
retval = None
348+
if connection_params.mon_protocol == 'grpcs':
349+
creds = grpc.ssl_channel_credentials(connection_params.get_cafile_data())
350+
with grpc.secure_channel(hostport, creds, options) as channel:
351+
retval = work(channel)
352+
else:
353+
with grpc.insecure_channel(hostport, options) as channel:
354+
retval = work(channel)
355+
return retval
335356

336357
def invoke_bsc_request(request):
337358
if connection_params.http:

0 commit comments

Comments
 (0)