Check minimum size of networks on creation
authorHelga Velroyen <helgav@google.com>
Mon, 21 Jan 2013 15:20:34 +0000 (16:20 +0100)
committerHelga Velroyen <helgav@google.com>
Wed, 10 Apr 2013 15:45:58 +0000 (17:45 +0200)
When creating a network, so far no size constraints were checked.
We now limit the size of a network to a /30 or bigger, although
tecnically, the ipaddr library supports even /32 networks.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

lib/network.py

index a1c51c5..170a8b7 100644 (file)
@@ -29,6 +29,9 @@ from bitarray import bitarray
 
 from ganeti import errors
 
+IPV4_NETWORK_MIN_SIZE = 30
+IPV4_NETWORK_MIN_NUM_HOSTS = 2 ** (32 - IPV4_NETWORK_MIN_SIZE)
+
 
 class AddressPool(object):
   """Address pool class, wrapping an C{objects.Network} object.
@@ -55,6 +58,12 @@ class AddressPool(object):
     self.net = network
 
     self.network = ipaddr.IPNetwork(self.net.network)
+    if self.network.numhosts < IPV4_NETWORK_MIN_NUM_HOSTS:
+      raise errors.AddressPoolError("A network with only %s host(s) is too"
+                                    " small, please specify at least a /%s"
+                                    " network" %
+                                    (str(self.network.numhosts),
+                                     IPV4_NETWORK_MIN_SIZE))
     if self.net.gateway:
       self.gateway = ipaddr.IPAddress(self.net.gateway)