certbot_nginx.parser
¶
NginxParser is a member object of the NginxConfigurator class.
-
class
certbot_nginx.parser.
NginxParser
(root, ssl_options)[source]¶ Bases:
object
Class handles the fine details of parsing the Nginx Configuration.
Variables: - root (str) – Normalized absolute path to the server root directory. Without trailing slash.
- parsed (dict) – Mapping of file paths to parsed trees
-
_parse_recursively
(filepath)[source]¶ Parses nginx config files recursively by looking at ‘include’ directives inside ‘http’ and ‘server’ blocks. Note that this only reads Nginx files that potentially declare a virtual host.
Parameters: filepath (str) – The path to the files to parse, as a glob
-
abs_path
(path)[source]¶ Converts a relative path to an absolute path relative to the root. Does nothing for paths that are already absolute.
Parameters: path (str) – The path Returns: The absolute path Return type: str
-
_build_addr_to_ssl
()[source]¶ Builds a map from address to whether it listens on ssl in any server block
-
get_vhosts
()[source]¶ Gets list of all ‘virtual hosts’ found in Nginx configuration. Technically this is a misnomer because Nginx does not have virtual hosts, it has ‘server blocks’.
Returns: List of VirtualHost
objects found in configurationReturn type: list
-
_update_vhosts_addrs_ssl
(vhosts)[source]¶ Update a list of raw parsed vhosts to include global address sslishness
-
_get_included_directives
(block)[source]¶ Returns array with the “include” directives expanded out by concatenating the contents of the included file to the block.
Parameters: block (list) – Return type: list
-
_parse_files
(filepath, override=False)[source]¶ Parse files from a glob
Parameters: - filepath (str) – Nginx config file path
- override (bool) – Whether to parse a file that has been parsed
Returns: list of parsed tree structures
Return type: list
-
_set_locations
(ssl_options)[source]¶ Set default location for directives.
Locations are given as file_paths .. todo:: Make sure that files are included
-
filedump
(ext='tmp', lazy=True)[source]¶ Dumps parsed configurations into files.
Parameters: - ext (str) – The file extension to use for the dumped files. If empty, this overrides the existing conf files.
- lazy (bool) – Only write files that have been modified
-
parse_server
(server)[source]¶ Parses a list of server directives, accounting for global address sslishness.
Parameters: server (list) – list of directives in a server block Return type: dict
-
has_ssl_on_directive
(vhost)[source]¶ Does vhost have ssl on for all ports?
:param
VirtualHost
vhost: The vhost in questionReturns: True if ‘ssl on’ directive is included Return type: bool
-
add_server_directives
(vhost, directives, replace)[source]¶ Add or replace directives in the server block identified by vhost.
This method modifies vhost to be fully consistent with the new directives.
..note :: If replace is True, this raises a misconfiguration error if the directive does not already exist. ..note :: If replace is False nothing gets added if an identical block exists already.
- ..todo :: Doesn’t match server blocks whose server_name directives are
- split across multiple conf files.
- :param
VirtualHost
vhost: The vhost - whose information we use to match on
Parameters: - directives (list) – The directives to add
- replace (bool) – Whether to only replace existing directives
-
certbot_nginx.parser.
_do_for_subarray
(entry, condition, func, path=None)[source]¶ Executes a function for a subarray of a nested array if it matches the given condition.
Parameters: - entry (list) – The list to iterate over
- condition (function) – Returns true iff func should be executed on item
- func (function) – The function to call for each matching item
-
certbot_nginx.parser.
get_best_match
(target_name, names)[source]¶ Finds the best match for target_name out of names using the Nginx name-matching rules (exact > longest wildcard starting with * > longest wildcard ending with * > regex).
Parameters: - target_name (str) – The name to match
- names (set) – The candidate server names
Returns: Tuple of (type of match, the name that matched)
Return type: tuple
-
certbot_nginx.parser.
_is_include_directive
(entry)[source]¶ Checks if an nginx parsed entry is an ‘include’ directive.
Parameters: entry (list) – the parsed entry Returns: Whether it’s an ‘include’ directive Return type: bool
-
certbot_nginx.parser.
_get_servernames
(names)[source]¶ Turns a server_name string into a list of server names
Parameters: names (str) – server names Return type: list
-
certbot_nginx.parser.
_add_directives
(block, directives, replace)[source]¶ Adds or replaces directives in a config block.
When replace=False, it’s an error to try and add a directive that already exists in the config block with a conflicting value.
When replace=True, a directive with the same name MUST already exist in the config block, and the first instance will be replaced.
..todo :: Find directives that are in included files.
Parameters: - block (list) – The block to replace in
- directives (list) – The new directives.
-
certbot_nginx.parser.
_comment_directive
(block, location)[source]¶ Add a comment to the end of the line at location.
-
certbot_nginx.parser.
_add_directive
(block, directive, replace)[source]¶ Adds or replaces a single directive in a config block.
See _add_directives for more documentation.