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

Prevent assigning of multiple IP addresses to interface #769

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions web/blueprints/host/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,15 @@ def interface_edit(interface_id: int) -> ResponseReturnValue:

form = InterfaceForm(obj=interface)
form.meta.current_mac = interface.mac
form.ips.choices = [(str(ip), str(ip)) for ip in current_ips + unused_ips]
form.ip.choices = [("None", "None")] + [(str(ip), str(ip)) for ip in current_ips + unused_ips]
if current_ips:
form.ip.default = str(current_ips[0])

if len(current_ips) > 1:
flash(
"Bearbeite Interface mit mehreren IP-Adressen. Beim Übernehmen, werden alle IP-Adressen außer der ausgewählten entfernt!",
"warning",
)

def default_response() -> ResponseReturnValue:
form_args = {
Expand All @@ -264,16 +272,18 @@ def default_response() -> ResponseReturnValue:
form_args=form_args)

if not form.is_submitted():
form.ips.process_data(ip for ip in current_ips)
form.ip.process_data(str(current_ips[0]) if current_ips else "None")
return default_response()
if not form.validate():
return default_response()

ips = {IPAddress(ip) for ip in form.ips.data}

with abort_on_error(default_response), session.session.begin_nested():
lib_host.interface_edit(
interface, form.name.data, form.mac.data, ips, processor=current_user
interface,
form.name.data,
form.mac.data,
[IPAddress(ip)] if (ip := form.ip.data) else [],
processor=current_user,
)
session.session.commit()

Expand Down
6 changes: 4 additions & 2 deletions web/blueprints/host/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask_wtf import FlaskForm as Form
from wtforms.validators import Optional
from wtforms_widgets.fields.core import TextField, SelectMultipleField
from wtforms_widgets.fields.core import TextField, SelectField
from wtforms_widgets.fields.custom import MacField
from wtforms_widgets.fields.validators import MacAddress

Expand All @@ -22,4 +22,6 @@ class InterfaceForm(Form):
description="z.B. eth0, en0 oder enp0s29u1u1u5",
validators=[Optional()])
mac = MacField("MAC", [MacAddress(message="MAC ist ungültig!"), UniqueMac(annex_field=None)])
ips = SelectMultipleField("IPs", validators=[Optional()])
ip = SelectField(
"IP", validators=[Optional()], coerce=lambda value: None if value == "None" else value
)
Loading