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

fixed user name issue in the audit log for leave bot api #1567

Conversation

GMayank0310
Copy link
Contributor

@GMayank0310 GMayank0310 commented Oct 8, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a method to remove members from a bot, enhancing user management capabilities.
  • Improvements

    • Enhanced error handling for bot access management, ensuring better user feedback.
    • Improved audit logging to associate updates with the user initiating the action.
  • Refactor

    • Streamlined code for clarity and maintainability, removing redundant checks and comments.

Copy link

coderabbitai bot commented Oct 8, 2024

Walkthrough

The changes in this pull request involve modifications to the AccountProcessor class in kairon/shared/account/processor.py and the Auditlog class in kairon/shared/data/audit/data_objects.py. The remove_bot_access method has been updated to include conditional checks and streamlined logic for managing bot access. A new method, remove_member, has been introduced to handle member removals. Additionally, the update method in the Auditlog class has been modified to improve user logging during updates. Overall, these changes enhance bot access management and audit logging.

Changes

File Path Change Summary
kairon/shared/account/processor.py - Modified remove_bot_access to include accessor_email and streamline logic for status updates.
- Added remove_member method for user removal from bots.
- Improved error handling in remove_bot_access.
- General code cleanup for clarity.
kairon/shared/data/audit/data_objects.py - Updated update method in Auditlog to pass user from kwargs to auditlog.send, enhancing logging detail.
- Corrected indentation in the update method.

Possibly related PRs

Suggested reviewers

  • hiteshghuge

🐰 In the meadow where bunnies play,
A change was made to brighten the day.
With bots and members, we now can see,
Who's in and out, oh so clearly!
Access removed, logs now refined,
A hop of joy for the code we find! 🐇


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.

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.

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (2)
kairon/shared/account/processor.py (2)

601-606: LGTM! Consider adding a comment for clarity.

The changes to the remove_bot_access method look good. The logic for handling both single user removal and all users removal is correct.

Consider adding a brief comment to explain the difference between the two cases:

if kwargs:
    # Single user removal
    active_bot_access.update(set__status=ACTIVITY_STATUS.DELETED.value, user=kwargs.get('accessor_email'))
else:
    # All users removal
    active_bot_access.update(set__status=ACTIVITY_STATUS.DELETED.value)

Line range hint 1012-1037: LGTM! New method handles bot leaving process well.

The new process_leave_bot method is a great addition. It correctly handles the process of a user leaving a bot with appropriate checks and actions. The method prevents the bot owner from leaving and ensures that all integration tokens are deleted before a user can leave, which is a good security practice.

Consider adding more specific error messages for different scenarios:

if owner_info["accessor_email"] == current_user.email:
    raise AppException("Bot owner cannot leave the bot. Transfer ownership first if needed.")

if tokens_data:
    raise AppException("Please delete all your integration tokens for this bot before leaving.")

This will provide clearer guidance to users on why they can't leave the bot in specific situations.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cf77a0f and 3bb7465.

📒 Files selected for processing (2)
  • kairon/shared/account/processor.py (1 hunks)
  • kairon/shared/data/audit/data_objects.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
kairon/shared/data/audit/data_objects.py (1)

75-77: Approved: User logging enhancement implemented correctly.

The addition of user=kwargs.get('user') in the auditlog.send call effectively addresses the PR objective of fixing the user name issue in the audit log. This change is consistent with other methods in the class and doesn't break the existing method signature.

However, consider the following suggestions for further improvement:

  1. Add input validation for the 'user' parameter to ensure data integrity in the audit log.
  2. Consider using a default value for the user if it's not provided in kwargs, e.g., kwargs.get('user', 'system').

Here's a suggested improvement:

 def update(self, event_url=None, **kwargs):
     obj = super().update(**kwargs)
-    auditlog.send(self.__class__, document=self, action=AuditlogActions.UPDATE.value, event_url=event_url, user=kwargs.get('user'))
+    user = kwargs.get('user', 'system')
+    if not isinstance(user, str) or len(user) > 100:  # Adjust max length as needed
+        raise ValueError("Invalid user value for audit log")
+    auditlog.send(self.__class__, document=self, action=AuditlogActions.UPDATE.value, event_url=event_url, user=user)
     return obj

To ensure this change fully addresses the user name issue, please confirm:

  1. Are there any other locations in the codebase where user names are logged that might need similar updates?
  2. Is there a need to update existing audit log entries, or is this change sufficient for future logs?
✅ Verification successful

Verified: All relevant audit logging calls are updated.

No other instances of auditlog.send were found in the codebase that require similar updates.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other audit logging calls that might need similar updates
rg --type python 'auditlog\.send' -g '!kairon/shared/data/audit/data_objects.py'

Length of output: 113


Script:

#!/bin/bash
# Search for other audit logging calls that might need similar updates
rg -t py 'auditlog\.send' -g '!kairon/shared/data/audit/data_objects.py'

Length of output: 74

kairon/shared/account/processor.py (1)

Line range hint 608-614: LGTM! New method adds important checks.

The new remove_member method is a good addition. It correctly prevents users from removing themselves and ensures that the bot owner cannot be removed. The method then appropriately calls remove_bot_access for the actual member removal.

Copy link
Collaborator

@hiteshghuge hiteshghuge left a comment

Choose a reason for hiding this comment

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

approve

@hiteshghuge hiteshghuge merged commit 981b92a into digiteinfotech:master Oct 8, 2024
8 checks passed
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