Skip to content

Commit 7621674

Browse files
realmd: Adding new methods and rearrange code
Adding new methods: 1. permit 2. deny 3. renew Adding helper executer function to remove redundant codeline.
1 parent 6232066 commit 7621674

File tree

1 file changed

+144
-23
lines changed

1 file changed

+144
-23
lines changed

sssd_test_framework/utils/realmd.py

Lines changed: 144 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
from __future__ import annotations
44

55
from pytest_mh import MultihostHost, MultihostUtility
6+
from pytest_mh.cli import CLIBuilder, CLIBuilderArgs
67
from pytest_mh.conn import ProcessResult
78

9+
# Add imports for docstring type hints
10+
11+
812
__all__ = [
913
"RealmUtils",
1014
]
@@ -19,11 +23,70 @@ class RealmUtils(MultihostUtility[MultihostHost]):
1923
2024
@pytest.mark.topology(KnownTopology.AD)
2125
def test_realm_discover(client: Client, provider: ADProvider):
22-
r = client.realm.discover(["--use-ldaps"])
26+
r = client.realm.discover(provider.host.domain, args=["--use-ldaps"])
2327
assert provider.host.domain in r.stdout, "realm failed to discover domain info!"
2428
2529
"""
2630

31+
def __init__(self, host: MultihostHost) -> None:
32+
"""
33+
Initialize the RealmUtils.
34+
35+
:param host: The multihost host instance.
36+
:type host: MultihostHost
37+
"""
38+
super().__init__(host)
39+
self.cli: CLIBuilder = self.host.cli
40+
"""Command line builder."""
41+
42+
def _exec_realm(
43+
self,
44+
subcommand: str,
45+
*,
46+
domain: str | None = None,
47+
args: list[str] | None = None,
48+
password: str,
49+
user: str,
50+
krb: bool = False,
51+
) -> ProcessResult:
52+
"""
53+
Execute realm commands.
54+
55+
:param subcommand: Subcommand (e.g., "join", "leave", "renew").
56+
:type subcommand: str
57+
:param domain: domain.
58+
:type domain: str, optional
59+
:param args: Additional arguments.
60+
:type args: list[str] | None, optional
61+
:param password: Password.
62+
:type password: str
63+
:param user: User.
64+
:type user: str
65+
:param krb: Use Kerberos.
66+
:type krb: bool
67+
:return: ProcessResult
68+
:rtype: ProcessResult
69+
"""
70+
if args is None:
71+
args = []
72+
73+
# Base command
74+
command = ["realm", subcommand, "--verbose", *args]
75+
76+
if krb:
77+
self.host.conn.exec(["kinit", f"{user}"], input=password)
78+
if domain:
79+
command.append(domain)
80+
return self.host.conn.exec(command)
81+
elif subcommand is "renew":
82+
return self.host.conn.exec(command)
83+
else:
84+
# execute with password as input
85+
command.extend(["-U", user])
86+
if domain:
87+
command.append(domain)
88+
return self.host.conn.exec(command, input=password)
89+
2790
def discover(self, domain: str | None = None, *, args: list[str] | None = None) -> ProcessResult:
2891
"""
2992
Discover a realm and it's capabilities.
@@ -32,6 +95,8 @@ def discover(self, domain: str | None = None, *, args: list[str] | None = None)
3295
:type domain: str, optional
3396
:param args: Additional arguments, defaults to None
3497
:type args: list[str] | None, optional
98+
:return: Result of called command.
99+
:rtype: ProcessResult
35100
"""
36101
if args is None:
37102
args = []
@@ -52,27 +117,27 @@ def leave(
52117
"""
53118
Deconfigure and remove a client from realm.
54119
55-
:param domain: domain to leave.
56-
:type domain: str,
120+
:param domain: domain.
121+
:type domain: str
57122
:param args: Additional arguments, defaults to None.
58123
:type args: list[str] | None, optional
59124
:param password: Password to run the operation.
60125
:type password: str
61126
:param user: Authenticating user.
62127
:type user: str
63-
:param krb: Enable kerberos authentication, defaults to False.
128+
:param krb: kerberos authentication, defaults to False.
64129
:type krb: bool
130+
:return: Result of called command.
131+
:rtype: ProcessResult
65132
"""
66-
if args is None:
67-
args = []
68-
69-
if krb:
70-
self.host.conn.exec(["kinit", user], input=password)
71-
result = self.host.conn.exec(["realm", "leave", "--verbose", *args, domain])
72-
else:
73-
result = self.host.conn.exec(["realm", "leave", "--verbose", *args, "-U", user, domain], input=password)
74-
75-
return result
133+
return self._exec_realm(
134+
"leave",
135+
domain=domain or None, # Pass None to helper if empty string
136+
args=args,
137+
password=password,
138+
user=user,
139+
krb=krb,
140+
)
76141

77142
def join(
78143
self,
@@ -86,34 +151,90 @@ def join(
86151
"""
87152
Join and configure a client to realm.
88153
89-
:param domain: Domain to join.
154+
:param domain: Domain.
90155
:type domain: str
91156
:param args: Additional arguments, defaults to None
92157
:type args: list[str] | None, optional
93-
:param password: Password to run the operation.
158+
:param password: Password.
94159
:type password: str
95160
:param user: Authenticating user.
96161
:type user: str
97-
:param krb: Enable kerberos authentication, defaults to False
162+
:param krb: Kerberos authentication, defaults to False
98163
:type krb: bool
164+
:return: Result of called command.
165+
:rtype: ProcessResult
166+
"""
167+
return self._exec_realm(
168+
"join",
169+
domain=domain,
170+
args=args,
171+
password=password,
172+
user=user,
173+
krb=krb,
174+
)
175+
176+
def renew(
177+
self,
178+
*,
179+
domain: str | None = None,
180+
args: list[str] | None = None,
181+
) -> ProcessResult:
182+
"""
183+
Renew host keytab.
184+
185+
:param domain: Domain.
186+
:type domain: str, optional
187+
:param args: Additional arguments, defaults to None
188+
:type args: list[str] | None, optional
189+
:return: Result of called command.
190+
:rtype: ProcessResult
99191
"""
192+
return self._exec_realm(
193+
"renew",
194+
domain=domain,
195+
args=args,
196+
)
197+
198+
def permit(self, user: str, *, withdraw: bool = False, args: list[str] | None = None) -> ProcessResult:
199+
"""
200+
Permit users log in.
201+
202+
:param user: User to permit.
203+
:type user: str
204+
:param withdraw: Withdraw permission, defaults to False
205+
:type withdraw: bool, optional
206+
:param args: Additional arguments, defaults to None
207+
:type args: list[str] | None, optional
208+
:return: Result of called command.
209+
:rtype: ProcessResult
210+
"""
211+
cli_args: CLIBuilderArgs = {"withdraw": (self.cli.option.SWITCH, withdraw)}
100212
if args is None:
101213
args = []
102214

103-
if krb:
104-
self.host.conn.exec(["kinit", user], input=password)
105-
result = self.host.conn.exec(["realm", "join", "--verbose", *args, domain])
106-
else:
107-
result = self.host.conn.exec(["realm", "join", "--verbose", *args, "-U", user, domain], input=password)
215+
return self.host.conn.exec(["realm", "permit", *self.cli.args(cli_args), *args, user])
216+
217+
def deny(self, user: str, *, args: list[str] | None = None) -> ProcessResult:
218+
"""
219+
Deny users log in.
108220
109-
return result
221+
:param user: User.
222+
:type user: str
223+
:param args: Additional arguments, defaults to None
224+
:type args: list[str] | None, optional
225+
:return: Result of called command.
226+
:rtype: ProcessResult
227+
"""
228+
return self.permit(user, withdraw=True, args=args)
110229

111230
def list(self, *, args: list[str] | None = None) -> ProcessResult:
112231
"""
113232
List discovered, and configured realms.
114233
115234
:param args: Additional arguments, defaults to None
116235
:type args: list[str] | None, optional
236+
:return: Result of called command.
237+
:rtype: ProcessResult
117238
"""
118239
if args is None:
119240
args = []

0 commit comments

Comments
 (0)