Debian Codegraph @

ansible_collections/community/crypto/plugins/module_utils/_caa.py

raw
1 # Copyright (c) 2020, Felix Fontein <[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 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import re
11
12 _VALUE_RE = re.compile("^[\x21-\x3a\x3c-\x7e]*$")
13 _LABEL_RE = re.compile("^[0-9a-zA-Z][0-9a-zA-Z-]*$")
14
15
16 def _check_value(value: str) -> None:
17 if not _VALUE_RE.match(value):
18 raise ValueError(f"Invalid value {value!r}")
19
20
21 def _check_label(label: str, what: str) -> None:
22 if not _LABEL_RE.match(label):
23 raise ValueError(f"Invalid {what} {label!r}")
24
25
26 def _check_domain_name(value: str) -> None:
27 for p in value.split("."):
28 _check_label(p, "label")
29
30
31 def parse_issue_value(
32 value: str, *, check_for_duplicates: bool = True, strict: bool = True
33 ) -> tuple[str | None, list[tuple[str, str]]]:
34 """
35 Given a CAA issue property, parses it according to the syntax defined in RFC 8659.
36
37 More precisely, see https://www.rfc-editor.org/rfc/rfc8659.html#section-4.2.
38
39 If ``check_for_duplicates == True``, duplicate tags are reported as an error.
40 If ``strict == True``, invalid characters are reported as an error.
41 """
42 parts = [v.strip(" \t") for v in value.split(";")]
43 if len(parts) > 1 and not parts[-1]:
44 del parts[-1]
45 domain_name = parts[0] or None
46 if domain_name is not None and strict:
47 _check_domain_name(domain_name)
48 pairs = []
49 previous_tags: set[str] = set()
50 for part in parts[1:]:
51 pieces = part.split("=", 1)
52 if len(pieces) != 2:
53 raise ValueError(f"{part!r} is not of the form tag=value")
54 tag, value = pieces[0].rstrip(" \t"), pieces[1].lstrip(" \t")
55 if strict:
56 _check_label(tag, "tag")
57 _check_value(value)
58 pairs.append((tag, value))
59 if check_for_duplicates:
60 if tag in previous_tags:
61 raise ValueError(f"Tag {tag!r} appears multiple times")
62 previous_tags.add(tag)
63 return domain_name, pairs
64
65
66 def join_issue_value(
67 domain_name: str | None,
68 pairs: list[tuple[str, str]],
69 *,
70 check_for_duplicates: bool = True,
71 strict: bool = True,
72 ) -> str:
73 """
74 Given a domain name and a list of tag-value pairs, joins them according
75 to the syntax defined in RFC 8659.
76
77 More precisely, see https://www.rfc-editor.org/rfc/rfc8659.html#section-4.2.
78
79 If ``check_for_duplicates == True``, duplicate tags are reported as an error.
80 If ``strict == True``, invalid characters are reported as an error.
81 """
82 if domain_name is not None and strict:
83 _check_domain_name(domain_name)
84 parts = [domain_name or ""]
85 previous_tags: set[str] = set()
86 for tag, value in pairs:
87 if strict:
88 _check_label(tag, "tag")
89 _check_value(value)
90 parts.append(f"{tag}={value}")
91 if check_for_duplicates:
92 if tag in previous_tags:
93 raise ValueError(f"Tag {tag!r} appears multiple times")
94 previous_tags.add(tag)
95 return "; ".join(parts)
96
97
98 __all__ = ("parse_issue_value",)