33from __future__ import annotations
44
55from pytest_mh import MultihostHost , MultihostUtility
6+ from pytest_mh .cli import CLIBuilder , CLIBuilderArgs
67from pytest_mh .conn import ProcessResult
78
9+ # Add imports for docstring type hints
10+
11+
812__all__ = [
913 "RealmUtils" ,
1014]
@@ -19,11 +23,68 @@ 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+ else :
82+ # execute with password as input
83+ command .extend (["-U" , user ])
84+ if domain :
85+ command .append (domain )
86+ return self .host .conn .exec (command , input = password )
87+
2788 def discover (self , domain : str | None = None , * , args : list [str ] | None = None ) -> ProcessResult :
2889 """
2990 Discover a realm and it's capabilities.
@@ -32,6 +93,8 @@ def discover(self, domain: str | None = None, *, args: list[str] | None = None)
3293 :type domain: str, optional
3394 :param args: Additional arguments, defaults to None
3495 :type args: list[str] | None, optional
96+ :return: Result of called command.
97+ :rtype: ProcessResult
3598 """
3699 if args is None :
37100 args = []
@@ -52,27 +115,27 @@ def leave(
52115 """
53116 Deconfigure and remove a client from realm.
54117
55- :param domain: domain to leave .
56- :type domain: str,
118+ :param domain: domain.
119+ :type domain: str
57120 :param args: Additional arguments, defaults to None.
58121 :type args: list[str] | None, optional
59122 :param password: Password to run the operation.
60123 :type password: str
61124 :param user: Authenticating user.
62125 :type user: str
63- :param krb: Enable kerberos authentication, defaults to False.
126+ :param krb: kerberos authentication, defaults to False.
64127 :type krb: bool
128+ :return: Result of called command.
129+ :rtype: ProcessResult
65130 """
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
131+ return self ._exec_realm (
132+ "leave" ,
133+ domain = domain or None , # Pass None to helper if empty string
134+ args = args ,
135+ password = password ,
136+ user = user ,
137+ krb = krb ,
138+ )
76139
77140 def join (
78141 self ,
@@ -86,34 +149,87 @@ def join(
86149 """
87150 Join and configure a client to realm.
88151
89- :param domain: Domain to join .
152+ :param domain: Domain.
90153 :type domain: str
91154 :param args: Additional arguments, defaults to None
92155 :type args: list[str] | None, optional
93- :param password: Password to run the operation .
156+ :param password: Password.
94157 :type password: str
95158 :param user: Authenticating user.
96159 :type user: str
97- :param krb: Enable kerberos authentication, defaults to False
160+ :param krb: Kerberos authentication, defaults to False
98161 :type krb: bool
162+ :return: Result of called command.
163+ :rtype: ProcessResult
164+ """
165+ return self ._exec_realm (
166+ "join" ,
167+ domain = domain ,
168+ args = args ,
169+ password = password ,
170+ user = user ,
171+ krb = krb ,
172+ )
173+
174+ def renew (
175+ self ,
176+ * ,
177+ args : list [str ] | None = None ,
178+ ) -> ProcessResult :
179+ """
180+ Renew host keytab.
181+
182+ :param args: Additional arguments, defaults to None
183+ :type args: list[str] | None, optional
184+ :return: Result of called command.
185+ :rtype: ProcessResult
99186 """
100187 if args is None :
101188 args = []
102189
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 )
190+ command = ["realm" , "renew" , "--verbose" , * args ]
191+ return self .host .conn .exec (command )
108192
109- return result
193+ def permit (self , user : str , * , withdraw : bool = False , args : list [str ] | None = None ) -> ProcessResult :
194+ """
195+ Permit users log in.
196+
197+ :param user: User to permit.
198+ :type user: str
199+ :param withdraw: Withdraw permission, defaults to False
200+ :type withdraw: bool, optional
201+ :param args: Additional arguments, defaults to None
202+ :type args: list[str] | None, optional
203+ :return: Result of called command.
204+ :rtype: ProcessResult
205+ """
206+ cli_args : CLIBuilderArgs = {"withdraw" : (self .cli .option .SWITCH , withdraw )}
207+ if args is None :
208+ args = []
209+
210+ return self .host .conn .exec (["realm" , "permit" , * self .cli .args (cli_args ), * args , user ])
211+
212+ def deny (self , user : str , * , args : list [str ] | None = None ) -> ProcessResult :
213+ """
214+ Deny users log in.
215+
216+ :param user: User.
217+ :type user: str
218+ :param args: Additional arguments, defaults to None
219+ :type args: list[str] | None, optional
220+ :return: Result of called command.
221+ :rtype: ProcessResult
222+ """
223+ return self .permit (user , withdraw = True , args = args )
110224
111225 def list (self , * , args : list [str ] | None = None ) -> ProcessResult :
112226 """
113227 List discovered, and configured realms.
114228
115229 :param args: Additional arguments, defaults to None
116230 :type args: list[str] | None, optional
231+ :return: Result of called command.
232+ :rtype: ProcessResult
117233 """
118234 if args is None :
119235 args = []
0 commit comments