|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Action plugin for ansible role apache - apache port config generator. |
| 3 | +
|
| 4 | +MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2024 Zdenek Styblik |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +""" |
| 26 | +from ansible.errors import AnsibleError |
| 27 | +from ansible.plugins.action import ActionBase |
| 28 | +from ansible.utils.display import Display |
| 29 | + |
| 30 | +display = Display() |
| 31 | + |
| 32 | + |
| 33 | +class ActionModule(ActionBase): |
| 34 | + """Generator of data for ports.conf configuration.""" |
| 35 | + |
| 36 | + TRANSFERS_FILES = False |
| 37 | + _requires_connection = False |
| 38 | + |
| 39 | + def _format_binding(self, listen_ip, port, proto): |
| 40 | + """Return pre-formatted string for Listen directive.""" |
| 41 | + lip_fmt = "{:s}:".format(listen_ip) if listen_ip else "" |
| 42 | + proto_fmt = " {:s}".format(proto) if proto else "" |
| 43 | + return "{:s}{:d}{:s}".format(lip_fmt, port, proto_fmt) |
| 44 | + |
| 45 | + def run(self, tmp=None, task_vars=None): |
| 46 | + """Transform virtual host definitions into data for ports.conf.""" |
| 47 | + result = super().run(tmp, task_vars) |
| 48 | + del tmp |
| 49 | + |
| 50 | + vhosts = self._task.args.get("vhosts", []) |
| 51 | + if not vhosts: |
| 52 | + vhosts = [{"listen_ip": "", "port": 80, "ssl": False}] |
| 53 | + |
| 54 | + # Ref. https://httpd.apache.org/docs/2.4/bind.html |
| 55 | + # Structure: |
| 56 | + # port -> ip addr -> ssl yes/no |
| 57 | + # |
| 58 | + # Example: |
| 59 | + # 80 -> 1.2.3.4 -> False |
| 60 | + # 80 -> '*' -> ... |
| 61 | + # => EXCEPTION due to collision |
| 62 | + # |
| 63 | + # 80 -> '*' -> False |
| 64 | + # 80 -> '*' -> True |
| 65 | + # => EXCEPTION due to collision |
| 66 | + # |
| 67 | + # then transform it for jinja2 |
| 68 | + bindings = {} |
| 69 | + for vhost in vhosts: |
| 70 | + try: |
| 71 | + port = int(vhost["port"]) |
| 72 | + except KeyError as exception: |
| 73 | + msg = "vhost '{}' is missing port attribute".format( |
| 74 | + vhost.get("servername", "unknown") |
| 75 | + ) |
| 76 | + raise AnsibleError(msg) from exception |
| 77 | + except ValueError as exception: |
| 78 | + msg = "failed to convert port '{}' of vhost '{}' to int".format( |
| 79 | + vhost.get("servername", "unknown"), |
| 80 | + vhost.get("port", None), |
| 81 | + ) |
| 82 | + raise AnsibleError(msg) from exception |
| 83 | + |
| 84 | + if port not in bindings: |
| 85 | + bindings[port] = {} |
| 86 | + |
| 87 | + ssl = vhost.get("ssl", False) |
| 88 | + # According to documentation, https is default proto for port 443. |
| 89 | + # Therefore there is no need to specify it. |
| 90 | + if ssl is True and port != 443: |
| 91 | + ssl = "https" |
| 92 | + else: |
| 93 | + ssl = "" |
| 94 | + |
| 95 | + listen_ip = vhost.get("listen_ip", "") |
| 96 | + if bindings[port]: |
| 97 | + # We need to check for possible numerous and various conflicts. |
| 98 | + if ( |
| 99 | + listen_ip in bindings[port] |
| 100 | + and bindings[port][listen_ip] != ssl |
| 101 | + ): |
| 102 | + # Reasoning: 'IP:Port' is the same and protocol is |
| 103 | + # different -> error |
| 104 | + msg = ( |
| 105 | + "HTTP/HTTPS collision for IP '{}' " |
| 106 | + "and port '{}' in vhost '{}'".format( |
| 107 | + listen_ip, |
| 108 | + port, |
| 109 | + vhost.get("servername", "unknown"), |
| 110 | + ) |
| 111 | + ) |
| 112 | + raise AnsibleError(msg) |
| 113 | + |
| 114 | + if ( |
| 115 | + listen_ip == "" and listen_ip not in bindings[port].keys() |
| 116 | + ) or (listen_ip != "" and "" in bindings[port].keys()): |
| 117 | + # Reasoning: if listening on *:80, then we cannot listen |
| 118 | + # on 1.2.3.4:80 as well and vice versa. |
| 119 | + msg = ( |
| 120 | + "bind collision any Vs. IP for IP '{}' " |
| 121 | + "and port '{}' in vhost '{}'".format( |
| 122 | + listen_ip, |
| 123 | + port, |
| 124 | + vhost.get("servername", "unknown"), |
| 125 | + ) |
| 126 | + ) |
| 127 | + raise AnsibleError(msg) |
| 128 | + |
| 129 | + bindings[port][listen_ip] = ssl |
| 130 | + |
| 131 | + result["data"] = { |
| 132 | + "{}:{}:{}".format(listen_ip, port, binding[listen_ip]): { |
| 133 | + "listen_ip": listen_ip, |
| 134 | + "port": port, |
| 135 | + "proto": binding[listen_ip], |
| 136 | + "formatted": self._format_binding( |
| 137 | + listen_ip, port, binding[listen_ip] |
| 138 | + ), |
| 139 | + } |
| 140 | + for port, binding in bindings.items() |
| 141 | + for listen_ip in binding |
| 142 | + } |
| 143 | + return result |
0 commit comments