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

Add key package validation #393

Merged
merged 1 commit into from
Jan 7, 2025
Merged

Conversation

neekolas
Copy link
Contributor

@neekolas neekolas commented Jan 6, 2025

Summary by CodeRabbit

  • New Features

    • Added key package validation service to enhance envelope processing integrity
    • Introduced validation result tracking with a new IsOk flag
  • Bug Fixes

    • Improved error handling for key package validation process
  • Refactor

    • Updated service initialization to include validation service
    • Modified validation result communication approach

Copy link

coderabbitai bot commented Jan 6, 2025

Walkthrough

The pull request introduces key package validation functionality across multiple components of the system. A new validation service is added to the ReplicationServer and integrated into the message service. The changes modify the Service struct to include a validationService, update the constructor method to accept this service, and add a new method validateKeyPackage to handle validation of key packages. The validation approach shifts from error-based to a result-based model, with a new IsOk field added to the KeyPackageValidationResult struct to indicate validation status.

Changes

File Change Summary
pkg/api/message/service.go - Added validationService field to Service struct
- Updated NewReplicationApiService constructor to accept validationService
- Added validateKeyPackage method for key package validation
pkg/mlsvalidate/interface.go - Added IsOk bool field to KeyPackageValidationResult struct
pkg/mlsvalidate/service.go - Modified ValidateKeyPackages method to use result-based validation approach
- Populate KeyPackageValidationResult with validation status
pkg/server/server.go - Added validationService field to ReplicationServer struct
- Initialized validation service in NewReplicationServer
pkg/testutils/api/api.go - Added mock validation service in NewTestAPIServer
- Imported mlsvalidateMocks

Possibly related issues

  • Key package validation #360 (Key package validation): The changes directly address this issue by implementing a comprehensive key package validation mechanism across the system.

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.

Copy link
Contributor Author

neekolas commented Jan 6, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@neekolas neekolas mentioned this pull request Jan 6, 2025
@neekolas neekolas marked this pull request as ready for review January 6, 2025 19:50
@neekolas neekolas requested a review from a team as a code owner January 6, 2025 19:50
@mkysel
Copy link
Collaborator

mkysel commented Jan 6, 2025

this will be a massive performance hit if we have to add a roundtrip to the MLS validation service for every request.

The good news is that it can be done concurrently and is not on the DB hot path. It might increase latency of package writes, but should not impact throughput.

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: 3

🧹 Nitpick comments (4)
pkg/api/message/service.go (2)

62-68: Initialization of the new validationService
The service is correctly assigned in the returned Service instance. Consider adding a nil-check if you expect scenarios where validation might be optional or uninitialized.

 if err != nil {
   return nil, err
 }

-return &Service{
+svc := &Service{
     ctx:               ctx,
     log:               log,
     registrant:        registrant,
     store:             store,
     publishWorker:     publishWorker,
     subscribeWorker:   subscribeWorker,
     validationService: validationService,
 }
+if svc.validationService == nil {
+    svc.log.Warn("validationService is nil, skipping key package validation")
+}
+return svc, nil

411-437: Key package validation workflow

  1. Checks the payload type before calling the validation service.
  2. Makes a synchronous request to validate the key packages and examines the result.
  3. Returns an InvalidArgument code if validation fails.

This approach is well-structured. Consider covering these lines with direct unit tests to ensure the correct error codes are returned in various edge cases (e.g., empty or multiple key packages).

pkg/mlsvalidate/service.go (1)

121-126: Consider logging validation failures for debugging.

When validation fails, it would be helpful to log the failure reason for debugging purposes. This is especially important since we're silently converting errors into false results.

 if !response.IsOk {
+    log.Debug("Key package validation failed",
+        zap.String("error", response.ErrorMessage))
     out[i] = KeyPackageValidationResult{
         IsOk:            false,
         InstallationKey: nil,
         Credential:      nil,
         Expiration:      0,
     }
pkg/server/server.go (1)

Line range hint 102-115: Consider refactoring duplicate initialization logic.

The validation service initialization is duplicated between the indexer and API server code paths. Consider extracting it into a helper method to maintain DRY principles.

+func (s *ReplicationServer) initValidationService(ctx context.Context) error {
+    if s.validationService != nil {
+        return nil
+    }
+    var err error
+    s.validationService, err = mlsvalidate.NewMlsValidationService(
+        ctx,
+        s.log,
+        s.options.MlsValidation,
+    )
+    return err
+}

Then use it in both locations:

 if options.Indexer.Enable {
-    s.validationService, err = mlsvalidate.NewMlsValidationService(
-        ctx,
-        log,
-        options.MlsValidation,
-    )
-    if err != nil {
-        return nil, err
-    }
+    if err := s.initValidationService(ctx); err != nil {
+        return nil, err
+    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ab9d996 and b4f6507.

📒 Files selected for processing (5)
  • pkg/api/message/service.go (5 hunks)
  • pkg/mlsvalidate/interface.go (1 hunks)
  • pkg/mlsvalidate/service.go (1 hunks)
  • pkg/server/server.go (4 hunks)
  • pkg/testutils/api/api.go (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test (Node)
🔇 Additional comments (10)
pkg/api/message/service.go (5)

12-12: New import for validation service
No issues spotted. This import is required for accessing the validation interfaces.


16-16: Import for topic management
This import is consistent with referencing constant definitions in topic. No concerns.


36-42: Added validationService field
Introducing a dedicated validation service field is a good architectural approach that clearly segregates validation logic from other concerns. This fosters testability and maintainability.


50-50: Added validationService parameter to constructor
Ensuring the validation service is part of the constructor extends the possibility for dependency injection, enabling better control during testing.


330-336: Conditional key package validation
Good approach to invoke enhanced validation only when the topic kind is TOPIC_KIND_KEY_PACKAGES_V1. Ensure that future expansions to other topic kinds also handle or skip validation correctly.

pkg/mlsvalidate/interface.go (1)

13-13: Added IsOk field in the validation result
Having a dedicated boolean field clarifies success/failure state at a glance. Make sure to maintain consistent usage across all validation paths to avoid ambiguous results.

pkg/testutils/api/api.go (3)

17-17: Importing mock validation service
This import effectively supports testing the newly introduced validation logic. No issues spotted.


82-82: Mock validation service creation
Instantiating this mock is an excellent way to isolate the validation logic for unit tests, aiding in comprehensive coverage.


92-92: Wiring the mock into NewReplicationApiService
This ensures that the replication API tests will use the mock validation service, facilitating robust and isolated testing of your validation flows.

pkg/server/server.go (1)

39-47: LGTM! Field placement is appropriate.

The validationService field is correctly placed with other service-related fields in the ReplicationServer struct.

pkg/mlsvalidate/service.go Show resolved Hide resolved
pkg/server/server.go Show resolved Hide resolved
pkg/server/server.go Show resolved Hide resolved
Copy link
Contributor Author

neekolas commented Jan 6, 2025

Would just be requests for uploading key packages, which should be pretty low volume.

I would at some point in the project like to move the MLS Validation stuff from a RPC to some sort of embedded FFI call. There are uniffi bindings for Go, so it's possible in theory.

Copy link
Contributor Author

neekolas commented Jan 7, 2025

Merge activity

  • Jan 7, 12:25 PM PST: A user started a stack merge that includes this pull request via Graphite.
  • Jan 7, 12:25 PM PST: A user merged this pull request with Graphite.

@neekolas neekolas merged commit 5071a0c into main Jan 7, 2025
8 checks passed
@neekolas neekolas deleted the 01-06-add_key_package_validation branch January 7, 2025 20:25
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