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

Fix hostname validation #181

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions promgen/forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

import re

from dateutil import parser

from promgen import models, plugins, prometheus, validators

from django import forms
from django.core.exceptions import ValidationError


class ImportConfigForm(forms.Form):
Expand Down Expand Up @@ -163,6 +166,21 @@ class Meta:
class HostForm(forms.Form):
hosts = forms.CharField(widget=forms.Textarea)

def clean(self):
# Hosts may be submitted delimiated by either , or newline so
# once we split on that, we want to make sure there are no invalid
# hostnames
hosts = set()
for hostname in re.split("[,\s]+", self.cleaned_data["hosts"]):
if hostname == "":
continue
if ":" in hostname:
raise ValidationError("Invalid hostname %s" % hostname)
hosts.add(hostname)
if not hosts:
raise ValidationError("No valid hosts")
self.cleaned_data["hosts"] = list(hosts)


LabelFormset = forms.inlineformset_factory(
models.Rule,
Expand Down
22 changes: 9 additions & 13 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,31 +948,27 @@ def form_valid(self, form):

class HostRegister(LoginRequiredMixin, FormView):
model = models.Host
template_name = 'promgen/host_form.html'
template_name = "promgen/host_form.html"
form_class = forms.HostForm

def get_context_data(self, **kwargs):
context = super(HostRegister, self).get_context_data(**kwargs)
context['farm'] = get_object_or_404(models.Farm, id=self.kwargs['pk'])
context['project'] = context['farm'].project_set.first()
context["farm"] = get_object_or_404(models.Farm, pk=self.kwargs["pk"])
context["project"] = context["farm"].project_set.first()
return context

def form_valid(self, form):
farm = get_object_or_404(models.Farm, id=self.kwargs['pk'])
for hostname in re.split('[,\s]+', form.clean()['hosts']):
if hostname == '':
continue

farm = get_object_or_404(models.Farm, id=self.kwargs["pk"])
for hostname in form.cleaned_data["hosts"]:
host, created = models.Host.objects.get_or_create(
name=hostname,
farm_id=farm.id,
name=hostname, farm_id=farm.id
)
if created:
logger.debug('Added %s to %s', host.name, farm.name)
logger.debug("Added %s to %s", host.name, farm.name)

if farm.project_set.count() == 0:
return HttpResponseRedirect(reverse('farm-detail', args=[farm.id]))
return HttpResponseRedirect(reverse('project-detail', args=[farm.project_set.first().id]))
return redirect("farm-detail", pk=farm.id)
return redirect("project-detail", pk=farm.project_set.first().id)


class ApiConfig(View):
Expand Down