def validate_cidr(self): if not self.cidr: return if not validate_cidr(self.cidr): raise ValidationError( 'Wrong cidr value. Expected cidr format: <0-255>.<0-255>.<0-255>.<0-255>/<0-32>' )
def validate(self, rule): """ Please note that validate function accepts rule object instead of validated data because it is used as a child of list serializer. """ protocol = rule.protocol from_port = rule.from_port to_port = rule.to_port cidr = rule.cidr if cidr and not validate_cidr(cidr): raise serializers.ValidationError({ 'cidr': _('Expected cidr format: <0-255>.<0-255>.<0-255>.<0-255>/<0-32>') }) if to_port is None: raise serializers.ValidationError({ 'to_port': _('Empty value is not allowed.') }) if from_port is None: raise serializers.ValidationError({ 'from_port': _('Empty value is not allowed.') }) if protocol == 'icmp': if from_port is not None and not -1 <= from_port <= 255: raise serializers.ValidationError({ 'from_port': _('Value should be in range [-1, 255], found %d') % from_port}) if to_port is not None and not -1 <= to_port <= 255: raise serializers.ValidationError({ 'to_port': _('Value should be in range [-1, 255], found %d') % to_port }) elif protocol in ('tcp', 'udp'): if from_port is not None and to_port is not None: if from_port > to_port: raise serializers.ValidationError(_('"from_port" should be less or equal to "to_port"')) if from_port is not None and from_port < 1: raise serializers.ValidationError({ 'from_port': _('Value should be in range [1, 65535], found %d') % from_port }) if to_port is not None and to_port < 1: raise serializers.ValidationError({ 'to_port': _('Value should be in range [1, 65535], found %d') % to_port }) else: raise serializers.ValidationError({ 'protocol': _('Value should be one of (tcp, udp, icmp), found %s') % protocol }) return rule
def check_cidr(target, default=None): while True: try: cidr = prompt("Enter %s: " % target, default) if validate_cidr(cidr): pass else: print "%s is not a valid CIDR." % cidr if int(cidr.split('/')[1]) > 27: print "%s is too small, use a larger network size." % cidr else: return cidr except Exception: pass
def check_cidr(target, default=None): while True: try: cidr = prompt("Enter %s: " % target, default) if validate_cidr(cidr): pass else: print "%s is not a valid CIDR." % cidr if int(cidr.split('/')[1]) > 27: print "%s is too small, use a larger network size." % cidr else: return cidr except: pass
def check_cidr(target, default=None): while True: try: cidr = prompt('Enter {}: '.format(target), default) if validate_cidr(cidr): pass else: print('{} is not a valid CIDR.'.format(cidr)) if int(cidr.split('/')[1]) > 27: print( '{} is too small, use a larger network size.'.format(cidr)) else: return cidr except Exception: pass
def main(): #grab arguments get_args = parse_args() check_prefix = validate_cidr(get_args.prefix) #check if prefix is valid, if it is not then do not proceed assert(check_prefix), 'Prefix is NOT a valid ipv4 prefix, Exiting...' #ask user for prefix length prefix_length = user_input(get_args.prefix.split('/')[1]) logging.info('Generating number of /{} that could fit in {}'.format(prefix_length,get_args.prefix)) # do not proceed further if user provided prefix is larger than prefix assert(prefix_length), 'Provided prefix length cannot be larger than {}'.format(get_args.prefix.split('/')[1]) #generate subnets generatesubnets = generate_subnets(get_args.prefix,int(prefix_length)) #generate table generatetable = generate_table(generatesubnets) print(generatetable)
def validate(self, rule): """ Please note that validate function accepts rule object instead of validated data because it is used as a child of list serializer. """ protocol = rule.protocol from_port = rule.from_port to_port = rule.to_port cidr = rule.cidr if cidr and not validate_cidr(cidr): raise serializers.ValidationError({ 'cidr': _('Expected cidr format: <0-255>.<0-255>.<0-255>.<0-255>/<0-32>' ) }) if to_port is None: raise serializers.ValidationError( {'to_port': _('Empty value is not allowed.')}) if from_port is None: raise serializers.ValidationError( {'from_port': _('Empty value is not allowed.')}) if protocol == 'icmp': if from_port is not None and not -1 <= from_port <= 255: raise serializers.ValidationError({ 'from_port': _('Value should be in range [-1, 255], found %d') % from_port }) if to_port is not None and not -1 <= to_port <= 255: raise serializers.ValidationError({ 'to_port': _('Value should be in range [-1, 255], found %d') % to_port }) elif protocol in ('tcp', 'udp'): if from_port is not None and to_port is not None: if from_port > to_port: raise serializers.ValidationError( _('"from_port" should be less or equal to "to_port"')) if from_port is not None and from_port < 1: raise serializers.ValidationError({ 'from_port': _('Value should be in range [1, 65535], found %d') % from_port }) if to_port is not None and to_port < 1: raise serializers.ValidationError({ 'to_port': _('Value should be in range [1, 65535], found %d') % to_port }) elif protocol == '': # See also: https://github.com/openstack/neutron/blob/af130e79cbe5d12b7c9f9f4dcbcdc8d972bfcfd4/neutron/db/securitygroups_db.py#L500 if from_port != -1: raise serializers.ValidationError({ 'from_port': _('Port range is not supported if protocol is not specified.' ) }) if to_port != -1: raise serializers.ValidationError({ 'to_port': _('Port range is not supported if protocol is not specified.' ) }) else: raise serializers.ValidationError({ 'protocol': _('Value should be one of (tcp, udp, icmp), found %s') % protocol }) return rule
def validate_network(network): return validate_cidr(network)
def validate_prefix(ip): # validate both prefix and prefix length return validate_cidr(ip)