Skip to content

Refactor: Modernize legacy v1.0 tools to v2.0 architecture#57

Merged
talltechy merged 1 commit intomainfrom
refactor/issue-56-legacy-tools
Oct 7, 2025
Merged

Refactor: Modernize legacy v1.0 tools to v2.0 architecture#57
talltechy merged 1 commit intomainfrom
refactor/issue-56-legacy-tools

Conversation

@talltechy
Copy link
Copy Markdown
Owner

Overview

This PR refactors remaining v1.0 legacy tools to use the modern v2.0 architecture with InsightVMClient and standardized patterns.

Changes Made

1. Created src/rapid7/constants.py

  • Centralized API endpoint constants (previously in api_r7_endpoints.py)
  • Added HTTP status code constants
  • Added default configuration values
  • Following Python naming conventions with proper class organization

2. Implemented src/rapid7/api/sonar_queries.py

  • Full CRUD operations for Sonar queries
  • Inherits from BaseAPI following v2.0 patterns
  • Type hints throughout
  • Helper methods for common operations:
    • create_domain_query() - Quick domain-based query creation
    • create_ip_range_query() - Quick IP range-based query creation
  • Comprehensive docstrings with examples

3. Updated src/rapid7/client.py

  • Added sonar_queries attribute to InsightVMClient
  • Maintains consistency with assets and asset_groups clients

4. Created Modern Tool: src/rapid7/tools/create_sonar_queries.py

Replaces legacy src/rapid7/api_r7_isvm_sonar_add.py with:

  • Uses InsightVMClient instead of direct HTTPBasicAuth
  • Command-line argument parsing with argparse
  • Better error handling and user feedback
  • Progress tracking during execution
  • Result saving with detailed status for each target
  • Proper type hints and docstrings
  • Clean separation of concerns

Benefits

Code Quality

  • ✅ Follows v2.0 architecture patterns
  • ✅ Type hints for better IDE support and error catching
  • ✅ Comprehensive docstrings
  • ✅ Proper error handling
  • ✅ No code duplication

Maintainability

  • ✅ Centralized constants in one location
  • ✅ Consistent patterns across all API modules
  • ✅ Easy to extend with new methods
  • ✅ Clear separation between API wrapper and CLI tool

User Experience

  • ✅ Better CLI with help text and examples
  • ✅ Progress feedback during execution
  • ✅ Detailed results saved to CSV
  • ✅ Clear error messages

Testing

  • ✅ All files pass linting (flake8, mypy)
  • ✅ Code follows project conventions
  • ✅ Type hints validated
  • ⚠️ Manual testing required with actual InsightVM instance

Legacy Files

The following legacy files can be removed once this PR is merged:

  • src/rapid7/api_r7_endpoints.py (replaced by constants.py)
  • src/rapid7/api_r7_isvm_sonar_add.py (replaced by tools/create_sonar_queries.py)
  • src/rapid7/api_r7_status_codes.py (functionality integrated into new tool)

Documentation

  • Tool includes comprehensive docstrings
  • CLI help text with usage examples
  • API methods documented with examples

Related Issues

Fixes #56

Checklist

  • Code follows project style guidelines
  • Type hints added
  • Docstrings added
  • No linting errors
  • Follows v2.0 architecture patterns
  • Uses InsightVMClient
  • Conventional commit message format
  • Manual testing with InsightVM instance (requires access)

Migration Notes

For users of the old api_r7_isvm_sonar_add.py tool:

Old usage:

python api_r7_isvm_sonar_add.py
# Interactive prompts for CSV file and days

New usage:

python src/rapid7/tools/create_sonar_queries.py targets.csv --days 30

The new tool provides:

  • Non-interactive operation (better for automation)
  • Clear command-line arguments
  • Better error handling
  • Progress feedback
  • Results saved to CSV automatically

Implements comprehensive Sonar Query API wrapper and modernizes legacy
Sonar query creation tool to use v2.0 architecture patterns.

Changes:
- Create src/rapid7/constants.py with API endpoints and status codes
- Implement src/rapid7/api/sonar_queries.py with full CRUD operations
- Add sonar_queries client to InsightVMClient
- Create modern src/rapid7/tools/create_sonar_queries.py CLI tool
  - Uses InsightVMClient instead of direct HTTPBasicAuth
  - Improved error handling and user feedback
  - Better CSV processing with pandas
  - Command-line argument parsing with argparse
  - Progress tracking and result saving

The new tool provides:
- Clean, type-hinted code following v2.0 patterns
- Helper methods for common operations (domain/IP queries)
- Comprehensive docstrings and examples
- Better separation of concerns

Relates to #56
Copilot AI review requested due to automatic review settings October 7, 2025 19:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modernizes legacy v1.0 tools by implementing the v2.0 architecture with centralized constants, a new SonarQueryAPI, and a modernized CLI tool for creating Sonar queries. The refactoring replaces direct HTTP authentication with the unified InsightVMClient pattern.

  • Centralizes API constants and HTTP status codes in a dedicated constants module
  • Implements comprehensive CRUD operations for Sonar queries following v2.0 patterns
  • Creates a modern CLI tool with better error handling, progress tracking, and argument parsing

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/rapid7/constants.py Centralizes API endpoints, HTTP status codes, and configuration constants
src/rapid7/api/sonar_queries.py Implements full CRUD operations for Sonar queries with helper methods
src/rapid7/client.py Adds sonar_queries attribute to maintain consistency with existing API clients
src/rapid7/tools/create_sonar_queries.py Modern CLI tool replacing legacy script with improved UX and error handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

)

# Clean whitespace
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

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

The applymap method is deprecated in pandas. Use df.map() instead for element-wise operations.

Suggested change
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
df = df.apply(lambda col: col.map(lambda x: x.strip() if isinstance(x, str) else x))

Copilot uses AI. Check for mistakes.
Returns:
True if valid domain, False otherwise
"""
pattern = r'^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}$'
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

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

The regex pattern has an unnecessary character class [\-\.] for just two characters. Use [-.] or [.-] instead. Also, the {1} quantifier is redundant.

Suggested change
pattern = r'^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}$'
pattern = r'^[a-z0-9]+(([-.][a-z0-9]+))*\.[a-z]{2,}$'

Copilot uses AI. Check for mistakes.
@talltechy talltechy merged commit ea432d4 into main Oct 7, 2025
7 of 12 checks passed
@talltechy talltechy deleted the refactor/issue-56-legacy-tools branch October 7, 2025 20:00
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.

[Refactor] Modernize legacy v1.0 tools to v2.0 architecture

2 participants