Skip to content

Commit

Permalink
Validates CIDR for security group rule input.
Browse files Browse the repository at this point in the history
Fixes bug 917983.

Change-Id: I57558ba4e3e3c7b0cc9f460a0a7522e4b4f0cfc7
  • Loading branch information
gabrielhurley committed Feb 29, 2012
1 parent b85d1aa commit b79a3f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Expand Up @@ -29,6 +29,7 @@
from horizon import api
from horizon import exceptions
from horizon import forms
from horizon.utils.validators import validate_ipv4_cidr


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -77,7 +78,8 @@ class AddRule(forms.SelfHandlingForm):
'data-icmp': _('Code')}))
cidr = forms.CharField(label=_("CIDR"),
help_text=_("Classless Inter-Domain Routing "
"(i.e. 192.168.0.0/24"))
"(i.e. 192.168.0.0/24"),
validators=[validate_ipv4_cidr])
# TODO (anthony) source group support
# group_id = forms.CharField()

Expand Down
42 changes: 42 additions & 0 deletions horizon/horizon/tests/utils_tests.py
@@ -0,0 +1,42 @@
# 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 horizon import test
from horizon.utils import validators


class ValidatorsTests(test.TestCase):
def test_validate_ipv4_cidr(self):
GOOD_CIDRS = ("192.168.1.1/16",
"192.0.0.1/17",
"0.0.0.0/16",
"10.144.11.107/4",
"255.255.255.255/0",
"0.1.2.3/16",
"0.0.0.0/32")
BAD_CIDRS = ("255.255.255.256",
"256.255.255.255",
"1.2.3.4.5",
"0.0.0.0",
"127.0.0.1/",
"127.0.0.1/33",
"127.0.0.1/-1",
"127.0.0.1/100")
for cidr in GOOD_CIDRS:
self.assertTrue(validators.ipv4_cidr_re.match(cidr))
for cidr in BAD_CIDRS:
self.assertFalse(validators.ipv4_cidr_re.match(cidr))
27 changes: 27 additions & 0 deletions horizon/horizon/utils/validators.py
@@ -0,0 +1,27 @@
# 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.

import re

from django.core import validators


ipv4_cidr_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)' # 0-255
'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}' # 3x .0-255
'/(3[0-2]|[1-2]?\d)$') # /0-32


validate_ipv4_cidr = validators.RegexValidator(ipv4_cidr_re)

0 comments on commit b79a3f7

Please sign in to comment.