2013-09-14 11:08:48 -04:00
|
|
|
# Copyright 2013 IBM Corporation
|
|
|
|
# ALl rights reserved
|
|
|
|
|
|
|
|
# This is the socket api layer.
|
|
|
|
# It implement unix and tls sockets
|
2013-10-10 14:17:08 -04:00
|
|
|
#
|
2013-09-14 11:08:48 -04:00
|
|
|
# TODO: SO_PEERCRED for unix socket
|
|
|
|
import confluent.console as console
|
|
|
|
import confluent.config as config
|
|
|
|
import eventlet.green.socket as socket
|
|
|
|
import eventlet.green.ssl as ssl
|
2013-09-14 20:21:58 -04:00
|
|
|
import eventlet
|
2013-10-14 09:21:55 -04:00
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
|
|
|
|
SO_PEERCRED = 17
|
2013-09-14 11:08:48 -04:00
|
|
|
|
|
|
|
def sessionhdl(connection):
|
|
|
|
#TODO: authenticate and authorize peer
|
|
|
|
# For now, trying to test the console stuff, so let's just do n1.
|
|
|
|
cfm = config.ConfigManager(tenant=0)
|
|
|
|
consession = console.ConsoleSession(node='n1', configmanager=cfm,
|
2013-10-14 09:21:55 -04:00
|
|
|
datacallback=connection.sendall)
|
2013-09-14 11:08:48 -04:00
|
|
|
while (1):
|
2013-10-14 09:21:55 -04:00
|
|
|
data = connection.recv(4096)
|
2013-10-09 20:14:34 -04:00
|
|
|
if not data:
|
2013-10-10 14:17:08 -04:00
|
|
|
consession.destroy()
|
2013-10-09 20:14:34 -04:00
|
|
|
return
|
2013-09-14 11:08:48 -04:00
|
|
|
consession.write(data)
|
|
|
|
|
|
|
|
|
2013-10-14 09:21:55 -04:00
|
|
|
def _tlshandler():
|
2013-09-14 11:08:48 -04:00
|
|
|
plainsocket = socket.socket()
|
2013-10-09 20:14:34 -04:00
|
|
|
plainsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2013-09-14 11:08:48 -04:00
|
|
|
srv = ssl.wrap_socket(plainsocket, keyfile="/etc/confluent/privkey.pem",
|
2013-09-14 20:21:58 -04:00
|
|
|
certfile="/etc/confluent/srvcert.pem",
|
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1,
|
|
|
|
server_side=True)
|
2013-09-14 11:08:48 -04:00
|
|
|
srv.bind(('0.0.0.0', 4001))
|
|
|
|
srv.listen(5)
|
|
|
|
while (1): # TODO: exithook
|
|
|
|
cnn, addr = srv.accept()
|
|
|
|
eventlet.spawn_n(sessionhdl, cnn)
|
|
|
|
|
2013-10-14 09:21:55 -04:00
|
|
|
|
|
|
|
def _unixdomainhandler():
|
|
|
|
unixsocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
try:
|
|
|
|
os.remove("/var/run/confluent/api.sock")
|
|
|
|
except OSError: # if file does not exist, no big deal
|
|
|
|
pass
|
|
|
|
unixsocket.bind("/var/run/confluent/api.sock")
|
|
|
|
unixsocket.listen(5)
|
|
|
|
while (1):
|
|
|
|
cnn, addr = unixsocket.accept()
|
|
|
|
creds = cnn.getsockopt(socket.SOL_SOCKET, SO_PEERCRED,
|
|
|
|
struct.calcsize('3i'))
|
|
|
|
print struct.unpack('3i',creds)
|
|
|
|
eventlet.spawn_n(sessionhdl, cnn)
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-14 11:08:48 -04:00
|
|
|
class SockApi(object):
|
|
|
|
def start(self):
|
2013-10-14 09:21:55 -04:00
|
|
|
self.tlsserver = eventlet.spawn(_tlshandler)
|
|
|
|
self.unixdomainserver = eventlet.spawn(_unixdomainhandler)
|