tuskar-ui/horizon/utils/validators.py
Tihomir Trifonov cb48cebefc Implementation of blueprint ip-validation
First draft. Added a forms.Field wrapper for IPAddress.
Implemented IPv4 and IPv6 checks, subnet mask range,
optional mask range limitation.

As far as I see now, there is only 1 place in Dashboard
to accept IP fields as input - the Security rules.
I've tried to input IPv6 rule and it was accepted.
The previous version of the code doesn't accept
IPv6, only IPv4. I am not sure if IPv6 should be
accepted here. It however works.

Patch set 3: Now using netaddr library(used also by nova),
which provides support for validation of IP addresses.
Using this library, now the IPField can support more
ways to enter an IP - like short versions:
10/8 - for all 10.xxx.xxx.xxx
192.168/16 - for all 192.168.xxx.xxx

Regarding IPy library - it performs some strict
subnet validation, which will not accept cidr like this:
192.168.1.1/20
because the only mask that matches this IP is 32.
IPy doesn't allow broader masks. But my assumption is
that the operators should take the responsibility for
the data they enter. At least this CIDR is valid after all.

Change-Id: Ie497fe65fde3af25a18109a182ab78255ad7ec60
2012-05-11 00:23:44 +03:00

36 lines
1.2 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
horizon_config = getattr(settings, "HORIZON_CONFIG", {})
password_config = horizon_config.get("password_validator", {})
def validate_port_range(port):
if port not in range(-1, 65536):
raise ValidationError("Not a valid port number")
def password_validator():
return password_config.get("regex", ".*")
def password_validator_msg():
return password_config.get("help_text", _("Password is not accepted"))