| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2022, Felix Fontein (@felixfontein) <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# The data inside here is private to this collection. If you use this from outside the collection, |
| 7 |
# you are on your own. There can be random changes to its format even in bugfix releases! |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
import re |
| 13 |
|
| 14 |
from ansible.module_utils.common.text.converters import to_text |
| 15 |
|
| 16 |
|
| 17 |
def value_to_str(value, compat_bool=False, none_to_empty=False): |
| 18 |
if value is None: |
| 19 |
return '' if none_to_empty else None |
| 20 |
if value is True: |
| 21 |
return 'true' if compat_bool else 'yes' |
| 22 |
if value is False: |
| 23 |
return 'false' if compat_bool else 'no' |
| 24 |
return to_text(value) |
| 25 |
|
| 26 |
|
| 27 |
def validate_and_prepare_restrict(module, path_info, compat=True): |
| 28 |
restrict = module.params['restrict'] |
| 29 |
if restrict is None: |
| 30 |
return None |
| 31 |
restrict_data = [] |
| 32 |
for rule in restrict: |
| 33 |
field = rule['field'] |
| 34 |
if field.startswith('!'): |
| 35 |
module.fail_json(msg='restrict: the field name "{0}" must not start with "!"'.format(field)) |
| 36 |
f = path_info.fields.get(field) |
| 37 |
if f is None: |
| 38 |
module.fail_json(msg='restrict: the field "{0}" does not exist for this path'.format(field)) |
| 39 |
|
| 40 |
new_rule = dict( |
| 41 |
field=field, |
| 42 |
match_disabled=rule['match_disabled'], |
| 43 |
invert=rule['invert'], |
| 44 |
) |
| 45 |
if rule['values'] is not None: |
| 46 |
if compat: |
| 47 |
new_rule['values'] = rule['values'] |
| 48 |
else: |
| 49 |
new_rule['values'] = [value_to_str(v, none_to_empty=False) for v in rule['values']] |
| 50 |
if rule['regex'] is not None: |
| 51 |
regex = rule['regex'] |
| 52 |
try: |
| 53 |
new_rule['regex'] = re.compile(regex) |
| 54 |
new_rule['regex_source'] = regex |
| 55 |
except Exception as exc: |
| 56 |
module.fail_json(msg='restrict: invalid regular expression "{0}": {1}'.format(regex, exc)) |
| 57 |
restrict_data.append(new_rule) |
| 58 |
return restrict_data |
| 59 |
|
| 60 |
|
| 61 |
def _test_rule_except_invert(value, rule, compat=False): |
| 62 |
if value is None and rule['match_disabled']: |
| 63 |
return True |
| 64 |
if 'values' in rule: |
| 65 |
v = value if compat else value_to_str(value, none_to_empty=False) |
| 66 |
if v in rule['values']: |
| 67 |
return True |
| 68 |
if 'regex' in rule and value is not None and rule['regex'].match(value_to_str(value, compat_bool=compat)): |
| 69 |
return True |
| 70 |
return False |
| 71 |
|
| 72 |
|
| 73 |
def restrict_entry_accepted(entry, path_info, restrict_data, compat=True): |
| 74 |
if restrict_data is None: |
| 75 |
return True |
| 76 |
for rule in restrict_data: |
| 77 |
# Obtain field and value |
| 78 |
field = rule['field'] |
| 79 |
field_info = path_info.fields[field] |
| 80 |
value = entry.get(field) |
| 81 |
if value is None: |
| 82 |
value = field_info.default |
| 83 |
if field not in entry and field_info.absent_value: |
| 84 |
value = field_info.absent_value |
| 85 |
|
| 86 |
# Check |
| 87 |
matches_rule = _test_rule_except_invert(value, rule, compat=compat) |
| 88 |
if rule['invert']: |
| 89 |
matches_rule = not matches_rule |
| 90 |
if not matches_rule: |
| 91 |
return False |
| 92 |
return True |
| 93 |
|
| 94 |
|
| 95 |
def restrict_argument_spec(): |
| 96 |
return dict( |
| 97 |
restrict=dict( |
| 98 |
type='list', |
| 99 |
elements='dict', |
| 100 |
options=dict( |
| 101 |
field=dict(type='str', required=True), |
| 102 |
match_disabled=dict(type='bool', default=False), |
| 103 |
values=dict(type='list', elements='raw'), |
| 104 |
regex=dict(type='str'), |
| 105 |
invert=dict(type='bool', default=False), |
| 106 |
), |
| 107 |
), |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
def apply_value_sanitizer(key_info, value, key_name, warn=None): |
| 112 |
"""Apply ``key_info.value_sanitizer`` to *value* and warn on change. |
| 113 |
|
| 114 |
This is the central call-site for value sanitisation. It is intentionally |
| 115 |
kept separate from ``KeyInfo`` itself so that no specific plugin framework |
| 116 |
(e.g. ``AnsibleModule``) is ever imported by ``api_data.py``. |
| 117 |
|
| 118 |
Parameters |
| 119 |
---------- |
| 120 |
key_info : KeyInfo |
| 121 |
Metadata for the field being sanitised. |
| 122 |
value : any |
| 123 |
The value to sanitise. Non-string and ``None`` handling is delegated |
| 124 |
to the sanitizer itself. If no sanitizer is registered, *value* is |
| 125 |
returned unchanged. |
| 126 |
key_name : str |
| 127 |
Human-readable field name used in the warning message. |
| 128 |
warn : callable or None |
| 129 |
Optional warning function (e.g. ``module.warn``). Called with a |
| 130 |
single string message when the sanitizer modifies *value*. Pass |
| 131 |
``None`` to suppress warnings entirely. |
| 132 |
|
| 133 |
Returns |
| 134 |
------- |
| 135 |
any |
| 136 |
The sanitised value, or *value* unchanged if no sanitizer is |
| 137 |
registered. |
| 138 |
""" |
| 139 |
if key_info.value_sanitizer is None: |
| 140 |
return value |
| 141 |
|
| 142 |
sanitized = key_info.value_sanitizer(value) |
| 143 |
|
| 144 |
if warn is not None and sanitized != value: |
| 145 |
warn( |
| 146 |
'Value of field "{key}" was automatically normalised from {original!r} to ' |
| 147 |
'{sanitized!r}. Consider updating your playbook to use the normalised ' |
| 148 |
'value directly to avoid this warning.'.format( |
| 149 |
key=key_name, |
| 150 |
original=value, |
| 151 |
sanitized=sanitized, |
| 152 |
) |
| 153 |
) |
| 154 |
|
| 155 |
return sanitized |