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
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 password: Password, defaults to None.
55+ :type password: str
56+ :param user: User, defaults to None.
57+ :type user: str
58+ :param domain: domain.
59+ :type domain: str, optional
60+ :param args: Additional arguments.
61+ :type args: list[str] | None, optional
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,90 @@ 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+ domain ,
175+ * ,
176+ args : list [str ] | None = None ,
177+ ) -> ProcessResult :
178+ """
179+ Renew host keytab.
180+
181+ :param domain: domain.
182+ :type domain: str
183+ :param args: Additional arguments, defaults to None
184+ :type args: list[str] | None, optional
185+ :return: Result of called command.
186+ :rtype: ProcessResult
99187 """
100188 if args is None :
101189 args = []
102190
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 )
191+ command = ["realm" , "renew" , domain , "--verbose" , * args ]
192+ return self .host .conn .exec (command )
193+
194+ def permit (self , user : str , * , withdraw : bool = False , args : list [str ] | None = None ) -> ProcessResult :
195+ """
196+ Permit users log in.
197+
198+ :param user: User to permit.
199+ :type user: str
200+ :param withdraw: Withdraw permission, defaults to False
201+ :type withdraw: bool, optional
202+ :param args: Additional arguments, defaults to None
203+ :type args: list[str] | None, optional
204+ :return: Result of called command.
205+ :rtype: ProcessResult
206+ """
207+ cli_args : CLIBuilderArgs = {"withdraw" : (self .cli .option .SWITCH , withdraw )}
208+ if args is None :
209+ args = []
108210
109- return result
211+ return self .host .conn .exec (["realm" , "permit" , * self .cli .args (cli_args ), * args , user ])
212+
213+ def deny (self , user : str , * , args : list [str ] | None = None ) -> ProcessResult :
214+ """
215+ Deny users log in.
216+
217+ :param user: User.
218+ :type user: str
219+ :param args: Additional arguments, defaults to None
220+ :type args: list[str] | None, optional
221+ :return: Result of called command.
222+ :rtype: ProcessResult
223+ """
224+ return self .permit (user , withdraw = True , args = args )
110225
111226 def list (self , * , args : list [str ] | None = None ) -> ProcessResult :
112227 """
113228 List discovered, and configured realms.
114229
115230 :param args: Additional arguments, defaults to None
116231 :type args: list[str] | None, optional
232+ :return: Result of called command.
233+ :rtype: ProcessResult
117234 """
118235 if args is None :
119236 args = []
0 commit comments