Skip to content

Commit d6c9815

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 d6c9815

File tree

1 file changed

+137
-23
lines changed

1 file changed

+137
-23
lines changed

sssd_test_framework/utils/realmd.py

Lines changed: 137 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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

89
__all__ = [
@@ -19,11 +20,69 @@ class RealmUtils(MultihostUtility[MultihostHost]):
1920
2021
@pytest.mark.topology(KnownTopology.AD)
2122
def test_realm_discover(client: Client, provider: ADProvider):
22-
r = client.realm.discover(["--use-ldaps"])
23+
r = client.realm.discover(provider.host.domain, args=["--use-ldaps"])
2324
assert provider.host.domain in r.stdout, "realm failed to discover domain info!"
2425
2526
"""
2627

28+
def __init__(self, host: MultihostHost) -> None:
29+
"""
30+
Initialize the RealmUtils.
31+
32+
:param host: The multihost host instance.
33+
:type host: MultihostHost
34+
"""
35+
super().__init__(host)
36+
self.cli: CLIBuilder = self.host.cli
37+
"""Command line builder."""
38+
39+
def _exec_realm(
40+
self,
41+
subcommand: str,
42+
*,
43+
password: str | None = None,
44+
user: str | None = None,
45+
domain: str | None = None,
46+
args: list[str] | None = None,
47+
krb: bool = False,
48+
) -> ProcessResult:
49+
"""
50+
Execute realm commands.
51+
52+
:param subcommand: Subcommand (e.g., "join", "leave").
53+
:type subcommand: str
54+
:param domain: domain.
55+
:type domain: str, optional
56+
:param args: Additional arguments.
57+
:type args: list[str] | None, optional
58+
:param password: Password, defaults to None.
59+
:type password: str
60+
:param user: User, defaults to None.
61+
:type user: str
62+
:param krb: Use Kerberos.
63+
:type krb: bool
64+
:return: ProcessResult
65+
:rtype: ProcessResult
66+
"""
67+
if args is None:
68+
args = []
69+
70+
# Base command
71+
command = ["realm", subcommand, "--verbose", *args]
72+
73+
if krb:
74+
self.host.conn.exec(["kinit", f"{user}"], input=password)
75+
if domain:
76+
command.append(domain)
77+
return self.host.conn.exec(command)
78+
else:
79+
# execute with password as input
80+
if user:
81+
command.extend(["-U", user])
82+
if domain:
83+
command.append(domain)
84+
return self.host.conn.exec(command, input=password)
85+
2786
def discover(self, domain: str | None = None, *, args: list[str] | None = None) -> ProcessResult:
2887
"""
2988
Discover a realm and it's capabilities.
@@ -32,6 +91,8 @@ def discover(self, domain: str | None = None, *, args: list[str] | None = None)
3291
:type domain: str, optional
3392
:param args: Additional arguments, defaults to None
3493
:type args: list[str] | None, optional
94+
:return: Result of called command.
95+
:rtype: ProcessResult
3596
"""
3697
if args is None:
3798
args = []
@@ -52,27 +113,27 @@ def leave(
52113
"""
53114
Deconfigure and remove a client from realm.
54115
55-
:param domain: domain to leave.
56-
:type domain: str,
116+
:param domain: domain.
117+
:type domain: str
57118
:param args: Additional arguments, defaults to None.
58119
:type args: list[str] | None, optional
59120
:param password: Password to run the operation.
60121
:type password: str
61122
:param user: Authenticating user.
62123
:type user: str
63-
:param krb: Enable kerberos authentication, defaults to False.
124+
:param krb: kerberos authentication, defaults to False.
64125
:type krb: bool
126+
:return: Result of called command.
127+
:rtype: ProcessResult
65128
"""
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
129+
return self._exec_realm(
130+
"leave",
131+
domain=domain or None, # Pass None to helper if empty string
132+
args=args,
133+
password=password,
134+
user=user,
135+
krb=krb,
136+
)
76137

77138
def join(
78139
self,
@@ -86,34 +147,87 @@ def join(
86147
"""
87148
Join and configure a client to realm.
88149
89-
:param domain: Domain to join.
150+
:param domain: Domain.
90151
:type domain: str
91152
:param args: Additional arguments, defaults to None
92153
:type args: list[str] | None, optional
93-
:param password: Password to run the operation.
154+
:param password: Password.
94155
:type password: str
95156
:param user: Authenticating user.
96157
:type user: str
97-
:param krb: Enable kerberos authentication, defaults to False
158+
:param krb: Kerberos authentication, defaults to False
98159
:type krb: bool
160+
:return: Result of called command.
161+
:rtype: ProcessResult
162+
"""
163+
return self._exec_realm(
164+
"join",
165+
domain=domain,
166+
args=args,
167+
password=password,
168+
user=user,
169+
krb=krb,
170+
)
171+
172+
def renew(
173+
self,
174+
*,
175+
args: list[str] | None = None,
176+
) -> ProcessResult:
177+
"""
178+
Renew host keytab.
179+
180+
:param args: Additional arguments, defaults to None
181+
:type args: list[str] | None, optional
182+
:return: Result of called command.
183+
:rtype: ProcessResult
99184
"""
100185
if args is None:
101186
args = []
102187

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)
188+
command = ["realm", "renew", "--verbose", *args]
189+
return self.host.conn.exec(command)
190+
191+
def permit(self, user: str, *, withdraw: bool = False, args: list[str] | None = None) -> ProcessResult:
192+
"""
193+
Permit users log in.
108194
109-
return result
195+
:param user: User to permit.
196+
:type user: str
197+
:param withdraw: Withdraw permission, defaults to False
198+
:type withdraw: bool, optional
199+
:param args: Additional arguments, defaults to None
200+
:type args: list[str] | None, optional
201+
:return: Result of called command.
202+
:rtype: ProcessResult
203+
"""
204+
cli_args: CLIBuilderArgs = {"withdraw": (self.cli.option.SWITCH, withdraw)}
205+
if args is None:
206+
args = []
207+
208+
return self.host.conn.exec(["realm", "permit", *self.cli.args(cli_args), *args, user])
209+
210+
def deny(self, user: str, *, args: list[str] | None = None) -> ProcessResult:
211+
"""
212+
Deny users log in.
213+
214+
:param user: User.
215+
:type user: str
216+
:param args: Additional arguments, defaults to None
217+
:type args: list[str] | None, optional
218+
:return: Result of called command.
219+
:rtype: ProcessResult
220+
"""
221+
return self.permit(user, withdraw=True, args=args)
110222

111223
def list(self, *, args: list[str] | None = None) -> ProcessResult:
112224
"""
113225
List discovered, and configured realms.
114226
115227
:param args: Additional arguments, defaults to None
116228
:type args: list[str] | None, optional
229+
:return: Result of called command.
230+
:rtype: ProcessResult
117231
"""
118232
if args is None:
119233
args = []

0 commit comments

Comments
 (0)