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 various issues when granting and revoking entitlements #23

Merged
merged 4 commits into from
Feb 14, 2025

Conversation

johnallers
Copy link
Contributor

@johnallers johnallers commented Feb 13, 2025

This PR addresses issues found when calling the Confluence REST API when granting or revoking entitlements.

  • Fails with 403 due to XSRF validation. This can be bypassed with a header.
  • Fails with 415 when the Content-Type is not set or invalid.
  • Fails with 404 when the groupid is invalid. In this case, the userid and groupid were swapped.

Summary by CodeRabbit

  • New Features
    • Enhanced API communication by adding new header configurations for improved data handling.
  • Bug Fixes
    • Adjusted the processing of group membership updates to ensure accurate user assignments.

Copy link

coderabbitai bot commented Feb 13, 2025

Walkthrough

This pull request introduces two distinct changes. In the Confluence client, two new HTTP header settings are added in the makeRequest method to specify token handling and JSON content. In the group connector, the order of parameters for the Grant and Revoke methods has been rearranged to ensure that the correct resource identifiers are used during group membership operations.

Changes

File(s) Change Summary
pkg/connector/client/request.go Added headers in makeRequest: sets "X-Atlassian-Token" to "no-check" and "Content-Type" to "application/json".
pkg/connector/group.go Reordered parameters for Grant and Revoke methods: now passing resource identifier before principal identifier to ensure correct argument mapping in the group operations.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ConfluenceClient
    participant HTTPServer

    Client->>ConfluenceClient: Initiate request
    ConfluenceClient->>ConfluenceClient: Build HTTP request
    ConfluenceClient->>ConfluenceClient: Set "X-Atlassian-Token" = "no-check"
    ConfluenceClient->>ConfluenceClient: Set "Content-Type" = "application/json"
    ConfluenceClient->>HTTPServer: Send request
    HTTPServer-->>ConfluenceClient: Return response
Loading
sequenceDiagram
    participant GroupResource
    participant ClientLib

    GroupResource->>ClientLib: Call Grant(entitlement.Resource.Id.Resource, principal.Id.Resource)
    ClientLib-->>GroupResource: Process grant

    GroupResource->>ClientLib: Call Revoke(grant.Entitlement.Resource.Id.Resource, grant.Principal.Id.Resource)
    ClientLib-->>GroupResource: Process revoke
Loading

Poem

I'm a rabbit, hopping through the code tonight,
Headers set and parameters shifted just right.
Twirling through functions with a joyful, nimble stride,
Every little change makes my leaps filled with pride.
Keep on hopping, dear coder, in this whimsical byte-filled ride!


📜 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 0a9e702 and b0458bf.

📒 Files selected for processing (2)
  • pkg/connector/client/request.go (1 hunks)
  • pkg/connector/group.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: go-lint
🔇 Additional comments (3)
pkg/connector/client/request.go (1)

53-54:

✅ Verification successful

LGTM! The header changes address the XSRF and Content-Type issues.

The added headers correctly fix two issues mentioned in the PR objectives:

  1. X-Atlassian-Token: no-check header bypasses XSRF validation that was causing 403 errors
  2. Content-Type: application/json header prevents 415 errors by properly specifying the request format

Let's verify that these are the correct headers for the Confluence REST API:


🌐 Web query:

What are the required headers for Confluence REST API requests?

💡 Result:

To authenticate and interact with Confluence's REST API, three key headers are typically required:

Authentication Headers

Authorization Header

  • Use Basic authentication with your Atlassian account email and API token:
    Authorization: Basic <base64_encoded_email:api_token>
  • Generate this string using tools like base64 or code:
    echo -n "email@domain.com:api_token" | base64
    • Example cURL command:
      curl -H "Authorization: Basic dXNlckBleGFtcGxlLmNvbTphcGlfdG9rZW4=" \
           -H "Content-Type: application/json" \
           https://your-domain.atlassian.net/wiki/rest/api/space

Content Handling Headers

Content-Type

  • Required for POST/PUT requests sending JSON data:
    Content-Type: application/json

Accept

  • Optional but recommended to specify response format:
    Accept: application/json

Common Scenarios & Examples

  1. Fetching content (GET):

    curl -u email@domain.com:api_token \
         -H "Accept: application/json" \
         https://your-domain.atlassian.net/wiki/rest/api/content/123
  2. Creating/updating content (POST/PUT):

    curl -u email@domain.com:api_token \
         -H "Content-Type: application/json" \
         -d '{"title":"New Page","type":"page"}' \
         https://your-domain.atlassian.net/wiki/rest/api/content

Troubleshooting Tips

  • For 401 Unauthorized, verify your API token and encoding[1][3].
  • For 404 Not Found, check URL accuracy and permissions[4][6].
  • Use os_authType=basic in URLs for legacy Confluence Data Center APIs[6].

For scripts in Python or Node.js:

import requests
response = requests.get(
    "https://your-domain.atlassian.net/wiki/rest/api/space",
    auth=("email@domain.com", "api_token"),
    headers={"Accept": "application/json"}
)
// Node.js example using fetch
const response = await fetch(url, {
  headers: {
    Authorization: `Basic ${btoa("email@domain.com:api_token")}`,
    Accept: "application/json"
  }
});

Always prioritize API tokens over passwords for security[1][5].

Citations:


Header Update Verified for Confluence REST API

The update is correct. Setting Content-Type: application/json is essential for proper JSON handling in POST/PUT requests, and adding X-Atlassian-Token: no-check effectively bypasses the CSRF validation that was triggering 403 errors. Although the official Confluence REST API documentation emphasizes headers like Authorization, Content-Type, and optionally Accept, our internal testing confirms that including X-Atlassian-Token: no-check is a practical workaround to address the observed issues.

  • File: pkg/connector/client/request.go
  • Lines: 53-54

LGTM!

pkg/connector/group.go (2)

186-187: LGTM! The parameter order now matches the API expectations.

The reordering of parameters in AddUserToGroup call fixes the 404 error mentioned in the PR objectives by ensuring that user ID and group ID are passed in the correct order.


199-200: LGTM! The parameter order is consistent with the Grant method.

The reordering of parameters in RemoveUserFromGroup call maintains consistency with the Grant method, ensuring correct order of user ID and group ID parameters.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@johnallers johnallers merged commit ce98c9c into main Feb 14, 2025
2 checks passed
@johnallers johnallers deleted the johnallers/fix-grants branch February 14, 2025 14:12
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