aabccb9151770cdd93bc37b4ae89c4fb0bb5aa14
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / api / validation.py
1 #!/usr/bin/python
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import logging
18 import re
19
20 from netaddr import IPAddress
21 from netaddr import IPNetwork
22 from netaddr import IPRange
23 from netaddr import AddrFormatError
24
25 from cmdatahandlers.api import configerror
26
27
28 class ValidationError(configerror.ConfigError):
29     def __init__(self, description):
30         configerror.ConfigError.__init__(self, ' Validation error: %s' % description)
31
32
33
34 class ValidationUtils:
35
36     def validate_ip_address(self, addr):
37         try:
38             IPAddress(addr)
39         except AddrFormatError as exc:
40             raise ValidationError("Invalid ip address: {0}".format(exc))
41
42
43     def validate_subnet_address(self, cidr):
44         try:
45             net = IPNetwork(cidr)
46         except AddrFormatError as exc:
47             raise ValidationError('Invalid subnet address: {0}'.format(exc))
48         #it seems ipnetwork compress ipv6 cidr.
49         #skip this for ipv6
50         if net.version == 4:
51             if cidr != str(net.cidr):
52                raise ValidationError('Given CIDR %s is not equal to network CIDR %s'
53                                   % (cidr, str(net.cidr)))
54
55
56     def validate_ip_range(self, start_ip, end_ip):
57         try:
58             IPRange(start_ip, end_ip)
59         except AddrFormatError as exc:
60             raise ValidationError('Invalid ip range: {0}'.format(exc))
61
62
63     def validate_ip_in_subnet(self, ip_addr, cidr):
64         self.validate_ip_address(ip_addr)
65         ip = IPAddress(ip_addr)
66
67         self.validate_subnet_address(cidr)
68         subnet = IPNetwork(cidr)
69         if not ip in subnet or ip == subnet.ip  or ip == subnet.broadcast:
70             raise ValidationError('IP %s is not a valid address in subnet %s' % (ip_addr, cidr))
71
72
73     def validate_ip_in_range(self, addr, start, end):
74         self.validate_ip_address(addr)
75         self.validate_ip_range(start, end)
76         if IPAddress(addr) not in IPRange(start, end):
77             raise ValidationError('IP %s is not in range %s - %s' % (addr, start, end))
78
79
80     def validate_ip_not_in_range(self, addr, start, end):
81         self.validate_ip_address(addr)
82         self.validate_ip_range(start, end)
83         if IPAddress(addr) in IPRange(start, end):
84             raise ValidationError('IP %s is in reserved range %s - %s' % (addr, start, end))
85
86
87     def validate_username(self, user):
88         if not re.match('^[a-zA-Z][\da-zA-Z-_]+[\da-zA-Z]$', user):
89             raise ValidationError('Invalid user name %s' % user)