def _create_directivenode(self, path): """Helper function to create a DirectiveNode from Augeas path""" name = self.parser.get_arg(path) metadata = {"augeasparser": self.parser, "augeaspath": path} # Check if the file was included from the root config or initial state enabled = self.parser.parsed_in_original( apache_util.get_file_path(path)) return AugeasDirectiveNode(name=name, ancestor=assertions.PASS, enabled=enabled, filepath=apache_util.get_file_path(path), metadata=metadata)
def add_child_directive( self, name: str, parameters=None, position=None) -> "AugeasDirectiveNode": # pragma: no cover """Adds a new DirectiveNode to the sequence of children""" if not parameters: raise errors.PluginError( "Directive requires parameters and none were set.") insertpath, realpath, before = self._aug_resolve_child_position( "directive", position) new_metadata = {"augeasparser": self.parser, "augeaspath": realpath} # Create the new directive self.parser.aug.insert(insertpath, "directive", before) # Set the directive key self.parser.aug.set(realpath, name) # Check if the file was included from the root config or initial state file_path = apache_util.get_file_path(realpath) if file_path is None: raise errors.Error(f"No file path found for vhost: {realpath}") enabled = self.parser.parsed_in_original(file_path) return AugeasDirectiveNode( name=name, parameters=parameters, enabled=enabled, ancestor=assertions.PASS, filepath=file_path, metadata=new_metadata, )
def add_child_block( self, name: str, parameters: Optional[str] = None, position: Optional[int] = None ) -> "AugeasBlockNode": # pragma: no cover """Adds a new BlockNode to the sequence of children""" insertpath, realpath, before = self._aug_resolve_child_position( name, position) new_metadata: Dict[str, Any] = { "augeasparser": self.parser, "augeaspath": realpath } # Create the new block self.parser.aug.insert(insertpath, name, before) # Check if the file was included from the root config or initial state file_path = apache_util.get_file_path(realpath) if file_path is None: raise errors.Error(f"No file path found for vhost: {realpath}") enabled = self.parser.parsed_in_original(file_path) # Parameters will be set at the initialization of the new object return AugeasBlockNode( name=name, parameters=parameters, enabled=enabled, ancestor=assertions.PASS, filepath=file_path, metadata=new_metadata, )
def _create_blocknode(self, path: str) -> "AugeasBlockNode": """ Helper function to create a BlockNode from Augeas path. This is used by AugeasParserNode.find_ancestors and AugeasBlockNode. and AugeasBlockNode.find_blocks """ name: str = self._aug_get_name(path) metadata: Dict[str, Union[parser.ApacheParser, str]] = { "augeasparser": self.parser, "augeaspath": path } # Check if the file was included from the root config or initial state file_path = apache_util.get_file_path(path) if file_path is None: raise ValueError( f"No file path found for vhost: {path}.") # pragma: no cover enabled = self.parser.parsed_in_original(file_path) return AugeasBlockNode(name=name, enabled=enabled, ancestor=assertions.PASS, filepath=file_path, metadata=metadata)
def _create_blocknode(self, path): """ Helper function to create a BlockNode from Augeas path. This is used by AugeasParserNode.find_ancestors and AugeasBlockNode. and AugeasBlockNode.find_blocks """ name = self._aug_get_name(path) metadata = {"augeasparser": self.parser, "augeaspath": path} # Check if the file was included from the root config or initial state enabled = self.parser.parsed_in_original( apache_util.get_file_path(path)) return AugeasBlockNode(name=name, enabled=enabled, ancestor=assertions.PASS, filepath=apache_util.get_file_path(path), metadata=metadata)
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 of AugeasParser and the fact that we're # not populating the complete node tree, the ancestor has a dummy value return AugeasCommentNode(comment=comment, ancestor=assertions.PASS, filepath=apache_util.get_file_path(path), metadata=metadata)
def test_deploy_cert_newssl(self): self.config = util.get_apache_configurator(self.config_path, self.vhost_path, self.config_dir, self.work_dir, version=(2, 4, 16)) self.config = self.mock_deploy_cert(self.config) self.config.parser.modules["ssl_module"] = None self.config.parser.modules["mod_ssl.c"] = None # Get the default 443 vhost self.config.assoc["random.demo"] = self.vh_truth[1] with certbot_util.patch_display_util(): self.config.deploy_cert("random.demo", "example/cert.pem", "example/key.pem", "example/cert_chain.pem", "example/fullchain.pem") self.config.save() # Verify ssl_module was enabled. self.assertTrue(self.vh_truth[1].enabled) self.assertTrue("ssl_module" in self.config.parser.modules) loc_cert = self.config.parser.find_dir("sslcertificatefile", "example/fullchain.pem", self.vh_truth[1].path) loc_key = self.config.parser.find_dir("sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path) # Verify one directive was found in the correct file self.assertEqual(len(loc_cert), 1) self.assertEqual(apache_util.get_file_path(loc_cert[0]), self.vh_truth[1].filep) self.assertEqual(len(loc_key), 1) self.assertEqual(apache_util.get_file_path(loc_key[0]), self.vh_truth[1].filep)
def add_child_block(self, name, parameters=None, position=None): # pragma: no cover """Adds a new BlockNode to the sequence of children""" insertpath, realpath, before = self._aug_resolve_child_position( name, position ) new_metadata = {"augeasparser": self.parser, "augeaspath": realpath} # Create the new block self.parser.aug.insert(insertpath, name, before) # Check if the file was included from the root config or initial state enabled = self.parser.parsed_in_original( apache_util.get_file_path(realpath) ) # Parameters will be set at the initialization of the new object new_block = AugeasBlockNode(name=name, parameters=parameters, enabled=enabled, ancestor=assertions.PASS, filepath=apache_util.get_file_path(realpath), metadata=new_metadata) return new_block
def add_child_comment(self, comment="", position=None): """Adds a new CommentNode to the sequence of children""" insertpath, realpath, before = self._aug_resolve_child_position( "#comment", position) new_metadata = {"augeasparser": self.parser, "augeaspath": realpath} # Create the new comment self.parser.aug.insert(insertpath, "#comment", before) # Set the comment content self.parser.aug.set(realpath, comment) new_comment = AugeasCommentNode( comment=comment, ancestor=assertions.PASS, filepath=apache_util.get_file_path(realpath), metadata=new_metadata) return new_comment