Merge ~tiago.pasqualini/maas:lp1807725 into maas:master

Proposed by Tiago Pasqualini da Silva
Status: Superseded
Proposed branch: ~tiago.pasqualini/maas:lp1807725
Merge into: maas:master
Diff against target: 140 lines (+75/-3)
4 files modified
src/maasserver/models/staticipaddress.py (+9/-2)
src/maasserver/models/tests/test_staticipaddress.py (+45/-1)
src/maasserver/utils/dns.py (+10/-0)
src/maasserver/utils/tests/test_dns.py (+11/-0)
Reviewer Review Type Date Requested Status
MAAS Lander Needs Fixing
Björn Tillenius Pending
Review via email: mp+439090@code.launchpad.net

This proposal supersedes a proposal from 2022-12-02.

This proposal has been superseded by a proposal from 2023-03-17.

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote : Posted in a previous version of this proposal

UNIT TESTS
-b lp1807725 lp:~tiago.pasqualini/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/1547/consoleText
COMMIT: d5b537062408b0f05dbbe98bc95f21e4d542ca41

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote : Posted in a previous version of this proposal

UNIT TESTS
-b lp1807725 lp:~tiago.pasqualini/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/1549/consoleText
COMMIT: a3337f142376a14d3b1b300033e176fbadfbbbff

review: Needs Fixing
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote : Posted in a previous version of this proposal

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote : Posted in a previous version of this proposal

UNIT TESTS
-b lp1807725 lp:~tiago.pasqualini/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: a3337f142376a14d3b1b300033e176fbadfbbbff

review: Approve
Revision history for this message
Thorsten Merten (thorsten-merten) wrote : Posted in a previous version of this proposal

Thanks for the MP! This would fix the DNS resolution.

While we are at it, I have the feeling that a dot in the interface name (.) is also a bit weird to be taken over to a hostname as a dot has its own semantics in DNS.

Finally, it might be confusing to the user if we replace characters "at random" without a warning in the UI or the CLI in case that the interface name will be used as the host name.

We will need to check with UX before merging.

Revision history for this message
Björn Tillenius (bjornt) wrote : Posted in a previous version of this proposal

I think the patch looks fine as it is. Could you please add a test for this, though?

review: Needs Information
Revision history for this message
Tiago Pasqualini da Silva (tiago.pasqualini) wrote :

Added the tests, please let me know if that looks fine.

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp1807725 lp:~tiago.pasqualini/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/2161/consoleText
COMMIT: f2d97c69435a5968daf6ab84d0e6e39f80ac1c50

review: Needs Fixing

Unmerged commits

d3af974... by Tiago Pasqualini da Silva

Fix incorrect hostname from interface name

MAAS currently creates a DNS record for each interface in a host
by simply using its interface name. Whenever an interface has the
'_' character, the code uses it anyway, which is currently breaking
bind as this character is not allowed on domain names.
This patch fixes that by verifying it and replacing the incorrect
character.

LP: #1807725

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/models/staticipaddress.py b/src/maasserver/models/staticipaddress.py
2index b5794a1..81c7a82 100644
3--- a/src/maasserver/models/staticipaddress.py
4+++ b/src/maasserver/models/staticipaddress.py
5@@ -52,7 +52,10 @@ from maasserver.models.domain import Domain
6 from maasserver.models.subnet import Subnet
7 from maasserver.models.timestampedmodel import TimestampedModel
8 from maasserver.utils import orm
9-from maasserver.utils.dns import get_ip_based_hostname
10+from maasserver.utils.dns import (
11+ get_iface_name_based_hostname,
12+ get_ip_based_hostname,
13+)
14 from provisioningserver.utils.enum import map_enum_reverse
15
16 StaticIPAddress = TypeVar("StaticIPAddress")
17@@ -841,7 +844,11 @@ class StaticIPAddressManager(Manager):
18 # node, then consider adding the IP.
19 if result.assigned or not assigned_ips[result.fqdn]:
20 if result.ip not in mapping[result.fqdn].ips:
21- entry = mapping[f"{result.iface_name}.{result.fqdn}"]
22+ fqdn = "{}.{}".format(
23+ get_iface_name_based_hostname(result.iface_name),
24+ result.fqdn,
25+ )
26+ entry = mapping[fqdn]
27 entry.node_type = result.node_type
28 entry.system_id = result.system_id
29 if result.user_id is not None:
30diff --git a/src/maasserver/models/tests/test_staticipaddress.py b/src/maasserver/models/tests/test_staticipaddress.py
31index ee9c08a..08cdff1 100644
32--- a/src/maasserver/models/tests/test_staticipaddress.py
33+++ b/src/maasserver/models/tests/test_staticipaddress.py
34@@ -50,7 +50,10 @@ from maasserver.testing.testcase import (
35 MAASTransactionServerTestCase,
36 )
37 from maasserver.utils import orm
38-from maasserver.utils.dns import get_ip_based_hostname
39+from maasserver.utils.dns import (
40+ get_iface_name_based_hostname,
41+ get_ip_based_hostname,
42+)
43 from maasserver.utils.orm import reload_object, transactional
44 from maasserver.websockets.base import dehydrate_datetime
45
46@@ -483,6 +486,47 @@ class TestStaticIPAddressManagerMapping(MAASServerTestCase):
47 }
48 self.assertEqual(expected, mapping)
49
50+ def test_get_hostname_ip_mapping_sanitized_iface_name(self):
51+ hostname = factory.make_name("hostname")
52+ domainname = factory.make_name("domain")
53+ factory.make_Domain(name=domainname)
54+ full_hostname = f"{hostname}.{domainname}"
55+ subnet = factory.make_Subnet()
56+ node = factory.make_Node_with_Interface_on_Subnet(
57+ extra_ifnames=["eth_1"],
58+ hostname=full_hostname,
59+ interface_count=2,
60+ subnet=subnet,
61+ )
62+ boot_interface = node.get_boot_interface()
63+ staticip = factory.make_StaticIPAddress(
64+ alloc_type=IPADDRESS_TYPE.STICKY,
65+ ip=factory.pick_ip_in_Subnet(subnet),
66+ subnet=subnet,
67+ interface=boot_interface,
68+ )
69+ iface2 = node.current_config.interface_set.exclude(
70+ id=boot_interface.id
71+ ).first()
72+ sip2 = factory.make_StaticIPAddress(
73+ alloc_type=IPADDRESS_TYPE.STICKY,
74+ ip=factory.pick_ip_in_Subnet(subnet),
75+ subnet=subnet,
76+ interface=iface2,
77+ )
78+ mapping = StaticIPAddress.objects.get_hostname_ip_mapping(subnet)
79+ sanitized_if2name = get_iface_name_based_hostname(iface2.name)
80+ expected = {
81+ full_hostname: HostnameIPMapping(
82+ node.system_id, 30, {staticip.ip}, node.node_type
83+ ),
84+ "%s.%s"
85+ % (sanitized_if2name, full_hostname): HostnameIPMapping(
86+ node.system_id, 30, {sip2.ip}, node.node_type
87+ ),
88+ }
89+ self.assertEqual(expected, mapping)
90+
91 def make_mapping(self, node, raw_ttl=False):
92 if raw_ttl or node.address_ttl is not None:
93 ttl = node.address_ttl
94diff --git a/src/maasserver/utils/dns.py b/src/maasserver/utils/dns.py
95index 0c8cde5..76f553a 100644
96--- a/src/maasserver/utils/dns.py
97+++ b/src/maasserver/utils/dns.py
98@@ -91,6 +91,16 @@ def get_ip_based_hostname(ip):
99 return hostname
100
101
102+def get_iface_name_based_hostname(iface_name):
103+ """Given the specified interface name, creates an automatically generated
104+ hostname by converting the '_' characters in it to '-' characters.
105+
106+ :param iface_name: Input value for the interface name.
107+ """
108+ hostname = iface_name.replace("_", "-")
109+ return hostname
110+
111+
112 def validate_url(url, schemes=("http", "https")):
113 """Validator for URLs.
114
115diff --git a/src/maasserver/utils/tests/test_dns.py b/src/maasserver/utils/tests/test_dns.py
116index e6be8ab..863b8d9 100644
117--- a/src/maasserver/utils/tests/test_dns.py
118+++ b/src/maasserver/utils/tests/test_dns.py
119@@ -8,6 +8,7 @@ from django.core.validators import URLValidator
120 from testtools.matchers import Equals, HasLength
121
122 from maasserver.utils.dns import (
123+ get_iface_name_based_hostname,
124 get_ip_based_hostname,
125 validate_domain_name,
126 validate_hostname,
127@@ -244,3 +245,13 @@ class TestIpBasedHostnameGenerator(MAASTestCase):
128 get_ip_based_hostname("2001:67c:1562::15"),
129 Equals("2001-67c-1562--15"),
130 )
131+
132+
133+class TestIfaceBasedHostnameGenerator(MAASTestCase):
134+ def test_interface_name_changed(self):
135+ self.expectThat(
136+ get_iface_name_based_hostname("eth_0"), Equals("eth-0")
137+ )
138+
139+ def test_interface_name_unchanged(self):
140+ self.expectThat(get_iface_name_based_hostname("eth0"), Equals("eth0"))

Subscribers

People subscribed via source and target branches