Skip to content
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

fix(webAPI): Specifying EndUserId on the ServiceOwner Search endpoint produces 500 - Internal Server error #1234

Merged
merged 5 commits into from
Oct 7, 2024

Conversation

knuhau
Copy link
Collaborator

@knuhau knuhau commented Oct 7, 2024

GetOrCreateClaimsBasedOnEndUserId() caused this. Dropping the method entirely fixes the problem and simplifies the code.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced claim handling for authorization requests, simplifying the process of adding specific claims based on the end user's credentials.
    • Improved authorization logic to incorporate end user ID checks for accessing dialog details.
  • Bug Fixes

    • Adjusted logic to ensure claims are populated correctly when the end user ID is null or invalid.
    • Updated method signatures for clarity and usability, removing unnecessary parameters.

@knuhau knuhau requested a review from a team as a code owner October 7, 2024 08:41
Copy link
Contributor

coderabbitai bot commented Oct 7, 2024

Warning

Rate limit exceeded

@knuhau has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between c5cfe7d and 009513e.

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to several classes and interfaces related to authorization within the Digdir.Domain.Dialogporten namespace. Key updates include the introduction of a new method AddClaimIfExists in the AltinnAuthorizationClient class, which simplifies claims handling by removing the GetOrCreateClaimsBasedOnEndUserId method. The IAltinnAuthorization interface has had the optional endUserId parameter removed from method signatures, affecting how authorization methods are called. Additionally, the GetDialogQueryHandler class has been updated to incorporate the end user ID in authorization checks.

Changes

File Change Summary
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs - Added method AddClaimIfExists for claim checking and addition.
- Removed method GetOrCreateClaimsBasedOnEndUserId.
src/Digdir.Domain.Dialogporten.Application/Externals/AltinnAuthorization/IAltinnAuthorization.cs - Updated method signatures to remove optional endUserId parameter from GetDialogDetailsAuthorization and GetAuthorizedResourcesForSearch.
src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Get/GetDialogQuery.cs - Modified Handle method to include request.EndUserId in authorization checks and updated handling of authorization results.
src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQuery.cs - Corrected comment regarding SystemLabel property; no functional changes.
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/LocalDevelopmentAltinnAuthorization.cs - Updated method signatures to remove unused parameters and set default values for others.

Possibly related PRs

Suggested reviewers

  • oskogstad
  • MagnusSandgren

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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.

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range comments (1)
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (1)

Line range hint 193-206: Improved claim handling logic.

The changes to the GetOrCreateClaimsBasedOnEndUserId method enhance the flexibility and security of claim handling. The use of a list instead of an array allows for more dynamic claim management. The separation of logic based on the validity of endUserId provides better control over which claims are included.

Consider adding a comment explaining the rationale behind adding only specific claims when a valid endUserId is provided, as this change in behavior might not be immediately obvious to other developers.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 0082310 and 62c79b6.

📒 Files selected for processing (1)
  • src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (2 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (1)

Line range hint 1-241: Address lack of tests for the changes.

While the changes to the AltinnAuthorizationClient class appear to address the 500 Internal Server error issue, it's concerning that no new tests have been added to verify this fix. Given the critical nature of authorization handling, it's important to ensure that these changes are properly tested.

Please consider adding the following tests:

  1. A unit test for the new AddClaimIfExists method to verify its behavior with various input scenarios.
  2. Integration tests for the GetOrCreateClaimsBasedOnEndUserId method to ensure it correctly handles different endUserId values and produces the expected claim sets.
  3. An end-to-end test that simulates the ServiceOwner Search endpoint with a specified EndUserId to confirm that the 500 Internal Server error has been resolved.

Additionally, update the PR description to include information about these new tests once they are added.

To help identify existing tests that might need updating, you can run the following command:

This will help locate any existing test files that might need to be updated to account for the new changes.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 62c79b6 and 0961db8.

📒 Files selected for processing (1)
  • src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (2 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (1)

Line range hint 1-231: Request for clarification and additional tests.

The changes appear to address the issue by simplifying claims handling. However, there are two concerns:

  1. The PR objective mentions fixing an error when specifying EndUserId, but the changes don't explicitly handle EndUserId. Could you clarify how these changes resolve the specific issue with EndUserId?

  2. No new tests were added for this bug fix. Consider adding unit tests to verify the fix and prevent regression.

To help verify the changes, you could add unit tests that:

  1. Mock the user principal with various claim configurations.
  2. Call the modified methods with different inputs, including cases where EndUserId is specified.
  3. Assert that the expected authorization results are returned.

Would you like assistance in drafting these unit tests?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 0961db8 and c5cfe7d.

📒 Files selected for processing (5)
  • src/Digdir.Domain.Dialogporten.Application/Externals/AltinnAuthorization/IAltinnAuthorization.cs (0 hunks)
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Get/GetDialogQuery.cs (0 hunks)
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQuery.cs (1 hunks)
  • src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/AltinnAuthorizationClient.cs (2 hunks)
  • src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/LocalDevelopmentAltinnAuthorization.cs (1 hunks)
💤 Files with no reviewable changes (2)
  • src/Digdir.Domain.Dialogporten.Application/Externals/AltinnAuthorization/IAltinnAuthorization.cs
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Get/GetDialogQuery.cs
🧰 Additional context used
📓 Learnings (1)
src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQuery.cs (1)
Learnt from: elsand
PR: digdir/dialogporten#1192
File: src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogLabelAssignmentLog/Queries/Search/SearchDialogLabelAssignmentLogQuery.cs:29-31
Timestamp: 2024-10-03T06:14:49.015Z
Learning: In this codebase, null checks are used in constructors by convention to ensure proper wiring in non-DI contexts, such as tests.
🔇 Additional comments (7)
src/Digdir.Domain.Dialogporten.Infrastructure/Altinn/Authorization/LocalDevelopmentAltinnAuthorization.cs (6)

24-28: LGTM: Simplified method signature

The removal of the unused parameter string? _ from the GetDialogDetailsAuthorization method signature is a good cleanup. This change simplifies the method without affecting its functionality.


29-31: LGTM: Simplified method signature and improved usability

The removal of the endUserId parameter and the addition of a default value for cancellationToken are good improvements. These changes simplify the method signature and enhance its usability without affecting the core functionality.


Line range hint 38-39: Consider reviewing the Take() limits

The method uses Take(1000) for parties and resources, and Take(30) for roles. While these limits prevent excessive data retrieval, they might need review to ensure they meet the application's requirements and scalability needs.

Could you provide more context on why these specific limits were chosen? Are there any performance considerations or business requirements that dictate these values?

Also applies to: 41-41


Line range hint 54-55: LGTM: Improved parameter naming

Renaming the __ parameter to cancellationToken enhances code readability. This change makes the method signature more self-explanatory without altering its functionality.


Line range hint 53-55: Consider reviewing the SuppressMessage attribute

The method is marked with [SuppressMessage("Performance", "CA1822:Mark members as static")]. Given that the method doesn't use any instance members, consider making it static or review why this suppression is necessary.

Could you explain why this method isn't marked as static? Is there a specific reason for suppressing this warning instead of addressing it?


Line range hint 1-57: Overall, changes align well with PR objectives

The modifications to LocalDevelopmentAltinnAuthorization class successfully simplify the code by removing unused parameters and improving method signatures. These changes are consistent with the PR objectives of resolving the 500 Internal Server Error issue and simplifying the code.

Key improvements:

  1. Removal of unused endUserId parameter from GetAuthorizedResourcesForSearch.
  2. Simplified signatures for all methods.
  3. Improved parameter naming for better readability.

While the changes are generally good, consider addressing the following:

  1. Review the Take() limits in GetAuthorizedResourcesForSearch.
  2. Evaluate the need for the SuppressMessage attribute in GetAuthorizedParties.

These considerations could further enhance the code quality and maintainability.

src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Queries/Search/SearchDialogQuery.cs (1)

Line range hint 1-241: Overall code structure and alignment with PR objectives.

The file is well-structured and implements a comprehensive search functionality for dialogs. It aligns with the PR objectives by providing the necessary query parameters and handling logic for the ServiceOwner Search endpoint. The code follows good practices, including proper separation of concerns and use of LINQ for query composition.

While the changes in this file are minimal, the overall implementation supports the goal of resolving the issue with the ServiceOwner Search endpoint. The SearchDialogQueryHandler class, in particular, seems to handle the search logic effectively, including the case where an end user is impersonated.

Copy link

sonarqubecloud bot commented Oct 7, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
C Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@knuhau knuhau merged commit 49c0d34 into main Oct 7, 2024
21 of 22 checks passed
@knuhau knuhau deleted the fix/enduserid-for-SO-search-broken branch October 7, 2024 10:36
oskogstad pushed a commit that referenced this pull request Oct 7, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.22.0](v1.21.0...v1.22.0)
(2024-10-07)


### Features

* Add support for supplied transmission attachment ID on create/update
([#1242](#1242))
([c7bfb07](c7bfb07))


### Bug Fixes

* Only allow legacy HTML on AditionalInfo content
([#1210](#1210))
([aa4acde](aa4acde))
* **webAPI:** Specifying EndUserId on the ServiceOwner Search endpoint
produces 500 - Internal Server error
([#1234](#1234))
([49c0d34](49c0d34))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
@coderabbitai coderabbitai bot mentioned this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants