Skip to content

Commit 504d9fd

Browse files
author
Sam Clements
committed
Added a TLSTransport (closes #3)
* Added --ca-certs to command line client * Fixed .format() bug in command.main on Python 2.6
1 parent 735ebc8 commit 504d9fd

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

riemann_client/command.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,28 @@
1010
import riemann_client.client
1111
import riemann_client.transport
1212

13-
TRANSPORT_CLASSES = {
14-
'udp': riemann_client.transport.UDPTransport,
15-
'tcp': riemann_client.transport.TCPTransport
13+
__all__ = ['main']
14+
15+
16+
def udp_transport_factory(args):
17+
return riemann_client.transport.UDPTransport(args.host, args.port)
18+
19+
20+
def tcp_transport_factory(args):
21+
return riemann_client.transport.TCPTransport(args.host, args.port)
22+
23+
24+
def tls_transport_factory(args):
25+
if args.ca_certs is None:
26+
parser.error('--ca-certs must be set when using the TLS transport')
27+
return riemann_client.transport.TLSTransport(
28+
args.host, args.port, args.ca_certs)
29+
30+
31+
TRANSPORT_FACTORIES = {
32+
'udp': udp_transport_factory,
33+
'tcp': tcp_transport_factory,
34+
'tls': tls_transport_factory
1635
}
1736

1837
parser = argparse.ArgumentParser(add_help=False, description=(
@@ -42,7 +61,11 @@
4261
help="The port to connect to the Riemann server on (environ: %(default)s)")
4362

4463
parser.add_argument(
45-
'-t', '--transport', choices=TRANSPORT_CLASSES.keys(), default='tcp',
64+
'-c', '--ca-certs', type=str, metavar='CERT',
65+
help="The path to the CA certificate bundle to use with the TLS transport")
66+
67+
parser.add_argument(
68+
'-t', '--transport', choices=TRANSPORT_FACTORIES.keys(), default='tcp',
4669
help="The transport to use (default: %(default)s)")
4770

4871
subparsers = parser.add_subparsers(dest='subparser')
@@ -86,9 +109,7 @@ def send_function(args, client):
86109

87110

88111
def query_function(args, client):
89-
"""Queries the Riemann index and outputs the returned events as JSON
90-
91-
UDP is not supported when querying"""
112+
"""Queries the Riemann index and outputs the returned events as JSON"""
92113
print(json.dumps(client.query(args.query), sort_keys=True, indent=2))
93114

94115
query = subparsers.add_parser('query', help='Query the Riemann index')
@@ -99,13 +120,12 @@ def query_function(args, client):
99120
def main():
100121
args = parser.parse_args()
101122

102-
transport_class = TRANSPORT_CLASSES[args.transport]
103-
transport = transport_class(args.host, args.port)
123+
transport = TRANSPORT_FACTORIES[args.transport](args)
104124

105125
with riemann_client.client.Client(transport=transport) as client:
106126
try:
107127
args.function(args, client)
108128
except riemann_client.transport.RiemannError as exception:
109-
print("The Riemann server responded with an error: {}".format(
129+
print("The Riemann server responded with an error: {0}".format(
110130
exception.message), file=sys.stderr)
111131
exit(1)

riemann_client/transport.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import abc
66
import socket
7+
import ssl
78
import struct
89

910
import riemann_client.riemann_pb2
@@ -85,3 +86,17 @@ def send(self, message):
8586
raise RiemannError(response.error)
8687

8788
return response
89+
90+
91+
class TLSTransport(TCPTransport):
92+
def __init__(self, host='localhost', port=5554, ca_certs=None):
93+
super(TLSTransport, self).__init__(host, port)
94+
self.ca_certs = ca_certs
95+
96+
def connect(self):
97+
super(TLSTransport, self).connect()
98+
self.socket = ssl.wrap_socket(
99+
self.socket,
100+
ssl_version=ssl.PROTOCOL_TLSv1,
101+
cert_reqs=ssl.CERT_REQUIRED,
102+
ca_certs=self.ca_certs)

0 commit comments

Comments
 (0)