| 1 |
# Copyright (c) 2023, Vladimir Botka <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = """ |
| 8 |
name: fqdn_valid |
| 9 |
short_description: Validates fully-qualified domain names against RFC 1123 |
| 10 |
version_added: 8.1.0 |
| 11 |
author: Vladimir Botka (@vbotka) |
| 12 |
requirements: |
| 13 |
- fqdn>=1.5.1 (PyPI) |
| 14 |
description: |
| 15 |
- This test validates Fully Qualified Domain Names (FQDNs) |
| 16 |
conforming to the Internet Engineering Task Force specification |
| 17 |
RFC 1123 and RFC 952. |
| 18 |
- The design intent is to validate that a string would be |
| 19 |
traditionally acceptable as a public Internet hostname to |
| 20 |
RFC-conforming software, which is a strict subset of the logic |
| 21 |
in modern web browsers like Mozilla Firefox and Chromium that |
| 22 |
determines whether make a DNS lookup. |
| 23 |
- Certificate Authorities like Let's Encrypt run a narrower set of |
| 24 |
string validation logic to determine validity for issuance. This |
| 25 |
test is not intended to achieve functional parity with CA |
| 26 |
issuance. |
| 27 |
- Single label names are allowed by default (O(min_labels=1)). |
| 28 |
options: |
| 29 |
_input: |
| 30 |
description: Name of the host. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
min_labels: |
| 34 |
description: Required minimum of labels, separated by period. |
| 35 |
default: 1 |
| 36 |
type: int |
| 37 |
required: false |
| 38 |
allow_underscores: |
| 39 |
description: Allow underscore characters. |
| 40 |
default: false |
| 41 |
type: bool |
| 42 |
required: false |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = """ |
| 46 |
- name: Make sure that hostname is valid |
| 47 |
ansible.builtin.assert: |
| 48 |
that: hostname is community.general.fqdn_valid |
| 49 |
|
| 50 |
- name: Make sure that hostname is at least 3 labels long (a.b.c) |
| 51 |
ansible.builtin.assert: |
| 52 |
that: hostname is community.general.fqdn_valid(min_labels=3) |
| 53 |
|
| 54 |
- name: Make sure that hostname is at least 2 labels long (a.b). Allow '_' |
| 55 |
ansible.builtin.assert: |
| 56 |
that: hostname is community.general.fqdn_valid(min_labels=2, allow_underscores=True) |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = """ |
| 60 |
_value: |
| 61 |
description: Whether the name is valid. |
| 62 |
type: bool |
| 63 |
""" |
| 64 |
|
| 65 |
import typing as t |
| 66 |
from collections.abc import Callable |
| 67 |
|
| 68 |
from ansible.errors import AnsibleFilterError |
| 69 |
|
| 70 |
ANOTHER_LIBRARY_IMPORT_ERROR: ImportError | None |
| 71 |
try: |
| 72 |
from fqdn import FQDN |
| 73 |
except ImportError as imp_exc: |
| 74 |
ANOTHER_LIBRARY_IMPORT_ERROR = imp_exc |
| 75 |
else: |
| 76 |
ANOTHER_LIBRARY_IMPORT_ERROR = None |
| 77 |
|
| 78 |
|
| 79 |
def fqdn_valid(name: t.Any, min_labels: t.Any = 1, allow_underscores: t.Any = False) -> bool: |
| 80 |
""" |
| 81 |
Example: |
| 82 |
- 'srv.example.com' is community.general.fqdn_valid |
| 83 |
- 'foo_bar.example.com' is community.general.fqdn_valid(allow_underscores=True) |
| 84 |
""" |
| 85 |
|
| 86 |
if ANOTHER_LIBRARY_IMPORT_ERROR: |
| 87 |
raise AnsibleFilterError( |
| 88 |
"Python package fqdn must be installed to use this test." |
| 89 |
) from ANOTHER_LIBRARY_IMPORT_ERROR |
| 90 |
|
| 91 |
if not isinstance(name, str): |
| 92 |
raise AnsibleFilterError(f"The name parameter must be a string, got {name!r} of type {type(name)}") |
| 93 |
|
| 94 |
if not isinstance(min_labels, int): |
| 95 |
raise AnsibleFilterError( |
| 96 |
f"The min_labels parameter must be an integer, got {min_labels!r} of type {type(min_labels)}" |
| 97 |
) |
| 98 |
|
| 99 |
if not isinstance(allow_underscores, bool): |
| 100 |
raise AnsibleFilterError( |
| 101 |
f"The allow_underscores parameter must be a boolean, got {allow_underscores!r} of type {type(allow_underscores)}" |
| 102 |
) |
| 103 |
|
| 104 |
fobj = FQDN(name, min_labels=min_labels, allow_underscores=allow_underscores) |
| 105 |
return fobj.is_valid |
| 106 |
|
| 107 |
|
| 108 |
class TestModule: |
| 109 |
"""Ansible test hostname validity. |
| 110 |
https://pypi.org/project/fqdn/ |
| 111 |
""" |
| 112 |
|
| 113 |
def tests(self) -> dict[str, Callable]: |
| 114 |
return { |
| 115 |
"fqdn_valid": fqdn_valid, |
| 116 |
} |