From d19b35d668184fdac00a8ef56ff58c0748f22816 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Thu, 12 Aug 2021 11:09:58 -0500 Subject: T3574: add constraintGroup for combining validators with logical AND (cherry picked from commit 591b8bcadd8b6bbd46c61484193d2bf7e16bd1ae commit 31553283aaa929da63147082e85513e8d4dacf0e commit 59a4aadfe419eca16e6288b37d6c51acd9789903) --- scripts/build-command-templates | 76 +++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 30 deletions(-) (limited to 'scripts/build-command-templates') diff --git a/scripts/build-command-templates b/scripts/build-command-templates index 452c420eb..d8abb0a13 100755 --- a/scripts/build-command-templates +++ b/scripts/build-command-templates @@ -86,6 +86,37 @@ def make_path(l): print(path) return path +def collect_validators(ve): + regexes = [] + regex_elements = ve.findall("regex") + if regex_elements is not None: + regexes = list(map(lambda e: e.text.strip().replace('\\','\\\\'), regex_elements)) + if "" in regexes: + print("Warning: empty regex, node will be accepting any value") + + validator_elements = ve.findall("validator") + validators = [] + if validator_elements is not None: + for v in validator_elements: + v_name = os.path.join(validator_dir, v.get("name")) + + # XXX: lxml returns None for empty arguments + v_argument = None + try: + v_argument = v.get("argument") + except: + pass + if v_argument is None: + v_argument = "" + + validators.append("{0} {1}".format(v_name, v_argument)) + + + regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes)) + validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators)) + + return regex_args + " " + validator_args + def get_properties(p): props = {} @@ -108,7 +139,8 @@ def get_properties(p): except: props["val_help"] = [] - # Get the constraint statements + # Get the constraint and constraintGroup statements + error_msg = default_constraint_err_msg # Get the error message if it's there try: @@ -117,40 +149,24 @@ def get_properties(p): pass vce = p.find("constraint") - vc = [] + + distinct_validator_string = "" if vce is not None: # The old backend doesn't support multiple validators in OR mode # so we emulate it - regexes = [] - regex_elements = vce.findall("regex") - if regex_elements is not None: - regexes = list(map(lambda e: e.text.strip().replace('\\','\\\\'), regex_elements)) - if "" in regexes: - print("Warning: empty regex, node will be accepting any value") - - validator_elements = vce.findall("validator") - validators = [] - if validator_elements is not None: - for v in validator_elements: - v_name = os.path.join(validator_dir, v.get("name")) - - # XXX: lxml returns None for empty arguments - v_argument = None - try: - v_argument = v.get("argument") - except: - pass - if v_argument is None: - v_argument = "" - - validators.append("{0} {1}".format(v_name, v_argument)) - - - regex_args = " ".join(map(lambda s: "--regex \\\'{0}\\\'".format(s), regexes)) - validator_args = " ".join(map(lambda s: "--exec \\\"{0}\\\"".format(s), validators)) + distinct_validator_string = collect_validators(vce) + + vcge = p.findall("constraintGroup") + + group_validator_string = "" + if len(vcge): + for vcg in vcge: + group_validator_string = group_validator_string + " --grp " + collect_validators(vcg) + + if vce is not None or len(vcge): validator_script = '${vyos_libexec_dir}/validate-value' - validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, regex_args, validator_args, error_msg) + validator_string = "exec \"{0} {1} {2} --value \\\'$VAR(@)\\\'\"; \"{3}\"".format(validator_script, distinct_validator_string, group_validator_string, error_msg) props["constraint"] = validator_string -- cgit v1.2.3