Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 206 cname check #336

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Reject host names that are already in use as cnames.
Dmytro Karpenko committed Jun 24, 2019
commit bdcb30d33301565065ad4d0af91faac0a6a0099a
15 changes: 15 additions & 0 deletions mreg/api/v1/tests/tests.py
Original file line number Diff line number Diff line change
@@ -439,6 +439,14 @@ def test_hosts_post_409_conflict_name(self):
""""Posting a new host with a name already in use should return 409"""
self.assert_post_and_409('/hosts/', self.post_data_name)

def test_hosts_post_409_conflict_cname(self):
""""Posting a new host with a name already in use as cname should return 409"""
cname_data = {'name': 'host3.example.org', "host": self.host_one.id}
response = self.assert_post_and_201('/cnames/', cname_data)
conflicting_host_data = {'name': 'host3.example.org', "ipaddress": '127.0.0.3',
'contact': 'hostmaster@example.org'}
self.assert_post_and_409('/hosts/', conflicting_host_data)

def test_hosts_patch_204_no_content(self):
"""Patching an existing and valid entry should return 204 and Location"""
response = self.assert_patch_and_204('/hosts/%s' % self.host_one.name, self.patch_data)
@@ -468,6 +476,13 @@ def test_hosts_patch_409_conflict_name(self):
"""Patching an entry with a name that already exists should return 409"""
self.assert_patch_and_409('/hosts/%s' % self.host_one.name, {'name': self.host_two.name})

def test_hosts_patch_409_conflict_cname(self):
""""Patching an entry host with a name already in use as cname should return 409"""
cname_data = {'name': 'host3.example.org', "host": self.host_one.id}
response = self.assert_post_and_201('/cnames/', cname_data)
conflicting_host_data = {'name': 'host3.example.org'}
self.assert_patch_and_409('/hosts/%s' % self.host_one.name, conflicting_host_data)


class APIHostsTestCaseAsAdminuser(APIHostsTestCase):
"""Same tests as in APIHostsTestCase, only test as admin and not super"""
17 changes: 16 additions & 1 deletion mreg/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -43,6 +43,15 @@
from .zonefile import ZoneFile


def cname_conflict(cname):
try:
conflicting_cname = Cname.objects.get(name=cname)
# No exception, means such Cname exists
return True
except Cname.DoesNotExist:
return False


# These filtersets are used for applying generic filtering to all objects.
class CnameFilterSet(ModelFilterSet):
class Meta:
@@ -309,7 +318,10 @@ def post(self, request, *args, **kwargs):
if self.queryset.filter(name=request.data["name"]).exists():
content = {'ERROR': 'name already in use'}
return Response(content, status=status.HTTP_409_CONFLICT)

elif cname_conflict(request.data["name"]):
content = {'ERROR': 'name already in use as cname'}
return Response(content, status=status.HTTP_409_CONFLICT)

hostdata = request.data.copy()

if 'ipaddress' in hostdata:
@@ -358,6 +370,9 @@ def patch(self, request, *args, **kwargs):
if self.get_queryset().filter(name=request.data["name"]).exists():
content = {'ERROR': 'name already in use'}
return Response(content, status=status.HTTP_409_CONFLICT)
elif cname_conflict(request.data["name"]):
content = {'ERROR': 'name already in use as cname'}
return Response(content, status=status.HTTP_409_CONFLICT)

return super().patch(request, *args, **kwargs)