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

feat: net/http api #2057

Merged
merged 11 commits into from
Sep 4, 2024
Merged

feat: net/http api #2057

merged 11 commits into from
Sep 4, 2024

Conversation

chronark
Copy link
Collaborator

@chronark chronark commented Aug 31, 2024

  • test(swr.test.ts): add test case for revalidating data with fresh=0 option
  • wip

Summary by CodeRabbit

  • New Features

    • Introduced a migrate-clickhouse task for database migrations.
    • Added ClickHouse integration for enhanced data handling.
    • New configuration options for ClickHouse connection details.
    • Implemented a structured API request logging system with ClickHouse.
    • Introduced middleware for HTTP metrics collection using Prometheus.
    • Added support for user management and quotas in ClickHouse.
    • Introduced OpenAPI specification for improved API documentation.
  • Bug Fixes

    • Improved request handling and logging capabilities.
  • Documentation

    • Updated configuration files for ClickHouse deployment and management.
    • Added OpenAPI documentation for API endpoints.
  • Chores

    • Enhanced Docker Compose setup to include ClickHouse services.

@chronark chronark requested a review from perkinsjr as a code owner August 31, 2024 13:25
Copy link

vercel bot commented Aug 31, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 3, 2024 6:24pm
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 3, 2024 6:24pm
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
dashboard ⬜️ Ignored (Inspect) Visit Preview Sep 3, 2024 6:24pm
planetfall ⬜️ Ignored (Inspect) Visit Preview Sep 3, 2024 6:24pm
workflows ⬜️ Ignored (Inspect) Visit Preview Sep 3, 2024 6:24pm

Copy link

changeset-bot bot commented Aug 31, 2024

⚠️ No Changeset found

Latest commit: 194f033

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chronark chronark changed the title clickhouse [WIP] clickhouse Aug 31, 2024
Copy link
Contributor

github-actions bot commented Aug 31, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

coderabbitai bot commented Aug 31, 2024

Walkthrough

Walkthrough

The changes introduce integration of ClickHouse into the Go application, including new migration tasks and updates to configuration files for ClickHouse connection details. New files facilitate ClickHouse interactions, such as client and schema definitions, while existing files are updated to support ClickHouse functionality, improve error handling, and enhance request logging. Additionally, middleware for metrics and logging is introduced, along with restructuring of various API routes for better handling of requests and responses.

Changes

Files Change Summary
apps/agent/Dockerfile Added a command to copy the openapi.json file from the builder stage into the container.
apps/agent/Taskfile.yml Added new tasks migrate-clickhouse and migrate-db for executing database migrations. Updated the seed task to depend on these new tasks.
apps/agent/cmd/agent/agent.go Integrated ClickHouse by modifying the run function to include ClickHouse configuration and instance creation. Removed previous registrations related to vault and ratelimit services.
apps/agent/config.docker.json Added a new configuration section for ClickHouse with connection parameters. Removed authToken from ratelimit and eventRouter services.
apps/agent/config.production.json Introduced a new authToken property at the root level and removed it from ratelimit and eventRouter services.
apps/agent/config.staging.json Added a new authToken property at the root level and removed it from ratelimit and eventRouter services.
apps/agent/gen/openapi/gen.go Generated data models and request/response structures for OpenAPI interactions.
apps/agent/go.mod Added several new dependencies related to ClickHouse and other libraries for enhanced functionality.
apps/agent/pkg/api/errors/internal_server_error.go Created a new error handling function HandleError for internal server errors.
apps/agent/pkg/api/errors/validation_error.go Introduced a new function HandleValidationError for handling validation errors.
apps/agent/pkg/api/mw_logging.go Implemented middleware for logging HTTP requests and responses, capturing relevant details for monitoring.
apps/agent/pkg/api/mw_metrics.go Implemented middleware for HTTP metrics collection using Prometheus.
apps/agent/pkg/api/register_routes.go Defined a method RegisterRoutes to set up various API routes, including ClickHouse integration.
apps/agent/pkg/api/routes/not_found/handler.go Implemented a handler for unmatched routes, returning a structured 404 error response.
apps/agent/pkg/api/routes/sender.go Defined an interface Sender and a concrete implementation JsonSender for managing HTTP responses.
apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go Transitioned to a new handler approach for rate limit commit lease, simplifying request handling.
apps/agent/pkg/api/routes/v1_ratelimit_multiRatelimit/handler.go Updated the handler to streamline request processing and integrate validation.
apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler.go Restructured request and response handling for rate limit service, simplifying the code.
apps/agent/pkg/api/routes/v1_vault_decrypt/handler.go Refactored the decryption handler to streamline request processing and improve error management.
apps/agent/pkg/api/routes/v1_vault_encrypt/handler.go Restructured the encryption handler for better clarity and efficiency.
apps/agent/pkg/api/routes/v1_vault_encrypt_bulk/handler.go Refactored the bulk encryption handler to simplify request and response handling.
apps/agent/pkg/clickhouse/client.go Implemented a ClickHouse client for connection handling and batch processing of API requests.
apps/agent/pkg/clickhouse/flush.go Added a generic flush function for batch data insertion into ClickHouse.
apps/agent/pkg/clickhouse/noop.go Created a no-op implementation of the Bufferer interface for testing purposes.
apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql Added SQL to create a table for storing API request data in ClickHouse.
apps/agent/pkg/clickhouse/schema/requests.go Defined ApiRequestV1 structure for representing API request data.
deployment/docker-compose.yaml Integrated ClickHouse as a service within Docker Compose, including environment variables for configuration.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Server
    participant Clickhouse
    participant API

    User->>Server: Send API Request
    Server->>API: Process Request
    API->>Clickhouse: BufferApiRequest
    Clickhouse-->>API: Acknowledge Buffering
    API-->>Server: Return Response
    Server-->>User: Send Response
Loading

Tip

We have updated our review workflow to use the Anthropic's Claude family of models. Please share any feedback in the discussion post on our Discord.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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
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: 5

Outside diff range, codebase verification and nitpick comments (4)
deployment/config/clickhouse/etc/clickhouse-server/config.d/config.xml (1)

1-30: Review network security settings and approve configuration.

The configuration for the ClickHouse server is generally well-set, with appropriate logging levels, file paths, and system settings. However, the listen_host is set to 0.0.0.0, which allows connections from any IP address. This setting should be reviewed to ensure it aligns with the intended network security policies, especially if the server is accessible over public networks.

Consider restricting this setting to specific IP addresses or ensure adequate firewall rules are in place to protect the server.

deployment/config/clickhouse/etc/clickhouse-keeper/keeper_config.xml/keeper_config.xml (1)

1-28: Review network security settings and approve configuration.

The configuration for the ClickHouse Keeper is generally well-set, with appropriate logging levels, file paths, and system settings. However, the listen_host is set to 0.0.0.0, which allows connections from any IP address. This setting should be reviewed to ensure it aligns with the intended network security policies, especially if the server is accessible over public networks.

Consider restricting this setting to specific IP addresses or ensure adequate firewall rules are in place to protect the server.

packages/cache/src/swr.test.ts (1)

83-106: Enhance Test Clarity with Comments

The test suite with fresh=0 is well-constructed and effectively tests the revalidation process. However, adding comments to explain the logic within the loop, especially the condition if (i > 1), would enhance readability and maintainability.

apps/agent/pkg/api/server.go (1)

37-37: Approved addition of clickhouse field in Server struct.

The new clickhouse field integrates ClickHouse functionality, enhancing the server's capability to handle events and logs. Ensure that this field is checked for nil before usage to prevent potential runtime panics.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fde3d83 and 6e033a3.

Files ignored due to path filters (1)
  • apps/agent/go.sum is excluded by !**/*.sum
Files selected for processing (21)
  • apps/agent/Taskfile.yml (2 hunks)
  • apps/agent/cmd/agent/agent.go (2 hunks)
  • apps/agent/config.docker.json (1 hunks)
  • apps/agent/go.mod (5 hunks)
  • apps/agent/pkg/api/interface.go (1 hunks)
  • apps/agent/pkg/api/routes/services.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go (3 hunks)
  • apps/agent/pkg/api/server.go (4 hunks)
  • apps/agent/pkg/clickhouse/client.go (1 hunks)
  • apps/agent/pkg/clickhouse/flush.go (1 hunks)
  • apps/agent/pkg/clickhouse/interface.go (1 hunks)
  • apps/agent/pkg/clickhouse/noop.go (1 hunks)
  • apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql (1 hunks)
  • apps/agent/pkg/clickhouse/schema/requests.go (1 hunks)
  • apps/agent/pkg/config/agent.go (1 hunks)
  • deployment/config/clickhouse/etc/clickhouse-keeper/keeper_config.xml/keeper_config.xml (1 hunks)
  • deployment/config/clickhouse/etc/clickhouse-server/config.d/config.xml (1 hunks)
  • deployment/config/clickhouse/etc/clickhouse-server/users.d/users.xml (1 hunks)
  • deployment/docker-compose.yaml (4 hunks)
  • fly (1 hunks)
  • packages/cache/src/swr.test.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • apps/agent/pkg/clickhouse/noop.go
  • fly
Additional comments not posted (29)
apps/agent/pkg/api/interface.go (1)

5-7: Interface Definition Approved

The EventIngester interface is well-defined and aligns with Go's conventions for interface design. However, consider adding documentation to the interface and its method to enhance clarity and maintainability.

apps/agent/pkg/clickhouse/interface.go (1)

7-9: Interface Definition Approved

The Ingester interface is correctly defined. However, it appears similar to the EventIngester interface in another package. Consider evaluating the necessity of maintaining both interfaces to avoid redundancy and confusion.

apps/agent/pkg/api/routes/services.go (1)

12-16: Struct Enhancement Approved

The addition of the Clickhouse field to the Services struct is well-implemented and aligns with the integration objectives. Consider adding documentation to the Services struct to describe the role and usage of each field, particularly the new Clickhouse field, to enhance clarity and maintainability.

apps/agent/pkg/clickhouse/schema/requests.go (1)

3-14: Well-defined struct for ClickHouse integration.

The ApiRequestV1 struct is well-defined with appropriate data types and ClickHouse tags. Ensure that the field names and data types align with the corresponding database schema to avoid integration issues.

Run the following script to verify the alignment with the database schema:

apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql (1)

1-26: Well-structured SQL script for ClickHouse table creation.

The SQL script for creating the api_requests__v1 table is well-structured and includes detailed comments. Ensure that the table schema aligns with the application's data model and the ApiRequestV1 struct to maintain consistency across the application.

Run the following script to verify the alignment with the application's data model:

apps/agent/pkg/clickhouse/flush.go (1)

12-28: Well-implemented generic batch insertion function.

The flush function is well-implemented with appropriate error handling and use of generics. The use of fault.Wrap for error handling is consistent and provides clear, contextual error messages which are beneficial for debugging. The function signature using generics (T any) is flexible and suitable for this use case, allowing any type that can be serialized into a ClickHouse row to be processed.

Consider adding a comment explaining the use of generics if this approach is not commonly used across the project, to aid other developers in understanding this choice.

apps/agent/config.docker.json (1)

47-50: Ensure secure handling of sensitive configuration data.

The new ClickHouse configuration section includes sensitive information such as username and password. It's crucial to ensure that these values are securely handled and not exposed in the codebase. Using environment variables, as done here, is a good practice as it keeps sensitive data out of the source code. However, ensure that these environment variables are securely managed and not exposed in logs or error messages.

deployment/config/clickhouse/etc/clickhouse-server/users.d/users.xml (1)

1-37: Review and validate ClickHouse user configuration settings.

This new XML configuration file sets various user permissions and system settings for ClickHouse. It's important to ensure that these settings are appropriate for your use case. For example, the <max_memory_usage> setting is quite high, which could impact system performance if not monitored. Additionally, the <access_management> and <named_collection_control> settings enable significant control over user access, which is good for security but should be carefully managed to avoid misconfigurations. Consider reviewing these settings with a database administrator to ensure they are optimized for security and performance.

apps/agent/pkg/clickhouse/client.go (4)

15-20: Struct Definition Approved

The Clickhouse struct is well-defined, with appropriate types for managing connections, logging, and batch processing.


22-27: Struct Definition Approved

The Config struct is appropriately designed to encapsulate the necessary configuration parameters for establishing a ClickHouse connection.


73-76: Function Implementation Approved

The Shutdown function correctly handles the cleanup of resources, ensuring both the batch processor and the ClickHouse connection are closed.


78-80: Function Implementation Approved

The InsertApiRequest function correctly buffers API requests for batch processing, utilizing the established infrastructure effectively.

deployment/docker-compose.yaml (3)

74-88: Service Configuration Approved

The ClickHouse service is correctly configured with appropriate settings for image, user permissions, container name, hostname, ports, and volume mappings. The dependency on clickhouse-keeper is also correctly established.


58-60: Environment Variable Configuration Approved

The new environment variables for ClickHouse (CLICKHOUSE_ADDR, CLICKHOUSE_USERNAME, CLICKHOUSE_PASSWORD) are correctly defined and essential for configuring the database connection.


89-96: Service Configuration Approved

The ClickHouse Keeper service is correctly configured with appropriate settings for image, user permissions, container name, hostname, ports, and volume mappings.

apps/agent/pkg/api/server.go (3)

17-17: Approved import addition for UID generation.

The addition of the uid package is necessary for generating unique request IDs, aligning with the PR's objectives to enhance request tracking.


41-44: Approved addition of ClickHouse configuration fields in Config struct.

The new fields in the Config struct allow for external configuration of the ClickHouse integration, enhancing flexibility and functionality.


61-61: Approved modifications in New function for ClickHouse integration and request tracking.

The changes in the New function properly initialize the clickhouse field and add middleware for generating and appending a unique request ID. Consider adding error handling for the uid.New function to ensure robustness.

Also applies to: 69-70, 72-72

apps/agent/go.mod (10)

8-8: Verify the necessity and compatibility of ClickHouse dependencies.

The addition of github.com/ClickHouse/clickhouse-go/v2 v2.28.1 suggests significant integration with ClickHouse. Ensure that this library version is compatible with other project dependencies and that it is necessary for the project's requirements.


46-46: Review the indirect dependency on ClickHouse.

The dependency github.com/ClickHouse/ch-go v0.62.0 is marked as indirect. Verify whether this dependency is essential or if it can be removed to simplify the dependency graph.


51-51: Assess the addition of the brotli compression library.

github.com/andybalholm/brotli v1.1.0 has been added as an indirect dependency. Confirm its usage within the project, particularly if it's being used for data compression in network communications or file storage.


106-106: Evaluate the inclusion of city hash library.

github.com/go-faster/city v1.0.1 is added as an indirect dependency. This library is typically used for fast hashing functions. Ensure that its performance benefits are leveraged appropriately in the project.


107-107: Check the new error handling library.

The addition of github.com/go-faster/errors v0.7.1 suggests an enhancement in error handling. Review the project's current error handling strategy to ensure compatibility and improvement.


114-114: Confirm the necessity of the MySQL driver.

github.com/go-sql-driver/mysql v1.4.0 is added as an indirect dependency. If the project is integrating with MySQL databases alongside ClickHouse, confirm that this driver is required and correctly configured.


186-186: Review the addition of the orb library for geospatial data.

github.com/paulmach/orb v0.11.1 has been added as an indirect dependency. This library is used for handling geospatial data structures and algorithms. Verify its use case within the project, especially if geospatial data is a part of the data handling or visualization features.


188-188: Assess the inclusion of the LZ4 compression library.

github.com/pierrec/lz4/v4 v4.1.21 is added as an indirect dependency. This library is known for its high-speed data compression. Confirm its integration and performance impact, particularly in scenarios involving large data volumes.


201-201: Evaluate the addition of the assembly optimizations library.

github.com/segmentio/asm v1.2.0 is added as an indirect dependency. This library provides assembly optimizations. Review the specific areas of the application that benefit from these optimizations to ensure they are effectively utilized.


206-206: Check the decimal handling library.

github.com/shopspring/decimal v1.4.0 is added as an indirect dependency. This library is crucial for precise decimal calculations. Ensure that it is used appropriately in financial or other precision-critical calculations within the project.

apps/agent/cmd/agent/agent.go (1)

137-148: Clarify the use of NoopIngester and verify error handling.

The integration of Clickhouse in the run function uses a NoopIngester by default, which is replaced if a Clickhouse configuration is provided. Consider adding comments to clarify the purpose of using a NoopIngester as the default. Additionally, ensure that all error paths are adequately tested to handle potential runtime issues effectively.

	ch := clickhouse.NewNoopIngester()  // Default NoopIngester used when no Clickhouse configuration is provided
	if cfg.Clickhouse != nil {
		ch, err = clickhouse.New(clickhouse.Config{
			Addr:     cfg.Clickhouse.Addr,
			Username: cfg.Clickhouse.Username,
			Password: cfg.Clickhouse.Password,
			Logger:   logger.With().Str("pkg", "clickhouse").Logger(),
		})
		if err != nil {
			return err  // Ensure this error is properly logged and handled
		}
	}

apps/agent/Taskfile.yml Show resolved Hide resolved
apps/agent/pkg/clickhouse/client.go Outdated Show resolved Hide resolved
apps/agent/pkg/config/agent.go Outdated Show resolved Hide resolved
feat(agent): add new context utility functions for request ID handling
feat(agent): implement error handling functions for validation and unhandled errors
feat(agent): refactor logging middleware to capture request details and log

fix(api): refactor middleware to use http.ResponseWriter instead of fiber.Ctx
feat(api): add support for request ID middleware
feat(api): add tracing middleware for http requests
feat(api): add not found route handler
feat(api): add sender interface and JSON sender implementation
feat(api): update route handling to use http.ServeMux instead of fiber.App

fix(handler.go): change fiber.Ctx to http.ResponseWriter and http.Request
fix(handler.go): update error handling for validation and encryption errors
fix(handler.go): update response handling to use http.ResponseWriter and context

refactor(server.go): replace fiber framework with standard net/http package
feat(server.go): implement custom server struct with http.ServeMux and http.Server
feat(server.go): add support for custom error handling and timeouts
feat(server.go): update server initialization to use custom server struct
feat(server.go): refactor route handling to use http.ServeMux instead of fiber
feat(server.go): update server Listen and Shutdown methods to use http.Server
refactor(harness.go): replace fiber framework with standard net/http package
feat(harness.go): implement custom Harness struct with http.ServeMux
feat(harness.go): update Register method to use http.ServeMux instead of fiber
feat(harness.go): update CallRoute function to use http.ServeMux
refactor(validator.go): remove dependency on fiber framework
feat(validator.go): implement custom Body method for request validation

chore(openapi): optimize required fields formatting in openapi.json
feat(openapi): add new request prefix constant in uid.go
feat(schema): simplify required fields formatting in schema.json

fix(eventrouter): change fiber.Handler to http.HandlerFunc for CreateHandler
fix(eventrouter): update request and response handling in CreateHandler
fix(eventrouter): update error handling and response status codes in CreateHandler
fix(eventrouter): update handling of query parameters in CreateHandler
fix(eventrouter): update handling of request body and response in CreateHandler
fix(v1_keys_verifyKey.multilimit.test.ts): add missing semicolon to console.info
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: 3

Outside diff range, codebase verification and nitpick comments (9)
apps/agent/pkg/api/routes/v1_liveness/handler_test.go (1)

18-18: Consider adding assertions to verify the response body content.

The liveness endpoint returns a JSON body with a message "OK" in addition to the 200 status code. While the current test checks the status code, it would be beneficial to also assert the response body to ensure comprehensive validation of the endpoint's behavior.

  • Add assertions to verify that the response body contains the expected JSON structure and message.
Analysis chain

Verify the sufficiency of the simplified assertion checks.

The assertion checks have been simplified to verify only the response status code, while the previous JSON body check has been removed.

Please ensure that the simplified assertion checks are sufficient for validating the correctness of the liveness endpoint response. If necessary, consider adding more comprehensive assertions to verify the response content or structure.

Run the following script to search for potential usages of the liveness endpoint:

Review the search results to determine if additional assertions are needed based on how the liveness endpoint is being used in the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for potential usages of the liveness endpoint.

# Test: Search for usages of the liveness endpoint. Expect: Only valid usages.
rg --type go -g '!*_test.go' $'/v1/liveness'

Length of output: 345


Script:

#!/bin/bash
# Extract the implementation of the liveness endpoint handler to understand its response.
ast-grep --lang go --pattern 'func $_($_, $_) { $$$ }' apps/agent/pkg/api/routes/v1_liveness/handler.go

Length of output: 105


Script:

#!/bin/bash
# Use rg to search for function definitions in the handler.go file to locate the liveness endpoint handler implementation.
rg 'func ' apps/agent/pkg/api/routes/v1_liveness/handler.go -A 10

Length of output: 369

apps/agent/cmd/main.go (1)

29-36: LGTM! Consider extracting the formatting logic into a separate function.

The code changes are correctly implemented and achieve the desired output formatting. The changes improve the readability and clarity of the output by providing a structured format that distinguishes between the location and the message associated with each element in the chain.

To further improve the code, consider extracting the formatting logic into a separate function for better modularity and reusability. Here's an example:

func formatChain(chain []fault.Fault) string {
	var output string
	for _, e := range chain {
		output += " - "
		if e.Location != "" {
			output += fmt.Sprintf("%s\n", e.Location)
			output += "    > "
		}
		output += fmt.Sprintf("%s\n", e.Message)
	}
	return output
}

Then, you can replace the loop in the main function with:

fmt.Println(formatChain(chain))

This refactoring makes the code more modular and reusable, as the formatting logic can be easily reused in other parts of the codebase if needed.

apps/agent/pkg/api/routes/route.go (1)

22-22: Fix the typo in the type name.

The type name Middeware has a typo. It should be Middleware.

Apply this diff to fix the typo:

-type Middeware func(http.HandlerFunc) http.HandlerFunc
+type Middleware func(http.HandlerFunc) http.HandlerFunc
apps/agent/pkg/api/errors/validation.go (1)

12-32: LGTM with a minor suggestion!

The code changes are approved. The function is correctly implemented and follows the OpenAPI specification. It uses the fmsg package to get the issues from the error and the ctxutil package to get the request ID from the context, which are good practices for error handling, logging, and tracing. It also returns an openapi.ErrorModel with a status code of 500, which is a good practice for internal server errors.

However, please consider replacing the TODO comments with actual values for the instance URL and the type.

-		Instance:  "https://errors.unkey.com/todo",
+		Instance:  "https://errors.unkey.com/internal-server-error",
-		Type:      "TODO docs link",
+		Type:      "https://docs.unkey.com/errors/internal-server-error",
apps/agent/config.local.json (3)

5-8: Use strong, unique credentials for the pprof section.

Using default credentials is not recommended. Please consider using strong, unique credentials for the username and password properties.

{
  "pprof": {
    "username": "<strong_unique_username>",
    "password": "<strong_unique_password>"
  }
}

11-20: Improve the cluster configuration section.

Please consider the following suggestions:

  • Use a strong, unique token for the authToken property.
  • Use environment variables for the authToken and join properties to make the configuration more flexible.
{
  "cluster": {
    "authToken": "<strong_unique_token>",
    "serfAddr": "localhost:9090",
    "rpcAddr": "localhost:9095",
    "join": {
      "env": {
        "addrs": ["${UNKEY_AGENT_JOIN_ADDR}"]
      }
    }
  }
}

21-34: Improve the services configuration section.

Please consider the following suggestions:

  • Use strong, unique tokens for the authToken properties in the ratelimit and eventRouter sections.
  • Use a valid token for the token property in the tinybird section.
  • Use environment variables for the authToken and token properties to make the configuration more flexible.
{
  "services": {
    "ratelimit": {
      "authToken": "<strong_unique_token>"
    },
    "eventRouter": {
      "authToken": "<strong_unique_token>",
      "tinybird": {
        "token": "${TINYBIRD_TOKEN}",
        "batchSize": 1000,
        "flushInterval": 1,
        "bufferSize": 10000
      }
    }
  }
}
apps/agent/pkg/api/mw_metrics.go (1)

36-52: LGTM!

The code changes are approved.

Consider capturing additional metrics such as request size, response size, etc. to gain more insights into the API performance.

For example, you can capture the request size using r.ContentLength and the response size using wi.bytesWritten (you'll need to add a new field to responseWriterStatusInterceptor to capture the bytes written).

Then, you can expose these metrics using Prometheus:

prometheus.HTTPRequestSize.WithLabelValues(r.URL.Path).Observe(float64(r.ContentLength))
prometheus.HTTPResponseSize.WithLabelValues(r.URL.Path).Observe(float64(wi.bytesWritten))
apps/agent/pkg/api/register_routes.go (1)

15-54: Suggestions for improving the RegisterRoutes method:

  1. Consider moving the Services struct to a separate file for better organization and reusability.

  2. Create the staticBearerAuth middleware once and reuse it for all routes that require authentication. This will reduce duplication and improve maintainability.

  3. Refactor the route registration code to reduce duplication and improve readability. For example, you could create a helper function that takes a route and middleware and registers them with the mux.

Here's an example of how you could refactor the route registration code:

func registerRoute(mux *http.ServeMux, route routes.Route, middleware ...func(http.Handler) http.Handler) {
    handler := route.Handler()
    for _, mw := range middleware {
        handler = mw(handler)
    }
    mux.Handle(route.Path(), handler)
}

func (s *Server) RegisterRoutes() {
    // ...

    registerRoute(s.mux, v1Liveness.New(svc))
    registerRoute(s.mux, v1RatelimitCommitLease.New(svc), staticBearerAuth)
    registerRoute(s.mux, v1RatelimitMultiRatelimit.New(svc), staticBearerAuth)
    registerRoute(s.mux, v1RatelimitRatelimit.New(svc), staticBearerAuth)
    registerRoute(s.mux, v1VaultDecrypt.New(svc), staticBearerAuth)
    registerRoute(s.mux, v1VaultEncrypt.New(svc), staticBearerAuth)
    registerRoute(s.mux, v1VaultEncryptBulk.New(svc), staticBearerAuth)
    registerRoute(s.mux, notFound.New(svc))
}

This refactoring makes the route registration more concise and easier to read while still allowing for flexibility in applying middleware to specific routes.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6e033a3 and 7f577c6.

Files ignored due to path filters (2)
  • apps/agent/go.sum is excluded by !**/*.sum
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (47)
  • apps/agent/Dockerfile (1 hunks)
  • apps/agent/Taskfile.yml (2 hunks)
  • apps/agent/cmd/agent/agent.go (4 hunks)
  • apps/agent/cmd/main.go (1 hunks)
  • apps/agent/config.local.json (1 hunks)
  • apps/agent/gen/openapi/gen.go (1 hunks)
  • apps/agent/go.mod (11 hunks)
  • apps/agent/integration/cluster/docker/ratelimits_test.go (3 hunks)
  • apps/agent/pkg/api/agent_auth.go (1 hunks)
  • apps/agent/pkg/api/ctxutil/context.go (1 hunks)
  • apps/agent/pkg/api/errors/unhandled.go (1 hunks)
  • apps/agent/pkg/api/errors/validation.go (1 hunks)
  • apps/agent/pkg/api/interface.go (1 hunks)
  • apps/agent/pkg/api/mw_logging.go (1 hunks)
  • apps/agent/pkg/api/mw_metrics.go (1 hunks)
  • apps/agent/pkg/api/mw_request_id.go (1 hunks)
  • apps/agent/pkg/api/mw_tracing.go (1 hunks)
  • apps/agent/pkg/api/register_routes.go (1 hunks)
  • apps/agent/pkg/api/routes/not_found/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/route.go (1 hunks)
  • apps/agent/pkg/api/routes/sender.go (1 hunks)
  • apps/agent/pkg/api/routes/services.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_liveness/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_liveness/handler_test.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler_test.go (2 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_multiRatelimit/handler.go (2 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler_test.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_decrypt/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_encrypt/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_encrypt_bulk/handler.go (1 hunks)
  • apps/agent/pkg/api/server.go (4 hunks)
  • apps/agent/pkg/api/testutil/harness.go (5 hunks)
  • apps/agent/pkg/api/validation/validator.go (1 hunks)
  • apps/agent/pkg/clickhouse/client.go (1 hunks)
  • apps/agent/pkg/clickhouse/interface.go (1 hunks)
  • apps/agent/pkg/clickhouse/noop.go (1 hunks)
  • apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql (1 hunks)
  • apps/agent/pkg/clickhouse/schema/requests.go (1 hunks)
  • apps/agent/pkg/openapi/config.yaml (1 hunks)
  • apps/agent/pkg/openapi/openapi.json (1 hunks)
  • apps/agent/pkg/tinybird/tinybird.go (3 hunks)
  • apps/agent/pkg/uid/uid.go (2 hunks)
  • apps/agent/schema.json (3 hunks)
  • apps/agent/services/eventrouter/service.go (3 hunks)
  • apps/api/src/routes/v1_keys_verifyKey.multilimit.test.ts (1 hunks)
Files skipped from review due to trivial changes (3)
  • apps/agent/gen/openapi/gen.go
  • apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql
  • apps/api/src/routes/v1_keys_verifyKey.multilimit.test.ts
Files skipped from review as they are similar to previous changes (8)
  • apps/agent/cmd/agent/agent.go
  • apps/agent/go.mod
  • apps/agent/pkg/api/interface.go
  • apps/agent/pkg/api/routes/services.go
  • apps/agent/pkg/api/server.go
  • apps/agent/pkg/clickhouse/client.go
  • apps/agent/pkg/clickhouse/noop.go
  • apps/agent/pkg/clickhouse/schema/requests.go
Additional context used
checkov
apps/agent/pkg/openapi/openapi.json

[HIGH] 1-786: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-786: Ensure that security operations is not empty.

(CKV_OPENAPI_5)

Additional comments not posted (92)
apps/agent/pkg/openapi/config.yaml (1)

1-6: LGTM!

The OpenAPI configuration looks good:

  • The package is correctly set to openapi.
  • The output is correctly set to ./gen/openapi/gen.go.
  • The generate section correctly enables generating models.
  • The output-options section correctly enables nullable types.
apps/agent/pkg/clickhouse/interface.go (1)

7-9: LGTM!

The Bufferer interface is correctly defined:

  • The interface name follows Go naming conventions.
  • The BufferApiRequest method signature is clear and uses a type from the imported schema package.
  • The interface has a clear and focused responsibility.
apps/agent/pkg/api/mw_tracing.go (1)

1-18: LGTM!

The withTracing middleware function is well-implemented and follows best practices for tracing in Go:

  • It wraps the next handler using http.HandlerFunc to create a middleware function.
  • It starts a new span using tracing.Start and defers the span's End method to ensure proper tracing.
  • It updates the request context with the new context containing the span before calling the next handler.

The code changes are approved.

apps/agent/pkg/api/mw_request_id.go (1)

10-16: LGTM!

The middleware function withRequestId is correctly implemented and follows the standard middleware pattern. It uses the uid package to generate a unique request ID and the ctxutil package to set the request ID in the context.

apps/agent/pkg/api/routes/v1_liveness/handler.go (4)

1-1: LGTM!

The package declaration and imports have been updated to reflect the restructuring of the liveness check functionality.

Also applies to: 4-4, 6-8


10-21: LGTM!

The New function simplifies the endpoint registration process and improves the response handling by:

  • Creating a new route for the liveness check using routes.NewRoute.
  • Utilizing the http.ResponseWriter and http.Request types in the route handler.
  • Sending the response using svc.Sender.Send with the openapi.V1LivenessResponseBody structure.

14-14: LGTM!

The log message is appropriate for debugging purposes.


16-18: LGTM!

The response body is appropriate for a liveness check.

apps/agent/pkg/uid/uid.go (2)

12-13: LGTM!

The code changes are approved.


30-32: LGTM!

The code changes are approved.

apps/agent/pkg/api/routes/v1_liveness/handler_test.go (3)

1-1: LGTM: The package name change aligns with the API versioning and is more descriptive.

The package name has been changed from handler_test to v1Liveness_test, which aligns with the versioning of the API and provides a more descriptive naming convention.


14-16: LGTM: The test function implementation changes enhance modularity and reusability.

The test function implementation has undergone a significant overhaul:

  • The test now uses testutil.NewHarness for setup, which indicates a more modular approach to testing.
  • The route is set up using h.SetupRoute(v1Liveness.New), which encapsulates the route registration process more effectively.
  • The route is invoked using testutil.CallRoute, which provides a generic and reusable way to handle route calls in tests.

These changes enhance the clarity, maintainability, and robustness of the test code.


7-9: Verify the impact of the import statement changes.

The import statements have been modified to remove the humatest package and include a new testutil package. This change suggests a shift in the testing framework or methodology being employed.

Please ensure that the removal of the humatest package and the inclusion of the new testutil package do not introduce any breaking changes or inconsistencies in the test suite.

Run the following script to search for potential usages of the removed and added packages:

Verification successful

Transition from humatest to testutil verified successfully.

The testutil package is actively used in test files, replacing the humatest package. Functions like NewHarness and CallRoute are utilized, indicating that testutil is fulfilling the necessary testing roles without introducing breaking changes. No occurrences of humatest were found in non-test files, confirming its removal.

  • The transition from humatest to testutil is complete and consistent across the test suite.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for potential usages of the removed and added packages.

# Test 1: Search for usages of the removed `humatest` package. Expect: No occurrences.
rg --type go -g '!*_test.go' $'humatest'

# Test 2: Search for usages of the added `testutil` package. Expect: Only valid usages.
rg --type go -g '!*_test.go' $'testutil'

Length of output: 135


Script:

#!/bin/bash
# Description: Search for usages of the `testutil` package in test files to verify its correct usage.

# Test 3: Search for usages of the `testutil` package in test files.
rg --type go -g '*_test.go' $'testutil'

Length of output: 2169

apps/agent/pkg/api/ctxutil/context.go (5)

1-3: LGTM!

The package declaration and import statements are approved.


5-7: LGTM!

The constant declaration is approved.


9-17: LGTM!

The getValue function is well-implemented and documented.


19-21: LGTM!

The GetRequestId function is well-implemented.


23-25: LGTM!

The SetRequestId function is well-implemented.

apps/agent/Dockerfile (1)

19-19: LGTM!

The code change is approved. It correctly copies the openapi.json file from the builder stage to the final image, which is beneficial for including the OpenAPI specification in the Docker image.

apps/agent/pkg/api/routes/route.go (5)

8-12: LGTM!

The Route struct is well-defined with appropriately named fields.


14-20: LGTM!

The NewRoute function is a correctly implemented constructor for creating a new Route instance.


24-29: LGTM!

The WithMiddleware method is correctly implemented to apply middleware to the route's handler.


31-33: LGTM!

The Register method is correctly implemented to register the route with the provided http.ServeMux.


35-37: LGTM!

The Method and Path getter methods are correctly implemented.

Also applies to: 39-41

apps/agent/pkg/api/routes/not_found/handler.go (2)

1-9: LGTM!

The package declaration and imports are correctly implemented and follow Go conventions.


12-27: LGTM!

The New function is correctly implemented and follows Go conventions. It returns a 404 response with an error model containing relevant details.

Some good practices observed:

  • The function uses the ctxutil.GetRequestId function to get the request ID from the context, which is useful for logging and tracing.
  • The function uses the svc.Sender.Send method to send the response, which separates concerns.
apps/agent/config.local.json (2)

1-10: LGTM!

The top-level configuration properties are correctly defined.


35-38: LGTM!

The prometheus configuration section is correctly defined.

apps/agent/Taskfile.yml (2)

28-30: The existing comment suggesting to add comments or documentation for the new migrate task is still valid and applicable.


32-38: LGTM!

The new generate task looks good and enhances the code generation capabilities.

apps/agent/pkg/api/agent_auth.go (5)

8-8: LGTM!

The code changes are approved.


11-11: LGTM!

The code changes are approved.


14-39: LGTM!

The code changes are approved. The changes enhance the clarity and functionality of the middleware, aligning it more closely with standard HTTP practices while improving error handling.


32-36: LGTM!

The code changes are approved.


38-38: LGTM!

The code changes are approved.

apps/agent/pkg/api/routes/v1_vault_encrypt/handler.go (2)

1-1: LGTM!

The package name change and updated imports align with the restructuring of the code to use OpenAPI types and improve error handling.

Also applies to: 4-4, 6-8


12-35: LGTM!

The New function streamlines the request handling process and aligns the implementation with OpenAPI standards. The error handling has been improved using dedicated error handling functions.

apps/agent/pkg/api/routes/v1_vault_decrypt/handler.go (4)

14-15: LGTM!

The code changes are approved.


16-21: LGTM!

The code changes are approved.


23-27: LGTM!

The code changes are approved.


30-32: LGTM!

The code changes are approved.

apps/agent/pkg/api/mw_metrics.go (1)

15-34: LGTM!

The code changes are approved.

apps/agent/pkg/api/routes/v1_vault_encrypt_bulk/handler.go (5)

4-10: LGTM!

The code changes are approved.


14-15: LGTM!

The code changes are approved.


17-25: LGTM!

The code changes are approved.


27-32: LGTM!

The code changes are approved.


35-43: LGTM!

The code changes are approved.

apps/agent/pkg/tinybird/tinybird.go (1)

Line range hint 56-67: LGTM!

The code changes are approved for the following reasons:

  • The change leverages a predefined response structure from the OpenAPI specification, which improves consistency and integration with other components.
  • The change simplifies the response handling logic by using a standardized structure.
  • The change correctly checks if all rows were ingested by comparing the SuccessfulRows field with the length of the rows slice.
apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go (2)

16-48: LGTM!

The code changes are approved. The function is well-structured, modular, and follows best practices for request validation, error handling, and response sending.


22-26: Verify the error handling.

The function uses the errors.HandleValidationError and errors.HandleError functions to handle errors. This ensures that the errors are handled consistently and logged appropriately.

Run the following script to verify the error handling:

Also applies to: 31-34, 40-44

Verification successful

Error Handling Verified

The error handling in v1_ratelimit_commitLease/handler.go is correctly implemented using errors.HandleValidationError and errors.HandleError for handling validation and other errors, respectively. This ensures consistent error handling and logging across the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the error handling in the `New` function.

# Test 1: Search for the usage of `errors.HandleValidationError`. Expect: The function to be used for handling validation errors.
rg --type go -A 5 $'errors.HandleValidationError'

# Test 2: Search for the usage of `errors.HandleError`. Expect: The function to be used for handling other errors.
rg --type go -A 5 $'errors.HandleError'

Length of output: 7260

apps/agent/pkg/api/routes/v1_ratelimit_multiRatelimit/handler.go (5)

12-13: LGTM!

The code changes are approved. The introduction of the New function to construct a new route simplifies the route registration process.


15-20: LGTM!

The code changes are approved. The use of svc.OpenApiValidator.Body for request body validation and the dedicated error handling function errors.HandleValidationError for validation errors improve the code quality and maintainability.


23-26: LGTM!

The code changes are approved. The adjustment to the handling of the cost parameter to ensure a default value of 1 is used if not provided prevents potential issues related to uninitialized or nil cost values.


34-38: Verify the empty ratelimitv1.RatelimitMultiRequest parameter.

Please ensure that passing an empty ratelimitv1.RatelimitMultiRequest to the svc.Ratelimit.MultiRatelimit function is intentional and not an oversight.


52-52: LGTM!

The code changes are approved. The use of svc.Sender.Send to directly send the response simplifies the response handling and reduces the overhead of constructing response objects.

apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler_test.go (5)

1-1: LGTM!

The package name change to v1RatelimitCommitLease_test provides a more specific context for the tests.


19-20: LGTM!

The changes to the test function TestCommitLease enhance the clarity and maintainability of the test code by using a more modular and standardized approach to setting up routes with h.SetupRoute.


22-31: LGTM!

The changes to the request construction, shifting from v1RatelimitRatelimit.V1RatelimitRatelimitRequest to openapi.V1RatelimitRatelimitRequestBody, suggest a move towards a more standardized API definition, aligning the test with the open API specifications.


34-41: LGTM!

The changes to the response handling and assertions enhance the flexibility, readability, and maintainability of the test code by:

  • Utilizing a generic testutil.CallRoute function for response handling.
  • Streamlining assertions with direct comparisons to the response body properties.

43-47: LGTM!

The changes to the commit request construction, updating it to use openapi.V1RatelimitCommitLeaseRequestBody, align the test code with the open API specifications, enhancing the consistency and maintainability of the test code.

apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler_test.go (2)

15-30: LGTM!

The changes to the TestRatelimit function improve the clarity and structure of the test case by leveraging the openapi types for request and response structures. The test case is correctly verifying the expected rate limiting behavior.


32-55: Verify the reason for skipping the test case.

The changes to the TestRatelimitWithLease function improve the clarity and structure of the test case by leveraging the openapi types for request and response structures. The test case is correctly verifying the expected rate limiting behavior with a lease.

The test case is currently skipped. Please verify the reason for skipping the test case. If the lease functionality is not fully implemented or requires further testing, consider removing the t.Skip() call and updating the test case accordingly.

apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler.go (5)

15-15: LGTM!

The refactoring of the function to focus on creating a new route without registering it is a good change. It separates the concerns and improves the modularity of the code.


20-23: LGTM!

The usage of OpenApiValidator for request validation and errors.HandleValidationError for handling validation errors is a good improvement. It makes the code more robust and maintainable.


26-28: LGTM!

Setting a default value of 1 for the Cost field if it is not provided in the request is a good practice. It ensures that the field always has a valid value and improves the reliability of the code.


31-36: LGTM!

The handling of the optional Lease field in the request is implemented correctly. If the field is provided, a LeaseRequest object is created with the Cost and Timeout fields copied from the request. This ensures that the Lease field is processed correctly.


50-56: LGTM!

The creation of the V1RatelimitRatelimitResponseBody object and populating its fields from the RatelimitResponse object is implemented correctly. This ensures that the correct response is sent back to the client.

apps/agent/pkg/api/validation/validator.go (3)

18-20: LGTM!

The code changes are approved.


22-43: LGTM!

The code changes are approved.


45-68: LGTM!

The code changes are approved.

apps/agent/pkg/api/routes/sender.go (4)

13-18: LGTM!

The Sender interface is well-defined with a clear method signature.


20-22: LGTM!

The JsonSender struct is well-defined with an appropriately named field.


24-26: LGTM!

The NewJsonSender function is well-defined and follows the factory pattern.


30-65: LGTM!

The Send method is well-defined and handles the response body marshalling and error handling appropriately.

apps/agent/pkg/api/register_routes.go (1)

1-13: LGTM!

The package declaration and imports follow Go conventions and seem appropriate for the file's purpose.

apps/agent/pkg/api/mw_logging.go (1)

17-38: LGTM!

The responseWriterInterceptor struct is correctly implemented and captures the necessary information for logging.

apps/agent/pkg/api/testutil/harness.go (5)

30-30: LGTM!

The change in the Harness struct, replacing the api field with a mux field of type *http.ServeMux, aligns with the overall modifications in the file to enhance route management and testing using the HTTP multiplexer.


34-34: LGTM!

The update to the NewHarness function, initializing the mux field instead of the api field, aligns with the change in the Harness struct and ensures proper setup of the Harness with an HTTP multiplexer for route management and testing.


75-79: LGTM!

The changes to the Register method, accepting a *routes.Route parameter and registering it with the multiplexer using route.Register(h.mux), simplify the route registration process and align with the overall shift towards using the HTTP multiplexer for route management.


81-89: LGTM!

The addition of the SetupRoute method enhances the flexibility of route setup within the harness by allowing the construction of a *routes.Route using a provided constructor function and the existing services. It ensures that the route is properly set up and registered with the multiplexer using the Register method.


107-133: LGTM!

The addition of the CallRoute function is a valuable enhancement that simplifies the process of making HTTP requests to registered routes for testing purposes. It handles the setup, registration, and request/response cycle, and returns a TestResponse struct encapsulating the response details, making it convenient to inspect and assert the response in tests.

apps/agent/services/eventrouter/service.go (1)

Line range hint 85-149: LGTM!

The changes to the CreateHandler function are approved for the following reasons:

  1. The function signature change improves clarity by directly returning a function that processes HTTP requests.
  2. Explicitly defining the HTTP method and path improves documentation and understanding of the API's expected behavior.
  3. The error handling changes standardize the response format and allow for more control over the response body.
  4. The response structure change indicates a shift towards a more structured and standardized response format, which may enhance compatibility with other components.
apps/agent/integration/cluster/docker/ratelimits_test.go (4)

13-13: LGTM!

The code changes are approved.


91-95: LGTM!

The code changes are approved.


119-120: LGTM!

The code changes are approved.


Line range hint 126-130: LGTM!

The code changes are approved.

apps/agent/schema.json (3)

8-24: LGTM!

The new clickhouse object in the schema correctly defines the required properties for ClickHouse connection details with appropriate types and constraints.


177-194: LGTM!

The new prometheus object in the schema correctly defines the required properties for Prometheus metrics scraping with appropriate types, descriptions, and default values.


218-222: LGTM!

The new rpcPort property in the schema correctly defines the port for listening to RPC requests with an appropriate type, description, and default value.

apps/agent/pkg/openapi/openapi.json (3)

1-786: The OpenAPI specification looks good!

The specification is comprehensive and well-structured. It clearly defines the API contract including:

  • Schemas for request and response bodies
  • Paths defining the API endpoints
  • Servers defining the base URLs

No major issues found. The specification is approved.

Tools
checkov

[HIGH] 1-786: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-786: Ensure that security operations is not empty.

(CKV_OPENAPI_5)


1-786: Ignore static analysis hint about global security field.

The static analysis hint suggests defining rules in the global security field. However, the global security field is not mandatory in OpenAPI specifications.

Defining security at the operation level, as this specification seems to be doing, is an acceptable alternative.

Therefore, this hint is likely a false positive and can be ignored.

Tools
checkov

[HIGH] 1-786: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-786: Ensure that security operations is not empty.

(CKV_OPENAPI_5)


1-786: Verify if the lack of security definitions is intended.

The static analysis hint suggests ensuring security operations are defined. However, this specification does not include any security definitions at the operation level.

Please verify if this is intended. If the API is meant to be public and not require any authentication or authorization, then the lack of security definitions is acceptable.

However, if the API does require security, then the security definitions should be added.

If security definitions are needed, I can assist in adding them to the specification. Please let me know if you would like me to do that or open a GitHub issue to track this task.

Tools
checkov

[HIGH] 1-786: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-786: Ensure that security operations is not empty.

(CKV_OPENAPI_5)

apps/agent/pkg/api/errors/unhandled.go Outdated Show resolved Hide resolved
apps/agent/pkg/api/mw_metrics.go Outdated Show resolved Hide resolved
apps/agent/pkg/api/mw_logging.go Outdated Show resolved Hide resolved
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: 4

Outside diff range, codebase verification and nitpick comments (2)
apps/agent/pkg/api/errors/internal_server_error.go (1)

13-13: Consider renaming the function to better reflect its purpose.

The function name HandleError is a bit generic. Consider renaming it to something more specific like HandleInternalServerError to better reflect its purpose.

Apply this diff to rename the function:

-func HandleError(ctx context.Context, err error) openapi.BaseError {
+func HandleInternalServerError(ctx context.Context, err error) openapi.BaseError {
apps/agent/config.docker.json (1)

9-9: The authToken parameter is still in use in the codebase.

The authToken is present in several Go files and is used in various services and components, indicating that it remains an integral part of the authentication mechanism. The removal from the JSON configuration file suggests a change in how the token is provided or managed, but it does not eliminate its usage. Verify how the authToken is now being managed or set in the application.

  • Files with authToken usage:
    • apps/agent/services/eventrouter/service.go
    • apps/agent/pkg/connect/ratelimit.go
    • apps/agent/pkg/config/agent.go
    • apps/agent/pkg/cluster/cluster.go
    • apps/agent/pkg/auth/authorization.go
    • apps/agent/pkg/api/server.go
Analysis chain

Verify the impact of removing the authToken parameter on the authentication mechanism.

The authToken parameter has been removed from multiple places in the configuration, including the root level and the ratelimit and eventRouter services. This suggests a significant change in the authentication mechanism.

Run the following script to verify the usage of the authToken parameter in the codebase:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `authToken` parameter in the codebase.

# Test: Search for the usage of the `authToken` parameter. Expect: No occurrences or only occurrences in the new authentication mechanism.
rg --type go $'authToken'

Length of output: 2063

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7f577c6 and 4dfb5e7.

Files ignored due to path filters (1)
  • apps/agent/go.sum is excluded by !**/*.sum
Files selected for processing (30)
  • Taskfile.yml (1 hunks)
  • apps/agent/cmd/agent/agent.go (3 hunks)
  • apps/agent/config.docker.json (3 hunks)
  • apps/agent/config.production.json (3 hunks)
  • apps/agent/config.staging.json (2 hunks)
  • apps/agent/gen/openapi/gen.go (1 hunks)
  • apps/agent/go.mod (10 hunks)
  • apps/agent/pkg/api/errors/internal_server_error.go (1 hunks)
  • apps/agent/pkg/api/errors/validation_error.go (1 hunks)
  • apps/agent/pkg/api/mw_logging.go (1 hunks)
  • apps/agent/pkg/api/register_routes.go (1 hunks)
  • apps/agent/pkg/api/routes/not_found/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/sender.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_multiRatelimit/handler.go (2 hunks)
  • apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_decrypt/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_encrypt/handler.go (1 hunks)
  • apps/agent/pkg/api/routes/v1_vault_encrypt_bulk/handler.go (1 hunks)
  • apps/agent/pkg/api/server.go (3 hunks)
  • apps/agent/pkg/api/validation/validator.go (1 hunks)
  • apps/agent/pkg/clickhouse/client.go (1 hunks)
  • apps/agent/pkg/clickhouse/flush.go (1 hunks)
  • apps/agent/pkg/clickhouse/noop.go (1 hunks)
  • apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql (1 hunks)
  • apps/agent/pkg/clickhouse/schema/requests.go (1 hunks)
  • apps/agent/pkg/config/agent.go (3 hunks)
  • apps/agent/pkg/openapi/openapi.json (1 hunks)
  • apps/agent/schema.json (6 hunks)
  • deployment/docker-compose.yaml (4 hunks)
Files skipped from review as they are similar to previous changes (14)
  • apps/agent/cmd/agent/agent.go
  • apps/agent/gen/openapi/gen.go
  • apps/agent/go.mod
  • apps/agent/pkg/api/mw_logging.go
  • apps/agent/pkg/api/register_routes.go
  • apps/agent/pkg/api/routes/not_found/handler.go
  • apps/agent/pkg/api/routes/sender.go
  • apps/agent/pkg/api/validation/validator.go
  • apps/agent/pkg/clickhouse/client.go
  • apps/agent/pkg/clickhouse/flush.go
  • apps/agent/pkg/clickhouse/noop.go
  • apps/agent/pkg/clickhouse/schema/001_create_requests_table.sql
  • apps/agent/pkg/clickhouse/schema/requests.go
  • deployment/docker-compose.yaml
Additional context used
checkov
apps/agent/pkg/openapi/openapi.json

[HIGH] 1-898: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-898: Ensure that security operations is not empty.

(CKV_OPENAPI_5)

Additional comments not posted (48)
Taskfile.yml (2)

22-28: LGTM!

The migrate-clickhouse task is correctly defined with the required environment variables and command.


Line range hint 31-35: LGTM!

The migrate-db task is correctly defined with the required environment variable and command.

apps/agent/config.docker.json (2)

36-36: LGTM!

Using an environment variable for the masterKeys parameter is a good practice for security and configuration management.


42-44: LGTM!

The addition of the clickhouse configuration section with the url parameter set to an environment variable is a good practice for integrating ClickHouse database into the application.

apps/agent/pkg/api/routes/v1_vault_encrypt/handler.go (4)

1-1: LGTM!

The changes to the package name and imports align with the restructuring of the code to use a more standardized API structure and improve error handling.

Also applies to: 4-4, 6-6, 8-8


12-35: LGTM!

The New function streamlines the request handling process and aligns the implementation more closely with OpenAPI standards, while also improving error handling mechanisms.


17-21: LGTM!

The error handling changes improve the clarity and maintainability of error responses.


31-34: LGTM!

The response handling changes align the implementation more closely with OpenAPI standards.

apps/agent/config.staging.json (2)

13-13: LGTM!

The code changes are approved for the following reasons:

  • Adding the "authToken" field at the root level is a good change as it centralizes the definition of the authentication token.
  • Using an environment variable for the value of the "authToken" field is a good practice as it keeps the sensitive information out of the configuration file.

28-28: LGTM!

The code changes are approved for the following reasons:

  • The removal of the "authToken" field from the "vault" section is consistent with the addition of the "authToken" field at the root level.
  • The "vault" section no longer needs the "authToken" field as it can access the value from the root level.
apps/agent/pkg/api/routes/v1_vault_decrypt/handler.go (1)

14-35: LGTM!

The code changes are approved. The function is well-structured and follows a clear flow of validating the request, processing the request, and sending the response. The use of http.ResponseWriter and http.Request types directly allows for a more conventional HTTP handler pattern, which is a good practice. The request body validation using svc.OpenApiValidator.Body is a good practice as it ensures that the request body is valid before processing the request. The error handling using a custom error handler (errors.HandleError) is a good practice as it wraps the error with additional context, which can be useful for debugging and logging. The response handling using svc.Sender.Send is a good practice as it simplifies the response handling by directly sending the plaintext result without the need for an intermediate response struct.

apps/agent/pkg/api/routes/v1_vault_encrypt_bulk/handler.go (5)

4-10: LGTM!

The code changes are approved.


14-16: LGTM!

The code changes are approved.


17-25: LGTM!

The code changes are approved.


27-32: LGTM!

The code changes are approved.


35-43: LGTM!

The code changes are approved.

apps/agent/pkg/api/routes/v1_ratelimit_commitLease/handler.go (3)

1-1: Skipping the previous comment on the Redact method.

The Redact method and the associated request and response structs have been removed in the current changes, making the previous comment no longer applicable.


1-1: Skipping the previous comment on the InsertApiRequest call.

The InsertApiRequest call is not present in the current changes, making the previous comment no longer applicable.


16-48: LGTM!

The code changes in the New function look good. The changes improve the request handling, validation, and error management. The lease decoding and unmarshaling logic is correctly implemented. The CommitLease RPC is called with the appropriate parameters. The response sending logic is also correctly implemented.

apps/agent/config.production.json (2)

13-13: LGTM!

The addition of the authToken property at the root level is a good approach to centralize the management of authentication tokens. Using an environment variable for the token value is also a secure practice.


46-46: LGTM!

Setting the masterKeys value using an environment variable is a secure practice and aligns with the overall approach of using environment variables for sensitive configuration values.

apps/agent/pkg/api/routes/v1_ratelimit_multiRatelimit/handler.go (5)

12-13: LGTM!

The change from Register to New function is approved. It simplifies the API endpoint registration by moving to a more direct HTTP handler approach.


18-22: LGTM!

The change to use svc.OpenApiValidator.Body for request body validation and svc.Sender.Send for sending error response is approved. It enhances error handling for validation errors.


25-28: Verify the change in logic for handling the cost parameter.

Ensure that using a default cost value of 1, when not provided in the request, is the intended behavior.


36-41: Verify the empty request and the error handling change.

  • Ensure that passing an empty request to svc.Ratelimit.MultiRatelimit is the intended behavior.

  • The change to use errors.HandleError for error handling is approved. It simplifies the error handling.


53-53: LGTM!

The change to use svc.Sender.Send for sending the response is approved. It simplifies the response handling.

apps/agent/pkg/api/routes/v1_ratelimit_ratelimit/handler.go (6)

15-15: LGTM!

The function signature changes are consistent with the other route handlers in the codebase.


19-23: LGTM!

The request validation changes are approved:

  • The OpenAPI generated structure and validator improve the request validation process.
  • The error handling has been updated to use a dedicated error management function, which is a good practice.

26-28: LGTM!

Setting a default value for the cost parameter is a good practice to ensure a consistent behavior.


31-36: LGTM!

The lease handling changes simplify the code and improve readability.


50-56: LGTM!

The response construction changes simplify the code and improve readability.


58-65: LGTM!

The lease response handling changes are approved:

  • The changes are consistent with the other response handling changes.
  • The error handling has been updated to use a dedicated error management function, which is a good practice.
apps/agent/pkg/api/server.go (7)

8-8: LGTM!

The code changes are approved.


26-30: LGTM!

The code changes are approved.


34-40: LGTM!

The code changes are approved.


43-94: LGTM!

The code changes are approved.


101-103: LGTM!

The code changes are approved.


116-118: LGTM!

The code changes are approved.


127-127: LGTM!

The code changes are approved.

apps/agent/pkg/config/agent.go (4)

7-7: LGTM!

The code changes are approved.


40-46: LGTM!

The code changes are approved.


53-53: LGTM!

The code changes are approved.


80-82: LGTM!

The code changes are approved.

The past review comment suggesting the addition of validation constraints to the Username and Password fields is not applicable to the current code segment as these fields are not present.

apps/agent/schema.json (4)

8-12: LGTM!

The authToken property is correctly defined with appropriate type, description, and constraints.


13-29: LGTM!

The clickhouse object is correctly defined with appropriate properties and constraints.


167-183: LGTM!

The prometheus object is correctly defined with appropriate properties, types, and default values.


208-212: LGTM!

The rpcPort property is correctly defined with an appropriate type, description, and default value.

apps/agent/pkg/openapi/openapi.json (1)

1-898: The file is a valid OpenAPI specification.

The file follows the OpenAPI 3.0 specification and defines various schemas, paths, and operations for an API. The structure and syntax of the file are correct.

Tools
checkov

[HIGH] 1-898: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-898: Ensure that security operations is not empty.

(CKV_OPENAPI_5)

apps/agent/pkg/api/errors/internal_server_error.go Outdated Show resolved Hide resolved
apps/agent/pkg/api/errors/internal_server_error.go Outdated Show resolved Hide resolved
apps/agent/pkg/api/errors/validation_error.go Outdated Show resolved Hide resolved
apps/agent/pkg/openapi/openapi.json Show resolved Hide resolved
@chronark chronark changed the title [WIP] clickhouse feat: net/http api Sep 3, 2024
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4dfb5e7 and 76677ec.

Files selected for processing (2)
  • apps/agent/pkg/api/errors/internal_server_error.go (1 hunks)
  • apps/agent/pkg/api/errors/validation_error.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • apps/agent/pkg/api/errors/internal_server_error.go
  • apps/agent/pkg/api/errors/validation_error.go

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 76677ec and 6c33554.

Files selected for processing (1)
  • apps/agent/pkg/api/mw_metrics.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/agent/pkg/api/mw_metrics.go

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6c33554 and 194f033.

Files selected for processing (1)
  • apps/api/src/routes/v1_keys_createKey.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • apps/api/src/routes/v1_keys_createKey.ts

@chronark chronark merged commit 6973e5c into main Sep 4, 2024
27 checks passed
@chronark chronark deleted the clickhouse branch September 4, 2024 10: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.

1 participant