summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-05-16 02:05:06 +0200
committerDaniil Baturin <daniil@baturin.org>2018-05-16 02:05:06 +0200
commit3666ab2b4ed251718c6db549d273ef676bb77c2c (patch)
treefbeabeb88bab014dc02916fd54eea6028f0f04f1
parent30030cc0cc808b9a1c942e89e8698ee2b522b87f (diff)
downloadvyos-1x-3666ab2b4ed251718c6db549d273ef676bb77c2c.tar.gz
vyos-1x-3666ab2b4ed251718c6db549d273ef676bb77c2c.zip
T643: correct the logic for generating node constraints and add emulation of multiple validation options.
-rwxr-xr-xdebian/rules3
-rwxr-xr-xscripts/build-command-templates48
-rwxr-xr-xsrc/helpers/validate-value.py41
3 files changed, 77 insertions, 15 deletions
diff --git a/debian/rules b/debian/rules
index d10f6f450..ed33706e4 100755
--- a/debian/rules
+++ b/debian/rules
@@ -27,6 +27,9 @@ override_dh_auto_install:
mkdir -p $(DIR)/$(VYOS_LIBEXEC_DIR)/validators
cp -r src/validators/* $(DIR)/$(VYOS_LIBEXEC_DIR)/validators
+ # Install helper scripts
+ cp -r src/helpers/* $(DIR)/$(VYOS_LIBEXEC_DIR)/
+
mkdir -p $(DIR)/$(VYOS_CFG_TMPL_DIR)
cp -r templates-cfg/* $(DIR)/$(VYOS_CFG_TMPL_DIR)
diff --git a/scripts/build-command-templates b/scripts/build-command-templates
index af46c10cf..415104e65 100755
--- a/scripts/build-command-templates
+++ b/scripts/build-command-templates
@@ -49,6 +49,8 @@ schema_file = args.SCHEMA_FILE
output_dir = args.OUTPUT_DIR
debug = args.debug
+debug = True
+
## Load and validate the inputs
try:
@@ -114,27 +116,44 @@ def get_properties(p):
except:
pass
- vce = p.findall("constraint")
+ vce = p.find("constraint")
vc = []
- for v in vce:
- if v.find("regex") is not None:
- vc.append("pattern $VAR(@) \"{0}\"; {1}".format(v.find("regex").text, error_msg))
- else:
- validator = v.find("validator")
- v_name = validator.get("name")
+
+ # The old backend doesn't support multiple validators in OR mode
+ # so we emulate it
+
+ regex_elements = vce.findall("regex")
+ regexes = []
+ if regex_elements is not None:
+ regexes = list(map(lambda e: e.text, regex_elements))
+
+ 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 = validator.get("argument")
+ v_argument = v.get("argument")
except:
pass
if v_argument is None:
v_argument = ""
- vc.append("exec \"{0}/{1} {2} $VAR(@)\"; \"{3}\"".format(validator_dir, v_name, v_argument, error_msg))
- props["constraints"] = vc
- except:
- props["constraints"] = []
+
+ validators.append("{0} {1} \\\'$VAR(@)\\\'".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))
+ validator_script = '${vyos_libexecdir}/validate-value.py'
+ validator_string = "exec \"{0} {1} {2}\"; \"{3}\"".format(validator_script, regex_args, validator_args, error_msg)
+
+ props["constraint"] = validator_string
+ except Exception as exn:
+ print(exn)
+ pass
# Get the completion help strings
try:
@@ -204,9 +223,8 @@ def make_node_def(props):
if "comp_help" in props:
node_def += "allowed: {0}\n".format(props["comp_help"])
- if "constraints" in props:
- for c in props["constraints"]:
- node_def += "syntax:expression: {0}\n".format(c)
+ if "constraint" in props:
+ node_def += "syntax:expression: {0}\n".format(props["constraint"])
if "owner" in props:
node_def += "end: sudo sh -c \"{0}\"\n".format(props["owner"])
diff --git a/src/helpers/validate-value.py b/src/helpers/validate-value.py
new file mode 100755
index 000000000..2625663a2
--- /dev/null
+++ b/src/helpers/validate-value.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import re
+import os
+import sys
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--regex', action='append')
+parser.add_argument('--exec', action='append')
+
+args = parser.parse_args()
+
+debug = False
+
+# Multiple arguments work like logical OR
+
+try:
+ for r in args.regex:
+ if re.match(r, args.value):
+ sys.exit(0)
+except Exception as exn:
+ if debug:
+ print(exn)
+ else:
+ pass
+
+try:
+ for cmd in args.exec:
+ if debug:
+ print(cmd)
+ res = os.system(cmd)
+ if res == 0:
+ sys.exit(0)
+except Exception as exn:
+ if debug:
+ print(exn)
+ else:
+ pass
+
+sys.exit(1)