2020)
2121from .enums import (
2222 AuthMode ,
23+ InputPasswordFilter ,
2324 JoinSourceType ,
25+ PasswordFilter ,
26+ ShowResults ,
2427 SMBClustering ,
2528 UserGroupSourceType ,
2629)
@@ -139,13 +142,49 @@ def internal_store_backend(self) -> str:
139142 self .get_module_option ('internal_store_backend' , '' ),
140143 )
141144
145+ def _apply_res (
146+ self ,
147+ resource_input : List [resources .SMBResource ],
148+ create_only : bool = False ,
149+ password_filter : InputPasswordFilter = InputPasswordFilter .NONE ,
150+ password_filter_out : Optional [PasswordFilter ] = None ,
151+ ) -> results .ResultGroup :
152+ in_pf = password_filter .to_password_filter () # update enum type
153+ # use the same filtering as the input unless expliclitly set
154+ out_pf = password_filter_out if password_filter_out else in_pf
155+ if in_pf is not PasswordFilter .NONE :
156+ in_op = (in_pf , PasswordFilter .NONE )
157+ log .debug ('Password filtering for resource apply: %r' , in_op )
158+ resource_input = [r .convert (in_op ) for r in resource_input ]
159+ all_results = self ._handler .apply (
160+ resource_input , create_only = create_only
161+ )
162+ if out_pf is not PasswordFilter .NONE :
163+ # we need to apply the conversion filter to the output
164+ # resources in the results - otherwise we would show raw
165+ # passwords - this will be the inverse of the filter applied to
166+ # the input
167+ out_op = (PasswordFilter .NONE , out_pf )
168+ log .debug ('Password filtering for smb apply output: %r' , in_op )
169+ all_results = all_results .convert_results (out_op )
170+ return all_results
171+
142172 @cli .SMBCommand ('apply' , perm = 'rw' )
143- def apply_resources (self , inbuf : str ) -> results .ResultGroup :
173+ def apply_resources (
174+ self ,
175+ inbuf : str ,
176+ password_filter : InputPasswordFilter = InputPasswordFilter .NONE ,
177+ password_filter_out : Optional [PasswordFilter ] = None ,
178+ ) -> results .ResultGroup :
144179 """Create, update, or remove smb configuration resources based on YAML
145180 or JSON specs
146181 """
147182 try :
148- return self ._handler .apply (resources .load_text (inbuf ))
183+ return self ._apply_res (
184+ resources .load_text (inbuf ),
185+ password_filter = password_filter ,
186+ password_filter_out = password_filter_out ,
187+ )
149188 except resources .InvalidResourceError as err :
150189 # convert the exception into a result and return it as the only
151190 # item in the result group
@@ -172,6 +211,8 @@ def cluster_create(
172211 placement : Optional [str ] = None ,
173212 clustering : Optional [SMBClustering ] = None ,
174213 public_addrs : Optional [List [str ]] = None ,
214+ password_filter : InputPasswordFilter = InputPasswordFilter .NONE ,
215+ password_filter_out : Optional [PasswordFilter ] = None ,
175216 ) -> results .Result :
176217 """Create an smb cluster"""
177218 domain_settings = None
@@ -282,13 +323,24 @@ def cluster_create(
282323 public_addrs = c_public_addrs ,
283324 )
284325 to_apply .append (cluster )
285- return self ._handler .apply (to_apply , create_only = True ).squash (cluster )
326+ return self ._apply_res (
327+ to_apply ,
328+ create_only = True ,
329+ password_filter = password_filter ,
330+ password_filter_out = password_filter_out ,
331+ ).squash (cluster )
286332
287333 @cli .SMBCommand ('cluster rm' , perm = 'rw' )
288- def cluster_rm (self , cluster_id : str ) -> results .Result :
334+ def cluster_rm (
335+ self ,
336+ cluster_id : str ,
337+ password_filter : PasswordFilter = PasswordFilter .NONE ,
338+ ) -> results .Result :
289339 """Remove an smb cluster"""
290340 cluster = resources .RemovedCluster (cluster_id = cluster_id )
291- return self ._handler .apply ([cluster ]).one ()
341+ return self ._apply_res (
342+ [cluster ], password_filter_out = password_filter
343+ ).one ()
292344
293345 @cli .SMBCommand ('share ls' , perm = 'r' )
294346 def share_ls (self , cluster_id : str ) -> List [str ]:
@@ -324,18 +376,23 @@ def share_create(
324376 subvolume = subvolume ,
325377 ),
326378 )
327- return self ._handler . apply ([share ], create_only = True ).one ()
379+ return self ._apply_res ([share ], create_only = True ).one ()
328380
329381 @cli .SMBCommand ('share rm' , perm = 'rw' )
330382 def share_rm (self , cluster_id : str , share_id : str ) -> results .Result :
331383 """Remove an smb share"""
332384 share = resources .RemovedShare (
333385 cluster_id = cluster_id , share_id = share_id
334386 )
335- return self ._handler . apply ([share ]).one ()
387+ return self ._apply_res ([share ]).one ()
336388
337- @cli .SMBCommand ('show' , perm = 'r' )
338- def show (self , resource_names : Optional [List [str ]] = None ) -> Simplified :
389+ @cli .SMBCommand ("show" , perm = "r" )
390+ def show (
391+ self ,
392+ resource_names : Optional [List [str ]] = None ,
393+ results : ShowResults = ShowResults .COLLAPSED ,
394+ password_filter : PasswordFilter = PasswordFilter .NONE ,
395+ ) -> Simplified :
339396 """Show resources fetched from the local config store based on resource
340397 type or resource type and id(s).
341398 """
@@ -346,9 +403,13 @@ def show(self, resource_names: Optional[List[str]] = None) -> Simplified:
346403 resources = self ._handler .matching_resources (resource_names )
347404 except handler .InvalidResourceMatch as err :
348405 raise cli .InvalidInputValue (str (err )) from err
349- if len (resources ) == 1 :
406+ if password_filter is not PasswordFilter .NONE :
407+ op = (PasswordFilter .NONE , password_filter )
408+ log .debug ('Password filtering for smb show: %r' , op )
409+ resources = [r .convert (op ) for r in resources ]
410+ if len (resources ) == 1 and results is ShowResults .COLLAPSED :
350411 return resources [0 ].to_simplified ()
351- return {' resources' : [r .to_simplified () for r in resources ]}
412+ return {" resources" : [r .to_simplified () for r in resources ]}
352413
353414 def submit_smb_spec (self , spec : SMBSpec ) -> None :
354415 """Submit a new or updated smb spec object to ceph orchestration."""
0 commit comments