def test_insecure(self, mock_TendrilPartial, mock_SafeConfigParser, mock_expanduser): result = util.cert_wrapper(None, 'test', secure=False) self.assertEqual(None, result) self.assertFalse(mock_expanduser.called) self.assertFalse(mock_SafeConfigParser.called) self.assertFalse(mock_TendrilPartial.called)
def test_alt_profile(self, mock_TendrilPartial, mock_SafeConfigParser, mock_expanduser): cp = mock_SafeConfigParser.return_value result = util.cert_wrapper('alt_conf[alt_profile]', 'test') self.assertEqual(result, 'wrapper') mock_expanduser.assert_called_once_with('alt_conf') mock_SafeConfigParser.assert_called_once_with() cp.read.assert_called_once_with('/home/dir/.heyu.cert') cp.items.assert_called_once_with('alt_profile') mock_TendrilPartial.assert_called_once_with( ssl.wrap_socket, keyfile='key', certfile='cert', ca_certs='ca', server_side=False, cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1)
def __init__(self, hub, cert_conf=None, secure=True, app_name=None, app_id=None): """ Initialize a ``NotificationServer`` object. :param hub: The address of the hub, as a tuple of hostname and port. :param cert_conf: The path to the certificate configuration file. Optional. :param secure: If ``False``, SSL will not be used. Defaults to ``True``. :param app_name: The name of the application. If not specified, the name is derived from ``sys.argv[0]``. :param app_id: A UUID for notifications generated internal to the notifier. If not specified, a random UUID will be generated. """ # Handle the arguments self._hub = hub self._manager = tendril.get_manager('tcp', util.outgoing_endpoint(hub)) self._wrapper = util.cert_wrapper(cert_conf, 'notifier', secure=secure) # Save the app name and ID self._app_name = app_name or os.path.basename(sys.argv[0]) self._app_id = app_id or str(uuid.uuid4()) # Track running status and the queue of notifications self._hub_app = None self._notifications = [] self._notify_event = gevent.event.Event() # Set up behavior on signals gevent.signal(signal.SIGINT, self.stop) gevent.signal(signal.SIGTERM, self.stop) try: # pragma: no cover # Force an immediate shutdown gevent.signal(signal.SIGUSR1, self.shutdown) except Exception: # pragma: no cover # Ignore errors; SIGUSR1 isn't everywhere pass
def send_notification(hub, app_name, summary, body, urgency=None, category=None, id=None, cert_conf=None, secure=True): """ Sends a notification via the configured HeyU hub. The hub address is read from the "~/.heyu.hub" file, which should contain either "hostname" or "hostname:port". If the file doesn't exist, and "--host" is not given, "localhost" will be tried. Prints out the notification ID if the notification is accepted. Note that certificate configuration is specified in "~/.heyu.cert" by default. :param hub: The address of the hub, as a tuple of hostname and port. :param app_name: The name of the application the notification is for. :param summary: A summary of the notification. :param body: The body of the notification. :param urgency: The urgency level for the notification. Optional. :param category: A category for the notification. Optional. :param id: The ID of a notification to replace. Optional. :param cert_conf: The path to the certificate configuration file. Optional. :param secure: If ``False``, SSL will not be used. Defaults to ``True``. """ # Look up the manager manager = tendril.get_manager('tcp', util.outgoing_endpoint(hub)) manager.start() # Connect to the hub app = tendril.TendrilPartial(SubmitterApplication, app_name, summary, body, urgency, category, id) wrapper = util.cert_wrapper(cert_conf, 'submitter', secure=secure) manager.connect(hub, app, wrapper) # Wait for the submitter to exit gevent.wait()
def start(self, cert_conf=None, secure=True): """ Start the server. This ensures that the hub can receive connections on the declared endpoints. :param cert_conf: The path to the certificate configuration file. Optional. :param secure: If ``False``, SSL will not be used. Defaults to ``True``. """ # Don't allow redundant start if self._running: raise ValueError('server is already running') # Get the wrapper wrapper = util.cert_wrapper(cert_conf, 'hub', secure=secure) # Walk through all managers and start them for manager in self._listeners.values(): manager.start(self._acceptor, wrapper) self._running = True