diff --git a/certbot-apache/certbot_apache/_internal/configurator.py b/certbot-apache/certbot_apache/_internal/configurator.py index 8daa28173..39dd10c79 100644 --- a/certbot-apache/certbot_apache/_internal/configurator.py +++ b/certbot-apache/certbot_apache/_internal/configurator.py @@ -12,6 +12,11 @@ import pkg_resources import six import zope.component import zope.interface +try: + import apacheconfig + HAS_APACHECONFIG = True +except ImportError: # pragma: no cover + HAS_APACHECONFIG = False from acme import challenges from acme.magic_typing import DefaultDict @@ -261,7 +266,7 @@ class ApacheConfigurator(common.Installer): pn_meta = {"augeasparser": self.parser, "augeaspath": self.parser.get_root_augpath(), "ac_ast": None} - if self.USE_PARSERNODE: + if self.USE_PARSERNODE and HAS_APACHECONFIG: self.parser_root = self.get_parsernode_root(pn_meta) self.parsed_paths = self.parser_root.parsed_paths() @@ -369,6 +374,11 @@ class ApacheConfigurator(common.Installer): apache_vars["modules"] = apache_util.parse_modules(self.option("ctl")) metadata["apache_vars"] = apache_vars + with open(self.parser.loc["root"]) as f: + with apacheconfig.make_loader(writable=True, + **apacheconfig.flavors.NATIVE_APACHE) as loader: + metadata["ac_ast"] = loader.loads(f.read()) + return dualparser.DualBlockNode( name=assertions.PASS, ancestor=None, @@ -907,7 +917,7 @@ class ApacheConfigurator(common.Installer): """ v1_vhosts = self.get_virtual_hosts_v1() - if self.USE_PARSERNODE: + if self.USE_PARSERNODE and HAS_APACHECONFIG: v2_vhosts = self.get_virtual_hosts_v2() for v1_vh in v1_vhosts: diff --git a/certbot-apache/tests/augeasnode_test.py b/certbot-apache/tests/augeasnode_test.py index 9d663a05f..cca177b84 100644 --- a/certbot-apache/tests/augeasnode_test.py +++ b/certbot-apache/tests/augeasnode_test.py @@ -1,8 +1,15 @@ """Tests for AugeasParserNode classes""" import mock +import unittest import util +try: + import apacheconfig # pylint: disable=import-error,unused-import + HAS_APACHECONFIG = True +except ImportError: # pragma: no cover + HAS_APACHECONFIG = False + from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module from certbot import errors @@ -10,6 +17,7 @@ from certbot_apache._internal import assertions +@unittest.skipIf(not HAS_APACHECONFIG, reason='Tests require apacheconfig dependency') class AugeasParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-methods """Test AugeasParserNode using available test configurations""" diff --git a/certbot-apache/tests/parsernode_configurator_test.py b/certbot-apache/tests/parsernode_configurator_test.py index 67d65995a..afaaa6e43 100644 --- a/certbot-apache/tests/parsernode_configurator_test.py +++ b/certbot-apache/tests/parsernode_configurator_test.py @@ -5,7 +5,14 @@ import mock import util +try: + import apacheconfig + HAS_APACHECONFIG = True +except ImportError: # pragma: no cover + HAS_APACHECONFIG = False + +@unittest.skipIf(not HAS_APACHECONFIG, reason='Tests require apacheconfig dependency') class ConfiguratorParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-methods """Test AugeasParserNode using available test configurations"""