2
0

pypubsub.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env python3
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. """ This is the ASF's simplified publisher/subscriber service """
  19. import asyncio
  20. import aiohttp.web
  21. import time
  22. import json
  23. import yaml
  24. import netaddr
  25. import binascii
  26. import base64
  27. # Some consts
  28. PUBSUB_VERSION = '0.3.0'
  29. PUBSUB_BAD_REQUEST = "I could not understand your request, sorry! Please see https://pubsub.apache.org/api.html \
  30. for usage documentation.\n"
  31. PUBSUB_PAYLOAD_RECEIVED = "Payload received, thank you very much!\n"
  32. PUBSUB_NOT_ALLOWED = "You are not authorized to deliver payloads!\n"
  33. PUBSUB_BAD_PAYLOAD = "Bad payload type. Payloads must be JSON dictionary objects, {..}!\n"
  34. CONF = None
  35. ACL = None
  36. OLD_SCHOOLERS = ['svnwcsub', ] # Old-school clients that use \0 terminators.
  37. # Internal score-keeping vars
  38. NUM_REQUESTS = 0 # Number of total requests served
  39. SUBSCRIBERS = [] # Current subscribers to everything
  40. PENDING_PUBS = [] # Payloads pending publication
  41. LAST_PING = 0 # Last time we did a global ping (every 5 seconds)
  42. PAYLOADERS = [] # IPs that can deliver payloads
  43. class Subscriber:
  44. """ Basic subscriber (client) class.
  45. Holds information about the connection and ACL
  46. """
  47. def __init__(self, connection, request):
  48. self.connection = connection
  49. self.request = request
  50. self.acl = {}
  51. # Set topics subscribed to
  52. self.topics = [x for x in request.path.split('/') if x]
  53. # Is the client old and expecting zero-terminators?
  54. self.old_school = False
  55. for ua in OLD_SCHOOLERS:
  56. if ua in request.headers.get('User-Agent', ''):
  57. self.old_school = True
  58. break
  59. # Is there a basic auth in this request? If so, set up ACL
  60. auth = request.headers.get('Authorization')
  61. if auth:
  62. self.set_acl(auth)
  63. def set_acl(self, basic):
  64. """ Sets the ACL if possible, based on Basic Auth """
  65. try:
  66. decoded = str(base64.decodebytes(bytes(basic.replace('Basic ', ''), 'utf-8')), 'utf-8')
  67. u, p = decoded.split(':', 1)
  68. if u in ACL:
  69. acl_pass = ACL[u].get('password')
  70. if acl_pass and acl_pass == p:
  71. self.acl = ACL[u].get('acl', {})
  72. # Vet ACL for user
  73. if not isinstance(self.acl, dict):
  74. raise AssertionError(f"ACL for user {u} must be a dictionary of sub-IDs and topics, but is not.")
  75. # Make sure each ACL segment is a list of topics
  76. for k, v in self.acl.items():
  77. if not isinstance(v, list):
  78. raise AssertionError(f"ACL segment {k} for user {u} is not a list of topics!")
  79. print(f"Client {u} successfully authenticated (and ACL is valid).")
  80. except binascii.Error as e:
  81. self.acl = {}
  82. except AssertionError as e:
  83. print(e)
  84. print(f"ACL configuration error: ACL scheme for {u} contains errors, setting ACL to nothing.")
  85. self.acl = {}
  86. except Exception as e:
  87. print(f"Basic unknown exception occurred: {e}")
  88. async def ping(self):
  89. """ Generic ping-back to the client """
  90. js = b"%s\n" % json.dumps({"stillalive": time.time()}).encode('utf-8')
  91. if self.old_school:
  92. js += b"\0"
  93. await self.connection.write(js)
  94. class Payload:
  95. """ A payload (event) object sent by a registered publisher. """
  96. def __init__(self, path, data):
  97. self.json = data
  98. self.topics = [x for x in path.split('/') if x]
  99. self.path = path
  100. self.private = False
  101. # Private payload?
  102. if self.topics[0] == 'private':
  103. self.private = True
  104. self.topics = self.topics[1:] # Remove the private bit from topics now.
  105. self.json['pubsub_topics'] = self.topics
  106. self.json['pubsub_path'] = self.path
  107. async def publish(self, subscribers):
  108. """ Publishes an object to all subscribers using those topics (or a sub-set thereof) """
  109. js = b"%s\n" % json.dumps(self.json).encode('utf-8')
  110. ojs = js + b"\0"
  111. for sub in subscribers:
  112. # If a private payload, check ACL and bail if not a match
  113. if self.private:
  114. can_see = False
  115. for key, private_topics in sub.acl.items():
  116. if all(el in self.topics for el in private_topics):
  117. can_see = True
  118. break
  119. if not can_see:
  120. continue
  121. # If subscribed to all the topics, tell a subscriber about this
  122. if all(el in self.topics for el in sub.topics):
  123. try:
  124. if sub.old_school:
  125. await sub.connection.write(ojs)
  126. else:
  127. await sub.connection.write(js)
  128. except ConnectionResetError:
  129. pass
  130. except RuntimeError:
  131. pass
  132. except AssertionError: # drain helper throws these sometimes
  133. pass
  134. async def poll():
  135. """ Polls for new stuff to publish, and if found, publishes to whomever wants it. """
  136. global LAST_PING, PENDING_PUBS
  137. while True:
  138. for payload in PENDING_PUBS:
  139. await payload.publish(SUBSCRIBERS)
  140. PENDING_PUBS = []
  141. await asyncio.sleep(0.5)
  142. async def handler(request):
  143. """ Generic handler for all incoming HTTP requests """
  144. global NUM_REQUESTS
  145. NUM_REQUESTS += 1
  146. # Define response headers first...
  147. headers = {
  148. 'Server': 'PyPubSub/%s' % PUBSUB_VERSION,
  149. 'X-Subscribers': str(len(SUBSCRIBERS)),
  150. 'X-Requests': str(NUM_REQUESTS),
  151. }
  152. # Are we handling a publisher payload request? (PUT/POST)
  153. if request.method in ['PUT', 'POST']:
  154. ip = netaddr.IPAddress(request.remote)
  155. allowed = False
  156. for network in PAYLOADERS:
  157. if ip in network:
  158. allowed = True
  159. break
  160. if not allowed:
  161. resp = aiohttp.web.Response(headers=headers, status=403, text=PUBSUB_NOT_ALLOWED)
  162. return resp
  163. if request.can_read_body:
  164. try:
  165. body = await request.json()
  166. assert isinstance(body, dict) # Payload MUST be an dictionary object, {...}
  167. PENDING_PUBS.append(Payload(request.path, body))
  168. resp = aiohttp.web.Response(headers=headers, status=202, text=PUBSUB_PAYLOAD_RECEIVED)
  169. return resp
  170. except json.decoder.JSONDecodeError:
  171. resp = aiohttp.web.Response(headers=headers, status=400, text=PUBSUB_BAD_REQUEST)
  172. return resp
  173. except AssertionError:
  174. resp = aiohttp.web.Response(headers=headers, status=400, text=PUBSUB_BAD_PAYLOAD)
  175. return resp
  176. # Is this a subscriber request? (GET)
  177. elif request.method == 'GET':
  178. resp = aiohttp.web.StreamResponse(headers=headers)
  179. # We do not support HTTP 1.0 here...
  180. if request.version.major == 1 and request.version.minor == 0:
  181. return resp
  182. subscriber = Subscriber(resp, request)
  183. SUBSCRIBERS.append(subscriber)
  184. # We'll change the content type once we're ready
  185. # resp.content_type = 'application/vnd.apache-pubsub-stream'
  186. resp.content_type = 'application/json'
  187. try:
  188. resp.enable_chunked_encoding()
  189. await resp.prepare(request)
  190. while True:
  191. await subscriber.ping()
  192. await asyncio.sleep(5)
  193. # We may get exception types we don't have imported, so grab ANY exception and kick out the subscriber
  194. except:
  195. pass
  196. SUBSCRIBERS.remove(subscriber)
  197. return resp
  198. elif request.method == 'HEAD':
  199. resp = aiohttp.web.Response(headers=headers, status=204, text="")
  200. return resp
  201. # I don't know this type of request :/ (DELETE, PATCH, etc)
  202. else:
  203. resp = aiohttp.web.Response(headers=headers, status=400, text=PUBSUB_BAD_REQUEST)
  204. return resp
  205. async def main():
  206. """ Main loop... """
  207. server = aiohttp.web.Server(handler)
  208. runner = aiohttp.web.ServerRunner(server)
  209. await runner.setup()
  210. site = aiohttp.web.TCPSite(runner, CONF['server']['bind'], CONF['server']['port'])
  211. await site.start()
  212. print("==== PyPubSub v/%s starting... ====" % PUBSUB_VERSION)
  213. print("==== Serving up PubSub goodness at %s:%s ====" % (CONF['server']['bind'], CONF['server']['port']))
  214. await poll()
  215. if __name__ == '__main__':
  216. CONF = yaml.safe_load(open('pypubsub.yaml'))
  217. ACL = {}
  218. try:
  219. ACL = yaml.safe_load(open('pypubsub_acl.yaml'))
  220. except FileNotFoundError:
  221. print("No ACL configuration file found, private events will not be broadcast.")
  222. PAYLOADERS = [netaddr.IPNetwork(x) for x in CONF['clients']['payloaders']]
  223. loop = asyncio.get_event_loop()
  224. try:
  225. loop.run_until_complete(main())
  226. except KeyboardInterrupt:
  227. pass
  228. loop.close()