diff --git a/certbot-apache/certbot_apache/augeasparser.py b/certbot-apache/certbot_apache/augeasparser.py index 1bbf1ed77..0c582275c 100644 --- a/certbot-apache/certbot_apache/augeasparser.py +++ b/certbot-apache/certbot_apache/augeasparser.py @@ -152,12 +152,21 @@ class AugeasBlockNode(AugeasDirectiveNode): return nodes def find_comments(self, comment, exact=False): # pylint: disable=unused-argument - """Recursive search of DirectiveNodes from the sequence of children""" - new_metadata = {"augeasparser": self.parser} - return [AugeasCommentNode(comment=assertions.PASS, - ancestor=self, - filepath=assertions.PASS, - metadata=new_metadata)] + """ + Recursive search of DirectiveNodes from the sequence of children. + + Note that the argument exact is not implemented here, as it's not + currently in use in ApacheConfigurator. + """ + + nodes = list() + ownpath = self.metadata.get("augeaspath") + + comments = self.parser.find_comments(comment, start=ownpath) + for com in comments: + nodes.append(self._create_commentnode(com)) + + return nodes def delete_child(self, child): # pragma: no cover """Deletes a ParserNode from the sequence of children""" @@ -167,6 +176,19 @@ class AugeasBlockNode(AugeasDirectiveNode): """Returns a list of unsaved filepaths""" return [assertions.PASS] + def _create_commentnode(self, path): + """Helper function to create a CommentNode from Augeas path""" + + comment = self.parser.aug.get(path) + metadata = {"augeasparser": self.parser, "augeaspath": path} + + # Because of the dynamic nature, and the fact that we're not populating + # the complete ParserNode tree, we use the search parent as ancestor + return AugeasCommentNode(comment=comment, + ancestor=self, + filepath=apache_util.get_file_path(path), + metadata=metadata) + def _create_directivenode(self, path): """Helper function to create a DirectiveNode from Augeas path""" diff --git a/certbot-apache/certbot_apache/dualparser.py b/certbot-apache/certbot_apache/dualparser.py index d56e07de2..d9904daf1 100644 --- a/certbot-apache/certbot_apache/dualparser.py +++ b/certbot-apache/certbot_apache/dualparser.py @@ -245,7 +245,7 @@ class DualBlockNode(DualNodeBase): if pass_primary and pass_secondary: # Both unimplemented new_nodes.append(nodeclass(primary=primary_res[0], - secondary=secondary_res[0])) + secondary=secondary_res[0])) # pragma: no cover elif pass_primary: for c in secondary_res: new_nodes.append(nodeclass(primary=primary_res[0], diff --git a/certbot-apache/certbot_apache/tests/augeasnode_test.py b/certbot-apache/certbot_apache/tests/augeasnode_test.py index c4631c57c..acb2d5329 100644 --- a/certbot-apache/certbot_apache/tests/augeasnode_test.py +++ b/certbot-apache/certbot_apache/tests/augeasnode_test.py @@ -63,3 +63,12 @@ class AugeasParserNodeTest(util.ApacheTest): self.assertEqual(servername[0].parameters[0], "certbot.demo") found = True self.assertTrue(found) + + def test_find_comments(self): + rootcomment = self.config.parser_root.find_comments( + "This is the main Apache server configuration file. " + ) + self.assertEqual(len(rootcomment), 1) + self.assertTrue(rootcomment[0].filepath.endswith( + "debian_apache_2_4/multiple_vhosts/apache2/apache2.conf" + )) diff --git a/certbot-apache/certbot_apache/tests/dualnode_test.py b/certbot-apache/certbot_apache/tests/dualnode_test.py index b37a7fdf9..3101a9c6f 100644 --- a/certbot-apache/certbot_apache/tests/dualnode_test.py +++ b/certbot-apache/certbot_apache/tests/dualnode_test.py @@ -161,6 +161,17 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public- self.block.primary) def test_find_comments(self): + pri_comments = [augeasparser.AugeasCommentNode(comment="some comment", + ancestor=self.block, + filepath="/path/to/whatever")] + sec_comments = [augeasparser.AugeasCommentNode(comment=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS)] + find_coms_primary = mock.MagicMock(return_value=pri_comments) + find_coms_secondary = mock.MagicMock(return_value=sec_comments) + self.block.primary.find_comments = find_coms_primary + self.block.secondary.find_comments = find_coms_secondary + dcoms = self.block.find_comments("comment") p_dcoms = [d.primary for d in dcoms] s_dcoms = [d.secondary for d in dcoms]