def test_cleanup_cname(self): # Make sure CNAME record block c = Domain(name='foo1') c.save() self.assertFalse(c.purgeable) f_c = create_fake_zone('foo.foo1', suffix="") self.assertEqual(f_c.name, 'foo.foo1') self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1")) self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1")) self.assertFalse(Domain.objects.filter(name="z.foo.foo1")) self.assertTrue(Domain.objects.filter(name="foo.foo1")) self.assertFalse(f_c.purgeable) fqdn = "cname.x.y.z.foo.foo1" label, the_domain = ensure_label_domain(fqdn) cname = CNAME(label=label, domain=the_domain, target="foo") cname.save() self.assertFalse(prune_tree(the_domain)) cname.delete() self.assertFalse(Domain.objects.filter(name="x.y.z.foo.foo1")) self.assertFalse(Domain.objects.filter(name="y.z.foo.foo1")) self.assertFalse(Domain.objects.filter(name="z.foo.foo1")) fqdn = "bar.x.y.z.foo.poo" self.assertTrue(Domain.objects.filter(name="foo.foo1"))
def test_remove_has_child_records(self): Domain(name='com').save() f_c = Domain(name='foo.com') f_c.save() cn = CNAME(domain=f_c, label="no", target="asdf") cn.full_clean() cn.save() self.assertRaises(ValidationError, f_c.delete)
def test_basic_add_remove6(self): # Make sure CNAME record block f_c = create_fake_zone("foo.foo1", suffix="") f_c.save() self.assertFalse(f_c.purgeable) fqdn = "cname.x.y.z.foo.foo1" label, the_domain = ensure_label_domain(fqdn) cname = CNAME(label=label, domain=the_domain, target="foo") cname.save() self.assertFalse(prune_tree(the_domain)) cname.delete()
def test_delete_with_cname_pointing_to_a(self): label = 'foo100' a = AddressRecord(label=label, domain=self.o_e, ip_str= '128.193.1.10', ip_type='4') a.clean() a.save() cn = CNAME(label="foomom", domain=self.o_e, target=label + "." + self.o_e.name) cn.clean() cn.save() self.assertRaises(ValidationError, a.delete) a.delete(check_cname=False)
def test_delegation(self): name = "boom" dom = Domain(name=name, delegated=True) dom.save() # Creating objects in the domain should be locked. arec = AddressRecord(label="ns1", domain=dom, ip_str="128.193.99.9", ip_type='4') self.assertRaises(ValidationError, arec.save) ns = Nameserver(domain=dom, server="ns1." + dom.name) self.assertRaises(ValidationError, ns.save) cn = CNAME(label="999asdf", domain=dom, target="asdf.asdf") self.assertRaises(ValidationError, cn.full_clean) # Undelegate (unlock) the domain. dom.delegated = False dom.save() # Add glue and ns record. arec.save() ns.save() # Re delegate the domain. dom.delegated = True dom.save() # Creation should still be locked arec1 = AddressRecord(label="ns2", domain=dom, ip_str="128.193.99.9", ip_type='4') self.assertRaises(ValidationError, arec1.save) cn1 = CNAME(label="1000asdf", domain=dom, target="asdf.asdf") self.assertRaises(ValidationError, cn1.full_clean) # Editing should be allowed. arec = AddressRecord.objects.get(pk=arec.pk) arec.ip_str = "129.193.88.2" arec.save() # Adding new A records that have the same name as an NS should # be allowed. arec1 = AddressRecord(label="ns1", domain=dom, ip_str="128.193.100.10", ip_type='4') arec1.save()
def test_basic_add_remove7(self): # try a star record f_c = create_fake_zone("foo.foo2", suffix="") f_c.save() self.assertFalse(f_c.purgeable) fqdn = "*.x.y.z.foo.foo2" label, the_domain = ensure_label_domain(fqdn) self.assertEqual('*', label) cname = CNAME(label=label, domain=the_domain, target="foo") cname.save() self.assertFalse(prune_tree(the_domain)) cname.delete()
def do_add(self, label, domain, data): cn = CNAME(label=label, domain=domain, target=data) cn.full_clean() cn.save() cn.save() self.assertTrue(cn.get_absolute_url()) self.assertTrue(cn.get_edit_url()) self.assertTrue(cn.get_delete_url()) self.assertTrue(cn.details()) cs = CNAME.objects.filter( label=label, domain=domain, target=data) self.assertEqual(len(cs), 1) return cn
def test_existing_cname_new_domain(self): name = "bo" b_dom, _ = Domain.objects.get_or_create(name=name, delegated=False) name = "to.bo" t_dom, _ = Domain.objects.get_or_create(name=name, delegated=False) cn = CNAME(domain=t_dom, label="no", target="asdf") cn.full_clean() cn.save() name = "no.to.bo" n_dom = Domain(name=name, delegated=False) self.assertRaises(ValidationError, n_dom.save)
def migrate_CNAME(zone, root_domain, soa, views): for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'): name = name.to_text().strip('.') print str(name) + " CNAME " + str(rdata) exists_domain = Domain.objects.filter(name=name) if exists_domain: label = '' domain = exists_domain[0] else: label = name.split('.')[0] domain_name = name.split('.')[1:] domain = ensure_domain('.'.join(domain_name), force=True) data = rdata.target.to_text().strip('.') if CNAME.objects.filter(label=label, domain=domain, target=data).exists(): cn = CNAME.objects.get(label=label, domain=domain, target=data) else: cn = CNAME(label=label, domain=domain, target=data, description=rdata.comment, ttl=ttl) cn.full_clean() cn.save() for view in views: cn.views.add(view) cn.save()
def test_cname_point_to_itself(self): label = "foopy" data = "foopy.what.cd" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.clean)
def migrate_CNAME(zone, root_domain, soa, views): for (name, ttl, rdata) in zone.iterate_rdatas('CNAME'): name = name.to_text().strip('.') print str(name) + " CNAME " + str(rdata) exists_domain = Domain.objects.filter(name=name) if exists_domain: label = '' domain = exists_domain[0] else: label = name.split('.')[0] domain_name = name.split('.')[1:] domain = ensure_domain('.'.join(domain_name), force=True) data = rdata.target.to_text().strip('.') if CNAME.objects.filter(label=label, domain=domain, target=data).exists(): cn = CNAME.objects.get( label=label, domain=domain, target=data ) else: cn = CNAME( label=label, domain=domain, target=data, description=rdata.comment, ttl=ttl ) cn.full_clean() cn.save() for view in views: cn.views.add(view) cn.save()
def test_no_ns_in_view(self): root_domain = create_fake_zone("asdfdjhjd") ns = root_domain.nameserver_set.all()[0] cn = CNAME(label='asdf', domain=root_domain, target='test.com') cn.full_clean() cn.save() cn.views.add(self.public_view) self.assertTrue(ns.domain.soa == cn.domain.soa) # We now should have a nameserver and a cname in the public view. The # nameserver should not be allowed to disable it's public view # Try to remove the public view self.assertTrue(self.public_view in ns.views.all()) self.assertTrue(self.private_view in ns.views.all()) post_data = self.update_rdtype(self.post_data()) post_data['domain'] = ns.domain.pk post_data['views'] = [self.private_view.pk] post_data['record_pk'] = ns.pk resp = self.c.post('/en-US/mozdns/record/record_ajax/', data=post_data) self.assertEqual(resp.status_code, 200) # Make sure it's still there ns = Nameserver.objects.get(pk=ns.pk) # fetch # Make sure the view is still there # The clean method should prevent it from being deleted self.assertTrue(self.public_view in ns.views.all()) # Try to remove the private view # This should be allowed self.assertTrue(self.public_view in ns.views.all()) post_data = self.update_rdtype(self.post_data()) post_data['views'] = [self.public_view.pk] post_data['record_pk'] = ns.pk resp = self.c.post('/en-US/mozdns/record/record_ajax/', data=post_data) self.assertEqual(resp.status_code, 200) # Make sure it's still there ns = Nameserver.objects.get(pk=ns.pk) # fetch # Make sure the view is still there # The clean method should prevent it from being deleted self.assertTrue(self.private_view not in ns.views.all())
def test_ns_exists(self): # Duplicate test? data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec = Nameserver(domain=dom, server="asdf1") rec.save() cn = CNAME(label='', domain=dom, target=data) self.assertRaises(ValidationError, cn.clean)
def test_txt_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = TXT.objects.get_or_create( label=label, domain=dom, txt_data="asdf") cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_address_record_exists_upper_case(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = AddressRecord.objects.get_or_create( label=label, domain=dom, ip_type='4', ip_str="128.193.1.1") cn = CNAME(label=label.title(), domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_integration3_zone(self): root_domain = create_fake_zone("wee3.wee.mozilla.com", "") res, error = compile_to_django("zone=:wee3.wee.mozilla.com") self.assertFalse(error) self.assertEqual(len(res['SOA']), 1) self.assertEqual(len(res['NS']), 1) cn = CNAME(label="host1", domain=root_domain, target="whop.whop") cn.save() res, error = compile_to_django("zone=:wee3.wee.mozilla.com host1") self.assertFalse(error) self.assertEqual(len(res['SOA']), 0) self.assertEqual(len(res['NS']), 0) self.assertEqual(len(res['CNAME']), 1) res, error = compile_to_django("zone=:wee3.wee.mozilla.com " "type=:CNAME") self.assertFalse(error) self.assertEqual(len(res['SOA']), 0) self.assertEqual(len(res['NS']), 0) self.assertEqual(len(res['CNAME']), 1)
def test_basic_add_remove8(self): # Make sure a record's label is changed to '' when a domain with the # same name as it's fqdn is created. f_c = create_fake_zone("foo.foo3", suffix="") f_c.save() self.assertFalse(f_c.purgeable) fqdn = "www.x.y.z.foo.foo3" label, the_domain = ensure_label_domain(fqdn) self.assertEqual('www', label) self.assertEqual('x.y.z.foo.foo3', the_domain.name) self.assertTrue(the_domain.pk) cname = CNAME(label=label, domain=the_domain, target="foo") cname.save() fqdn = "*.www.x.y.z.foo.foo3" label2, the_domain2 = ensure_label_domain(fqdn) cname = CNAME.objects.get(fqdn=cname.fqdn) self.assertEqual('', cname.label) self.assertEqual('www.x.y.z.foo.foo3', cname.domain.name) self.assertEqual('*', label2) self.assertEqual('www.x.y.z.foo.foo3', the_domain2.name)
def test_sreg_exists(self): label = "tdfestyfoo" data = "waasdft" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") StaticReg.objects.create( label=label, domain=dom, ip_str="10.0.0.1", ip_type='4', system=self.s) cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_mx_exists(self): label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = MX.objects.get_or_create( label=label, domain=dom, server="asdf", priority=123, ttl=123) cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_srv_exists(self): label = "_testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec, _ = SRV.objects.get_or_create( label=label, domain=dom, target="asdf", port=2, priority=2, weight=4) cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_add_mx_with_cname(self): label = "cnamederp1" domain = self.c_g data = "foo.com" fqdn = label + '.' + domain.name mx_data = {'label': '', 'domain': self.c_g, 'server': fqdn, 'priority': 2, 'ttl': 2222} mx = MX(**mx_data) mx.save() cn = CNAME(label=label, domain=domain, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_ptr_exists(self): # No unittest.skip in 2.6 print "see BUG https://bugzilla.mozilla.org/show_bug.cgi?id=810106" return label = "testyfoo" data = "wat" dom, _ = Domain.objects.get_or_create(name="cd") dom, _ = Domain.objects.get_or_create(name="what.cd") rec = PTR(ip_str="10.193.1.1", ip_type='4', name='testyfoo.what.cd') rec.full_clean() rec.save() cn = CNAME(label=label, domain=dom, target=data) self.assertRaises(ValidationError, cn.full_clean)
def test_add_with_cname(self): label = "cnamederp" domain = self.o_e data = "foo.com" cn = CNAME(label=label, domain=domain, target=data) cn.full_clean() cn.save() data = {'label': '', 'domain': self.o_e, 'server': 'cnamederp.oregonstate.org', 'priority': 2, 'ttl': 2222} mx = MX(**data) self.assertRaises(ValidationError, mx.save)
def test_bad_nameserver_soa_state_case_1_0(self): # This is Case 1 root_domain = create_fake_zone("asdf10") for ns in root_domain.nameserver_set.all(): ns.delete() # At his point we should have a domain at the root of a zone with no # other records in it. # Adding a record shouldn't be allowed because there is no NS record on # the zone's root domain. a = AddressRecord(label='', domain=root_domain, ip_type="6", ip_str="1::") self.assertRaises(ValidationError, a.save) cn = CNAME(label='', domain=root_domain, target="asdf") self.assertRaises(ValidationError, cn.save)
def get_bulk_action_list(cls, query, fields=None, show_related=True): if not fields: fields = cls.get_api_fields() + ['pk', 'name'] # views is a M2M relationship and won't show up correctley in # values_list fields.remove('views') if show_related: # StaticReg objects are serialized. All other fields # are not serialized into JSON fields += ['system__hostname'] # Pull in all system blobs and tally which pks we've seen. In one swoop # pull in all staticreg blobs and put them with their systems. sreg_q = cls.objects.filter(query) sreg_t_bundles = sreg_q.values_list(*fields) sreg_fqdns = sreg_q.values_list('fqdn', flat=True) cname_q = Q(target__in=sreg_fqdns) cname_bundles = CNAME.get_bulk_action_list(cname_q) d_bundles = {} for t_bundle in sreg_t_bundles: d_bundle = dict(zip(fields, t_bundle)) d_bundle['keyvalue_set'] = create_key_index( cls.keyvalue_set.related.model.objects.filter( obj=d_bundle['pk'] ).values('key', 'value', 'pk') ) d_bundle['views'] = list( cls.objects.get(pk=d_bundle['pk']) .views.values_list('pk', flat=True) ) d_bundles[d_bundle['pk']] = d_bundle # Look up a cname and set it as the only value of 'cname' if *one* # cname exists. If there are more than one set the cname as a list. # cname_bundles is a inverted index (dict) on 'target' if d_bundle['fqdn'] in cname_bundles: d_bundle['cname'] = cname_bundles[d_bundle['fqdn']] return d_bundles
def import_cname(sreg, blobs): if not isinstance(blobs, list): raise BadImportData( bad_blob=blobs, msg='The cname attribute should be a list of CNAME blobs') save_functions = [] for blob in blobs: if 'pk' in blob: try: cname = CNAME.objects.get(pk=blob['pk']) save_functions += cname_update(cname, blob) except CNAME.DoesNotExist: raise BadImportData( bad_blob=blob, msg='Could not find the CNAME with primary key ' '{0}.'.format(blob['pk'])) else: recurse_confirm_no_pk(blob) save_functions += cname_update(CNAME(), blob) return save_functions
def test_delete_with_cname_pointing_to_a(self): label = 'foo100' a = AddressRecord(label=label, domain=self.o_e, ip_str='128.193.1.10', ip_type='4') a.clean() a.save() cn = CNAME(label="foomom", domain=self.o_e, target=label + "." + self.o_e.name) cn.clean() cn.save() self.assertRaises(ValidationError, a.delete) a.delete(check_cname=False)
def get_bulk_action_list(cls, query, fields=None, show_related=True): if not fields: fields = cls.get_api_fields() + ['pk', 'name'] # views is a M2M relationship and won't show up correctley in # values_list fields.remove('views') if show_related: # StaticReg objects are serialized. All other fields # are not serialized into JSON fields += ['system__hostname'] # Pull in all system blobs and tally which pks we've seen. In one swoop # pull in all staticreg blobs and put them with their systems. sreg_q = cls.objects.filter(query) sreg_t_bundles = sreg_q.values_list(*fields) sreg_fqdns = sreg_q.values_list('fqdn', flat=True) cname_q = Q(target__in=sreg_fqdns) cname_bundles = CNAME.get_bulk_action_list(cname_q) d_bundles = {} for t_bundle in sreg_t_bundles: d_bundle = dict(zip(fields, t_bundle)) d_bundle['keyvalue_set'] = create_key_index( cls.keyvalue_set.related.model.objects.filter( obj=d_bundle['pk']).values('key', 'value', 'pk')) d_bundle['views'] = list( cls.objects.get(pk=d_bundle['pk']).views.values_list( 'pk', flat=True)) d_bundles[d_bundle['pk']] = d_bundle # Look up a cname and set it as the only value of 'cname' if *one* # cname exists. If there are more than one set the cname as a list. # cname_bundles is a inverted index (dict) on 'target' if d_bundle['fqdn'] in cname_bundles: d_bundle['cname'] = cname_bundles[d_bundle['fqdn']] return d_bundles
def test_cname_update(self): cname = CNAME(target="foo") self.generic_check(cname)
class Meta: always_return_data = True queryset = CNAME.objects.all() fields = CNAME.get_api_fields() + ['views'] authorization = Authorization() allowed_methods = allowed_methods
class Meta: queryset = CNAME.objects.all() fields = CNAME.get_api_fields() + ['domain', 'views'] authorization = Authorization() allowed_methods = ['get', 'post', 'patch'] validation = FormValidation(form_class=CNAMEForm)