From 84b29c7c71af6663ef26dc40475cf4b0e5b179a6 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sat, 4 Dec 2021 14:15:37 +0100 Subject: validators: T4042: rename bgp-route-target -> bgp-rd-rt --- src/validators/bgp-rd-rt | 51 +++++++++++++++++++++++++++++++++++++++++ src/validators/bgp-route-target | 51 ----------------------------------------- 2 files changed, 51 insertions(+), 51 deletions(-) create mode 100755 src/validators/bgp-rd-rt delete mode 100755 src/validators/bgp-route-target (limited to 'src/validators') diff --git a/src/validators/bgp-rd-rt b/src/validators/bgp-rd-rt new file mode 100755 index 000000000..24b0043f9 --- /dev/null +++ b/src/validators/bgp-rd-rt @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from argparse import ArgumentParser +from vyos.template import is_ipv4 + +parser = ArgumentParser() +group = parser.add_mutually_exclusive_group() +group.add_argument('--route-target', action='store', help='Validate one BGP route-target') +group.add_argument('--route-target-multi', action='store', help='Validate multiple, whitespace separated BGP route-targets') +args = parser.parse_args() + +def is_valid_rt(rt): + # every route target needs to have a colon and must consists of two parts + value = rt.split(':') + if len(value) != 2: + return False + # A route target must either be only numbers, or the first part must be an + # IPv4 address + if (is_ipv4(value[0]) or value[0].isdigit()) and value[1].isdigit(): + return True + return False + +if __name__ == '__main__': + if args.route_target: + if not is_valid_rt(args.route_target): + exit(1) + + elif args.route_target_multi: + for rt in args.route_target_multi.split(' '): + if not is_valid_rt(rt): + exit(1) + + else: + parser.print_help() + exit(1) + + exit(0) diff --git a/src/validators/bgp-route-target b/src/validators/bgp-route-target deleted file mode 100755 index e7e4d403f..000000000 --- a/src/validators/bgp-route-target +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2021 VyOS maintainers and contributors -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 or later as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -from argparse import ArgumentParser -from vyos.template import is_ipv4 - -parser = ArgumentParser() -group = parser.add_mutually_exclusive_group() -group.add_argument('--single', action='store', help='Validate and allow only one route-target') -group.add_argument('--multi', action='store', help='Validate multiple, whitespace separated route-targets') -args = parser.parse_args() - -def is_valid_rt(rt): - # every route target needs to have a colon and must consists of two parts - value = rt.split(':') - if len(value) != 2: - return False - # A route target must either be only numbers, or the first part must be an - # IPv4 address - if (is_ipv4(value[0]) or value[0].isdigit()) and value[1].isdigit(): - return True - return False - -if __name__ == '__main__': - if args.single: - if not is_valid_rt(args.single): - exit(1) - - elif args.multi: - for rt in args.multi.split(' '): - if not is_valid_rt(rt): - exit(1) - - else: - parser.print_help() - exit(1) - - exit(0) -- cgit v1.2.3 From f89d51293fb276fe64367b3019004c3e53a79821 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sat, 4 Dec 2021 14:51:19 +0100 Subject: bgp: T4042: bugfix route-distinguisher value range --- .../include/bgp/route-distinguisher.xml.i | 2 +- src/validators/bgp-rd-rt | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/validators') diff --git a/interface-definitions/include/bgp/route-distinguisher.xml.i b/interface-definitions/include/bgp/route-distinguisher.xml.i index 6d0aa3ef1..8bc5b452e 100644 --- a/interface-definitions/include/bgp/route-distinguisher.xml.i +++ b/interface-definitions/include/bgp/route-distinguisher.xml.i @@ -7,7 +7,7 @@ Route Distinguisher, (x.x.x.x:yyy|xxxx:yyyy) - ^((25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[0-9]?)(\.(25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[0-9]?)){3}|[0-9]{1,10}):[0-9]{1,5}$ + diff --git a/src/validators/bgp-rd-rt b/src/validators/bgp-rd-rt index 24b0043f9..b2b69c9be 100755 --- a/src/validators/bgp-rd-rt +++ b/src/validators/bgp-rd-rt @@ -19,29 +19,37 @@ from vyos.template import is_ipv4 parser = ArgumentParser() group = parser.add_mutually_exclusive_group() +group.add_argument('--route-distinguisher', action='store', help='Validate BGP route distinguisher') group.add_argument('--route-target', action='store', help='Validate one BGP route-target') group.add_argument('--route-target-multi', action='store', help='Validate multiple, whitespace separated BGP route-targets') args = parser.parse_args() -def is_valid_rt(rt): - # every route target needs to have a colon and must consists of two parts +def is_valid(rt): + """ Verify BGP RD/RT - both can be verified using the same logic """ + # every RD/RT (route distinguisher/route target) needs to have a colon and + # must consists of two parts value = rt.split(':') if len(value) != 2: return False - # A route target must either be only numbers, or the first part must be an - # IPv4 address + + # An RD/RT must either be only numbers, or the first part must be an IPv4 + # address if (is_ipv4(value[0]) or value[0].isdigit()) and value[1].isdigit(): return True return False if __name__ == '__main__': - if args.route_target: - if not is_valid_rt(args.route_target): + if args.route_distinguisher: + if not is_valid(args.route_distinguisher): + exit(1) + + elif args.route_target: + if not is_valid(args.route_target): exit(1) elif args.route_target_multi: for rt in args.route_target_multi.split(' '): - if not is_valid_rt(rt): + if not is_valid(rt): exit(1) else: -- cgit v1.2.3 From b6fbe6d3a5e8de4f90aa9fba61ca7491f9959ed0 Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Mon, 6 Dec 2021 09:03:52 +0000 Subject: validators: T4052: Fix for warn message in the validator script Validator expects variable "script" for the Warning message But it gets undeclared "path" --- src/validators/script | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/validators') diff --git a/src/validators/script b/src/validators/script index 1d8a27e5c..46e8a57b2 100755 --- a/src/validators/script +++ b/src/validators/script @@ -37,6 +37,6 @@ if __name__ == '__main__': # File outside the config dir is just a warning if not vyos.util.file_is_persistent(script): sys.exit( - f'Warning: file {path} is outside the / config directory\n' + f'Warning: file {script} is outside the "/config" directory\n' 'It will not be automatically migrated to a new image on system update' ) -- cgit v1.2.3 From 001cc6655f1864a46b573dae13c8f33bbf224239 Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Mon, 6 Dec 2021 10:13:32 +0000 Subject: validators: T4053: Fix exit code for script --- src/validators/script | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/validators') diff --git a/src/validators/script b/src/validators/script index 46e8a57b2..4ffdeb2a0 100755 --- a/src/validators/script +++ b/src/validators/script @@ -36,7 +36,7 @@ if __name__ == '__main__': # File outside the config dir is just a warning if not vyos.util.file_is_persistent(script): - sys.exit( + sys.exit(0)( f'Warning: file {script} is outside the "/config" directory\n' 'It will not be automatically migrated to a new image on system update' ) -- cgit v1.2.3 From 45d2429aa5d2ffafacdc5d9d00b7097169592427 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 7 Dec 2021 19:18:28 +0700 Subject: T3006: add a range validator --- src/validators/range | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 src/validators/range (limited to 'src/validators') diff --git a/src/validators/range b/src/validators/range new file mode 100755 index 000000000..d4c25f3c4 --- /dev/null +++ b/src/validators/range @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import re +import sys +import argparse + +class MalformedRange(Exception): + pass + +def validate_range(value, min=None, max=None): + try: + lower, upper = re.match(r'^(\d+)-(\d+)$', value).groups() + + lower, upper = int(lower), int(upper) + + if int(lower) > int(upper): + raise MalformedRange("the lower bound exceeds the upper bound".format(value)) + + if min is not None: + if lower < min: + raise MalformedRange("the lower bound must not be less than {}".format(min)) + + if max is not None: + if upper > max: + raise MalformedRange("the upper bound must not be greater than {}".format(max)) + + except (AttributeError, ValueError): + raise MalformedRange("range syntax error") + +parser = argparse.ArgumentParser(description='Range validator.') +parser.add_argument('--min', type=int, action='store') +parser.add_argument('--max', type=int, action='store') +parser.add_argument('value', action='store') + +if __name__ == '__main__': + args = parser.parse_args() + + try: + validate_range(args.value, min=args.min, max=args.max) + except MalformedRange as e: + print("Incorrect range '{}': {}".format(args.value, e)) + sys.exit(1) -- cgit v1.2.3 From c3471fe9d4cf0aab46feae94618925a95bcd5411 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 12 Dec 2021 22:34:05 +0100 Subject: validator: T4036: validate if multicast address is single (no netmask) --- src/validators/ipv4-multicast | 2 +- src/validators/ipv6-multicast | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/validators') diff --git a/src/validators/ipv4-multicast b/src/validators/ipv4-multicast index e5cbc9532..5465c728d 100755 --- a/src/validators/ipv4-multicast +++ b/src/validators/ipv4-multicast @@ -1,3 +1,3 @@ #!/bin/sh -ipaddrcheck --is-ipv4-multicast $1 +ipaddrcheck --is-ipv4-multicast $1 && ipaddrcheck --is-ipv4-single $1 diff --git a/src/validators/ipv6-multicast b/src/validators/ipv6-multicast index 66cd90c9c..5afc437e5 100755 --- a/src/validators/ipv6-multicast +++ b/src/validators/ipv6-multicast @@ -1,3 +1,3 @@ #!/bin/sh -ipaddrcheck --is-ipv6-multicast $1 +ipaddrcheck --is-ipv6-multicast $1 && ipaddrcheck --is-ipv6-single $1 -- cgit v1.2.3