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,102 @@ 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
99164 """
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+ user : str ,
178+ password : str ,
179+ domain : str | None = None ,
180+ args : list [str ] | None = None ,
181+ krb : bool = False ,
182+ ) -> ProcessResult :
183+ """
184+ Renew host keytab.
185+
186+ :param user: Authenticating user.
187+ :type user: str
188+ :param password: Password.
189+ :type password: str
190+ :param domain: Domain.
191+ :type domain: str, optional
192+ :param args: Additional arguments, defaults to None
193+ :type args: list[str] | None, optional
194+ :param krb: Kerberos authentication, defaults to False
195+ :type krb: bool
196+ :return: Result of called command.
197+ :rtype: ProcessResult
198+ """
199+ return self ._exec_realm (
200+ "renew" ,
201+ domain = domain ,
202+ args = args ,
203+ password = password ,
204+ user = user ,
205+ krb = krb ,
206+ )
207+
208+ def permit (self , user : str , * , withdraw : bool = False , args : list [str ] | None = None ) -> ProcessResult :
209+ """
210+ Permit users log in.
211+
212+ :param user: User to permit.
213+ :type user: str
214+ :param withdraw: Withdraw permission, defaults to False
215+ :type withdraw: bool, optional
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+ cli_args : CLIBuilderArgs = {"withdraw" : (self .cli .option .SWITCH , withdraw )}
100222 if args is None :
101223 args = []
102224
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 )
225+ return self . host . conn . exec ([ "realm" , "permit" , * self . cli . args ( cli_args ), * args , user ])
226+
227+ def deny ( self , user : str , * , args : list [ str ] | None = None ) -> ProcessResult :
228+ """
229+ Deny users log in.
108230
109- return result
231+ :param user: User.
232+ :type user: str
233+ :param args: Additional arguments, defaults to None
234+ :type args: list[str] | None, optional
235+ :return: Result of called command.
236+ :rtype: ProcessResult
237+ """
238+ return self .permit (user , withdraw = True , args = args )
110239
111240 def list (self , * , args : list [str ] | None = None ) -> ProcessResult :
112241 """
113242 List discovered, and configured realms.
114243
115244 :param args: Additional arguments, defaults to None
116245 :type args: list[str] | None, optional
246+ :return: Result of called command.
247+ :rtype: ProcessResult
117248 """
118249 if args is None :
119250 args = []
0 commit comments