# `letsencrypt_apache.configurator`

Apache Configuration based off of Augeas Configurator.

class `letsencrypt_apache.configurator.``ApacheConfigurator`(*args, **kwargs)

  Bases: `letsencrypt_apache.augeas_configurator.AugeasConfigurator`

  Apache configurator.

  State of Configurator: This code has been been tested and built for Ubuntu 14.04 Apache 2.4 and it works for Ubuntu 12.04 Apache 2.2

  Variables:

  - config – Configuration.
  - parser – Handles low level parsing
  - version (tup) – version of Apache
  - vhosts (list) – All vhosts found in the configuration (`list` of `VirtualHost`)
  - assoc (dict) – Mapping between domains and vhosts

  `mod_ssl_conf`

    Full absolute path to SSL configuration file.

  `prepare`()

    Prepare the authenticator/installer.

    Raises:

    - .errors.NoInstallationError – If Apache configs cannot be found
    - .errors.MisconfigurationError – If Apache is misconfigured
    - .errors.NotSupportedError – If Apache version is not supported
    - .errors.PluginError – If there is any other error

  `_check_aug_version`()

    Checks that we have recent enough version of libaugeas. If augeas version is recent enough, it will support case insensitive regexp matching

  `deploy_cert`(domain, cert_path, key_path, chain_path=None, fullchain_path=None)

    Deploys certificate to specified virtual host.

    Currently tries to find the last directives to deploy the cert in the VHost associated with the given domain. If it can’t find the directives, it searches the “included” confs. The function verifies that it has located the three directives and finally modifies them to point to the correct destination. After the certificate is installed, the VirtualHost is enabled if it isn’t already.

    Todo

    Might be nice to remove chain directive if none exists This shouldn’t happen within letsencrypt though

    Raises: | errors.PluginError – When unable to deploy certificate due to a lack of directives

  `choose_vhost`(target_name, temp=False)

    Chooses a virtual host based on the given domain name.

    If there is no clear virtual host to be selected, the user is prompted with all available choices.

    The returned vhost is guaranteed to have TLS enabled unless temp is True. If temp is True, there is no such guarantee and the result is not cached.

    Parameters:

    - target_name (str) – domain name
    - temp (bool) – whether the vhost is only used temporarily

    Returns: | ssl vhost associated with name
    Return type: | `VirtualHost`
    Raises: | .errors.PluginError – If no vhost is available or chosen

  `included_in_wildcard`(names, target_name)

    Helper function to see if alias is covered by wildcard

  `_find_best_vhost`(target_name)

    Finds the best vhost for a target_name.

    This does not upgrade a vhost to HTTPS... it only finds the most appropriate vhost for the given target_name.

    Returns: | VHost or None

  `_non_default_vhosts`()

    Return all non _default_ only vhosts.

  `get_all_names`()

    Returns all names found in the Apache Configuration.

    Returns: | All ServerNames, ServerAliases, and reverse DNS entries for virtual host addresses
    Return type: | set

  `get_name_from_ip`(addr)

    Returns a reverse dns name if available.

    Parameters: | addr (Addr) – IP Address
    Returns: | name or empty string if name cannot be determined
    Return type: | str

  `_add_servernames`(host)

    Helper function for get_virtual_hosts().

    Parameters: | host (`VirtualHost`) – In progress vhost whose names will be added

  `_create_vhost`(path)

    Used by get_virtual_hosts to create vhost objects

    Parameters: | path (str) – Augeas path to virtual host
    Returns: | newly created vhost
    Return type: | `VirtualHost`

  `get_virtual_hosts`()

    Returns list of virtual hosts found in the Apache configuration.

    Returns: | List of `VirtualHost` objects found in configuration
    Return type: | list

  `is_name_vhost`(target_addr)

    Returns if vhost is a name based vhost

    NameVirtualHost was deprecated in Apache 2.4 as all VirtualHosts are now NameVirtualHosts. If version is earlier than 2.4, check if addr has a NameVirtualHost directive in the Apache config

    Parameters: | target_addr (letsencrypt_apache.obj.Addr) – vhost address
    Returns: | Success
    Return type: | bool

  `add_name_vhost`(addr)

    Adds NameVirtualHost directive for given address.

    Parameters: | addr (`Addr`) – Address that will be added as NameVirtualHost directive

  `prepare_server_https`(port, temp=False)

    Prepare the server for HTTPS.

    Make sure that the ssl_module is loaded and that the server is appropriately listening on port.

    Parameters: | port (str) – Port to listen on

  `prepare_https_modules`(temp)

    Helper method for prepare_server_https, taking care of enabling needed modules

    Parameters: | temp (boolean) – If the change is temporary

  `make_addrs_sni_ready`(addrs)

    Checks to see if the server is ready for SNI challenges.

    Parameters: | addrs (`Addr`) – Addresses to check SNI compatibility

  `make_vhost_ssl`(nonssl_vhost)

    Makes an ssl_vhost version of a nonssl_vhost.

    Duplicates vhost and adds default ssl options New vhost will reside as (nonssl_vhost.path) + `letsencrypt_apache.constants.os_constant("le_vhost_ext")`

    Note

    This function saves the configuration

    Parameters: | nonssl_vhost (`VirtualHost`) – Valid VH that doesn’t have SSLEngine on
    Returns: | SSL vhost
    Return type: | `VirtualHost`
    Raises: | .errors.PluginError – If more than one virtual host is in the file or if plugin is unable to write/read vhost files.

  `_sift_line`(line)

    Decides whether a line should be copied to a SSL vhost.

    A canonical example of when sifting a line is required: When the http vhost contains a RewriteRule that unconditionally redirects any request to the https version of the same site. e.g: RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] Copying the above line to the ssl vhost would cause a redirection loop.

    Parameters: | line (str) – a line extracted from the http vhost.
    Returns: | True - don’t copy line from http vhost to SSL vhost.
    Return type: | bool

  `_copy_create_ssl_vhost_skeleton`(avail_fp, ssl_fp)

    Copies over existing Vhost with IfModule mod_ssl.c> skeleton.

    Parameters:

    - avail_fp (str) – Pointer to the original available non-ssl vhost
    - ssl_fp (str) – Full path where the new ssl_vhost will reside.

    A new file is created on the filesystem.

  `_add_name_vhost_if_necessary`(vhost)

    Add NameVirtualHost Directives if necessary for new vhost.

    NameVirtualHosts was a directive in Apache < 2.4 https://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost

    Parameters: | vhost (`VirtualHost`) – New virtual host that was recently created.

  `supported_enhancements`()

    Returns currently supported enhancements.

  `enhance`(domain, enhancement, options=None)

    Enhance configuration.

    Parameters:

    - domain (str) – domain to enhance
    - enhancement (str) – enhancement type defined in `ENHANCEMENTS`
    - options – options for the enhancement See `ENHANCEMENTS` documentation for appropriate parameter.

    Raises: | .errors.PluginError – If Enhancement is not supported, or if there is any other problem with the enhancement.

  `_set_http_header`(ssl_vhost, header_substring)

    Enables header that is identified by header_substring on ssl_vhost.

    If the header identified by header_substring is not already set, a new Header directive is placed in ssl_vhost’s configuration with arguments from: constants.HTTP_HEADER[header_substring]

    Note

    This function saves the configuration

    Parameters:

    - ssl_vhost (`VirtualHost`) – Destination of traffic, an ssl enabled vhost
    - header_substring – string that uniquely identifies a header. e.g: Strict-Transport-Security, Upgrade-Insecure-Requests.

    :type str

    Returns: | Success, general_vhost (HTTP vhost)
    Return type: | (bool, `VirtualHost`)
    Raises: | .errors.PluginError – If no viable HTTP host can be created or set with header header_substring.

  `_verify_no_matching_http_header`(ssl_vhost, header_substring)

    Checks to see if an there is an existing Header directive that contains the string header_substring.

    Parameters:

    - ssl_vhost – vhost to check
    - header_substring – string that uniquely identifies a header. e.g: Strict-Transport-Security, Upgrade-Insecure-Requests.

    :type str

    Returns: | boolean
    Return type: | (bool)

    :raises errors.PluginEnhancementAlreadyPresent When header
      header_substring exists
  `_enable_redirect`(ssl_vhost, unused_options)

    Redirect all equivalent HTTP traffic to ssl_vhost.

    Todo

    This enhancement should be rewritten and will unfortunately require lots of debugging by hand.

    Adds Redirect directive to the port 80 equivalent of ssl_vhost First the function attempts to find the vhost with equivalent ip addresses that serves on non-ssl ports The function then adds the directive

    Note

    This function saves the configuration

    Parameters:

    - ssl_vhost (`VirtualHost`) – Destination of traffic, an ssl enabled vhost
    - unused_options (Not Available) – Not currently used

    Returns: | Success, general_vhost (HTTP vhost)
    Return type: | (bool, `VirtualHost`)
    Raises: | .errors.PluginError – If no viable HTTP host can be created or used for the redirect.

  `_verify_no_letsencrypt_redirect`(vhost)

    Checks to see if a redirect was already installed by letsencrypt.

    Checks to see if virtualhost already contains a rewrite rule that is identical to Letsencrypt’s redirection rewrite rule.

    Parameters: | vhost (`VirtualHost`) – vhost to check
    Raises: | errors.PluginEnhancementAlreadyPresent – When the exact letsencrypt redirection WriteRule exists in virtual host.

  `_is_rewrite_exists`(vhost)

    Checks if there exists a RewriteRule directive in vhost

    Parameters: | vhost (`VirtualHost`) – vhost to check
    Returns: | True if a RewriteRule directive exists.
    Return type: | bool

  `_is_rewrite_engine_on`(vhost)

    Checks if a RewriteEngine directive is on

    Parameters: | vhost (`VirtualHost`) – vhost to check

  `_create_redirect_vhost`(ssl_vhost)

    Creates an http_vhost specifically to redirect for the ssl_vhost.

    Parameters: | ssl_vhost (`VirtualHost`) – ssl vhost
    Returns: | tuple of the form (`success`, `VirtualHost`)
    Return type: | tuple

  `_get_http_vhost`(ssl_vhost)

    Find appropriate HTTP vhost for ssl_vhost.

  `_get_proposed_addrs`(vhost, port='80')

    Return all addrs of vhost with the port replaced with the specified.

    Parameters:

    - ssl_vhost (obj.VirtualHost) – Original Vhost
    - port (str) – Desired port for new addresses

    Returns: | `set` of `Addr`

  `get_all_certs_keys`()

    Find all existing keys, certs from configuration.

    Retrieve all certs and keys set in VirtualHosts on the Apache server

    Returns: | list of tuples with form [(cert, key, path)] cert - str path to certificate file key - str path to associated key file path - File path to configuration file.
    Return type: | list

  `is_site_enabled`(avail_fp)

    Checks to see if the given site is enabled.

    Todo

    fix hardcoded sites-enabled, check os.path.samefile

    Parameters: | avail_fp (str) – Complete file path of available site
    Returns: | Success
    Return type: | bool

  `enable_site`(vhost)

    Enables an available site, Apache reload required.

    Note

    Does not make sure that the site correctly works or that all modules are enabled appropriately.

    Todo

    This function should number subdomains before the domain vhost

    Todo

    Make sure link is not broken...

    Parameters: | vhost (`VirtualHost`) – vhost to enable
    Raises: | .errors.NotSupportedError – If filesystem layout is not supported.

  `enable_mod`(mod_name, temp=False)

    Enables module in Apache.

    Both enables and reloads Apache so module is active.

    Parameters:

    - mod_name (str) – Name of the module to enable. (e.g. ‘ssl’)
    - temp (bool) – Whether or not this is a temporary action.

    Raises:

    - .errors.NotSupportedError – If the filesystem layout is not supported.
    - .errors.MisconfigurationError – If a2enmod or a2dismod cannot be run.

  `_add_parser_mod`(mod_name)

    Shortcut for updating parser modules.

  `_enable_mod_debian`(mod_name, temp)

    Assumes mods-available, mods-enabled layout.

  `restart`()

    Runs a config test and reloads the Apache server.

    Raises: | .errors.MisconfigurationError – If either the config test or reload fails.

  `_reload`()

    Reloads the Apache server.

    Raises: | .errors.MisconfigurationError – If reload fails

  `config_test`()

    Check the configuration of Apache for errors.

    Raises: | .errors.MisconfigurationError – If config_test fails

  `get_version`()

    Return version of Apache Server.

    Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7))

    Returns: | version
    Return type: | tuple
    Raises: | .PluginError – if unable to find Apache version

  `more_info`()

    Human-readable string to help understand the module

  `get_chall_pref`(unused_domain)

    Return list of challenge preferences.

  `perform`(achalls)

    Perform the configuration related challenge.

    This function currently assumes all challenges will be fulfilled. If this turns out not to be the case in the future. Cleanup and outstanding challenges will have to be designed better.

  `cleanup`(achalls)

    Revert all challenges.

`letsencrypt_apache.configurator.``_get_mod_deps`(mod_name)

  Get known module dependencies.

  Note

  This does not need to be accurate in order for the client to run. This simply keeps things clean if the user decides to revert changes.

  Warning

  If all deps are not included, it may cause incorrect parsing behavior, due to enable_mod’s shortcut for updating the parser’s currently defined modules (`ApacheConfigurator._add_parser_mod`) This would only present a major problem in extremely atypical configs that use ifmod for the missing deps.

`letsencrypt_apache.configurator.``get_file_path`(vhost_path)

  Get file path from augeas_vhost_path.

  Takes in Augeas path and returns the file name

  Parameters: | vhost_path (str) – Augeas virtual host path
  Returns: | filename of vhost
  Return type: | str

`letsencrypt_apache.configurator.``install_ssl_options_conf`(options_ssl)

  Copy Let’s Encrypt’s SSL options file into the system’s config dir if required.