Skip to content

Add logging for JWT errors

Anton Smith requested to merge feature/add-logging-for-jwt-errors into master

What does this MR do and why?

Closes #447552 (closed)

When setting up the GitLab for Jira Cloud app, the request to JiraConnect::EventsController#installed (POST /jira_connect/events/installed) can return a 401. Even after checking the prerequisites, it can be difficult to diagnose the true reason behind the error as GitLab does not log the reason, nor does it show the user the reason either.

This change introduces logging to the rescue so we know the reason why the call fails.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Demo

This video shows one way to trigger an exception (use an invalid Jira Connect Proxy URL), and then afterwards we remove it.

147234-demo

Example logs

Before:

// # /var/log/gitlab/gitlab-rails/production_json.log
{
  "method": "POST",
  "path": "/-/jira_connect/events/installed",
  "format": "*/*",
  "controller": "JiraConnect::EventsController",
  "action": "installed",
  "status": 401,
  "time": "2024-03-05T20:14:19.758Z",
  "params": [
    { "key": "key", "value": "[FILTERED]" },
    { "key": "clientKey", "value": "<SANITIZED_CLIENT_KEY>" },
    { "key": "publicKey", "value": "<SANITIZED>" },
    { "key": "sharedSecret", "value": "[FILTERED]" },
    { "key": "serverVersion", "value": "100247" },
    { "key": "pluginsVersion", "value": "1001.0.0-SNAPSHOT" },
    { "key": "baseUrl", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "displayUrl", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "productType", "value": "jira" },
    { "key": "description", "value": "[FILTERED]" },
    { "key": "eventType", "value": "installed" },
    { "key": "displayUrlServicedeskHelpCenter", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "event", "value": {} }
  ],
  "correlation_id": "01HR841CVF5TSQG5W6G78E158R",
  "meta.caller_id": "JiraConnect::EventsController#installed",
  "meta.remote_ip": "<SANITIZED>",
  "meta.feature_category": "integrations",
  "meta.client_id": "ip/<SANITIZED>",
  "remote_ip": "<SANITIZED>",
  "ua": "Atlassian HttpClient unknown / JIRA-1001.0.0-SNAPSHOT (100247) / Atlassian-Connect/1001.0.0-SNAPSHOT",
  // truncated...
}

After:

// # /var/log/gitlab/gitlab-rails/production_json.log
{
  "method": "POST",
  "path": "/-/jira_connect/events/installed",
  "format": "*/*",
  "controller": "JiraConnect::EventsController",
  "action": "installed",
  "status": 401,
  "time": "2024-03-12T23:05:23.917Z",
  "params": [
    { "key": "key", "value": "[FILTERED]" },
    { "key": "clientKey", "value": "<SANITIZED_CLIENT_KEY>" },
    { "key": "publicKey", "value": "<SANITIZED>" },
    { "key": "sharedSecret", "value": "[FILTERED]" },
    { "key": "serverVersion", "value": "100247" },
    { "key": "pluginsVersion", "value": "1001.0.0-SNAPSHOT" },
    { "key": "baseUrl", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "displayUrl", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "productType", "value": "jira" },
    { "key": "description", "value": "[FILTERED]" },
    { "key": "eventType", "value": "installed" },
    { "key": "displayUrlServicedeskHelpCenter", "value": "https://<SANITIZED>.atlassian.net" },
    { "key": "event", "value": {} }
  ],
  "correlation_id": "01HRTEJRAGJESPEKW2NGT2KTW2",
  "meta.caller_id": "JiraConnect::EventsController#installed",
  "meta.remote_ip": "<SANITIZED>",
  "meta.feature_category": "integrations",
  "meta.client_id": "ip/<SANITIZED>",
  "remote_ip": "<SANITIZED>",
  "ua": "Atlassian HttpClient unknown / JIRA-1001.0.0-SNAPSHOT (100247) / Atlassian-Connect/1001.0.0-SNAPSHOT",
  // truncated...
}

// # /var/log/gitlab/gitlab-rails/exceptions_json.log
{
  "severity": "ERROR",
  "time": "2024-03-12T23:05:23.915Z",
  "correlation_id": "01HRTEJRAGJESPEKW2NGT2KTW2",
  "meta.caller_id": "JiraConnect::EventsController#installed",
  "meta.remote_ip": "<SANITIZED>",
  "meta.feature_category": "integrations",
  "meta.client_id": "ip/<SANITIZED>",
  "exception.class": "Atlassian::JiraConnect::Jwt::Asymmetric::KeyFetchError",
  "exception.message": "Atlassian::JiraConnect::Jwt::Asymmetric::KeyFetchError",
  "exception.backtrace": [
    "lib/atlassian/jira_connect/jwt/asymmetric.rb:68:in `retrieve_public_key'",
    "lib/atlassian/jira_connect/jwt/asymmetric.rb:39:in `block in claims'",
    "lib/atlassian/jira_connect/jwt/asymmetric.rb:37:in `claims'",
    "lib/atlassian/jira_connect/jwt/asymmetric.rb:25:in `valid?'",
    "app/controllers/jira_connect/events_controller.rb:55:in `verify_asymmetric_atlassian_jwt!'",
    // truncated...
  ],
  "user.username": null,
  "tags.program": "web",
  "tags.locale": "en",
  "tags.feature_category": "integrations",
  "tags.correlation_id": "01HRTEJRAGJESPEKW2NGT2KTW2"
}

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

I'm not sure how to reproduce this in the GDK since it would require the GDK to be publicly accessible - I just patched an Omnibus install to test this:

  1. Patch an Omnibus install exposed to the internet with https://handbook.gitlab.com/handbook/support/workflows/patching_an_instance/
  2. To test this MR works - configure an invalid Jira Connect Proxy URL ie. https://google.com https://docs.gitlab.com/ee/administration/settings/jira_cloud_app.html#set-up-your-instance
  3. Install the GitLab for Jira Cloud app manually into Jira https://docs.gitlab.com/ee/administration/settings/jira_cloud_app.html#install-the-gitlab-for-jira-cloud-app-manually
  4. The install should fail, and you should see a relevant error in the exceptions_json.log
Edited by Anton Smith

Merge request reports