-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(gitlab): Namespace webhook log attributes and add malformed-token context #117959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+69
−28
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,35 @@ def __call__( | |
| ) -> None: ... | ||
|
|
||
|
|
||
| def _extract_payload_repo_info(request) -> dict[str, Any]: | ||
| """ | ||
| Best-effort identifiers pulled from the webhook body. | ||
|
|
||
| The token (HTTP_X_GITLAB_TOKEN) is what we'd normally use to resolve the | ||
| integration/org, but when it's missing or malformed we can't. The payload | ||
| body is independent of the token, and GitLab events carry a ``project`` | ||
| object that identifies the repo/owner — enough to track down which customer | ||
| a bad webhook is coming from. Returns {} if anything is off. | ||
| """ | ||
| try: | ||
| payload = orjson.loads(request.body) | ||
| except (orjson.JSONDecodeError, TypeError, AttributeError): | ||
| return {} | ||
| if not isinstance(payload, dict): | ||
| return {} | ||
|
|
||
| project = payload.get("project") | ||
| project = project if isinstance(project, dict) else {} | ||
| info = { | ||
| # e.g. "cool-group/sentry" — the owning group/namespace plus repo | ||
| "webhook.repo.path": project.get("path_with_namespace"), | ||
| "webhook.repo.web_url": project.get("web_url"), | ||
| "webhook.repo.project_id": project.get("id"), | ||
| } | ||
| # Drop missing keys so the log attributes stay clean. | ||
| return {k: v for k, v in info.items() if v is not None} | ||
|
|
||
|
|
||
| def get_gitlab_external_id(request, extra) -> tuple[str, str] | HttpResponse: | ||
| token = "<unknown>" | ||
| try: | ||
|
|
@@ -78,17 +107,20 @@ def get_gitlab_external_id(request, extra) -> tuple[str, str] | HttpResponse: | |
| external_id = f"{instance}:{group_path}" | ||
| return (external_id, secret) | ||
| except KeyError: | ||
| extra["reason"] = "The customer needs to set a Secret Token in their webhook." | ||
| extra["webhook.reason"] = "The customer needs to set a Secret Token in their webhook." | ||
| logger.warning("gitlab.webhook.missing-gitlab-token", extra=extra) | ||
| return HttpResponse(status=400, reason=extra["reason"]) | ||
| return HttpResponse(status=400, reason=extra["webhook.reason"]) | ||
| except ValueError: | ||
| extra["reason"] = "The customer's Secret Token is malformed." | ||
| # The token is malformed so we can't resolve the integration/org from it. | ||
| # Fall back to identifiers in the payload body to find the source repo. | ||
| extra = {**extra, **_extract_payload_repo_info(request)} | ||
| extra["webhook.reason"] = "The customer's Secret Token is malformed." | ||
| logger.warning("gitlab.webhook.malformed-gitlab-token", extra=extra) | ||
| return HttpResponse(status=400, reason=extra["reason"]) | ||
| return HttpResponse(status=400, reason=extra["webhook.reason"]) | ||
| except Exception: | ||
| extra["reason"] = "Generic catch-all error." | ||
| extra["webhook.reason"] = "Generic catch-all error." | ||
| logger.warning("gitlab.webhook.invalid-token", extra=extra) | ||
| return HttpResponse(status=400, reason=extra["reason"]) | ||
| return HttpResponse(status=400, reason=extra["webhook.reason"]) | ||
|
|
||
|
|
||
| class GitlabWebhook(SCMWebhook, ABC): | ||
|
|
@@ -644,10 +676,10 @@ def post(self, request: HttpRequest) -> HttpResponse: | |
| clear_organization_info() | ||
| extra = { | ||
| # This tells us the Gitlab version being used (e.g. current gitlab.com version -> GitLab/15.4.0-pre) | ||
| "user-agent": request.META.get("HTTP_USER_AGENT"), | ||
| "webhook.user_agent": request.META.get("HTTP_USER_AGENT"), | ||
| # Gitlab does not seem to be the only host sending events | ||
| # AppPlatformEvents also hit this API | ||
| "event-type": request.META.get("HTTP_X_GITLAB_EVENT"), | ||
| "webhook.event_type": request.META.get("HTTP_X_GITLAB_EVENT"), | ||
| } | ||
| result = get_gitlab_external_id(request=request, extra=extra) | ||
| if isinstance(result, HttpResponse): | ||
|
|
@@ -661,49 +693,48 @@ def post(self, request: HttpRequest) -> HttpResponse: | |
| installs = org_contexts.organization_integrations | ||
| if integration is None: | ||
| logger.info("gitlab.webhook.invalid-organization", extra=extra) | ||
| extra["reason"] = "There is no integration that matches your organization." | ||
| logger.warning(extra["reason"]) | ||
| return HttpResponse(status=409, reason=extra["reason"]) | ||
| extra["webhook.reason"] = "There is no integration that matches your organization." | ||
| logger.warning(extra["webhook.reason"]) | ||
| return HttpResponse(status=409, reason=extra["webhook.reason"]) | ||
|
|
||
| extra = { | ||
| **extra, | ||
| **{ | ||
| "integration": { | ||
| # The metadata could be useful to debug | ||
| # domain_name -> gitlab.com/getsentry-ecosystem/foo' | ||
| # scopes -> ['api'] | ||
| "metadata": integration.metadata, | ||
| "id": integration.id, # This is useful to query via Redash | ||
| "status": integration.status, # 0 seems to be active | ||
| }, | ||
| "org_ids": [install.organization_id for install in installs], | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not supported |
||
| }, | ||
| # The metadata could be useful to debug | ||
| # domain_name -> gitlab.com/getsentry-ecosystem/foo' | ||
| # scopes -> ['api'] | ||
| "webhook.integration.metadata": integration.metadata, | ||
| "webhook.integration.id": integration.id, # This is useful to query via Redash | ||
| "webhook.integration.status": integration.status, # 0 seems to be active | ||
| # Logs/EAP attributes are scalar-first; a list serializes as an | ||
| # "array" attribute that the Logs explorer won't expose as a | ||
| # queryable column. Join to a string so it's filterable. | ||
| "webhook.org_ids": ",".join(str(install.organization_id) for install in installs), | ||
| } | ||
|
|
||
| if not constant_time_compare(secret, integration.metadata["webhook_secret"]): | ||
| # Summary and potential workaround mentioned here: | ||
| # https://github.com/getsentry/sentry/issues/34903#issuecomment-1262754478 | ||
| extra["reason"] = GITLAB_WEBHOOK_SECRET_INVALID_ERROR | ||
| extra["webhook.reason"] = GITLAB_WEBHOOK_SECRET_INVALID_ERROR | ||
| logger.info("gitlab.webhook.invalid-token-secret", extra=extra) | ||
| return HttpResponse(status=409, reason=GITLAB_WEBHOOK_SECRET_INVALID_ERROR) | ||
|
|
||
| try: | ||
| event = orjson.loads(request.body) | ||
| except orjson.JSONDecodeError: | ||
| extra["reason"] = "Data received is not JSON." | ||
| extra["webhook.reason"] = "Data received is not JSON." | ||
| logger.warning("gitlab.webhook.invalid-json", extra=extra) | ||
| return HttpResponse(status=400, reason=extra["reason"]) | ||
| return HttpResponse(status=400, reason=extra["webhook.reason"]) | ||
|
|
||
| try: | ||
| handler = self._handlers[request.META["HTTP_X_GITLAB_EVENT"]] | ||
| except KeyError: | ||
| supported_events = ", ".join(sorted(self._handlers.keys())) | ||
| extra["reason"] = ( | ||
| extra["webhook.reason"] = ( | ||
| "The customer has edited the webhook in Gitlab to include other types of events. We only support these kinds of events: %s" | ||
| % supported_events | ||
| ) | ||
| logger.warning("gitlab.webhook.wrong-event-type", extra=extra) | ||
| return HttpResponse(status=400, reason=extra["reason"]) | ||
| return HttpResponse(status=400, reason=extra["webhook.reason"]) | ||
|
|
||
| for install in installs: | ||
| org_context = organization_service.get_organization_by_id( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to flatten this