22
33from __future__ import annotations
44
5+
56from pytest_mh import MultihostHost , MultihostUtility
7+ from pytest_mh .cli import CLIBuilder , CLIBuilderArgs
68from pytest_mh .conn import ProcessResult
79
10+ # Add imports for docstring type hints
11+
12+
813__all__ = [
914 "RealmUtils" ,
1015]
@@ -19,11 +24,68 @@ class RealmUtils(MultihostUtility[MultihostHost]):
1924
2025 @pytest.mark.topology(KnownTopology.AD)
2126 def test_realm_discover(client: Client, provider: ADProvider):
22- r = client.realm.discover(["--use-ldaps"])
27+ r = client.realm.discover(provider.host.domain, args= ["--use-ldaps"])
2328 assert provider.host.domain in r.stdout, "realm failed to discover domain info!"
2429
2530 """
2631
32+ def __init__ (self , host : MultihostHost ) -> None :
33+ """
34+ Initialize the RealmUtils.
35+
36+ :param host: The multihost host instance.
37+ :type host: MultihostHost
38+ """
39+ super ().__init__ (host )
40+ self .cli : CLIBuilder = self .host .cli
41+ """Command line builder."""
42+
43+ def _exec_realm (
44+ self ,
45+ subcommand : str ,
46+ * ,
47+ domain : str | None = None ,
48+ args : list [str ] | None = None ,
49+ password : str ,
50+ user : str ,
51+ krb : bool = False ,
52+ ) -> ProcessResult :
53+ """
54+ Execute realm commands.
55+
56+ :param subcommand: Subcommand (e.g., "join", "leave", "renew").
57+ :type subcommand: str
58+ :param domain: domain.
59+ :type domain: str, optional
60+ :param args: Additional arguments.
61+ :type args: list[str] | None, optional
62+ :param password: Password.
63+ :type password: str
64+ :param user: User.
65+ :type user: str
66+ :param krb: Use Kerberos.
67+ :type krb: bool
68+ :return: ProcessResult
69+ :rtype: ProcessResult
70+ """
71+ if args is None :
72+ args = []
73+
74+ # Base command
75+ command = ["realm" , subcommand , "--verbose" , * args ]
76+
77+ if krb :
78+ self .host .conn .exec (["kinit" , f"{ user } " ], input = password )
79+ if domain :
80+ command .append (domain )
81+ return self .host .conn .exec (command )
82+ else :
83+ # execute with password as input
84+ command .extend (["-U" , user ])
85+ if domain :
86+ command .append (domain )
87+ return self .host .conn .exec (command , input = password )
88+
2789 def discover (self , domain : str | None = None , * , args : list [str ] | None = None ) -> ProcessResult :
2890 """
2991 Discover a realm and it's capabilities.
@@ -32,6 +94,8 @@ def discover(self, domain: str | None = None, *, args: list[str] | None = None)
3294 :type domain: str, optional
3395 :param args: Additional arguments, defaults to None
3496 :type args: list[str] | None, optional
97+ :return: Result of called command.
98+ :rtype: ProcessResult
3599 """
36100 if args is None :
37101 args = []
@@ -52,27 +116,27 @@ def leave(
52116 """
53117 Deconfigure and remove a client from realm.
54118
55- :param domain: domain to leave .
56- :type domain: str,
119+ :param domain: domain.
120+ :type domain: str
57121 :param args: Additional arguments, defaults to None.
58122 :type args: list[str] | None, optional
59123 :param password: Password to run the operation.
60124 :type password: str
61125 :param user: Authenticating user.
62126 :type user: str
63- :param krb: Enable kerberos authentication, defaults to False.
127+ :param krb: kerberos authentication, defaults to False.
64128 :type krb: bool
129+ :return: Result of called command.
130+ :rtype: ProcessResult
65131 """
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
132+ return self ._exec_realm (
133+ "leave" ,
134+ domain = domain or None , # Pass None to helper if empty string
135+ args = args ,
136+ password = password ,
137+ user = user ,
138+ krb = krb ,
139+ )
76140
77141 def join (
78142 self ,
@@ -86,34 +150,102 @@ def join(
86150 """
87151 Join and configure a client to realm.
88152
89- :param domain: Domain to join .
153+ :param domain: Domain.
90154 :type domain: str
91155 :param args: Additional arguments, defaults to None
92156 :type args: list[str] | None, optional
93- :param password: Password to run the operation .
157+ :param password: Password.
94158 :type password: str
95159 :param user: Authenticating user.
96160 :type user: str
97- :param krb: Enable kerberos authentication, defaults to False
161+ :param krb: Kerberos authentication, defaults to False
162+ :type krb: bool
163+ :return: Result of called command.
164+ :rtype: ProcessResult
165+ """
166+ return self ._exec_realm (
167+ "join" ,
168+ domain = domain ,
169+ args = args ,
170+ password = password ,
171+ user = user ,
172+ krb = krb ,
173+ )
174+
175+ def renew (
176+ self ,
177+ * ,
178+ user : str ,
179+ password : str ,
180+ domain : str | None = None ,
181+ args : list [str ] | None = None ,
182+ krb : bool = False ,
183+ ) -> ProcessResult :
184+ """
185+ Renew host keytab.
186+
187+ :param user: Authenticating user.
188+ :type user: str
189+ :param password: Password.
190+ :type password: str
191+ :param domain: Domain.
192+ :type domain: str, optional
193+ :param args: Additional arguments, defaults to None
194+ :type args: list[str] | None, optional
195+ :param krb: Kerberos authentication, defaults to False
98196 :type krb: bool
197+ :return: Result of called command.
198+ :rtype: ProcessResult
199+ """
200+ return self ._exec_realm (
201+ "renew" ,
202+ domain = domain ,
203+ args = args ,
204+ password = password ,
205+ user = user ,
206+ krb = krb ,
207+ )
208+
209+ def permit (self , user : str , * , withdraw : bool = False , args : list [str ] | None = None ) -> ProcessResult :
210+ """
211+ Permit users log in.
212+
213+ :param user: User to permit.
214+ :type user: str
215+ :param withdraw: Withdraw permission, defaults to False
216+ :type withdraw: bool, optional
217+ :param args: Additional arguments, defaults to None
218+ :type args: list[str] | None, optional
219+ :return: Result of called command.
220+ :rtype: ProcessResult
99221 """
222+ cli_args : CLIBuilderArgs = {"withdraw" : (self .cli .option .SWITCH , withdraw )}
100223 if args is None :
101224 args = []
102225
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 )
226+ return self . host . conn . exec ([ "realm" , "permit" , * self . cli . args ( cli_args ), * args , user ])
227+
228+ def deny ( self , user : str , * , args : list [ str ] | None = None ) -> ProcessResult :
229+ """
230+ Deny users log in.
108231
109- return result
232+ :param user: User.
233+ :type user: str
234+ :param args: Additional arguments, defaults to None
235+ :type args: list[str] | None, optional
236+ :return: Result of called command.
237+ :rtype: ProcessResult
238+ """
239+ return self .permit (user , withdraw = True , args = args )
110240
111241 def list (self , * , args : list [str ] | None = None ) -> ProcessResult :
112242 """
113243 List discovered, and configured realms.
114244
115245 :param args: Additional arguments, defaults to None
116246 :type args: list[str] | None, optional
247+ :return: Result of called command.
248+ :rtype: ProcessResult
117249 """
118250 if args is None :
119251 args = []
0 commit comments