summaryrefslogtreecommitdiff
path: root/src/validators/vrf-name
blob: 878893c46656e24ed3b227d75814ead18f3843eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
#
# Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.


import re
import sys


if __name__ == '__main__':
    if len(sys.argv) != 2:
        sys.exit(1)

    vrf = sys.argv[1]
    length = len(vrf)

    if length not in range(1, 17):
        sys.exit('VRF instance name must be 16 characters or less')

    # Treat loopback interface "lo" explicitly. Adding "lo" explicitly to the
    # following regex pattern would deny any VRF name starting with lo - thuse
    # local-vrf would be illegal - and that we do not want.
    if vrf == "lo":
        exit(f'"{vrf}" is invalid as VRF name as it is an interface name')

    pattern = "^(?!(bond|br|dum|eth|lan|eno|ens|enp|enx|gnv|ipoe|l2tp|l2tpeth|" \
              "vtun|ppp|pppoe|peth|tun|vti|vxlan|wg|wlan|wlm)\d+(\.\d+(v.+)?)?$).*$"
    if not re.match(pattern, vrf):
        sys.exit(f'"{vrf}" is invalid as VRF name as it is an interface name')

    sys.exit(0)