From 1a1ac03fcd5550a775dcc3d5dd04fbdc93d5ab29 Mon Sep 17 00:00:00 2001
From: Madhuri Upadhye <mupadhye@redhat.com>
Date: Tue, 24 Sep 2024 11:34:56 +0530
Subject: [PATCH] Tests: sss_ssh_knownhosts with port number

Add tests cases with port numbers

Signed-off-by: Madhuri Upadhye <mupadhye@redhat.com>
---
 src/tests/system/tests/test_ipa.py | 82 +++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/src/tests/system/tests/test_ipa.py b/src/tests/system/tests/test_ipa.py
index 1b5bd2a08fa..28f6d36ba11 100644
--- a/src/tests/system/tests/test_ipa.py
+++ b/src/tests/system/tests/test_ipa.py
@@ -12,6 +12,7 @@
 import time
 
 import pytest
+from pytest_mh import mh_fixture
 from sssd_test_framework.roles.client import Client
 from sssd_test_framework.roles.ipa import IPA
 from sssd_test_framework.topology import KnownTopology
@@ -51,7 +52,7 @@ def test_ipa__hostpublickeys_by_name(client: Client, ipa: IPA, public_keys: list
         1. All public keys were printed
     :customerscenario: False
     """
-    hostname = f"ssh.{ipa.domain}"
+    hostname = f"ssh-host.{ipa.domain}"
     ip = "10.255.251.10"
 
     ipa.host_account(hostname).add(ip=ip, sshpubkey=public_keys)
@@ -126,6 +127,85 @@ def test_ipa__hostpublickeys_by_ip(client: Client, ipa: IPA, public_keys: list[s
         assert f"{ip} {key}" in result.stdout_lines, "Did not get expected public keys!"
 
 
+@pytest.mark.ticket(gh=7583)
+@pytest.mark.importance("low")
+@pytest.mark.parametrize("option, result", [(None, "[{hostname}]:{port} {key}"), ("-o ", "[{hostname}] {key}")])
+@pytest.mark.topology(KnownTopology.IPA)
+def test_ipa__hostpublickeys_by_name_with_port(client: Client, ipa: IPA, public_keys: list[str],
+                                               option: str | None, result: str):
+    """
+    :title: sss_ssh_knownhosts returns public keys by host name with port
+    :setup:
+        1. Create host with SSH key
+        2. Configure SSSD with SSH responder
+        3. Start SSSD
+    :steps:
+        1. Lookup SSH key
+    :expectedresults:
+        1. All public keys were printed
+    :customerscenario: False
+    """
+    hostname = f"ssh-host.{ipa.domain}"
+    ip = "10.255.251.10"
+    port = 3333
+
+    if option == None:
+        option = ""
+
+    import pdb; pdb.set_trace()
+    ipa.host_account(hostname).add(ip=ip, sshpubkey=public_keys)
+    client.sssd.enable_responder("ssh")
+    client.sssd.start()
+    result = client.sss_ssh_knownhosts(f"{option}", f"{hostname}:{port}")
+    assert result.rc == 0, "Did not get OpenSSH known hosts public keys!"
+    assert len(public_keys) == len(result.stdout_lines), "Did not get expected number of public keys!"
+    for key in public_keys:
+        assert f"[{hostname}]:{port} {key}" in result.stdout_lines, ("Did not get expected public keys "
+                                                                     "with the host name with port")
+
+
+@pytest.mark.ticket(gh=7583)
+@pytest.mark.importance("low")
+@pytest.mark.topology(KnownTopology.IPA)
+def test_ipa__hostpublickeys_with_non_default_port(client: Client, ipa: IPA, public_keys: list[str]):
+    """
+    :title: sss_ssh_knownhosts returns public keys by hostname with non-default port
+    :setup:
+        1. Create host with SSH key
+        2. Add the ipasshpubkey with hostname and port
+        3. Configure SSSD with SSH responder
+        4. Start SSSD
+    :steps:
+        1. Lookup SSH key
+    :expectedresults:
+        1. All public keys were printed
+    :customerscenario: False
+    """
+    hostname = f"ssh-host.{ipa.domain}"
+    ip = "10.255.251.10"
+    port = 4444
+
+    ipa.host_account(hostname).add(ip=ip, sshpubkey=public_keys)
+    client.sssd.enable_responder("ssh")
+    client.sssd.start()
+    # IPA doesn't currently ipa host-mod with hostname and key
+    # this is workaround till IPA add the support.
+    for key in public_keys:
+        modify_content = ipa.fs.mktmp(rf"""
+                        dn: fqdn={hostname},cn=computers,cn=accounts,dc=ipa,dc=test
+                        changetype: modify
+                        add: ipaSshPubKey
+                        ipaSshPubKey: [{hostname}]:{port} {key}
+                        """, mode="a=rx",
+                        )
+
+        ipa.host.conn.run(command=f"ldapmodify -H ldap://master.ipa.test -f {modify_content}")
+    result = client.sss_ssh_knownhosts(f"[{hostname}]:{port}")
+    assert result.rc == 0, "Did not get OpenSSH known hosts public keys!"
+    for key in public_keys:
+        assert f"[{hostname}]:{port} {key}" in result.stdout_lines, ("Did not get expected public keys "
+                                                                   "with the host name with port")
+
 @pytest.mark.ticket(bz=1926622)
 @pytest.mark.integration
 @pytest.mark.importance("low")