Skip to content

Commit b45d66a

Browse files
masihkhatibzadeh99masih.khatibzdeh
andauthored
fix: Ensure fqdn is treated as string in get_hostname_fqdn (canonical#5993)
Explicitly cast fqdn to a string before processing. Fixes canonicalGH-5989 Co-authored-by: masih.khatibzdeh <masih.khatibzadeh@snapp.cab>
1 parent b38ab5f commit b45d66a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

cloudinit/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ def get_hostname_fqdn(cfg, cloud, metadata_only=False):
12151215
is_default = False
12161216
if "fqdn" in cfg:
12171217
# user specified a fqdn. Default hostname then is based off that
1218-
fqdn = cfg["fqdn"]
1218+
fqdn = str(cfg["fqdn"])
12191219
hostname = get_cfg_option_str(cfg, "hostname", fqdn.split(".")[0])
12201220
else:
12211221
if "hostname" in cfg and cfg["hostname"].find(".") > 0:

tests/unittests/test_util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,22 @@ def test_get_hostname_fqdn_from_without_fqdn_or_hostname(self):
799799
mock.call(metadata_only=False),
800800
] == cloud.get_hostname.call_args_list
801801

802+
def test_get_hostname_fqdn_from_numeric_fqdn(self):
803+
"""When cfg fqdn is numeric, ensure it is treated as a string."""
804+
hostname, fqdn, _ = util.get_hostname_fqdn(
805+
cfg={"fqdn": 12345}, cloud=None
806+
)
807+
self.assertEqual("12345", hostname)
808+
self.assertEqual("12345", fqdn)
809+
810+
def test_get_hostname_fqdn_from_numeric_fqdn_with_domain(self):
811+
"""When cfg fqdn is numeric with a domain, ensure correct parsing."""
812+
hostname, fqdn, _ = util.get_hostname_fqdn(
813+
cfg={"fqdn": "12345.example.com"}, cloud=None
814+
)
815+
self.assertEqual("12345", hostname)
816+
self.assertEqual("12345.example.com", fqdn)
817+
802818
def test_get_hostname_fqdn_from_passes_metadata_only_to_cloud(self):
803819
"""Calls to cloud.get_hostname pass the metadata_only parameter."""
804820
cloud = mock.MagicMock()

tools/.github-cla-signers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ ManassehZhou
133133
manuelisimo
134134
MarkMielke
135135
marlluslustosa
136+
masihkhatibzadeh99
136137
mathmarchand
137138
matthewruffell
138139
maxnet

0 commit comments

Comments
 (0)