Пример #1
0
def run_script(params):
    """Run the script with the given params.

    :param list params: List of parameters to pass to Popen

    """
    try:
        proc = subprocess.Popen(params,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)

    except (OSError, ValueError):
        msg = "Unable to run the command: %s" % " ".join(params)
        logger.error(msg)
        raise errors.SubprocessError(msg)

    stdout, stderr = proc.communicate()

    if proc.returncode != 0:
        msg = "Error while running %s.\n%s\n%s" % (" ".join(params), stdout,
                                                   stderr)
        # Enter recovery routine...
        logger.error(msg)
        raise errors.SubprocessError(msg)

    return stdout, stderr
Пример #2
0
def run_script(params, log=logger.error):
    """Run the script with the given params.

    :param list params: List of parameters to pass to Popen
    :param callable log: Logger method to use for errors

    """
    try:
        proc = subprocess.Popen(params,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                universal_newlines=True,
                                env=env_no_snap_for_external_calls())

    except (OSError, ValueError):
        msg = "Unable to run the command: %s" % " ".join(params)
        log(msg)
        raise errors.SubprocessError(msg)

    stdout, stderr = proc.communicate()

    if proc.returncode != 0:
        msg = "Error while running %s.\n%s\n%s" % (" ".join(params), stdout,
                                                   stderr)
        # Enter recovery routine...
        log(msg)
        raise errors.SubprocessError(msg)

    return stdout, stderr
Пример #3
0
def run_script(params: List[str],
               log: Callable[[str], None] = logger.error) -> Tuple[str, str]:
    """Run the script with the given params.

    :param list params: List of parameters to pass to subprocess.run
    :param callable log: Logger method to use for errors

    """
    try:
        proc = subprocess.run(params,
                              check=False,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              universal_newlines=True,
                              env=env_no_snap_for_external_calls())

    except (OSError, ValueError):
        msg = "Unable to run the command: %s" % " ".join(params)
        log(msg)
        raise errors.SubprocessError(msg)

    if proc.returncode != 0:
        msg = "Error while running %s.\n%s\n%s" % (" ".join(params),
                                                   proc.stdout, proc.stderr)
        # Enter recovery routine...
        log(msg)
        raise errors.SubprocessError(msg)

    return proc.stdout, proc.stderr
Пример #4
0
    def test_ocsp_revoked(self, mock_run, mock_na, mock_determine):
        now = pytz.UTC.fromutc(datetime.utcnow())
        cert_obj = mock.MagicMock()
        cert_obj.cert_path = "x"
        cert_obj.chain_path = "y"
        mock_na.return_value = now + timedelta(hours=2)

        self.checker.broken = True
        mock_determine.return_value = ("", "")
        self.assertIs(self.checker.ocsp_revoked(cert_obj), False)

        self.checker.broken = False
        mock_run.return_value = tuple(openssl_happy[1:])
        self.assertIs(self.checker.ocsp_revoked(cert_obj), False)
        self.assertEqual(mock_run.call_count, 0)

        mock_determine.return_value = ("http://x.co", "x.co")
        self.assertIs(self.checker.ocsp_revoked(cert_obj), False)
        mock_run.side_effect = errors.SubprocessError(
            "Unable to load certificate launcher")
        self.assertIs(self.checker.ocsp_revoked(cert_obj), False)
        self.assertEqual(mock_run.call_count, 2)

        # cert expired
        mock_na.return_value = now
        mock_determine.return_value = ("", "")
        count_before = mock_determine.call_count
        self.assertIs(self.checker.ocsp_revoked(cert_obj), False)
        self.assertEqual(mock_determine.call_count, count_before)
Пример #5
0
    def test_determine_ocsp_server(self, mock_run, mock_info):
        uri = "http://ocsp.stg-int-x1.letsencrypt.org/"
        host = "ocsp.stg-int-x1.letsencrypt.org"
        mock_run.return_value = uri, ""
        self.assertEqual(self.checker.determine_ocsp_server("beep"),
                         (uri, host))
        mock_run.return_value = "ftp:/" + host + "/", ""
        self.assertEqual(self.checker.determine_ocsp_server("beep"),
                         (None, None))
        self.assertEqual(mock_info.call_count, 1)

        c = "confusion"
        mock_run.side_effect = errors.SubprocessError(c)
        self.assertEqual(self.checker.determine_ocsp_server("beep"),
                         (None, None))
Пример #6
0
    def test_ocsp_revoked(self, mock_run, mock_determine):
        self.checker.broken = True
        mock_determine.return_value = ("", "")
        self.assertEqual(self.checker.ocsp_revoked("x", "y"), False)

        self.checker.broken = False
        mock_run.return_value = tuple(openssl_happy[1:])
        self.assertEqual(self.checker.ocsp_revoked("x", "y"), False)
        self.assertEqual(mock_run.call_count, 0)

        mock_determine.return_value = ("http://x.co", "x.co")
        self.assertEqual(self.checker.ocsp_revoked("blah.pem", "chain.pem"), False)
        mock_run.side_effect = errors.SubprocessError("Unable to load certificate launcher")
        self.assertEqual(self.checker.ocsp_revoked("x", "y"), False)
        self.assertEqual(mock_run.call_count, 2)
Пример #7
0
    def test_get_version(self, mock_script):
        mock_script.return_value = ("Server Version: Apache/2.4.2 (Debian)",
                                    "")
        self.assertEqual(self.config.get_version(), (2, 4, 2))

        mock_script.return_value = ("Server Version: Apache/2 (Linux)", "")
        self.assertEqual(self.config.get_version(), (2, ))

        mock_script.return_value = ("Server Version: Apache (Debian)", "")
        self.assertRaises(errors.PluginError, self.config.get_version)

        mock_script.return_value = (
            "Server Version: Apache/2.3{0} Apache/2.4.7".format(os.linesep),
            "")
        self.assertRaises(errors.PluginError, self.config.get_version)

        mock_script.side_effect = errors.SubprocessError("Can't find program")
        self.assertRaises(errors.PluginError, self.config.get_version)