def test_whitelist(self): WhitelistEntry(who=self.user, why='test', cidr='141.142.0.0/16').save() self.assertEqual(bool(is_whitelisted("1.2.3.4")), False) self.assertEqual(bool(is_whitelisted("1.2.3.0/24")), False) self.assertEqual(bool(is_whitelisted("141.142.2.2")), True) self.assertEqual(bool(is_whitelisted("141.142.4.0/24")), True) self.assertEqual(bool(is_whitelisted("141.0.0.0/8")), True)
def check_whitelist(cleaned_data): cidr = cleaned_data.get('cidr') source = cleaned_data.get('source') skip_whitelist = cleaned_data.get('skip_whitelist') if cidr and not skip_whitelist: item = is_whitelisted(cidr) if item: raise forms.ValidationError("whitelisted: %s: %s" % (item.who, item.why)) if is_prefixlen_too_small(cidr): raise forms.ValidationError("Prefix length in %s is too small" % cidr) item = is_source_blacklisted(source) if item: raise forms.ValidationError("Source %s is blacklisted: %s: %s" % (source, item.who, item.why)) return cleaned_data
def validate(self, attrs): if attrs.get('duration') and attrs.get('unblock_at'): raise serializers.ValidationError("Specify only one of duration and unblock_at") cidr = attrs.get('cidr') source = attrs.get('source') skip_whitelist = attrs.get('skip_whitelist') if cidr and not skip_whitelist: item = is_whitelisted(cidr) if item: raise serializers.ValidationError("whitelisted: %s: %s" % (item.who, item.why)) if is_prefixlen_too_small(cidr): raise serializers.ValidationError("Prefix length in %s is too small" % cidr) item = is_source_blacklisted(source) if item: raise serializers.ValidationError("Source %s is blacklisted: %s: %s" % (source, item.who, item.why)) return attrs
def validate(self, attrs): if attrs.get('duration') and attrs.get('unblock_at'): raise serializers.ValidationError( "Specify only one of duration and unblock_at") cidr = attrs.get('cidr') source = attrs.get('source') skip_whitelist = attrs.get('skip_whitelist') if cidr and not skip_whitelist: item = is_whitelisted(cidr) if item: raise serializers.ValidationError("whitelisted: %s: %s" % (item.who, item.why)) if is_prefixlen_too_small(cidr): raise serializers.ValidationError( "Prefix length in %s is too small" % cidr) item = is_source_blacklisted(source) if item: raise serializers.ValidationError( "Source %s is blacklisted: %s: %s" % (source, item.who, item.why)) return attrs