(self, hook, webhook_body_api, headers, requester_user)
| 124 | return triggers[0] |
| 125 | |
| 126 | def post(self, hook, webhook_body_api, headers, requester_user): |
| 127 | body = webhook_body_api.data |
| 128 | |
| 129 | permission_type = PermissionType.WEBHOOK_SEND |
| 130 | rbac_utils = get_rbac_backend().get_utils_class() |
| 131 | rbac_utils.assert_user_has_resource_db_permission( |
| 132 | user_db=requester_user, |
| 133 | resource_db=WebhookDB(name=hook), |
| 134 | permission_type=permission_type, |
| 135 | ) |
| 136 | |
| 137 | headers = self._get_headers_as_dict(headers) |
| 138 | headers = self._filter_authentication_headers(headers) |
| 139 | |
| 140 | # If webhook contains a trace-tag use that else create create a unique trace-tag. |
| 141 | trace_context = self._create_trace_context( |
| 142 | trace_tag=headers.pop(TRACE_TAG_HEADER, None), hook=hook |
| 143 | ) |
| 144 | |
| 145 | if hook == "st2" or hook == "st2/": |
| 146 | # When using st2 or system webhook, body needs to always be a dict |
| 147 | if not isinstance(body, dict): |
| 148 | type_string = get_json_type_for_python_value(body) |
| 149 | msg = "Webhook body needs to be an object, got: %s" % (type_string) |
| 150 | raise ValueError(msg) |
| 151 | |
| 152 | trigger = body.get("trigger", None) |
| 153 | payload = body.get("payload", None) |
| 154 | |
| 155 | if not trigger: |
| 156 | msg = "Trigger not specified." |
| 157 | return abort(http_client.BAD_REQUEST, msg) |
| 158 | |
| 159 | self._trigger_dispatcher_service.dispatch_with_context( |
| 160 | trigger=trigger, |
| 161 | payload=payload, |
| 162 | trace_context=trace_context, |
| 163 | throw_on_validation_error=True, |
| 164 | ) |
| 165 | else: |
| 166 | if not self._is_valid_hook(hook): |
| 167 | self._log_request("Invalid hook.", headers, body) |
| 168 | msg = "Webhook %s not registered with st2" % hook |
| 169 | return abort(http_client.NOT_FOUND, msg) |
| 170 | |
| 171 | triggers = self._hooks.get_triggers_for_hook(hook) |
| 172 | payload = {} |
| 173 | |
| 174 | payload["headers"] = headers |
| 175 | payload["headers_lower"] = {k.lower(): v for k, v in headers.items()} |
| 176 | payload["body"] = body |
| 177 | |
| 178 | # Dispatch trigger instance for each of the trigger found |
| 179 | for trigger_dict in triggers: |
| 180 | # TODO: Instead of dispatching the whole dict we should just |
| 181 | # dispatch TriggerDB.ref or similar |
| 182 | self._trigger_dispatcher_service.dispatch_with_context( |
| 183 | trigger=trigger_dict, |
nothing calls this directly
no test coverage detected