6363from cmk .gui .quick_setup .v0_unstable .predefined import build_formspec_map_from_stages
6464from cmk .gui .quick_setup .v0_unstable .predefined ._common import _find_id_in_form_data
6565from cmk .gui .quick_setup .v0_unstable .setups import (
66+ get_all_permissions ,
6667 QuickSetupActionMode ,
6768 QuickSetupBackgroundAction ,
6869 QuickSetupBackgroundStageAction ,
@@ -151,6 +152,7 @@ class QuickSetupMode(StrEnum):
151152 query_params = [QUICKSETUP_MODE , QUICKSETUP_OBJECT_ID ],
152153 path_params = [QUICKSETUP_ID ],
153154 response_schema = QuickSetupResponse ,
155+ additional_status_codes = [403 ],
154156)
155157def get_guided_stages_or_overview_stages (params : Mapping [str , Any ]) -> Response :
156158 """Get guided stages or overview stages"""
@@ -166,6 +168,15 @@ def get_guided_stages_or_overview_stages(params: Mapping[str, Any]) -> Response:
166168 detail = f"Quick setup with id '{ quick_setup_id } ' does not exist." ,
167169 )
168170
171+ permissions = get_all_permissions (quick_setup )
172+
173+ if permissions is not None and not all (user .may (perm ) for perm in permissions ):
174+ return _serve_error (
175+ title = "Action not allowed" ,
176+ detail = f"Requires { ', ' .join (f"'{ p } '" for p in permissions )} permissions." ,
177+ status_code = 403 ,
178+ )
179+
169180 mode : QuickSetupMode = params ["mode" ]
170181 prefill_data : ParsedFormData | None = None
171182 if object_id := params ["object_id" ]:
@@ -205,6 +216,7 @@ def get_guided_stages_or_overview_stages(params: Mapping[str, Any]) -> Response:
205216 path_params = [QUICKSETUP_ID , STAGE_INDEX ],
206217 query_params = [QUICKSETUP_OBJECT_ID ],
207218 response_schema = QuickSetupStageStructure ,
219+ additional_status_codes = [403 ],
208220)
209221def quick_setup_get_stage_structure (params : Mapping [str , Any ]) -> Response :
210222 """Get a Quick setup stage structure"""
@@ -220,6 +232,15 @@ def quick_setup_get_stage_structure(params: Mapping[str, Any]) -> Response:
220232 detail = f"Quick setup with id '{ quick_setup_id } ' does not exist." ,
221233 )
222234
235+ permissions = get_all_permissions (quick_setup )
236+
237+ if permissions is not None and not all (user .may (perm ) for perm in permissions ):
238+ return _serve_error (
239+ title = "Action not allowed" ,
240+ detail = f"Requires { ', ' .join (f"'{ p } '" for p in permissions )} permissions." ,
241+ status_code = 403 ,
242+ )
243+
223244 prefill_data : ParsedFormData | None = None
224245 if object_id :
225246 prefill_data = quick_setup .load_data (object_id )
@@ -246,13 +267,20 @@ def quick_setup_get_stage_structure(params: Mapping[str, Any]) -> Response:
246267 303 : "The stage validation and recap action has been started in the background. "
247268 "Redirecting to the 'Get background job status snapshot' endpoint."
248269 },
249- additional_status_codes = [303 ],
270+ additional_status_codes = [303 , 403 ],
250271 path_params = [QUICKSETUP_ID ],
251272 request_schema = QuickSetupStageActionRequest ,
252273 response_schema = QuickSetupStageActionResponse ,
253274)
254275def quicksetup_run_stage_action (params : Mapping [str , Any ]) -> Response :
255- """Run a Quick setup stage validation and recap action"""
276+ """Run a Quick setup stage validation and recap action
277+
278+ This endpoint performs permission validation but since permissions depend on dynamic actions
279+ and each action has its own required permission, they cannot be statically defined. If the
280+ required permissions for an action are not met, the endpoint returns a 403 Forbidden error
281+ informing the action ID and the missing permission.
282+ """
283+
256284 language = user .language
257285 body = params ["body" ]
258286 quick_setup_id = params ["quick_setup_id" ]
@@ -267,6 +295,15 @@ def quicksetup_run_stage_action(params: Mapping[str, Any]) -> Response:
267295 stage_index = StageIndex (len (body ["stages" ]) - 1 )
268296 stage_action = matching_stage_action (quick_setup .stages [stage_index ](), stage_action_id )
269297
298+ if stage_action .permissions is not None and not all (
299+ user .may (perm ) for perm in stage_action .permissions
300+ ):
301+ return _serve_error (
302+ title = "Action not allowed" ,
303+ detail = f"Action with id '{ stage_action_id } ' requires { ', ' .join (f"'{ x } '" for x in stage_action .permissions )} permissions." ,
304+ status_code = 403 ,
305+ )
306+
270307 built_stages = [stage () for stage in quick_setup .stages [: stage_index + 1 ]]
271308 form_spec_map = build_formspec_map_from_stages (built_stages )
272309 stages_raw_formspecs = [RawFormData (stage ["form_data" ]) for stage in body ["stages" ]]
@@ -384,7 +421,7 @@ def fetch_quick_setup_stage_action_result(params: Mapping[str, Any]) -> Response
384421 tag_group = "Checkmk Internal" ,
385422 path_params = [QUICKSETUP_ID ],
386423 query_params = [QUICKSETUP_MODE ],
387- additional_status_codes = [201 , 303 , 429 ],
424+ additional_status_codes = [201 , 303 , 403 , 429 ],
388425 status_descriptions = {
389426 303 : "The validation and complete action has been started in the background. "
390427 "Redirecting to the 'Get background job status snapshot' endpoint." ,
@@ -405,7 +442,7 @@ def quick_setup_run_action(params: Mapping[str, Any]) -> Response:
405442 tag_group = "Checkmk Internal" ,
406443 path_params = [QUICKSETUP_ID ],
407444 query_params = [QUICKSETUP_OBJECT_ID_REQUIRED ],
408- additional_status_codes = [201 , 303 , 429 ],
445+ additional_status_codes = [201 , 303 , 403 , 429 ],
409446 status_descriptions = {
410447 303 : "The validation and complete action has been started in the background. "
411448 "Redirecting to the 'Get background job status snapshot' endpoint." ,
@@ -450,6 +487,13 @@ def complete_quick_setup_action(params: Mapping[str, Any], mode: QuickSetupActio
450487 detail = f"Action with id '{ action_id } ' does not exist." ,
451488 )
452489
490+ if action .permissions is not None and not all (user .may (perm ) for perm in action .permissions ):
491+ return _serve_error (
492+ title = "Action not allowed" ,
493+ detail = f"Action with id '{ action_id } ' requires { ', ' .join (f"'{ x } '" for x in action .permissions )} permissions." ,
494+ status_code = 403 ,
495+ )
496+
453497 form_spec_map = build_formspec_map_from_stages ([stage () for stage in quick_setup .stages ])
454498 errors = validate_stages_form_data (
455499 stages_raw_form_data = [RawFormData (stage ["form_data" ]) for stage in body ["stages" ]],
0 commit comments