Skip to content

Conversation

@OrangeAndGreen
Copy link
Contributor

https://dimagi.atlassian.net/browse/CCCT-668

cross-request: dimagi/commcare-core#1455

Technical Summary

Some additional logging to help capture the failed token auths related to ConnectID-managed apps.
We're already logging the case where we're unable to get an HQ token just before making an HQ call.
But currently we still make the call anyway, which is guaranteed to fail.
Even once that's fixed, we still may get surprise failures due to tokens we thought were good actually not being good.
So this PR adds logging after HQ calls fail (key record or data pull requests) due to bad authorization when they should be managed by ConnectID via tokens.

Feature Flag

ConnectID

Safety Assurance

Safety story

Simple addition that silently logs a condition, and checks happen in a try-catch in case something goes wrong.

Automated test coverage

No automated tests for ConnectID yet

QA Plan

No extra QA plan necessary, this is for dev tracking.

Labels and Review

  • Do we need to enhance the manual QA test coverage ? If yes, the "QA Note" label is set correctly
  • Does the PR introduce any major changes worth communicating ? If yes, the "Release Note" label is set and a "Release Note" is specified in PR description.
  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

This pull request revises the error handling workflow for authentication failures related to ConnectID managed applications. In the LoginActivity and SyncCapableCommCareActivity classes, the handling for the AUTH_FAILED result now includes a check that identifies failed token authentication attempts through ConnectID. If such a failure is detected, an exception is logged with a detailed message, and previous actions like clearing credentials are removed or altered. Additionally, the CommcareRequestGenerator class’s authentication logic is streamlined by replacing a try-catch block with a direct conditional check using the new method. The new method checkForFailedConnectIdAuth added in the ConnectManager class encapsulates the logic to verify the ConnectID configuration and linked app status, further centralizing this aspect of error handling.

Sequence Diagram(s)

Authentication Failure Handling in LoginActivity and SyncCapableCommCareActivity

sequenceDiagram
    participant User
    participant Activity
    participant ConnectManager
    participant Logger

    User->>Activity: Initiate Login
    Activity->>Activity: Execute handlePullTaskResult
    Activity->>ConnectManager: checkForFailedConnectIdAuth(seatedAppId, username)
    ConnectManager-->>Activity: Boolean result
    alt Token auth failure detected
        Activity->>Logger: Log exception ("token authentication failed for ConnectID managed app")
    else
        Activity->>Activity: Proceed with standard bad credentials handling
    end
Loading

Authentication Flow in CommcareRequestGenerator

sequenceDiagram
    participant Component
    participant RequestGenerator
    participant ConnectManager
    participant Logger

    Component->>RequestGenerator: buildAuth() call
    RequestGenerator->>ConnectManager: checkForFailedConnectIdAuth(seatedAppId, username)
    ConnectManager-->>RequestGenerator: Boolean result
    alt Token auth failure detected
        RequestGenerator->>Logger: Log exception ("token authentication error for connect-managed app")
    else
        RequestGenerator->>Component: Return user session authentication
    end
Loading

Possibly related PRs

Suggested reviewers

  • shubham1g5
  • pm-dimagi

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 12a6338 and 5589445.

📒 Files selected for processing (4)
  • app/src/org/commcare/activities/LoginActivity.java (1 hunks)
  • app/src/org/commcare/activities/SyncCapableCommCareActivity.java (2 hunks)
  • app/src/org/commcare/connect/ConnectManager.java (1 hunks)
  • app/src/org/commcare/network/CommcareRequestGenerator.java (1 hunks)
🔇 Additional comments (5)
app/src/org/commcare/activities/LoginActivity.java (1)

881-885: Enhanced error logging for ConnectID authentication failures during login.

This addition allows the system to identify and log when authentication failures occur specifically for ConnectID managed applications during the login process. The code retrieves the seated app ID and checks if it's a ConnectID-managed app before logging a detailed exception.

app/src/org/commcare/activities/SyncCapableCommCareActivity.java (2)

33-33: Added Logger import for exception handling.

Added the necessary import to support the new exception logging functionality for ConnectID authentication failures.


109-114: Improved ConnectID auth failure handling during sync.

This change replaces the previous implementation that was forgetting app credentials with a more targeted approach that logs authentication failures specifically for ConnectID managed applications. The exception logging provides better visibility into auth token failures during sync operations.

app/src/org/commcare/connect/ConnectManager.java (1)

569-581: New utility method to check for ConnectID auth failures.

This method centralizes the logic for checking whether an authentication failure is related to a ConnectID managed application. It verifies if ConnectID is configured and whether the app is linked to a worker, with appropriate error handling. This method enables better logging throughout the application.

The exception handling is well-implemented, ensuring that any errors in the ConnectID check process won't affect the normal application flow.

app/src/org/commcare/network/CommcareRequestGenerator.java (1)

186-190: Streamlined authentication error handling for token failures.

This change refactors the authentication error logic to use the centralized checkForFailedConnectIdAuth method, simplifying the code while maintaining the same functionality. It properly logs token authentication errors for ConnectID managed applications when no token auth is available.

The implementation is more concise than the previous version and maintains the same error logging capability.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

case AUTH_FAILED:
String seatedAppId = CommCareApplication.instance().getCurrentApp().getUniqueId();
String username = CommCareApplication.instance().getRecordForCurrentUser().getUsername();
ConnectManager.forgetAppCredentials(seatedAppId, username);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is intentional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, getting rid of that on purpose. It was added with ConnectID-managed traditional apps in mind. But now that we know the token auth could fail for other reasons, we don't want to take the unnecessary step of forcing the app credentials to be setup again. The improved messaging is a better reaction to this error.

break;
case AUTH_FAILED:
String seatedAppId = CommCareApplication.instance().getCurrentApp().getUniqueId();
if(ConnectManager.checkForFailedConnectIdAuth(seatedAppId, uiController.getEnteredUsername())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move the seatedAppId eval in checkForFailedConnectIdAuth itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. 3078ade

@OrangeAndGreen OrangeAndGreen merged commit 06bf7d5 into connect_qa Mar 14, 2025
2 checks passed
@OrangeAndGreen OrangeAndGreen deleted the dv/log_hq_token_auth_failure branch March 14, 2025 15:44
@coderabbitai coderabbitai bot mentioned this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants