def test_ports_available(self, mock_getutil): import certbot.plugins.util as plugins_util # Ensure we don't get error with mock.patch("socket.socket.bind"): self.assertFalse(plugins_util.already_listening(80)) self.assertFalse(plugins_util.already_listening(80, True)) self.assertEqual(mock_getutil.call_count, 0)
def test_ports_blocked(self, mock_getutil): sys.modules["psutil"] = None import certbot.plugins.util as plugins_util import socket with mock.patch("socket.socket.bind", side_effect=socket.error): self.assertTrue(plugins_util.already_listening(80)) self.assertTrue(plugins_util.already_listening(80, True)) with mock.patch("socket.socket", side_effect=socket.error): self.assertFalse(plugins_util.already_listening(80)) self.assertEqual(mock_getutil.call_count, 2)
def perform(self, achalls): # pylint: disable=missing-docstring renewer = self.config.verb == "renew" if any( util.already_listening(port, renewer) for port in self._necessary_ports): raise errors.MisconfigurationError( "At least one of the (possibly) required ports is " "already taken.") try: return self.perform2(achalls) except errors.StandaloneBindError as error: display = zope.component.getUtility(interfaces.IDisplay) if error.socket_error.errno == socket.errno.EACCES: display.notification( "Could not bind TCP port {0} because you don't have " "the appropriate permissions (for example, you " "aren't running this program as " "root).".format(error.port)) elif error.socket_error.errno == socket.errno.EADDRINUSE: display.notification( "Could not bind TCP port {0} because it is already in " "use by another process on this system (such as a web " "server). Please stop the program in question and then " "try again.".format(error.port)) else: raise # XXX: How to handle unknown errors in binding?
def perform(self, achalls): # pylint: disable=missing-docstring renewer = self.config.verb == "renew" if any(util.already_listening(port, renewer) for port in self._necessary_ports): raise errors.MisconfigurationError( "At least one of the (possibly) required ports is " "already taken.") try: return self.perform2(achalls) except errors.StandaloneBindError as error: display = zope.component.getUtility(interfaces.IDisplay) if error.socket_error.errno == socket.errno.EACCES: display.notification( "Could not bind TCP port {0} because you don't have " "the appropriate permissions (for example, you " "aren't running this program as " "root).".format(error.port)) elif error.socket_error.errno == socket.errno.EADDRINUSE: display.notification( "Could not bind TCP port {0} because it is already in " "use by another process on this system (such as a web " "server). Please stop the program in question and then " "try again.".format(error.port)) else: raise # XXX: How to handle unknown errors in binding?
def _verify_ports_are_available(self, achalls): """Confirm the ports are available to solve all achalls. :param list achalls: list of :class:`~certbot.achallenges.AnnotatedChallenge` :raises .errors.MisconfigurationError: if required port is unavailable """ ports = [] if any(isinstance(ac.chall, challenges.HTTP01) for ac in achalls): ports.append(self.config.http01_port) if any(isinstance(ac.chall, challenges.TLSSNI01) for ac in achalls): ports.append(self.config.tls_sni_01_port) renewer = (self.config.verb == "renew") if any(util.already_listening(port, renewer) for port in ports): raise errors.MisconfigurationError( "At least one of the required ports is already taken.")
def _call(self, *args, **kwargs): from certbot.plugins.util import already_listening return already_listening(*args, **kwargs)